From bd7ac94e74d8b2da3ee0c7136c709636458b20a2 Mon Sep 17 00:00:00 2001 From: Md Imran Hossain Date: Mon, 7 Sep 2026 11:45:03 +0600 Subject: [PATCH] Add CoAP request/response test suite over UDP Exercises every route against a running server using the bundled client, so the request/response path can be checked without the HTTP bridge. Covers /hello with and without a name, percent- and plus-encoded query values, text and JSON POST bodies, and the 4.00/4.04/4.05 error paths. /health is matched by shape rather than an exact string since uptime moves. Exits non-zero on failure; COAP_HOST/COAP_PORT retarget it. Co-Authored-By: Claude Opus 5 --- test-coap.sh | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100755 test-coap.sh diff --git a/test-coap.sh b/test-coap.sh new file mode 100755 index 0000000..c295308 --- /dev/null +++ b/test-coap.sh @@ -0,0 +1,42 @@ +#!/usr/bin/env bash +# Request/response tests against the CoAP server over UDP. No HTTP involved. +# ./test-coap.sh (server must already be running) +# Override target with COAP_HOST / COAP_PORT. + +CLIENT=${CLIENT:-./target/release/client} +pass=0; fail=0 + +check() { # check [payload] + local desc=$1 want=$2; shift 2 + local got; got=$("$CLIENT" "$@" 2>&1) + if [[ $got == "$want" ]]; then + printf ' ok %-34s %s\n' "$desc" "$got"; ((pass++)) + else + printf ' FAIL %-34s\n want: %s\n got: %s\n' "$desc" "$want" "$got"; ((fail++)) + fi +} + +[[ -x $CLIENT ]] || { echo "build first: cargo build --release"; exit 1; } +echo "CoAP request/response tests -> udp://${COAP_HOST:-127.0.0.1}:${COAP_PORT:-5683}" + +check "GET /hello" "<- 2.05 Hello, World!" get /hello +check "GET ?name" "<- 2.05 Hello, Imran!" get "/hello?name=Imran" +check "GET percent-encoded" "<- 2.05 Hello, Md Imran!" get "/hello?name=Md%20Imran" +check "GET plus-encoded" "<- 2.05 Hello, Md Imran!" get "/hello?name=Md+Imran" +check "GET blank name" "<- 2.05 Hello, World!" get "/hello?name=" +check "POST text body" "<- 2.01 Hello, Imran!" post /hello "Imran" +check "POST JSON body" "<- 2.01 Hello, Imran!" post /hello '{"name":"Imran"}' +check "POST empty -> 4.00" "<- 4.00 Bad Request: send a name as the payload" post /hello "" +check "unknown path -> 4.04" "<- 4.04 Not Found: /nope" get /nope +check "wrong method -> 4.05" "<- 4.05 Method Not Allowed: Put /hello" put /hello x + +# /health has a moving uptime, so match the shape instead of an exact string. +h=$("$CLIENT" get /health 2>&1) +if [[ $h =~ ^\<-\ 2\.05\ \{\"status\":\"ok\",\"uptime\":[0-9]+\}$ ]]; then + printf ' ok %-34s %s\n' "GET /health" "$h"; ((pass++)) +else + printf ' FAIL %-34s got: %s\n' "GET /health" "$h"; ((fail++)) +fi + +echo "$pass passed, $fail failed" +[[ $fail -eq 0 ]]