github.com/msales/pkg/v3@v3.24.0/httpx/httptest/server_test.go (about) 1 package httptest_test 2 3 import ( 4 "bytes" 5 "io/ioutil" 6 "net/http" 7 "testing" 8 9 "github.com/msales/pkg/v3/httpx" 10 "github.com/msales/pkg/v3/httpx/httptest" 11 "github.com/stretchr/testify/assert" 12 ) 13 14 func TestServer_HandlesExpectation(t *testing.T) { 15 s := httptest.NewServer(t) 16 defer s.Close() 17 18 s.On("GET", "/test/path") 19 20 res, err := httpx.DefaultClient.Get(s.URL() + "/test/path") 21 assert.NoError(t, err) 22 assert.Equal(t, 200, res.StatusCode) 23 } 24 25 func TestServer_HandlesAnythingMethodExpectation(t *testing.T) { 26 s := httptest.NewServer(t) 27 defer s.Close() 28 29 s.On(httptest.Anything, "/test/path") 30 31 res, err := httpx.DefaultClient.Post(s.URL()+"/test/path", "text/plain", bytes.NewReader([]byte{})) 32 assert.NoError(t, err) 33 assert.Equal(t, 200, res.StatusCode) 34 } 35 36 func TestServer_HandlesAnythingPathExpectation(t *testing.T) { 37 s := httptest.NewServer(t) 38 defer s.Close() 39 40 s.On("GET", httptest.Anything) 41 42 res, err := httpx.DefaultClient.Get(s.URL() + "/test/path") 43 assert.NoError(t, err) 44 assert.Equal(t, 200, res.StatusCode) 45 } 46 47 func TestServer_HandlesWildcardPathExpectation(t *testing.T) { 48 s := httptest.NewServer(t) 49 defer s.Close() 50 51 s.On("GET", "/test/*") 52 53 res, err := httpx.DefaultClient.Get(s.URL() + "/test/path") 54 assert.NoError(t, err) 55 assert.Equal(t, 200, res.StatusCode) 56 } 57 58 func TestServer_HandlesUnexpectedMethodRequest(t *testing.T) { 59 mockT := new(testing.T) 60 defer func() { 61 if !mockT.Failed() { 62 t.Error("Expected error when no expectation on request") 63 } 64 65 }() 66 67 s := httptest.NewServer(mockT) 68 defer s.Close() 69 70 s.On("POST", "/") 71 72 httpx.DefaultClient.Get(s.URL() + "/test/path") 73 } 74 75 func TestServer_HandlesUnexpectedPathRequest(t *testing.T) { 76 mockT := new(testing.T) 77 defer func() { 78 if !mockT.Failed() { 79 t.Error("Expected error when no expectation on request") 80 } 81 82 }() 83 84 s := httptest.NewServer(mockT) 85 defer s.Close() 86 s.On("GET", "/foobar") 87 88 s.On("GET", "/") 89 90 httpx.DefaultClient.Get(s.URL() + "/test/path") 91 } 92 93 func TestServer_HandlesExpectationNTimes(t *testing.T) { 94 mockT := new(testing.T) 95 defer func() { 96 if !mockT.Failed() { 97 t.Error("Expected error when expectation times used") 98 } 99 100 }() 101 102 s := httptest.NewServer(mockT) 103 defer s.Close() 104 s.On("GET", "/test/path").Times(2) 105 106 httpx.DefaultClient.Get(s.URL() + "/test/path") 107 httpx.DefaultClient.Get(s.URL() + "/test/path") 108 httpx.DefaultClient.Get(s.URL() + "/test/path") 109 } 110 111 func TestServer_HandlesExpectationUnlimitedTimes(t *testing.T) { 112 mockT := new(testing.T) 113 defer func() { 114 if mockT.Failed() { 115 t.Error("Unexpected error on request") 116 } 117 118 }() 119 120 s := httptest.NewServer(mockT) 121 defer s.Close() 122 s.On("GET", "/test/path") 123 124 httpx.DefaultClient.Get(s.URL() + "/test/path") 125 httpx.DefaultClient.Get(s.URL() + "/test/path") 126 } 127 128 func TestServer_ExpectationReturnsBodyBytes(t *testing.T) { 129 s := httptest.NewServer(t) 130 defer s.Close() 131 132 s.On("GET", "/test/path").Returns(400, []byte("test")) 133 134 res, err := httpx.DefaultClient.Get(s.URL() + "/test/path") 135 assert.NoError(t, err) 136 assert.Equal(t, 400, res.StatusCode) 137 b, _ := ioutil.ReadAll(res.Body) 138 assert.Equal(t, []byte("test"), b) 139 140 res.Body.Close() 141 } 142 143 func TestServer_ExpectationReturnsBodyString(t *testing.T) { 144 s := httptest.NewServer(t) 145 defer s.Close() 146 147 s.On("GET", "/test/path").ReturnsString(400, "test") 148 149 res, err := httpx.DefaultClient.Get(s.URL() + "/test/path") 150 assert.NoError(t, err) 151 assert.Equal(t, 400, res.StatusCode) 152 b, _ := ioutil.ReadAll(res.Body) 153 assert.Equal(t, []byte("test"), b) 154 155 res.Body.Close() 156 } 157 158 func TestServer_ExpectationReturnsStatusCode(t *testing.T) { 159 s := httptest.NewServer(t) 160 defer s.Close() 161 162 s.On("GET", "/test/path").ReturnsStatus(400) 163 164 res, err := httpx.DefaultClient.Get(s.URL() + "/test/path") 165 assert.NoError(t, err) 166 assert.Equal(t, 400, res.StatusCode) 167 b, _ := ioutil.ReadAll(res.Body) 168 assert.Len(t, b, 0) 169 170 res.Body.Close() 171 } 172 173 func TestServer_ExpectationReturnsHeaders(t *testing.T) { 174 s := httptest.NewServer(t) 175 defer s.Close() 176 177 s.On("GET", "/test/path").Header("foo", "bar").ReturnsStatus(200) 178 179 res, err := httpx.DefaultClient.Get(s.URL() + "/test/path") 180 assert.NoError(t, err) 181 v := res.Header.Get("foo") 182 assert.Equal(t, "bar", v) 183 184 res.Body.Close() 185 } 186 187 func TestServer_ExpectationUsesHandleFunc(t *testing.T) { 188 s := httptest.NewServer(t) 189 defer s.Close() 190 191 s.On("GET", "/test/path").Handle(func(w http.ResponseWriter, r *http.Request) { 192 w.WriteHeader(400) 193 }) 194 195 res, err := httpx.DefaultClient.Get(s.URL() + "/test/path") 196 assert.NoError(t, err) 197 assert.Equal(t, 400, res.StatusCode) 198 } 199 200 func TestServer_AssertExpectationsOnUnlimited(t *testing.T) { 201 mockT := new(testing.T) 202 defer func() { 203 if !mockT.Failed() { 204 t.Error("Expected error when asserting expectations") 205 } 206 207 }() 208 209 s := httptest.NewServer(mockT) 210 defer s.Close() 211 s.On("POST", "/") 212 213 s.AssertExpectations() 214 } 215 216 func TestServer_AssertExpectationsOnNTimes(t *testing.T) { 217 mockT := new(testing.T) 218 defer func() { 219 if !mockT.Failed() { 220 t.Error("Expected error when asserting expectations") 221 } 222 223 }() 224 225 s := httptest.NewServer(mockT) 226 defer s.Close() 227 s.On("POST", "/").Times(1) 228 229 s.AssertExpectations() 230 }