tdhttpmock

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 4, 2023 License: BSD-2-Clause Imports: 8 Imported by: 0

README

tdhttpmock

Build Status Coverage Status GoDoc

The marriage of go-testdeep and httpmock.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Body

func Body(expectedBody any) httpmock.Matcher

Body returns an httpmock.Matcher matching request body against expectedBody. expectedBody can be a []byte, a string or a td.TestDeep operator.

httpmock.RegisterMatcherResponder(
  http.MethodPost,
  "/test",
  tdhttpmock.Body("OK!\n"),
  httpmock.NewStringResponder(200, "OK"))

httpmock.RegisterMatcherResponder(
  http.MethodPost,
  "/test",
  tdhttpmock.Body(td.Re(`\d+ test`)),
  httpmock.NewStringResponder(200, "OK test"))

The name of the returned httpmock.Matcher is auto-generated (see httpmock.NewMatcher). To name it explicitly, use httpmock.Matcher.WithName as in:

tdhttpmock.Body("OK!\n").WithName("01-body-OK")

func Cookies

func Cookies(expectedCookies any) httpmock.Matcher

Cookies returns an httpmock.Matcher matching request cookies against expectedCookies. expectedCookies can be a []*http.Cookie or a td.TestDeep operator. Keep in mind that if it is a []*http.Cookie, it has to match exactly the response cookies. Often only the presence of a cookie key is needed:

httpmock.RegisterMatcherResponder(
  http.MethodPost,
  "/test",
  tdhttpmock.Cookies(td.SuperBagOf(td.Smuggle("Name", "cookie_session"))),
  httpmock.NewStringResponder(200, "OK session"))

To make tests easier, http.Cookie.Raw and http.Cookie.RawExpires fields of each *http.Cookie are zeroed before doing the comparison. So no need to fill them when comparing against a simple literal as in:

httpmock.RegisterMatcherResponder(
  http.MethodPost,
  "/test",
  tdhttpmock.Cookies([]*http.Cookies{
    {Name: "cookieName1", Value: "cookieValue1"},
    {Name: "cookieName2", Value: "cookieValue2"},
  }),
  httpmock.NewStringResponder(200, "OK cookies"))

The name of the returned httpmock.Matcher is auto-generated (see httpmock.NewMatcher). To name it explicitly, use httpmock.Matcher.WithName as in:

tdhttpmock.Cookies([]*http.Cookies{}).WithName("01-cookies")
func Header(expectedHader any) httpmock.Matcher

Header returns an httpmock.Matcher matching request header against expectedHeader. expectedHeader can be a http.Header or a td.TestDeep operator. Keep in mind that if it is a http.Header, it has to match exactly the response header. Often only the presence of a header key is needed:

httpmock.RegisterMatcherResponder(
  http.MethodPost,
  "/test",
  tdhttpmock.Header(td.ContainsKey("X-Custom")),
  httpmock.NewStringResponder(200, "OK custom"))

or some specific key, value pairs:

httpmock.RegisterMatcherResponder(
  http.MethodPost,
  "/test",
  tdhttpmock.Header(td.SuperMapOf(
    http.Header{
    "X-Account": []string{"Bob"},
    },
    td.MapEntries{
      "X-Token": td.Bag(td.Re(`^[a-z0-9-]{32}\z`)),
    },
  )),
  httpmock.NewStringResponder(200, "OK account"))

The name of the returned httpmock.Matcher is auto-generated (see httpmock.NewMatcher). To name it explicitly, use httpmock.Matcher.WithName as in:

tdhttpmock.Header(td.ContainsKey("X-Custom")).WithName("01-header-custom")

func JSONBody

func JSONBody(expectedBody any) httpmock.Matcher

JSONBody returns an httpmock.Matcher expecting a JSON request body that can be json.Unmarshal'ed and that matches expectedBody. expectedBody can be any type one can json.Unmarshal into, or a td.TestDeep operator.

httpmock.RegisterMatcherResponder(
  http.MethodPost,
  "/test",
  tdhttpmock.JSONBody(Person{
    ID:   42,
    Name: "Bob",
    Age:  26,
  }),
  httpmock.NewStringResponder(200, "OK bob"))

The same using td.JSON:

httpmock.RegisterMatcherResponder(
  http.MethodPost,
  "/test",
  tdhttpmock.JSONBody(td.JSON(`
    {
      "id":   NotZero(),
      "name": "Bob",
      "age":  26
    }`)),
  httpmock.NewStringResponder(200, "OK bob"))

Note also the existence of td.JSONPointer:

httpmock.RegisterMatcherResponder(
  http.MethodPost,
  "/test",
  tdhttpmock.JSONBody(td.JSONPointer("/name", "Bob")),
  httpmock.NewStringResponder(200, "OK bob"))

The name of the returned httpmock.Matcher is auto-generated (see httpmock.NewMatcher). To name it explicitly, use httpmock.Matcher.WithName as in:

tdhttpmock.JSONBody(td.JSONPointer("/name", "Bob")).WithName("01-bob")

func XMLBody

func XMLBody(expectedBody any) httpmock.Matcher

XMLBody returns an httpmock.Matcher expecting an XML request body that can be xml.Unmarshal'ed and that matches expectedBody. expectedBody can be any type one can xml.Unmarshal into, or a td.TestDeep operator.

httpmock.RegisterMatcherResponder(
  http.MethodPost,
  "/test",
  tdhttpmock.XMLBody(Person{
    ID:   42,
    Name: "Bob",
    Age:  26,
  }),
  httpmock.NewStringResponder(200, "OK bob"))

httpmock.RegisterMatcherResponder(
  http.MethodPost,
  "/test",
  tdhttpmock.XMLBody(td.SStruct(
    Person{
      Name: "Bob",
      Age:  26,
    },
    td.StructFields{
      "ID": td.NotZero(),
    })),
  httpmock.NewStringResponder(200, "OK bob"))

The name of the returned httpmock.Matcher is auto-generated (see httpmock.NewMatcher). To name it explicitly, use httpmock.Matcher.WithName as in:

tdhttpmock.XMLBody(td.Struct(Person{Name: "Bob"})).WithName("01-bob")

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL