| 1 |
// Copyright (c) 2019, Maxime Soulé |
| 2 |
// All rights reserved. |
| 3 |
// |
| 4 |
// This source code is licensed under the BSD-style license found in the |
| 5 |
// LICENSE file in the root directory of this source tree. |
| 6 |
|
| 7 |
package tdhttp |
| 8 |
|
| 9 |
import ( |
| 10 |
"encoding/json" |
| 11 |
"encoding/xml" |
| 12 |
"fmt" |
| 13 |
"net/http" |
| 14 |
"reflect" |
| 15 |
"testing" |
| 16 |
|
| 17 |
"github.com/maxatome/go-testdeep/helpers/tdutil" |
| 18 |
"github.com/maxatome/go-testdeep/internal/trace" |
| 19 |
"github.com/maxatome/go-testdeep/td" |
| 20 |
) |
| 21 |
|
| 22 |
func init() { |
| 23 |
trace.IgnorePackage() |
| 24 |
} |
| 25 |
|
| 26 |
// Response is used by Cmp*Response functions to make the HTTP |
| 27 |
// response match easier. Each field, can be a [td.TestDeep] operator |
| 28 |
// as well as the exact expected value. |
| 29 |
type Response struct { |
| 30 |
Status any // is the expected status (ignored if nil) |
| 31 |
Header any // is the expected header (ignored if nil) |
| 32 |
Cookies any // is the expected cookies (ignored if nil) |
| 33 |
Body any // is the expected body (expected to be empty if nil) |
| 34 |
} |
| 35 |
|
| 36 |
func cmpMarshaledResponse(tb testing.TB, |
| 37 |
req *http.Request, |
| 38 |
handler func(w http.ResponseWriter, r *http.Request), |
| 39 |
acceptEmptyBody bool, |
| 40 |
unmarshal func([]byte, any) error, |
| 41 |
expectedResp Response, |
| 42 |
args ...any, |
| 43 |
) bool { |
| 44 |
tb.Helper() |
| 45 |
|
| 46 |
if testName := tdutil.BuildTestName(args...); testName != "" { |
| 47 |
tb.Log(testName) |
| 48 |
} |
| 49 |
|
| 50 |
t := td.NewT(tb) |
| 51 |
defer t.AnchorsPersistTemporarily()() |
| 52 |
|
| 53 |
ta := NewTestAPI(t, http.HandlerFunc(handler)).Request(req) |
| 54 |
|
| 55 |
// Check status, nil = ignore |
| 56 |
if expectedResp.Status != nil { |
| 57 |
ta.CmpStatus(expectedResp.Status) |
| 58 |
} |
| 59 |
|
| 60 |
// Check header, nil = ignore |
| 61 |
if expectedResp.Header != nil { |
| 62 |
ta.CmpHeader(expectedResp.Header) |
| 63 |
} |
| 64 |
|
| 65 |
// Check cookie, nil = ignore |
| 66 |
if expectedResp.Cookies != nil { |
| 67 |
ta.CmpCookies(expectedResp.Cookies) |
| 68 |
} |
| 69 |
|
| 70 |
if expectedResp.Body == nil { |
| 71 |
ta.NoBody() |
| 72 |
} else { |
| 73 |
ta.cmpMarshaledBody(acceptEmptyBody, unmarshal, expectedResp.Body) |
| 74 |
} |
| 75 |
|
| 76 |
return !ta.Failed() |
| 77 |
} |
| 78 |
|
| 79 |
// CmpMarshaledResponse is the base function used by some others in |
| 80 |
// tdhttp package. req is launched against handler. The response body |
| 81 |
// is unmarshaled using unmarshal. The response is then tested against |
| 82 |
// expectedResp. |
| 83 |
// |
| 84 |
// args... are optional and allow to name the test, a t.Log() done |
| 85 |
// before starting any test. If len(args) > 1 and the first item of |
| 86 |
// args is a string and contains a '%' rune then [fmt.Fprintf] is used |
| 87 |
// to compose the name, else args are passed to [fmt.Fprint]. |
| 88 |
// |
| 89 |
// It returns true if the tests succeed, false otherwise. |
| 90 |
// |
| 91 |
// See [TestAPI] type and its methods for more flexible tests. |
| 92 |
func CmpMarshaledResponse(t testing.TB, |
| 93 |
req *http.Request, |
| 94 |
handler func(w http.ResponseWriter, r *http.Request), |
| 95 |
unmarshal func([]byte, any) error, |
| 96 |
expectedResp Response, |
| 97 |
args ...any, |
| 98 |
) bool { |
| 99 |
t.Helper() |
| 100 |
return cmpMarshaledResponse(t, req, handler, false, unmarshal, expectedResp, args...) |
| 101 |
} |
| 102 |
|
| 103 |
// CmpResponse is used to match a []byte or string response body. req |
| 104 |
// is launched against handler. If expectedResp.Body is non-nil, the |
| 105 |
// response body is converted to []byte or string, depending on the |
| 106 |
// expectedResp.Body type. The response is then tested against |
| 107 |
// expectedResp. |
| 108 |
// |
| 109 |
// args... are optional and allow to name the test, a t.Log() done |
| 110 |
// before starting any test. If len(args) > 1 and the first item of |
| 111 |
// args is a string and contains a '%' rune then [fmt.Fprintf] is used |
| 112 |
// to compose the name, else args are passed to [fmt.Fprint]. |
| 113 |
// |
| 114 |
// It returns true if the tests succeed, false otherwise. |
| 115 |
// |
| 116 |
// ok := tdhttp.CmpResponse(t, |
| 117 |
// tdhttp.Get("/test"), |
| 118 |
// myAPI.ServeHTTP, |
| 119 |
// Response{ |
| 120 |
// Status: http.StatusOK, |
| 121 |
// Header: td.ContainsKey("X-Custom-Header"), |
| 122 |
// Body: "OK!\n", |
| 123 |
// }, |
| 124 |
// "/test route") |
| 125 |
// |
| 126 |
// Response.Status, Response.Header and Response.Body fields can all |
| 127 |
// be [td.TestDeep] operators as it is for Response.Header field |
| 128 |
// here. Otherwise, Response.Status should be an int, Response.Header |
| 129 |
// a [http.Header] and Response.Body a []byte or a string. |
| 130 |
// |
| 131 |
// See [TestAPI] type and its methods for more flexible tests. |
| 132 |
func CmpResponse(t testing.TB, |
| 133 |
req *http.Request, |
| 134 |
handler func(w http.ResponseWriter, r *http.Request), |
| 135 |
expectedResp Response, |
| 136 |
args ...any) bool { |
| 137 |
t.Helper() |
| 138 |
return cmpMarshaledResponse(t, |
| 139 |
req, |
| 140 |
handler, |
| 141 |
true, |
| 142 |
func(body []byte, target any) error { |
| 143 |
switch t := target.(type) { |
| 144 |
case *string: |
| 145 |
*t = string(body) |
| 146 |
case *[]byte: |
| 147 |
*t = body |
| 148 |
case *any: |
| 149 |
*t = body |
| 150 |
default: |
| 151 |
// cmpMarshaledBody (behind cmpMarshaledResponse) always calls |
| 152 |
// us with target as a pointer |
| 153 |
return fmt.Errorf( |
| 154 |
"CmpResponse only accepts expectedResp.Body be a []byte, a string or a TestDeep operator allowing to match these types, but not type %s", |
| 155 |
reflect.TypeOf(target).Elem()) |
| 156 |
} |
| 157 |
return nil |
| 158 |
}, |
| 159 |
expectedResp, |
| 160 |
args...) |
| 161 |
} |
| 162 |
|
| 163 |
// CmpJSONResponse is used to match a JSON response body. req is |
| 164 |
// launched against handler. If expectedResp.Body is non-nil, the |
| 165 |
// response body is [json.Unmarshal]'ed. The response is then tested |
| 166 |
// against expectedResp. |
| 167 |
// |
| 168 |
// args... are optional and allow to name the test, a t.Log() done |
| 169 |
// before starting any test. If len(args) > 1 and the first item of |
| 170 |
// args is a string and contains a '%' rune then [fmt.Fprintf] is used |
| 171 |
// to compose the name, else args are passed to [fmt.Fprint]. |
| 172 |
// |
| 173 |
// It returns true if the tests succeed, false otherwise. |
| 174 |
// |
| 175 |
// ok := tdhttp.CmpJSONResponse(t, |
| 176 |
// tdhttp.Get("/person/42"), |
| 177 |
// myAPI.ServeHTTP, |
| 178 |
// Response{ |
| 179 |
// Status: http.StatusOK, |
| 180 |
// Header: td.ContainsKey("X-Custom-Header"), |
| 181 |
// Body: Person{ |
| 182 |
// ID: 42, |
| 183 |
// Name: "Bob", |
| 184 |
// Age: 26, |
| 185 |
// }, |
| 186 |
// }, |
| 187 |
// "/person/{id} route") |
| 188 |
// |
| 189 |
// Response.Status, Response.Header and Response.Body fields can all |
| 190 |
// be [td.TestDeep] operators as it is for Response.Header field |
| 191 |
// here. Otherwise, Response.Status should be an int, Response.Header |
| 192 |
// a [http.Header] and Response.Body any type one can |
| 193 |
// [json.Unmarshal] into. |
| 194 |
// |
| 195 |
// If Response.Status and Response.Header are omitted (or nil), they |
| 196 |
// are not tested. |
| 197 |
// |
| 198 |
// If Response.Body is omitted (or nil), it means the body response has to be |
| 199 |
// empty. If you want to ignore the body response, use [td.Ignore] |
| 200 |
// explicitly. |
| 201 |
// |
| 202 |
// See [TestAPI] type and its methods for more flexible tests. |
| 203 |
func CmpJSONResponse(t testing.TB, |
| 204 |
req *http.Request, |
| 205 |
handler func(w http.ResponseWriter, r *http.Request), |
| 206 |
expectedResp Response, |
| 207 |
args ...any, |
| 208 |
) bool { |
| 209 |
t.Helper() |
| 210 |
return CmpMarshaledResponse(t, |
| 211 |
req, |
| 212 |
handler, |
| 213 |
json.Unmarshal, |
| 214 |
expectedResp, |
| 215 |
args...) |
| 216 |
} |
| 217 |
|
| 218 |
// CmpXMLResponse is used to match an XML response body. req |
| 219 |
// is launched against handler. If expectedResp.Body is |
| 220 |
// non-nil, the response body is [xml.Unmarshal]'ed. The response is |
| 221 |
// then tested against expectedResp. |
| 222 |
// |
| 223 |
// args... are optional and allow to name the test, a t.Log() done |
| 224 |
// before starting any test. If len(args) > 1 and the first item of |
| 225 |
// args is a string and contains a '%' rune then [fmt.Fprintf] is used |
| 226 |
// to compose the name, else args are passed to [fmt.Fprint]. |
| 227 |
// |
| 228 |
// It returns true if the tests succeed, false otherwise. |
| 229 |
// |
| 230 |
// ok := tdhttp.CmpXMLResponse(t, |
| 231 |
// tdhttp.Get("/person/42"), |
| 232 |
// myAPI.ServeHTTP, |
| 233 |
// Response{ |
| 234 |
// Status: http.StatusOK, |
| 235 |
// Header: td.ContainsKey("X-Custom-Header"), |
| 236 |
// Body: Person{ |
| 237 |
// ID: 42, |
| 238 |
// Name: "Bob", |
| 239 |
// Age: 26, |
| 240 |
// }, |
| 241 |
// }, |
| 242 |
// "/person/{id} route") |
| 243 |
// |
| 244 |
// Response.Status, Response.Header and Response.Body fields can all |
| 245 |
// be [td.TestDeep] operators as it is for Response.Header field |
| 246 |
// here. Otherwise, Response.Status should be an int, Response.Header |
| 247 |
// a [http.Header] and Response.Body any type one can [xml.Unmarshal] |
| 248 |
// into. |
| 249 |
// |
| 250 |
// If Response.Status and Response.Header are omitted (or nil), they |
| 251 |
// are not tested. |
| 252 |
// |
| 253 |
// If Response.Body is omitted (or nil), it means the body response |
| 254 |
// has to be empty. If you want to ignore the body response, use |
| 255 |
// [td.Ignore] explicitly. |
| 256 |
// |
| 257 |
// See [TestAPI] type and its methods for more flexible tests. |
| 258 |
func CmpXMLResponse(t testing.TB, |
| 259 |
req *http.Request, |
| 260 |
handler func(w http.ResponseWriter, r *http.Request), |
| 261 |
expectedResp Response, |
| 262 |
args ...any, |
| 263 |
) bool { |
| 264 |
t.Helper() |
| 265 |
return CmpMarshaledResponse(t, |
| 266 |
req, |
| 267 |
handler, |
| 268 |
xml.Unmarshal, |
| 269 |
expectedResp, |
| 270 |
args...) |
| 271 |
} |
| 272 |
|
| 273 |
// CmpMarshaledResponseFunc returns a function ready to be used with |
| 274 |
// [testing.T.Run], calling [CmpMarshaledResponse] behind the scene. As it |
| 275 |
// is intended to be used in conjunction with [testing.T.Run] which |
| 276 |
// names the sub-test, the test name part (args...) is voluntary |
| 277 |
// omitted. |
| 278 |
// |
| 279 |
// t.Run("Subtest name", tdhttp.CmpMarshaledResponseFunc( |
| 280 |
// tdhttp.Get("/text"), |
| 281 |
// mux.ServeHTTP, |
| 282 |
// tdhttp.Response{ |
| 283 |
// Status: http.StatusOK, |
| 284 |
// })) |
| 285 |
// |
| 286 |
// See [CmpMarshaledResponse] for details. |
| 287 |
// |
| 288 |
// See [TestAPI] type and its methods for more flexible tests. |
| 289 |
func CmpMarshaledResponseFunc(req *http.Request, |
| 290 |
handler func(w http.ResponseWriter, r *http.Request), |
| 291 |
unmarshal func([]byte, any) error, |
| 292 |
expectedResp Response) func(t *testing.T) { |
| 293 |
return func(t *testing.T) { |
| 294 |
t.Helper() |
| 295 |
CmpMarshaledResponse(t, req, handler, unmarshal, expectedResp) |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
// CmpResponseFunc returns a function ready to be used with |
| 300 |
// [testing.T.Run], calling [CmpResponse] behind the scene. As it is |
| 301 |
// intended to be used in conjunction with [testing.T.Run] which names |
| 302 |
// the sub-test, the test name part (args...) is voluntary omitted. |
| 303 |
// |
| 304 |
// t.Run("Subtest name", tdhttp.CmpResponseFunc( |
| 305 |
// tdhttp.Get("/text"), |
| 306 |
// mux.ServeHTTP, |
| 307 |
// tdhttp.Response{ |
| 308 |
// Status: http.StatusOK, |
| 309 |
// })) |
| 310 |
// |
| 311 |
// See [CmpResponse] documentation for details. |
| 312 |
// |
| 313 |
// See [TestAPI] type and its methods for more flexible tests. |
| 314 |
func CmpResponseFunc(req *http.Request, |
| 315 |
handler func(w http.ResponseWriter, r *http.Request), |
| 316 |
expectedResp Response) func(t *testing.T) { |
| 317 |
return func(t *testing.T) { |
| 318 |
t.Helper() |
| 319 |
CmpResponse(t, req, handler, expectedResp) |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
// CmpJSONResponseFunc returns a function ready to be used with |
| 324 |
// [testing.T.Run], calling [CmpJSONResponse] behind the scene. As it is |
| 325 |
// intended to be used in conjunction with [testing.T.Run] which names |
| 326 |
// the sub-test, the test name part (args...) is voluntary omitted. |
| 327 |
// |
| 328 |
// t.Run("Subtest name", tdhttp.CmpJSONResponseFunc( |
| 329 |
// tdhttp.Get("/json"), |
| 330 |
// mux.ServeHTTP, |
| 331 |
// tdhttp.Response{ |
| 332 |
// Status: http.StatusOK, |
| 333 |
// Body: JResp{Comment: "expected comment!"}, |
| 334 |
// })) |
| 335 |
// |
| 336 |
// See [CmpJSONResponse] documentation for details. |
| 337 |
// |
| 338 |
// See [TestAPI] type and its methods for more flexible tests. |
| 339 |
func CmpJSONResponseFunc(req *http.Request, |
| 340 |
handler func(w http.ResponseWriter, r *http.Request), |
| 341 |
expectedResp Response) func(t *testing.T) { |
| 342 |
return func(t *testing.T) { |
| 343 |
t.Helper() |
| 344 |
CmpJSONResponse(t, req, handler, expectedResp) |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
// CmpXMLResponseFunc returns a function ready to be used with |
| 349 |
// [testing.T.Run], calling [CmpXMLResponse] behind the scene. As it is |
| 350 |
// intended to be used in conjunction with [testing.T.Run] which names |
| 351 |
// the sub-test, the test name part (args...) is voluntary omitted. |
| 352 |
// |
| 353 |
// t.Run("Subtest name", tdhttp.CmpXMLResponseFunc( |
| 354 |
// tdhttp.Get("/xml"), |
| 355 |
// mux.ServeHTTP, |
| 356 |
// tdhttp.Response{ |
| 357 |
// Status: http.StatusOK, |
| 358 |
// Body: JResp{Comment: "expected comment!"}, |
| 359 |
// })) |
| 360 |
// |
| 361 |
// See [CmpXMLResponse] documentation for details. |
| 362 |
// |
| 363 |
// See [TestAPI] type and its methods for more flexible tests. |
| 364 |
func CmpXMLResponseFunc(req *http.Request, |
| 365 |
handler func(w http.ResponseWriter, r *http.Request), |
| 366 |
expectedResp Response) func(t *testing.T) { |
| 367 |
return func(t *testing.T) { |
| 368 |
t.Helper() |
| 369 |
CmpXMLResponse(t, req, handler, expectedResp) |
| 370 |
} |
| 371 |
} |