File: doc.go

package info (click to toggle)
golang-github-maxatome-go-testdeep 1.14.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,416 kB
  • sloc: perl: 1,012; yacc: 130; makefile: 2
file content (67 lines) | stat: -rw-r--r-- 1,904 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright (c) 2020, Maxime Soulé
// All rights reserved.
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree.

// Package tdhttp, from [go-testdeep], provides some functions to easily
// test HTTP handlers.
//
// Combined to [td] package it provides powerful testing features.
//
// # TestAPI
//
// The better way to test HTTP APIs using this package.
//
//	ta := tdhttp.NewTestAPI(t, mux)
//
//	ta.Get("/person/42", "Accept", "application/xml").
//	  CmpStatus(http.StatusOK).
//	  CmpHeader(td.ContainsKey("X-Custom-Header")).
//	  CmpCookie(td.SuperBagOf(td.Smuggle("Name", "cookie_session"))).
//	  CmpXMLBody(Person{
//	    ID:   ta.Anchor(td.NotZero(), uint64(0)).(uint64),
//	    Name: "Bob",
//	    Age:  26,
//	  })
//
//	ta.Get("/person/42", "Accept", "application/json").
//	  CmpStatus(http.StatusOK).
//	  CmpHeader(td.ContainsKey("X-Custom-Header")).
//	  CmpCookies(td.SuperBagOf(td.Struct(&http.Cookie{Name: "cookie_session"}, nil))).
//	  CmpJSONBody(td.JSON(`
//	{
//	  "id":   $1,
//	  "name": "Bob",
//	  "age":  26
//	}`,
//	    td.NotZero()))
//
// See the full example below.
//
// # Cmp…Response functions
//
// Historically, it was the only way to test HTTP APIs using
// this package.
//
//	ok := tdhttp.CmpJSONResponse(t,
//	  tdhttp.Get("/person/42"),
//	  myAPI.ServeHTTP,
//	  Response{
//	    Status:  http.StatusOK,
//	    Header:  td.ContainsKey("X-Custom-Header"),
//	    Cookies: td.SuperBagOf(td.Smuggle("Name", "cookie_session")),
//	    Body: Person{
//	      ID:   42,
//	      Name: "Bob",
//	      Age:  26,
//	    },
//	  },
//	  "/person/{id} route")
//
// It now uses [TestAPI] behind the scene. It is better to directly
// use [TestAPI] and its methods instead, as it is more flexible and
// readable.
//
// [go-testdeep]: https://go-testdeep.zetta.rocks/
package tdhttp