package agentapi import ( "context" "encoding/json" "net/http" "net/http/httptest" "testing" "time" "github.com/google/uuid" ) func TestFrameRoundTrip(t *testing.T) { in := Frame{ID: 7, Method: http.MethodPut, Path: "/storage?key=a", Header: http.Header{"If-None-Match": {`"x"`}}} f, body, err := DecodeFrame(EncodeFrame(in, []byte("line one\nline two"))) if err != nil || f.ID != 7 || f.Path != in.Path || f.Header.Get("If-None-Match") != `"x"` || string(body) != "line one\nline two" { t.Errorf("DecodeFrame(EncodeFrame()) = %+v, %q, %v", f, body, err) } if _, _, err := DecodeFrame([]byte(`{"id":1}`)); err == nil { t.Error("DecodeFrame(no newline) succeeded") } } // An API without the stream is used over HTTP. func TestStreamFallsBackToHTTP(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.URL.Path != Prefix+"/hello" { http.NotFound(w, r) return } _ = json.NewEncoder(w).Encode(Hello{Name: "plain", Kind: "checker"}) })) defer srv.Close() c := NewClient(srv.URL, "usa_x", uuid.New(), "test") ctx, cancel := context.WithCancel(context.Background()) defer cancel() c.UseStream(ctx, StreamOptions{}) time.Sleep(100 * time.Millisecond) h, err := c.Hello(ctx) if err != nil || h.Name != "plain" { t.Fatalf("Hello() = %+v, %v", h, err) } if c.Streaming() { t.Error("Streaming() = true against an API without the stream") } }