#!/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 ]]