#!/usr/bin/env bash # next/tests/httpc_request.sh — m2 Step 8e acceptance test. # # Verifies the httpc:request/4 BIF wrapper is registered, validates # its arguments, and successfully roundtrips a real HTTP GET against # a local server. Mirrors http_listen_bif.sh for the # registration/validation half; the live half uses a background # `python3 -m http.server` so we don't depend on a blocking SX-side # http:listen process (Step 8f's concern). # # This BIF is the briefing's allowed-exception scope addition to # lib/erlang/runtime.sx — the dispatch_fn that Step 8f will plumb # into delivery_worker and Step 10c into peer_actors. set -uo pipefail cd "$(git rev-parse --show-toplevel)" SX_SERVER="${SX_SERVER:-hosts/ocaml/_build/default/bin/sx_server.exe}" if [ ! -x "$SX_SERVER" ]; then SX_SERVER="/root/rose-ash/hosts/ocaml/_build/default/bin/sx_server.exe" fi if [ ! -x "$SX_SERVER" ]; then echo "ERROR: sx_server.exe not found." >&2 exit 1 fi VERBOSE="${1:-}" PASS=0; FAIL=0; ERRORS="" # ── live server (Python's stdlib, no extra deps) ───────────── PORT=$(python3 -c 'import socket;s=socket.socket();s.bind(("127.0.0.1",0));print(s.getsockname()[1]);s.close()') SRVROOT=$(mktemp -d) echo "hello from python" > "$SRVROOT/hello.txt" ( cd "$SRVROOT" && python3 -m http.server "$PORT" >/dev/null 2>&1 ) & SRV_PID=$! TMPFILE=$(mktemp) trap "rm -rf $SRVROOT $TMPFILE; kill $SRV_PID 2>/dev/null || true" EXIT # wait for it to come up (up to ~3s) for _ in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15; do if curl -fsS "http://127.0.0.1:$PORT/hello.txt" >/dev/null 2>&1; then break fi sleep 0.2 done # Spell URLs as Erlang byte-list binaries — <<"...">> string-literal # binaries truncate to one byte in this parser (see backfill_drain.sh # for the same workaround on inbox paths). bytes_of() { python3 -c "import sys; print(','.join(str(b) for b in sys.argv[1].encode()))" "$1"; } URL_HELLO_BYTES=$(bytes_of "http://127.0.0.1:$PORT/hello.txt") URL_404_BYTES=$(bytes_of "http://127.0.0.1:$PORT/not_there.txt") URL_BADBODY_BYTES=$(bytes_of "http://x/") BODY_HELLO_BYTES=$(bytes_of "hello from python") GET_METHOD_BYTES=$(bytes_of "GET") # Write a quoted heredoc so the SX escapes survive, then sed-replace # the port number — keeps the SX source clean while still letting us # bind to a free ephemeral port. cat > "$TMPFILE" <<'EPOCHS' (epoch 1) (load "lib/erlang/tokenizer.sx") (load "lib/erlang/parser.sx") (load "lib/erlang/parser-core.sx") (load "lib/erlang/parser-expr.sx") (load "lib/erlang/parser-module.sx") (load "lib/erlang/transpile.sx") (load "lib/erlang/runtime.sx") (load "lib/erlang/vm/dispatcher.sx") ;; BIF registered under httpc/request/4 (epoch 10) (eval "(not (= (er-lookup-bif \"httpc\" \"request\" 4) nil))") ;; BIF marked non-pure (network side effect) (epoch 11) (eval "(get (er-lookup-bif \"httpc\" \"request\" 4) :pure?)") ;; Wrong arity not registered (httpc/request/1 should be nil) (epoch 12) (eval "(= (er-lookup-bif \"httpc\" \"request\" 1) nil)") ;; Non-binary URL -> badarg (epoch 13) (eval "(get (erlang-eval-ast \"try httpc:request(not_a_binary, get, [], <<>>) catch error:badarg -> ok end\") :name)") ;; Non-binary body -> badarg (epoch 14) (eval "(get (erlang-eval-ast \"try httpc:request(<<__URL_BAD__>>, get, [], not_a_binary) catch error:badarg -> ok end\") :name)") ;; ── Live roundtrip: GET against python http.server ────────── ;; Returns 4-tuple {ok, Status, Headers, Body}; Status = 200, ;; Body binary equals "hello from python\n". (epoch 20) (eval "(get (erlang-eval-ast \"{ok, Status, _H, _B} = httpc:request(<<__URL_HELLO__>>, get, [], <<>>), case Status of 200 -> true; _ -> false end\") :name)") (epoch 21) (eval "(get (erlang-eval-ast \"{ok, _S, _H, Body} = httpc:request(<<__URL_HELLO__>>, get, [], <<>>), case Body of <<__BODY_HELLO__,10>> -> true; _ -> false end\") :name)") ;; Headers come back as Erlang proplist (i.e. a cons) (epoch 22) (eval "(get (erlang-eval-ast \"{ok, _S, Headers, _B} = httpc:request(<<__URL_HELLO__>>, get, [], <<>>), is_list(Headers)\") :name)") ;; 404 for unknown path -> Status 404 (not an error tuple) (epoch 23) (eval "(get (erlang-eval-ast \"{ok, Status, _H, _B} = httpc:request(<<__URL_404__>>, get, [], <<>>), case Status of 404 -> true; _ -> false end\") :name)") ;; Method passed as binary works too (epoch 24) (eval "(get (erlang-eval-ast \"{ok, Status, _H, _B} = httpc:request(<<__URL_HELLO__>>, <<__GET__>>, [], <<>>), case Status of 200 -> true; _ -> false end\") :name)") EPOCHS sed -i "s|__URL_HELLO__|${URL_HELLO_BYTES}|g; s|__URL_404__|${URL_404_BYTES}|g; s|__URL_BAD__|${URL_BADBODY_BYTES}|g; s|__BODY_HELLO__|${BODY_HELLO_BYTES}|g; s|__GET__|${GET_METHOD_BYTES}|g" "$TMPFILE" OUTPUT=$(timeout 120 "$SX_SERVER" < "$TMPFILE" 2>/dev/null) check() { local epoch="$1" desc="$2" expected="$3" local actual actual=$(echo "$OUTPUT" | awk -v e="$epoch" ' $0 ~ "^\\(ok-len " e " " { getline; print; exit } $0 ~ "^\\(ok " e " " { print; exit } $0 ~ "^\\(error " e " " { print; exit } ') [ -z "$actual" ] && actual="" if echo "$actual" | grep -qF -- "$expected"; then PASS=$((PASS+1)) [ "$VERBOSE" = "-v" ] && echo " ok $desc" else FAIL=$((FAIL+1)) ERRORS+=" FAIL [$desc] (epoch $epoch) expected: $expected | actual: $actual " fi } check 10 "BIF registered under httpc/request/4" "true" check 11 "BIF marked non-pure" "false" check 12 "no /1 arity registered" "true" check 13 "non-binary URL -> badarg" "ok" check 14 "non-binary body -> badarg" "ok" check 20 "live GET returns Status 200" "true" check 21 "live GET Body is hello text" "true" check 22 "Headers come back as proplist" "true" check 23 "404 surfaces as {ok, 404, ...}" "true" check 24 "method passed as binary works" "true" TOTAL=$((PASS+FAIL)) if [ $FAIL -eq 0 ]; then echo "ok $PASS/$TOTAL next/tests/httpc_request.sh passed" else echo "FAIL $PASS/$TOTAL passed, $FAIL failed:" echo "$ERRORS" fi [ $FAIL -eq 0 ]