@@ -12,6 +12,8 @@ import (
1212 "testing"
1313 "time"
1414
15+ ferretnet "github.com/MontFerret/ferret/v2/pkg/net"
16+ ferrethttp "github.com/MontFerret/ferret/v2/pkg/net/http"
1517 "github.com/MontFerret/ferret/v2/pkg/runtime"
1618)
1719
@@ -43,7 +45,7 @@ func TestClientQueryJSONList(t *testing.T) {
4345 }))
4446 defer server .Close ()
4547
46- ctx := context . Background ()
48+ ctx := networkContext ()
4749 cfg := DefaultConfig ()
4850 cfg .BaseURL = server .URL
4951 cfg .Headers .Set ("Authorization" , "Bearer token" )
@@ -107,7 +109,7 @@ func TestClientInfersPostAndReturnsFullResponse(t *testing.T) {
107109 }))
108110 defer server .Close ()
109111
110- ctx := context . Background ()
112+ ctx := networkContext ()
111113 cfg := DefaultConfig ()
112114 cfg .BaseURL = server .URL
113115 client := NewClient (cfg )
@@ -152,7 +154,7 @@ func TestClientErrorModes(t *testing.T) {
152154 }))
153155 defer server .Close ()
154156
155- ctx := context . Background ()
157+ ctx := networkContext ()
156158 cfg := DefaultConfig ()
157159 cfg .BaseURL = server .URL
158160
@@ -195,7 +197,7 @@ func TestClientResponseEncodings(t *testing.T) {
195197 }))
196198 defer server .Close ()
197199
198- ctx := context . Background ()
200+ ctx := networkContext ()
199201 cfg := DefaultConfig ()
200202 cfg .BaseURL = server .URL
201203 cfg .ResponseEncoding = EncodingText
@@ -253,7 +255,7 @@ func TestClientFormRequestEncoding(t *testing.T) {
253255 }))
254256 defer server .Close ()
255257
256- ctx := context . Background ()
258+ ctx := networkContext ()
257259 cfg := DefaultConfig ()
258260 cfg .BaseURL = server .URL
259261 client := NewClient (cfg )
@@ -297,15 +299,16 @@ func TestClientAcceptsHTTPDialect(t *testing.T) {
297299 cfg .BaseURL = server .URL
298300 client := NewClient (cfg )
299301
300- out , err := client .Query (context .Background (), runtime.Query {
302+ ctx := networkContext ()
303+ out , err := client .Query (ctx , runtime.Query {
301304 Kind : runtime .NewString ("http" ),
302305 Expression : runtime .NewString ("/users" ),
303306 })
304307 if err != nil {
305308 t .Fatalf ("unexpected query error: %v" , err )
306309 }
307310
308- length , err := out .Length (context . Background () )
311+ length , err := out .Length (ctx )
309312 if err != nil {
310313 t .Fatalf ("unexpected length error: %v" , err )
311314 }
@@ -332,7 +335,7 @@ func TestClientRequestTimeout(t *testing.T) {
332335 cfg .Timeout = int64 (10 * time .Millisecond )
333336 client := NewClient (cfg )
334337
335- _ , err := client .QueryOne (context . Background (), runtime.Query {Expression : runtime .NewString ("/slow" )})
338+ _ , err := client .QueryOne (networkContext (), runtime.Query {Expression : runtime .NewString ("/slow" )})
336339 if err == nil {
337340 t .Fatal ("expected timeout error" )
338341 }
@@ -370,6 +373,94 @@ func TestClientRejectsUnsupportedDialect(t *testing.T) {
370373 }
371374}
372375
376+ func TestClientUsesFerretHTTPClientFromContext (t * testing.T ) {
377+ t .Parallel ()
378+
379+ httpClient := & recordingHTTPClient {
380+ response : & ferrethttp.Response {
381+ StatusCode : http .StatusOK ,
382+ Status : "200 OK" ,
383+ Headers : ferrethttp.Headers {
384+ "X-Result" : []string {"ok" },
385+ },
386+ Body : []byte (`{"ok":true}` ),
387+ },
388+ }
389+ ctx := ferretnet .WithNetwork (
390+ context .Background (),
391+ ferretnet .New (ferretnet .WithHTTPClient (httpClient )),
392+ )
393+
394+ cfg := DefaultConfig ()
395+ cfg .BaseURL = "https://api.example.test/v1/"
396+ cfg .Headers .Set ("Authorization" , "Bearer token" )
397+ client := NewClient (cfg )
398+
399+ out , err := client .QueryOne (ctx , runtime.Query {
400+ Expression : runtime .NewString ("/users" ),
401+ Params : object (t , map [string ]runtime.Value {
402+ "method" : runtime .NewString ("POST" ),
403+ "query" : object (t , map [string ]runtime.Value {
404+ "active" : runtime .True ,
405+ }),
406+ "headers" : object (t , map [string ]runtime.Value {
407+ "X-Request-ID" : runtime .NewString ("req-1" ),
408+ }),
409+ "body" : object (t , map [string ]runtime.Value {
410+ "name" : runtime .NewString ("Ada" ),
411+ }),
412+ }),
413+ Options : object (t , map [string ]runtime.Value {
414+ "response" : runtime .NewString ("full" ),
415+ }),
416+ })
417+ if err != nil {
418+ t .Fatalf ("unexpected query error: %v" , err )
419+ }
420+ if got := field (t , out , "status" ); got != runtime .NewInt (http .StatusOK ) {
421+ t .Fatalf ("expected status 200, got %s" , got .String ())
422+ }
423+
424+ req := httpClient .request ()
425+ if req == nil {
426+ t .Fatal ("expected HTTP client to be called" )
427+ }
428+ if req .Method != http .MethodPost {
429+ t .Fatalf ("expected POST, got %s" , req .Method )
430+ }
431+ if req .URL != "https://api.example.test/users?active=true" {
432+ t .Fatalf ("unexpected URL: %s" , req .URL )
433+ }
434+ if got := req .Headers ["Authorization" ]; len (got ) != 1 || got [0 ] != "Bearer token" {
435+ t .Fatalf ("unexpected authorization header: %v" , got )
436+ }
437+ if got := req .Headers ["X-Request-Id" ]; len (got ) != 1 || got [0 ] != "req-1" {
438+ t .Fatalf ("unexpected request id header: %v" , got )
439+ }
440+ if got := req .Headers ["Content-Type" ]; len (got ) != 1 || got [0 ] != "application/json" {
441+ t .Fatalf ("unexpected content type: %v" , got )
442+ }
443+ if string (req .Body ) != `{"name":"Ada"}` {
444+ t .Fatalf ("unexpected body: %s" , req .Body )
445+ }
446+ }
447+
448+ func TestClientRequiresNetworkContext (t * testing.T ) {
449+ t .Parallel ()
450+
451+ cfg := DefaultConfig ()
452+ cfg .BaseURL = "https://api.example.test"
453+ client := NewClient (cfg )
454+
455+ _ , err := client .QueryOne (context .Background (), runtime.Query {Expression : runtime .NewString ("/users" )})
456+ if err == nil {
457+ t .Fatal ("expected missing network error" )
458+ }
459+ if ! strings .Contains (err .Error (), "network not found in context" ) {
460+ t .Fatalf ("unexpected error: %v" , err )
461+ }
462+ }
463+
373464func object (t * testing.T , props map [string ]runtime.Value ) * runtime.Object {
374465 t .Helper ()
375466
@@ -413,3 +504,7 @@ func readBody(t *testing.T, r *http.Request) string {
413504
414505 return string (data )
415506}
507+
508+ func networkContext () context.Context {
509+ return ferretnet .WithNetwork (context .Background (), ferretnet .New ())
510+ }
0 commit comments