Documentation ¶
Overview ¶
Package tdhttp provides some functions to easily test HTTP handlers.
Index ¶
- Variables
- 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 NewJSONRequest(method, target string, body interface{}) *http.Request
- func NewXMLRequest(method, target string, body interface{}) *http.Request
- type Response
Constants ¶
This section is empty.
Variables ¶
var NewRequest = httptest.NewRequest
NewRequest is an alias on net/http/httptest.NewRequest for convenience purpose.
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 NewJSONRequest ¶
NewJSONRequest creates a new HTTP request with body marshaled to JSON.
func NewXMLRequest ¶
NewXMLRequest creates a new HTTP request with body marshaled to XML.
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.