Skip to content

Instantly share code, notes, and snippets.

@okuryu
Created October 1, 2025 02:38
Show Gist options
  • Save okuryu/8f8df15016638945295c6f5176ac08e3 to your computer and use it in GitHub Desktop.
Save okuryu/8f8df15016638945295c6f5176ac08e3 to your computer and use it in GitHub Desktop.
test cookie in request header via runn
socat TCP-LISTEN:8080,fork,reuseaddr SYSTEM:'./server.sh'
runn run test.runn.yaml
runn run test.runn.yaml
F

1) test.runn.yaml eb638128977bdd2a7e2d70cd731f2334777cf916
  Failure/Error: test failed on "test runn".steps[1]: condition is not true

  Condition:
    current.res.status == 200
    && current.res.rawBody matches "Cookie: foo=xxx"

    │
    ├── current.res.status == 200 => true
    │   ├── current.res.status => 200
    │   └── 200
    └── current.res.rawBody matches "Cookie: foo=xxx" => false
        ├── current.res.rawBody => "You sent:
        │   GET /hello HTTP/1.1
        │   Host: localhost:8080
        │   User-Agent: Go-http-client/1.1
        │   Cookie: bar=yyy; foo=xxx
"       │   Accept-Encoding: gzip
        └── "Cookie: foo=xxx"

  Failure step (test.runn.yaml):
  17   - req:
  18       /hello:
  19         get:
  20           body: null
  21     test: |
  22       current.res.status == 200
  23       && current.res.rawBody matches "Cookie: foo=xxx"


1 scenario, 0 skipped, 1 failure
#!/bin/sh
read request || exit 0
path=$(printf "%s" "$request" | awk '{print $2}')
headers=""
while IFS= read -r line; do
[ "$line" = "$(printf '\r')" ] && break
headers="$headers$line\n"
done
status="200 OK"
extra_hdr=""
body=""
case "$path" in
/hello)
body=$(printf "You sent:\n%s\n%b" "$request" "$headers")
;;
/set-cookie)
body="Cookies set!\n"
extra_hdr="Set-Cookie: foo=xxx; Path=/; HttpOnly\r\nSet-Cookie: bar=yyy; Path=/; HttpOnly\r\n"
;;
*)
status="404 Not Found"
body="Not Found\n"
;;
esac
content_length=$(printf "%b" "$body" | wc -c | tr -d ' ')
printf 'HTTP/1.1 %s\r\n' "$status"
printf 'Content-Type: text/plain; charset=utf-8\r\n'
[ -n "$extra_hdr" ] && printf '%b' "$extra_hdr"
printf 'Content-Length: %s\r\n' "$content_length"
printf 'Connection: close\r\n'
printf '\r\n'
printf '%b' "$body"
desc: test runn
runners:
req:
endpoint: "${ENDPOINT:-http://localhost:8080/}"
useCookie: true
steps:
- req:
/set-cookie:
get:
body: null
test: |
current.res.status == 200
&& current.res.cookies.foo.Value == "xxx"
&& current.res.cookies.foo.HttpOnly == true
&& current.res.cookies.bar.Value == "yyy"
&& current.res.cookies.bar.HttpOnly == true
- req:
/hello:
get:
body: null
test: |
current.res.status == 200
&& current.res.rawBody matches "Cookie: foo=xxx"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment