| 1 |
// Copyright (c) 2020, 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, from [go-testdeep], provides some functions to easily |
| 8 |
// test HTTP handlers. |
| 9 |
// |
| 10 |
// Combined to [td] package it provides powerful testing features. |
| 11 |
// |
| 12 |
// # TestAPI |
| 13 |
// |
| 14 |
// The better way to test HTTP APIs using this package. |
| 15 |
// |
| 16 |
// ta := tdhttp.NewTestAPI(t, mux) |
| 17 |
// |
| 18 |
// ta.Get("/person/42", "Accept", "application/xml"). |
| 19 |
// CmpStatus(http.StatusOK). |
| 20 |
// CmpHeader(td.ContainsKey("X-Custom-Header")). |
| 21 |
// CmpCookie(td.SuperBagOf(td.Smuggle("Name", "cookie_session"))). |
| 22 |
// CmpXMLBody(Person{ |
| 23 |
// ID: ta.Anchor(td.NotZero(), uint64(0)).(uint64), |
| 24 |
// Name: "Bob", |
| 25 |
// Age: 26, |
| 26 |
// }) |
| 27 |
// |
| 28 |
// ta.Get("/person/42", "Accept", "application/json"). |
| 29 |
// CmpStatus(http.StatusOK). |
| 30 |
// CmpHeader(td.ContainsKey("X-Custom-Header")). |
| 31 |
// CmpCookies(td.SuperBagOf(td.Struct(&http.Cookie{Name: "cookie_session"}, nil))). |
| 32 |
// CmpJSONBody(td.JSON(` |
| 33 |
// { |
| 34 |
// "id": $1, |
| 35 |
// "name": "Bob", |
| 36 |
// "age": 26 |
| 37 |
// }`, |
| 38 |
// td.NotZero())) |
| 39 |
// |
| 40 |
// See the full example below. |
| 41 |
// |
| 42 |
// # Cmp…Response functions |
| 43 |
// |
| 44 |
// Historically, it was the only way to test HTTP APIs using |
| 45 |
// this package. |
| 46 |
// |
| 47 |
// ok := tdhttp.CmpJSONResponse(t, |
| 48 |
// tdhttp.Get("/person/42"), |
| 49 |
// myAPI.ServeHTTP, |
| 50 |
// Response{ |
| 51 |
// Status: http.StatusOK, |
| 52 |
// Header: td.ContainsKey("X-Custom-Header"), |
| 53 |
// Cookies: td.SuperBagOf(td.Smuggle("Name", "cookie_session")), |
| 54 |
// Body: Person{ |
| 55 |
// ID: 42, |
| 56 |
// Name: "Bob", |
| 57 |
// Age: 26, |
| 58 |
// }, |
| 59 |
// }, |
| 60 |
// "/person/{id} route") |
| 61 |
// |
| 62 |
// It now uses [TestAPI] behind the scene. It is better to directly |
| 63 |
// use [TestAPI] and its methods instead, as it is more flexible and |
| 64 |
// readable. |
| 65 |
// |
| 66 |
// [go-testdeep]: https://go-testdeep.zetta.rocks/ |
| 67 |
package tdhttp |