diff options
author | Harrison Healey <harrisonmhealey@gmail.com> | 2018-09-07 09:24:18 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-09-07 09:24:18 -0400 |
commit | 0027d998555d47f9a75a896d8c6c85a8b4645ad0 (patch) | |
tree | 4a15d63c143e9891921d75f102dd008852c9abb2 /app/http_service_test.go | |
parent | 291000703320cc53c3bdecd2553fa4a3021d7234 (diff) | |
download | chat-0027d998555d47f9a75a896d8c6c85a8b4645ad0.tar.gz chat-0027d998555d47f9a75a896d8c6c85a8b4645ad0.tar.bz2 chat-0027d998555d47f9a75a896d8c6c85a8b4645ad0.zip |
MM-11855 Add App.HTTPService to allow mocking of HTTP client (#9359)
* MM-11855 Add App.HTTPService to allow mocking of HTTP client
* Initialize HTTPService earlier
Diffstat (limited to 'app/http_service_test.go')
-rw-r--r-- | app/http_service_test.go | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/app/http_service_test.go b/app/http_service_test.go new file mode 100644 index 000000000..396a991b1 --- /dev/null +++ b/app/http_service_test.go @@ -0,0 +1,65 @@ +package app + +import ( + "io/ioutil" + "net/http" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestMockHTTPService(t *testing.T) { + getCalled := false + putCalled := false + + handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path == "/get" && r.Method == http.MethodGet { + getCalled = true + + w.WriteHeader(http.StatusOK) + w.Write([]byte("OK")) + } else if r.URL.Path == "/put" && r.Method == http.MethodPut { + putCalled = true + + w.WriteHeader(http.StatusCreated) + w.Write([]byte("CREATED")) + } else { + w.WriteHeader(http.StatusNotFound) + } + }) + + th := Setup().MockHTTPService(handler) + defer th.TearDown() + + url := th.MockedHTTPService.Server.URL + + t.Run("GET", func(t *testing.T) { + client := th.App.HTTPService.MakeClient(false) + + resp, err := client.Get(url + "/get") + defer consumeAndClose(resp) + + bodyContents, _ := ioutil.ReadAll(resp.Body) + + require.Nil(t, err) + assert.Equal(t, http.StatusOK, resp.StatusCode) + assert.Equal(t, "OK", string(bodyContents)) + assert.True(t, getCalled) + }) + + t.Run("PUT", func(t *testing.T) { + client := th.App.HTTPService.MakeClient(false) + + request, _ := http.NewRequest(http.MethodPut, url+"/put", nil) + resp, err := client.Do(request) + defer consumeAndClose(resp) + + bodyContents, _ := ioutil.ReadAll(resp.Body) + + require.Nil(t, err) + assert.Equal(t, http.StatusCreated, resp.StatusCode) + assert.Equal(t, "CREATED", string(bodyContents)) + assert.True(t, putCalled) + }) +} |