Documentation ¶
Index ¶
- func AssertJSONCall(c *gc.C, p JSONCallParams)
- func AssertJSONResponse(c *gc.C, rec *httptest.ResponseRecorder, expectStatus int, ...)
- func Do(c *gc.C, p DoRequestParams) *http.Response
- func DoRequest(c *gc.C, p DoRequestParams) *httptest.ResponseRecorder
- type BodyAsserter
- type DoRequestParams
- type JSONCallParams
- type URLRewritingTransport
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AssertJSONCall ¶
func AssertJSONCall(c *gc.C, p JSONCallParams)
AssertJSONCall asserts that when the given handler is called with the given parameters, the result is as specified.
func AssertJSONResponse ¶
func AssertJSONResponse(c *gc.C, rec *httptest.ResponseRecorder, expectStatus int, expectBody interface{})
AssertJSONResponse asserts that the given response recorder has recorded the given HTTP status, response body and content type. If expectBody is of type BodyAsserter it will be called with the response body to ensure the response is correct.
func Do ¶
func Do(c *gc.C, p DoRequestParams) *http.Response
Do invokes a request on the given handler with the given parameters and returns the resulting HTTP response. Note that, as with http.Client.Do, the response body must be closed.
func DoRequest ¶
func DoRequest(c *gc.C, p DoRequestParams) *httptest.ResponseRecorder
DoRequest is the same as Do except that it returns an httptest.ResponseRecorder instead of an http.Response. This function exists for backward compatibility reasons.
Types ¶
type BodyAsserter ¶
type BodyAsserter func(c *gc.C, body json.RawMessage)
BodyAsserter represents a function that can assert the correctness of a JSON reponse.
type DoRequestParams ¶
type DoRequestParams struct { // Do is used to make the HTTP request. // If it is nil, http.DefaultClient.Do will be used. // If the body reader implements io.Seeker, // req.Body will also implement that interface. Do func(req *http.Request) (*http.Response, error) // ExpectError holds the error regexp to match // against the error returned from the HTTP Do // request. If it is empty, the error is expected to be // nil. ExpectError string // Method holds the HTTP method to use for the call. // GET is assumed if this is empty. Method string // URL holds the URL to pass when making the request. // If the URL does not contain a host, a temporary // HTTP server is started running the Handler below // which is used for the host. URL string // Handler holds the handler to use to make the request. // It is ignored if the above URL field has a host part. Handler http.Handler // JSONBody specifies a JSON value to marshal to use // as the body of the request. If this is specified, Body will // be ignored and the Content-Type header will // be set to application/json. The request // body will implement io.Seeker. JSONBody interface{} // Body holds the body to send in the request. Body io.Reader // Header specifies the HTTP headers to use when making // the request. Header http.Header // ContentLength specifies the length of the body. // It may be zero, in which case the default net/http // content-length behaviour will be used. ContentLength int64 // Username, if specified, is used for HTTP basic authentication. Username string // Password, if specified, is used for HTTP basic authentication. Password string // Cookies, if specified, are added to the request. Cookies []*http.Cookie }
DoRequestParams holds parameters for DoRequest. If left empty, some fields will automatically be filled with defaults.
type JSONCallParams ¶
type JSONCallParams struct { // Do is used to make the HTTP request. // If it is nil, http.DefaultClient.Do will be used. // If the body reader implements io.Seeker, // req.Body will also implement that interface. Do func(req *http.Request) (*http.Response, error) // ExpectError holds the error regexp to match // against the error returned from the HTTP Do // request. If it is empty, the error is expected to be // nil. ExpectError string // Method holds the HTTP method to use for the call. // GET is assumed if this is empty. Method string // URL holds the URL to pass when making the request. // If the URL does not contain a host, a temporary // HTTP server is started running the Handler below // which is used for the host. URL string // Handler holds the handler to use to make the request. // It is ignored if the above URL field has a host part. Handler http.Handler // JSONBody specifies a JSON value to marshal to use // as the body of the request. If this is specified, Body will // be ignored and the Content-Type header will // be set to application/json. The request // body will implement io.Seeker. JSONBody interface{} // Body holds the body to send in the request. Body io.Reader // Header specifies the HTTP headers to use when making // the request. Header http.Header // ContentLength specifies the length of the body. // It may be zero, in which case the default net/http // content-length behaviour will be used. ContentLength int64 // Username, if specified, is used for HTTP basic authentication. Username string // Password, if specified, is used for HTTP basic authentication. Password string // ExpectStatus holds the expected HTTP status code. // http.StatusOK is assumed if this is zero. ExpectStatus int // ExpectBody holds the expected JSON body. // This may be a function of type BodyAsserter in which case it // will be called with the http response body to check the // result. ExpectBody interface{} // ExpectHeader holds any HTTP headers that must be present in the response. // Note that the response may also contain headers not in this field. ExpectHeader http.Header // Cookies, if specified, are added to the request. Cookies []*http.Cookie }
JSONCallParams holds parameters for AssertJSONCall. If left empty, some fields will automatically be filled with defaults.
type URLRewritingTransport ¶
type URLRewritingTransport struct { MatchPrefix string Replace string RoundTripper http.RoundTripper }
URLRewritingTransport is an http.RoundTripper that can rewrite request URLs. If the request URL has the prefix specified in Match that part will be changed to the value specified in Replace. RoundTripper will then be used to perform the resulting request. If RoundTripper is nil http.DefaultTransport will be used.
This can be used in tests that, for whatever reason, need to make a call to a URL that's not in our control but we want to control the results of HTTP requests to that URL.