From eb3dfa8bdb0da56294a603c4cb483ae055ce668e Mon Sep 17 00:00:00 2001 From: Joel Rebello Date: Wed, 7 Dec 2022 11:03:09 +0100 Subject: [PATCH] api/v1/requests: Adds json Unmarshal test case for Client.do --- pkg/api/v1/requests_test.go | 97 +++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 pkg/api/v1/requests_test.go diff --git a/pkg/api/v1/requests_test.go b/pkg/api/v1/requests_test.go new file mode 100644 index 00000000..e1cceb3b --- /dev/null +++ b/pkg/api/v1/requests_test.go @@ -0,0 +1,97 @@ +package serverservice + +import ( + "context" + "net/http" + "net/http/httptest" + "testing" + + "github.com/stretchr/testify/assert" +) + +func Test_Do(t *testing.T) { + serveMux1 := http.NewServeMux() + + serveMux1.HandleFunc( + "/test", + func(w http.ResponseWriter, r *http.Request) { + switch r.Method { + case http.MethodGet: + assert.Equal(t, "bearer dummy", r.Header.Get("Authorization")) + + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(`{"foo": "bar"}`)) + default: + t.Fatal("expected GET request, got: " + r.Method) + } + }, + ) + + serveMux2 := http.NewServeMux() + + serveMux2.HandleFunc( + "/brokenjson", + func(w http.ResponseWriter, r *http.Request) { + switch r.Method { + case http.MethodGet: + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(`{"foo": "bar"}\n{"foo": "bar"}`)) + default: + t.Fatal("expected GET request, got: " + r.Method) + } + }, + ) + + testcases := []struct { + name string + endpoint string + expectedResult interface{} + serveMux *http.ServeMux + expectedErrorContains string + }{ + { + "happy path", + "/test", + map[string]string{"foo": "bar"}, + serveMux1, + "", + }, + { + "broken json returns error", + "/brokenjson", + map[string]string{"foo": "bar"}, + serveMux2, + "invalid character", + }, + } + + for _, tc := range testcases { + t.Run(tc.name, func(t *testing.T) { + server := httptest.NewServer(tc.serveMux) + client, err := NewClientWithToken("dummy", server.URL, nil) + if err != nil { + t.Fatal(err) + } + + req, err := http.NewRequestWithContext(context.TODO(), "GET", server.URL+tc.endpoint, nil) + if err != nil { + t.Fatal(err) + } + + result := map[string]string{} + + err = client.do(req, &result) + if tc.expectedErrorContains != "" { + if err == nil { + t.Fatalf("expected error: '%s', got nil", tc.expectedErrorContains) + } + + assert.Contains(t, err.Error(), tc.expectedErrorContains) + return + } + + assert.Nil(t, err) + assert.Equal(t, tc.expectedResult, result) + }) + } +}