package sdk import ( "net/http" "net/http/httptest" "testing" ) func TestClient_Do(t *testing.T) { t.Parallel() var gotPath, gotAuth string server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { gotPath = r.URL.Path gotAuth = r.Header.Get("Authorization") w.WriteHeader(http.StatusOK) })) defer server.Close() c := NewClient(server.URL, "secret") req, err := http.NewRequest(http.MethodGet, "/some/path", nil) if err != nil { t.Fatal(err) } res, err := c.Do(req) if err != nil { t.Fatal(err) } defer res.Body.Close() if got, want := gotAuth, "Bearer secret"; got != want { t.Errorf("expected Authorization %q to be %q", got, want) } if got, want := gotPath, "/some/path"; got != want { t.Errorf("expected path %q to be %q", got, want) } } func TestHasContentType(t *testing.T) { t.Parallel() cases := []struct { name string contentType string mimetype string want bool }{ {"exact", "application/json", "application/json", true}, {"with charset", "application/json; charset=utf-8", "application/json", true}, {"multiple", "text/plain, application/json", "application/json", true}, {"wrong type", "text/plain", "application/json", false}, {"empty matches octet-stream", "", "application/octet-stream", true}, {"empty does not match json", "", "application/json", false}, } for _, tc := range cases { tc := tc t.Run(tc.name, func(t *testing.T) { t.Parallel() res := &http.Response{Header: http.Header{}} if tc.contentType != "" { res.Header.Set("content-type", tc.contentType) } if got := HasContentType(res, tc.mimetype); got != tc.want { t.Errorf("expected HasContentType(%q, %q) to be %v", tc.contentType, tc.mimetype, tc.want) } }) } }