github.com/jlevesy/mattermost-server@v5.3.2-0.20181003190404-7468f35cb0c8+incompatible/app/http_service_test.go (about) 1 package app 2 3 import ( 4 "io/ioutil" 5 "net/http" 6 "testing" 7 8 "github.com/stretchr/testify/assert" 9 "github.com/stretchr/testify/require" 10 ) 11 12 func TestMockHTTPService(t *testing.T) { 13 getCalled := false 14 putCalled := false 15 16 handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 17 if r.URL.Path == "/get" && r.Method == http.MethodGet { 18 getCalled = true 19 20 w.WriteHeader(http.StatusOK) 21 w.Write([]byte("OK")) 22 } else if r.URL.Path == "/put" && r.Method == http.MethodPut { 23 putCalled = true 24 25 w.WriteHeader(http.StatusCreated) 26 w.Write([]byte("CREATED")) 27 } else { 28 w.WriteHeader(http.StatusNotFound) 29 } 30 }) 31 32 th := Setup().MockHTTPService(handler) 33 defer th.TearDown() 34 35 url := th.MockedHTTPService.Server.URL 36 37 t.Run("GET", func(t *testing.T) { 38 client := th.App.HTTPService.MakeClient(false) 39 40 resp, err := client.Get(url + "/get") 41 defer consumeAndClose(resp) 42 43 bodyContents, _ := ioutil.ReadAll(resp.Body) 44 45 require.Nil(t, err) 46 assert.Equal(t, http.StatusOK, resp.StatusCode) 47 assert.Equal(t, "OK", string(bodyContents)) 48 assert.True(t, getCalled) 49 }) 50 51 t.Run("PUT", func(t *testing.T) { 52 client := th.App.HTTPService.MakeClient(false) 53 54 request, _ := http.NewRequest(http.MethodPut, url+"/put", nil) 55 resp, err := client.Do(request) 56 defer consumeAndClose(resp) 57 58 bodyContents, _ := ioutil.ReadAll(resp.Body) 59 60 require.Nil(t, err) 61 assert.Equal(t, http.StatusCreated, resp.StatusCode) 62 assert.Equal(t, "CREATED", string(bodyContents)) 63 assert.True(t, putCalled) 64 }) 65 }