Something went wrong. Try again.
Cloudflare Cap'n'Web Go implementation
Something went wrong. Try again.
capnweb-go batch_test.go
5.3 kB · 233 lines
Go
at cmd-repl
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234package capnweb
import ( "bytes" "context" "encoding/json" "io" "net/http" "net/http/httptest" "strings" "testing")
type batchTestService struct{}
func (s *batchTestService) Greet(_ context.Context, name string) (string, error) { return "Hello, " + name + "!", nil}
func (s *batchTestService) Add(_ context.Context, a, b float64) (float64, error) { return a + b, nil}
func (s *batchTestService) GetChild(_ context.Context) (*childService, error) { return &childService{}, nil}
func TestBatchHandlerSingleCall(t *testing.T) { handler := BatchHandler(&batchTestService{}) srv := httptest.NewServer(handler) defer srv.Close()
body := `["push",["import",0,"Greet",["World"]]]` + "\n" + `["pull",1]` + "\n"
resp, err := http.Post(srv.URL, "application/x-ndjson", strings.NewReader(body)) if err != nil { t.Fatal(err) } defer resp.Body.Close()
if resp.StatusCode != 200 { b, _ := io.ReadAll(resp.Body) t.Fatalf("status %d: %s", resp.StatusCode, b) }
msgs, err := ReadNDJSON(resp.Body) if err != nil { t.Fatalf("read response: %v", err) }
// Should have a resolve message. found := false for _, msg := range msgs { if rm, ok := msg.(ResolveMsg); ok { var val string _ = json.Unmarshal(rm.Expr, &val) if val == "Hello, World!" { found = true } } } if !found { t.Fatalf("expected resolve with 'Hello, World!', got %d messages", len(msgs)) }}
func TestBatchHandlerPipelinedCalls(t *testing.T) { handler := BatchHandler(&batchTestService{}) srv := httptest.NewServer(handler) defer srv.Close()
// Two calls in one batch. body := `["push",["import",0,"Greet",["Alice"]]]` + "\n" + `["pull",1]` + "\n" + `["push",["import",0,"Add",[3,4]]]` + "\n" + `["pull",2]` + "\n"
resp, err := http.Post(srv.URL, "application/x-ndjson", strings.NewReader(body)) if err != nil { t.Fatal(err) } defer resp.Body.Close()
msgs, err := ReadNDJSON(resp.Body) if err != nil { t.Fatalf("read response: %v", err) }
resolves := 0 for _, msg := range msgs { if _, ok := msg.(ResolveMsg); ok { resolves++ } } if resolves < 2 { t.Fatalf("expected at least 2 resolves, got %d (total msgs: %d)", resolves, len(msgs)) }}
func TestBatchHandlerPipeline(t *testing.T) { handler := BatchHandler(&batchTestService{}) srv := httptest.NewServer(handler) defer srv.Close()
// Pipeline: GetChild → ChildMethod in one batch, only pull the final result. body := `["push",["import",0,"GetChild",[]]]` + "\n" + `["push",["pipeline",1,"ChildMethod",[]]]` + "\n" + `["pull",2]` + "\n"
resp, err := http.Post(srv.URL, "application/x-ndjson", strings.NewReader(body)) if err != nil { t.Fatal(err) } defer resp.Body.Close()
msgs, err := ReadNDJSON(resp.Body) if err != nil { t.Fatalf("read response: %v", err) }
found := false for _, msg := range msgs { if rm, ok := msg.(ResolveMsg); ok && rm.ExportID == 2 { var val string _ = json.Unmarshal(rm.Expr, &val) if val == "from child" { found = true } } } if !found { t.Fatalf("expected resolve with 'from child', got %d messages", len(msgs)) }}
func TestBatchHandlerEmptyBody(t *testing.T) { handler := BatchHandler(&batchTestService{}) srv := httptest.NewServer(handler) defer srv.Close()
resp, err := http.Post(srv.URL, "application/x-ndjson", strings.NewReader("")) if err != nil { t.Fatal(err) } defer resp.Body.Close()
if resp.StatusCode != 200 { t.Fatalf("status = %d; want 200", resp.StatusCode) } body, _ := io.ReadAll(resp.Body) if len(bytes.TrimSpace(body)) != 0 { t.Fatalf("expected empty response body, got %q", body) }}
func TestBatchHandlerMethodNotAllowed(t *testing.T) { handler := BatchHandler(&batchTestService{}) srv := httptest.NewServer(handler) defer srv.Close()
resp, err := http.Get(srv.URL) if err != nil { t.Fatal(err) } defer resp.Body.Close()
if resp.StatusCode != http.StatusMethodNotAllowed { t.Fatalf("status = %d; want 405", resp.StatusCode) }}
func TestBatchClient(t *testing.T) { handler := BatchHandler(&batchTestService{}) srv := httptest.NewServer(handler) defer srv.Close()
client := &BatchClient{URL: srv.URL}
msgs := []Message{ PushMsg{Expr: json.RawMessage(`["import",0,"Greet",["Bob"]]`)}, PullMsg{ImportID: 1}, }
resp, err := client.Do(context.Background(), msgs) if err != nil { t.Fatal(err) }
found := false for _, msg := range resp { if rm, ok := msg.(ResolveMsg); ok { var val string _ = json.Unmarshal(rm.Expr, &val) if val == "Hello, Bob!" { found = true } } } if !found { t.Fatalf("expected resolve with 'Hello, Bob!', got %d messages", len(resp)) }}
func TestNDJSONRoundTrip(t *testing.T) { msgs := []Message{ PushMsg{Expr: json.RawMessage(`42`)}, PullMsg{ImportID: 1}, ResolveMsg{ExportID: 1, Expr: json.RawMessage(`"ok"`)}, }
var buf bytes.Buffer if err := WriteNDJSON(&buf, msgs); err != nil { t.Fatal(err) }
got, err := ReadNDJSON(&buf) if err != nil { t.Fatal(err) }
if len(got) != len(msgs) { t.Fatalf("got %d messages; want %d", len(got), len(msgs)) }
for i := range msgs { wantJSON, _ := MarshalMessage(msgs[i]) gotJSON, _ := MarshalMessage(got[i]) if string(wantJSON) != string(gotJSON) { t.Fatalf("message %d: %s != %s", i, gotJSON, wantJSON) } }}