Documentation ¶
Overview ¶
Package tdhttp provides some functions to easily test HTTP handlers.
Index ¶
- func CmpJSONResponse(t td.TestingFT, req *http.Request, ...) bool
- func CmpJSONResponseFunc(req *http.Request, handler func(w http.ResponseWriter, r *http.Request), ...) func(t *testing.T)
- func CmpMarshaledResponse(tt td.TestingFT, req *http.Request, ...) bool
- func CmpMarshaledResponseFunc(req *http.Request, handler func(w http.ResponseWriter, r *http.Request), ...) func(t *testing.T)
- func CmpResponse(t td.TestingFT, req *http.Request, ...) bool
- func CmpResponseFunc(req *http.Request, handler func(w http.ResponseWriter, r *http.Request), ...) func(t *testing.T)
- func CmpXMLResponse(t td.TestingFT, req *http.Request, ...) bool
- func CmpXMLResponseFunc(req *http.Request, handler func(w http.ResponseWriter, r *http.Request), ...) func(t *testing.T)
- func Delete(target string, body io.Reader, headers ...string) *http.Request
- func DeleteJSON(target string, body interface{}, headers ...string) *http.Request
- func DeleteXML(target string, body interface{}, headers ...string) *http.Request
- func Get(target string, headers ...string) *http.Request
- func NewJSONRequest(method, target string, body interface{}, headers ...string) *http.Request
- func NewRequest(method, target string, body io.Reader, headers ...string) *http.Request
- func NewXMLRequest(method, target string, body interface{}, headers ...string) *http.Request
- func Patch(target string, body io.Reader, headers ...string) *http.Request
- func PatchJSON(target string, body interface{}, headers ...string) *http.Request
- func PatchXML(target string, body interface{}, headers ...string) *http.Request
- func Post(target string, body io.Reader, headers ...string) *http.Request
- func PostJSON(target string, body interface{}, headers ...string) *http.Request
- func PostXML(target string, body interface{}, headers ...string) *http.Request
- func Put(target string, body io.Reader, headers ...string) *http.Request
- func PutJSON(target string, body interface{}, headers ...string) *http.Request
- func PutXML(target string, body interface{}, headers ...string) *http.Request
- type Response
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CmpJSONResponse ¶
func CmpJSONResponse(t td.TestingFT, req *http.Request, handler func(w http.ResponseWriter, r *http.Request), expectedResp Response, args ...interface{}, ) bool
CmpJSONResponse is used to match a JSON response body. The req *http.Request is launched against handler. If expectedResp.Body is non-nil, the response body is json.Unmarshal'ed. The response is then tested against expectedResp.
All the tests are enclosed in a testdeep.Run().
It returns true if the tests succeed, false otherwise.
func CmpJSONResponseFunc ¶
func CmpJSONResponseFunc(req *http.Request, handler func(w http.ResponseWriter, r *http.Request), expectedResp Response) func(t *testing.T)
CmpJSONResponseFunc returns a function ready to be used with testing.Run, calling CmpJSONResponse behind the scene. As it is intended to be used in conjunction with testing.Run() which names the sub-test, the test name part (args...) is voluntary omitted.
t.Run("Subtest name", tdhttp.CmpJSONResponseFunc( tdhttp.NewRequest("GET", "/json", nil), mux.ServeHTTP, tdhttp.Response{ Status: http.StatusOK, Body: JResp{Comment: "expected comment!"}, }))
func CmpMarshaledResponse ¶
func CmpMarshaledResponse(tt td.TestingFT, req *http.Request, handler func(w http.ResponseWriter, r *http.Request), unmarshal func([]byte, interface{}) error, expectedResp Response, args ...interface{}, ) bool
CmpMarshaledResponse is the base function used by all others in tdhttp package. The req *http.Request is launched against handler. The response body is unmarshaled using unmarshal. The response is then tested against expectedResp.
All the tests are enclosed in a testdeep.Run().
It returns true if the tests succeed, false otherwise.
func CmpMarshaledResponseFunc ¶
func CmpMarshaledResponseFunc(req *http.Request, handler func(w http.ResponseWriter, r *http.Request), unmarshal func([]byte, interface{}) error, expectedResp Response) func(t *testing.T)
CmpMarshaledResponseFunc returns a function ready to be used with testing.Run, calling CmpMarshaledResponse behind the scene. As it is intended to be used in conjunction with testing.Run() which names the sub-test, the test name part (args...) is voluntary omitted.
t.Run("Subtest name", tdhttp.CmpMarshaledResponseFunc( tdhttp.NewRequest("GET", "/text", nil), mux.ServeHTTP, tdhttp.Response{ Status: http.StatusOK, }))
func CmpResponse ¶
func CmpResponse(t td.TestingFT, req *http.Request, handler func(w http.ResponseWriter, r *http.Request), expectedResp Response, args ...interface{}) bool
CmpResponse is used to match a []byte or string response body. The req *http.Request is launched against handler. If expectedResp.Body is non-nil, the response body is converted to []byte or string, depending on the expectedResp.Body type. The response is then tested against expectedResp.
All the tests are enclosed in a testdeep.Run().
It returns true if the tests succeed, false otherwise.
func CmpResponseFunc ¶
func CmpResponseFunc(req *http.Request, handler func(w http.ResponseWriter, r *http.Request), expectedResp Response) func(t *testing.T)
CmpResponseFunc returns a function ready to be used with testing.Run, calling CmpResponse behind the scene. As it is intended to be used in conjunction with testing.Run() which names the sub-test, the test name part (args...) is voluntary omitted.
t.Run("Subtest name", tdhttp.CmpResponseFunc( tdhttp.NewRequest("GET", "/text", nil), mux.ServeHTTP, tdhttp.Response{ Status: http.StatusOK, }))
func CmpXMLResponse ¶
func CmpXMLResponse(t td.TestingFT, req *http.Request, handler func(w http.ResponseWriter, r *http.Request), expectedResp Response, args ...interface{}, ) bool
CmpXMLResponse is used to match an XML response body. The req *http.Request is launched against handler. If expectedResp.Body is non-nil, the response body is xml.Unmarshal'ed. The response is then tested against expectedResp.
All the tests are enclosed in a testdeep.Run().
It returns true if the tests succeed, false otherwise.
func CmpXMLResponseFunc ¶
func CmpXMLResponseFunc(req *http.Request, handler func(w http.ResponseWriter, r *http.Request), expectedResp Response) func(t *testing.T)
CmpXMLResponseFunc returns a function ready to be used with testing.Run, calling CmpXMLResponse behind the scene. As it is intended to be used in conjunction with testing.Run() which names the sub-test, the test name part (args...) is voluntary omitted.
t.Run("Subtest name", tdhttp.CmpXMLResponseFunc( tdhttp.NewRequest("GET", "/xml", nil), mux.ServeHTTP, tdhttp.Response{ Status: http.StatusOK, Body: JResp{Comment: "expected comment!"}, }))
func Delete ¶ added in v1.1.1
Delete is a shortcut for:
NewRequest(http.MethodDelete, target, body, headers...)
func DeleteJSON ¶ added in v1.1.1
DeleteJSON is a shortcut for:
NewJSONRequest(http.MethodDelete, target, body, headers...)
func DeleteXML ¶ added in v1.1.1
DeleteXML is a shortcut for:
NewXMLRequest(http.MethodDelete, target, body, headers...)
func NewJSONRequest ¶
NewJSONRequest creates a new HTTP request with body marshaled to JSON. "Content-Type" header is automatically set to "application/json". Other headers can be added via headers, as in:
req := NewJSONRequest("POST", "/data", body, "X-Foo", "Foo-value", "X-Zip", "Zip-value", )
func NewRequest ¶
NewRequest creates a new HTTP request as net/http/httptest.NewRequest does, with the ability to immediately add some headers.
req := NewRequest("POST", "/pdf", body, "Content-type", "application/pdf", )
func NewXMLRequest ¶
NewXMLRequest creates a new HTTP request with body marshaled to XML. "Content-Type" header is automatically set to "application/xml". Other headers can be added via headers, as in:
req := NewXMLRequest("POST", "/data", body, "X-Foo", "Foo-value", "X-Zip", "Zip-value", )
func Patch ¶ added in v1.1.1
Patch is a shortcut for:
NewRequest(http.MethodPatch, target, body, headers...)
func PatchJSON ¶ added in v1.1.1
PatchJSON is a shortcut for:
NewJSONRequest(http.MethodPatch, target, body, headers...)
func PatchXML ¶ added in v1.1.1
PatchXML is a shortcut for:
NewXMLRequest(http.MethodPatch, target, body, headers...)
func Post ¶ added in v1.1.1
Post is a shortcut for:
NewRequest(http.MethodPost, target, body, headers...)
func PostJSON ¶ added in v1.1.1
PostJSON is a shortcut for:
NewJSONRequest(http.MethodPost, target, body, headers...)
func PostXML ¶ added in v1.1.1
PostXML is a shortcut for:
NewXMLRequest(http.MethodPost, target, body, headers...)
func Put ¶ added in v1.1.1
Put is a shortcut for:
NewRequest(http.MethodPut, target, body, headers...)
Types ¶
type Response ¶
type Response struct { Status interface{} // Status is the expected status (ignored if nil) Header interface{} // Header is the expected header (ignored if nil) Body interface{} // Body is the expected body (expected to be empty if nil) }
Response is used by Cmp*Response functions to make the HTTP response match easier. Each field, can be a TestDeep operator as well as the exact expected value.