Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Body ¶
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 ¶
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 ¶
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 ¶
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 ¶
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.