fed-sx-m1: Step 8d-dispatch-post — format-aware POST /activity (cid_response_for + post_activity_response_for) + 13 tests
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 28s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 28s
This commit is contained in:
@@ -12,7 +12,8 @@
|
||||
cid_response/1,
|
||||
accept_format/1, accept_format_from/1,
|
||||
capabilities_body_for/1,
|
||||
content_type_for/1, ok_response/2]).
|
||||
content_type_for/1, ok_response/2,
|
||||
cid_response_for/2, post_activity_response_for/1]).
|
||||
|
||||
%% HTTP request router per design §16.1.
|
||||
%%
|
||||
@@ -209,27 +210,30 @@ post_activity_response() ->
|
||||
handle_post_activity(Req, Cfg) ->
|
||||
case check_bearer(Req, Cfg) of
|
||||
ok ->
|
||||
publish_if_kernel(Req);
|
||||
F = accept_format_from(Req),
|
||||
publish_if_kernel(Req, F);
|
||||
{error, _} ->
|
||||
unauthorized_response()
|
||||
end.
|
||||
|
||||
%% publish_if_kernel/1 — if the nx_kernel gen_server is registered,
|
||||
%% publish_if_kernel/2 — if the nx_kernel gen_server is registered,
|
||||
%% delegate the publish there and translate the result. Otherwise
|
||||
%% keep the stub response so the auth-only tests stay green without
|
||||
%% having to spin up a kernel process.
|
||||
publish_if_kernel(Req) ->
|
||||
%% having to spin up a kernel process. Format threads through to
|
||||
%% both stub and CID responses so the Content-Type matches what
|
||||
%% the client asked for via Accept.
|
||||
publish_if_kernel(Req, F) ->
|
||||
case erlang:whereis(nx_kernel) of
|
||||
undefined ->
|
||||
post_activity_response();
|
||||
post_activity_response_for(F);
|
||||
_Pid ->
|
||||
Body = field(body, Req),
|
||||
Request = [{type, create}, {object, Body}],
|
||||
case nx_kernel:publish(Request) of
|
||||
{ok, Result} ->
|
||||
case envelope:get_field(cid, Result) of
|
||||
{ok, Cid} -> cid_response(Cid);
|
||||
_ -> post_activity_response()
|
||||
{ok, Cid} -> cid_response_for(Cid, F);
|
||||
_ -> post_activity_response_for(F)
|
||||
end;
|
||||
{error, _} ->
|
||||
validation_failed_response()
|
||||
@@ -423,3 +427,58 @@ ok_response(Body, Format) ->
|
||||
[{status, 200},
|
||||
{headers, [{CTKey, content_type_for(Format)}]},
|
||||
{body, Body}].
|
||||
|
||||
%% cid_response_for/2 — format-aware version of cid_response/1.
|
||||
%% Each variant emits a syntactically appropriate body for the
|
||||
%% chosen format and tags the response with the matching
|
||||
%% Content-Type via ok_response/2.
|
||||
|
||||
cid_response_for(Cid, text) ->
|
||||
cid_response(Cid);
|
||||
%% `{"cid":"<cid>"}\n` — 8-byte prefix + cid + 3-byte suffix
|
||||
cid_response_for(Cid, json) ->
|
||||
Pre = <<123,34,99,105,100,34,58,34>>, % '{"cid":"'
|
||||
Suf = <<34,125,10>>, % '"}\n'
|
||||
ok_response(<<Pre/binary, Cid/binary, Suf/binary>>, json);
|
||||
cid_response_for(Cid, activity_json) ->
|
||||
Pre = <<123,34,99,105,100,34,58,34>>,
|
||||
Suf = <<34,125,10>>,
|
||||
ok_response(<<Pre/binary, Cid/binary, Suf/binary>>, activity_json);
|
||||
%% `(cid "<cid>")\n` — 6-byte prefix + cid + 3-byte suffix
|
||||
cid_response_for(Cid, sx) ->
|
||||
Pre = <<40,99,105,100,32,34>>, % '(cid "'
|
||||
Suf = <<34,41,10>>, % '")\n'
|
||||
ok_response(<<Pre/binary, Cid/binary, Suf/binary>>, sx);
|
||||
%% v1 cbor stub: the raw CID bytes with the application/cbor CT.
|
||||
%% Real cbor encoding (A1 63 cid 78 <len> ...) lands later.
|
||||
cid_response_for(Cid, cbor) ->
|
||||
ok_response(Cid, cbor);
|
||||
cid_response_for(Cid, _) ->
|
||||
cid_response(Cid).
|
||||
|
||||
%% post_activity_response_for/1 — format-aware version of
|
||||
%% post_activity_response/0 (the kernel-absent stub).
|
||||
|
||||
post_activity_response_for(text) ->
|
||||
post_activity_response();
|
||||
%% `{"status":"stub"}\n` — hand-spelled
|
||||
post_activity_response_for(json) ->
|
||||
Body = <<123,34,115,116,97,116,117,115,34,58,34,
|
||||
115,116,117,98,34,125,10>>,
|
||||
ok_response(Body, json);
|
||||
post_activity_response_for(activity_json) ->
|
||||
Body = <<123,34,115,116,97,116,117,115,34,58,34,
|
||||
115,116,117,98,34,125,10>>,
|
||||
ok_response(Body, activity_json);
|
||||
%% `(status "stub")\n`
|
||||
post_activity_response_for(sx) ->
|
||||
Body = <<40,115,116,97,116,117,115,32,34,
|
||||
115,116,117,98,34,41,10>>,
|
||||
ok_response(Body, sx);
|
||||
post_activity_response_for(cbor) ->
|
||||
%% Same body as text but with cbor CT — clients see the same
|
||||
%% bytes as the text fallback. Step 8d-cbor encoder will replace.
|
||||
[_, _, {body, Body}] = post_activity_response(),
|
||||
ok_response(Body, cbor);
|
||||
post_activity_response_for(_) ->
|
||||
post_activity_response().
|
||||
|
||||
142
next/tests/http_post_format.sh
Executable file
142
next/tests/http_post_format.sh
Executable file
@@ -0,0 +1,142 @@
|
||||
#!/usr/bin/env bash
|
||||
# next/tests/http_post_format.sh — Step 8d-dispatch-post test.
|
||||
#
|
||||
# Verifies POST /activity returns format-specific bodies + the
|
||||
# right Content-Type, both for the kernel-absent stub path and
|
||||
# the kernel-present cid response. 14 cases.
|
||||
|
||||
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=""
|
||||
TMPFILE=$(mktemp); trap "rm -f $TMPFILE" EXIT
|
||||
|
||||
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")
|
||||
(epoch 2)
|
||||
(eval "(er-load-gen-server!)")
|
||||
(epoch 3)
|
||||
(eval "(get (erlang-load-module (file-read \"next/kernel/envelope.erl\")) :name)")
|
||||
(epoch 4)
|
||||
(eval "(get (erlang-load-module (file-read \"next/kernel/log.erl\")) :name)")
|
||||
(epoch 5)
|
||||
(eval "(get (erlang-load-module (file-read \"next/kernel/pipeline.erl\")) :name)")
|
||||
(epoch 6)
|
||||
(eval "(get (erlang-load-module (file-read \"next/kernel/outbox.erl\")) :name)")
|
||||
(epoch 7)
|
||||
(eval "(get (erlang-load-module (file-read \"next/kernel/nx_kernel.erl\")) :name)")
|
||||
(epoch 8)
|
||||
(eval "(get (erlang-load-module (file-read \"next/kernel/http_server.erl\")) :name)")
|
||||
|
||||
;; cid_response_for(json) body: {"cid":"foo"}\n
|
||||
(epoch 10)
|
||||
(eval "(get (erlang-eval-ast \"R = http_server:cid_response_for(<<102,111,111>>, json), case R of [_, _, {body, B}] -> B =:= <<123,34,99,105,100,34,58,34,102,111,111,34,125,10>>; _ -> false end\") :name)")
|
||||
|
||||
;; cid_response_for(json) CT is application/json
|
||||
(epoch 11)
|
||||
(eval "(get (erlang-eval-ast \"R = http_server:cid_response_for(<<102,111,111>>, json), case R of [_, {headers, [{_, CT}]}, _] -> CT =:= http_server:content_type_for(json); _ -> false end\") :name)")
|
||||
|
||||
;; cid_response_for(sx) body: (cid "foo")\n
|
||||
(epoch 12)
|
||||
(eval "(get (erlang-eval-ast \"R = http_server:cid_response_for(<<102,111,111>>, sx), case R of [_, _, {body, B}] -> B =:= <<40,99,105,100,32,34,102,111,111,34,41,10>>; _ -> false end\") :name)")
|
||||
|
||||
;; cid_response_for(text) matches cid_response/1
|
||||
(epoch 13)
|
||||
(eval "(get (erlang-eval-ast \"http_server:cid_response_for(<<102,111,111>>, text) =:= http_server:cid_response(<<102,111,111>>)\") :name)")
|
||||
|
||||
;; cid_response_for(activity_json) body == cid_response_for(json) body
|
||||
(epoch 14)
|
||||
(eval "(get (erlang-eval-ast \"[_, _, {body, BJ}] = http_server:cid_response_for(<<102,111,111>>, json), [_, _, {body, BAJ}] = http_server:cid_response_for(<<102,111,111>>, activity_json), BJ =:= BAJ\") :name)")
|
||||
|
||||
;; cid_response_for(activity_json) CT is application/activity+json
|
||||
(epoch 15)
|
||||
(eval "(get (erlang-eval-ast \"R = http_server:cid_response_for(<<102,111,111>>, activity_json), case R of [_, {headers, [{_, CT}]}, _] -> CT =:= http_server:content_type_for(activity_json); _ -> false end\") :name)")
|
||||
|
||||
;; cid_response_for(cbor) carries the raw CID as body
|
||||
(epoch 16)
|
||||
(eval "(get (erlang-eval-ast \"R = http_server:cid_response_for(<<102,111,111>>, cbor), case R of [_, _, {body, B}] -> B =:= <<102,111,111>>; _ -> false end\") :name)")
|
||||
|
||||
;; post_activity_response_for(json) has json CT
|
||||
(epoch 17)
|
||||
(eval "(get (erlang-eval-ast \"R = http_server:post_activity_response_for(json), case R of [_, {headers, [{_, CT}]}, _] -> CT =:= http_server:content_type_for(json); _ -> false end\") :name)")
|
||||
|
||||
;; post_activity_response_for(text) matches the original
|
||||
(epoch 18)
|
||||
(eval "(get (erlang-eval-ast \"http_server:post_activity_response_for(text) =:= http_server:post_activity_response()\") :name)")
|
||||
|
||||
;; End-to-end: POST /activity with Accept: application/json returns
|
||||
;; the json stub when nx_kernel is not running
|
||||
(epoch 19)
|
||||
(eval "(get (erlang-eval-ast \"Token = <<102,111,111>>, AuthKey = <<97,117,116,104,111,114,105,122,97,116,105,111,110>>, AuthVal = <<66,101,97,114,101,114,32,102,111,111>>, AcceptKey = <<97,99,99,101,112,116>>, AcceptVal = <<97,112,112,108,105,99,97,116,105,111,110,47,106,115,111,110>>, Req = [{method, <<80,79,83,84>>}, {path, http_server:activity_path()}, {headers, [{AuthKey, AuthVal}, {AcceptKey, AcceptVal}]}, {body, <<>>}], Cfg = [{publish_token, Token}], R = http_server:route(Req, Cfg), case R of [_, {headers, [{_, CT}]}, _] -> CT =:= http_server:content_type_for(json); _ -> false end\") :name)")
|
||||
|
||||
;; End-to-end: POST /activity with kernel running + Accept: application/sx
|
||||
;; returns body shaped as (cid "...")
|
||||
(epoch 20)
|
||||
(eval "(get (erlang-eval-ast \"KM = <<1,2,3,4>>, KS = [{key_id,k1},{algorithm,ed25519},{value,KM}], AS = [{public_keys,[[{id,k1},{created,0},{value,KM}]]}], nx_kernel:start_link(alice, KS, AS), Token = <<102,111,111>>, AuthKey = <<97,117,116,104,111,114,105,122,97,116,105,111,110>>, AuthVal = <<66,101,97,114,101,114,32,102,111,111>>, AcceptKey = <<97,99,99,101,112,116>>, AcceptVal = <<97,112,112,108,105,99,97,116,105,111,110,47,115,120>>, Req = [{method, <<80,79,83,84>>}, {path, http_server:activity_path()}, {headers, [{AuthKey, AuthVal}, {AcceptKey, AcceptVal}]}, {body, <<104,105>>}], Cfg = [{publish_token, Token}], R = http_server:route(Req, Cfg), case R of [_, _, {body, B}] -> http_server:match_prefix(<<40,99,105,100,32,34>>, B) =/= nomatch; _ -> false end\") :name)")
|
||||
|
||||
;; End-to-end CT for kernel-publish with json Accept matches application/json
|
||||
(epoch 21)
|
||||
(eval "(get (erlang-eval-ast \"KM = <<1,2,3,4>>, KS = [{key_id,k1},{algorithm,ed25519},{value,KM}], AS = [{public_keys,[[{id,k1},{created,0},{value,KM}]]}], nx_kernel:start_link(alice, KS, AS), Token = <<102,111,111>>, AuthKey = <<97,117,116,104,111,114,105,122,97,116,105,111,110>>, AuthVal = <<66,101,97,114,101,114,32,102,111,111>>, AcceptKey = <<97,99,99,101,112,116>>, AcceptVal = <<97,112,112,108,105,99,97,116,105,111,110,47,106,115,111,110>>, Req = [{method, <<80,79,83,84>>}, {path, http_server:activity_path()}, {headers, [{AuthKey, AuthVal}, {AcceptKey, AcceptVal}]}, {body, <<104,105>>}], Cfg = [{publish_token, Token}], R = http_server:route(Req, Cfg), case R of [_, {headers, [{_, CT}]}, _] -> CT =:= http_server:content_type_for(json); _ -> false end\") :name)")
|
||||
EPOCHS
|
||||
|
||||
OUTPUT=$(timeout 240 "$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="<no output for epoch $epoch>"
|
||||
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 8 "http_server loaded" "http_server"
|
||||
check 10 "cid_response_for(json) body" "true"
|
||||
check 11 "cid_response_for(json) CT" "true"
|
||||
check 12 "cid_response_for(sx) body" "true"
|
||||
check 13 "cid_response_for(text) preserves" "true"
|
||||
check 14 "activity_json body == json body" "true"
|
||||
check 15 "activity_json CT differs" "true"
|
||||
check 16 "cbor carries raw cid" "true"
|
||||
check 17 "post_activity stub json CT" "true"
|
||||
check 18 "post_activity stub text preserves" "true"
|
||||
check 19 "POST kernel-absent json CT" "true"
|
||||
check 20 "POST kernel-publish sx body" "true"
|
||||
check 21 "POST kernel-publish json CT" "true"
|
||||
|
||||
TOTAL=$((PASS+FAIL))
|
||||
if [ $FAIL -eq 0 ]; then
|
||||
echo "ok $PASS/$TOTAL next/tests/http_post_format.sh passed"
|
||||
else
|
||||
echo "FAIL $PASS/$TOTAL passed, $FAIL failed:"
|
||||
echo "$ERRORS"
|
||||
fi
|
||||
[ $FAIL -eq 0 ]
|
||||
Reference in New Issue
Block a user