@@ -2,6 +2,8 @@ package loops
22
33import (
44 "bytes"
5+ "context"
6+ "encoding/json"
57 "fmt"
68 "io"
79 "net/http"
@@ -239,3 +241,118 @@ func TestDo_Retries(t *testing.T) {
239241 })
240242 }
241243}
244+
245+ func TestDo_Exported (t * testing.T ) {
246+ var gotMethod , gotPath , gotAuth , gotUA , gotCT string
247+ var gotBody []byte
248+ server := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
249+ gotMethod = r .Method
250+ gotPath = r .URL .Path
251+ gotAuth = r .Header .Get ("Authorization" )
252+ gotUA = r .Header .Get ("User-Agent" )
253+ gotCT = r .Header .Get ("Content-Type" )
254+ gotBody , _ = io .ReadAll (r .Body )
255+ w .WriteHeader (http .StatusCreated )
256+ fmt .Fprint (w , `{"id":"abc"}` )
257+ }))
258+ defer server .Close ()
259+
260+ client := NewClient ("test-key" , WithBaseURL (server .URL ))
261+ payload := map [string ]any {"email" : "user@example.com" }
262+ resp , err := client .Do (context .Background (), http .MethodPost , "/contacts/create" , payload )
263+ if err != nil {
264+ t .Fatalf ("unexpected error: %v" , err )
265+ }
266+ defer resp .Body .Close ()
267+
268+ if resp .StatusCode != http .StatusCreated {
269+ t .Errorf ("status = %d, want %d" , resp .StatusCode , http .StatusCreated )
270+ }
271+ if gotMethod != http .MethodPost {
272+ t .Errorf ("method = %q, want %q" , gotMethod , http .MethodPost )
273+ }
274+ if gotPath != "/contacts/create" {
275+ t .Errorf ("path = %q, want %q" , gotPath , "/contacts/create" )
276+ }
277+ if gotAuth != "Bearer test-key" {
278+ t .Errorf ("Authorization = %q, want %q" , gotAuth , "Bearer test-key" )
279+ }
280+ if gotUA != "loops-go/" + Version {
281+ t .Errorf ("User-Agent = %q, want %q" , gotUA , "loops-go/" + Version )
282+ }
283+ if gotCT != "application/json" {
284+ t .Errorf ("Content-Type = %q, want %q" , gotCT , "application/json" )
285+ }
286+ var decoded map [string ]any
287+ if err := json .Unmarshal (gotBody , & decoded ); err != nil {
288+ t .Fatalf ("body not valid JSON: %v (body=%q)" , err , gotBody )
289+ }
290+ if decoded ["email" ] != "user@example.com" {
291+ t .Errorf ("body email = %v, want %q" , decoded ["email" ], "user@example.com" )
292+ }
293+ }
294+
295+ func TestDo_NilBody (t * testing.T ) {
296+ var gotCT string
297+ var gotLen int64
298+ server := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
299+ gotCT = r .Header .Get ("Content-Type" )
300+ gotLen = r .ContentLength
301+ w .WriteHeader (http .StatusOK )
302+ }))
303+ defer server .Close ()
304+
305+ client := NewClient ("test-key" , WithBaseURL (server .URL ))
306+ resp , err := client .Do (context .Background (), http .MethodGet , "/api-key" , nil )
307+ if err != nil {
308+ t .Fatalf ("unexpected error: %v" , err )
309+ }
310+ defer resp .Body .Close ()
311+
312+ if gotCT != "" {
313+ t .Errorf ("Content-Type = %q, want empty" , gotCT )
314+ }
315+ if gotLen > 0 {
316+ t .Errorf ("ContentLength = %d, want 0" , gotLen )
317+ }
318+ }
319+
320+ func TestDo_NonOKReturnsRawResponse (t * testing.T ) {
321+ server := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
322+ w .WriteHeader (http .StatusBadRequest )
323+ fmt .Fprint (w , `{"error":"bad request"}` )
324+ }))
325+ defer server .Close ()
326+
327+ client := NewClient ("test-key" , WithBaseURL (server .URL ))
328+ resp , err := client .Do (context .Background (), http .MethodGet , "/whatever" , nil )
329+ if err != nil {
330+ t .Fatalf ("unexpected error: %v" , err )
331+ }
332+ defer resp .Body .Close ()
333+
334+ if resp .StatusCode != http .StatusBadRequest {
335+ t .Errorf ("status = %d, want %d" , resp .StatusCode , http .StatusBadRequest )
336+ }
337+ body , _ := io .ReadAll (resp .Body )
338+ if string (body ) != `{"error":"bad request"}` {
339+ t .Errorf ("body = %q, want raw error body passthrough" , body )
340+ }
341+ }
342+
343+ func TestDo_ContextCancel (t * testing.T ) {
344+ server := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
345+ <- r .Context ().Done ()
346+ }))
347+ defer server .Close ()
348+
349+ client := NewClient ("test-key" , WithBaseURL (server .URL ))
350+ ctx , cancel := context .WithCancel (context .Background ())
351+ cancel ()
352+
353+ resp , err := client .Do (ctx , http .MethodGet , "/" , nil )
354+ if err == nil {
355+ resp .Body .Close ()
356+ t .Fatal ("expected error from cancelled context, got nil" )
357+ }
358+ }
0 commit comments