Documentation
¶
Overview ¶
Package apitest provides methods for testing client-server communication. It can be used to test http.Handler to build complex assertions on the HTTP responses.
Index ¶
- type AssertResponse
- func HandleDELETE(tb testing.TB, handler http.Handler, url string, options ...RequestOption) *AssertResponse
- func HandleGET(tb testing.TB, handler http.Handler, url string, options ...RequestOption) *AssertResponse
- func HandlePATCH(tb testing.TB, handler http.Handler, url string, body io.Reader, ...) *AssertResponse
- func HandlePOST(tb testing.TB, handler http.Handler, url string, body io.Reader, ...) *AssertResponse
- func HandlePUT(tb testing.TB, handler http.Handler, url string, body io.Reader, ...) *AssertResponse
- func HandleRequest(tb testing.TB, handler http.Handler, request *http.Request) *AssertResponse
- func (r *AssertResponse) Code() int
- func (r *AssertResponse) HasCode(code int)
- func (r *AssertResponse) HasContentType(contentType string)
- func (r *AssertResponse) HasHeader(key, value string)
- func (r *AssertResponse) HasJSON(jsonAssert assertjson.JSONAssertFunc)
- func (r *AssertResponse) HasNoContent()
- func (r *AssertResponse) HasXML(xmlAssert assertxml.XMLAssertFunc)
- func (r *AssertResponse) IsAccepted()
- func (r *AssertResponse) IsBadRequest()
- func (r *AssertResponse) IsConflict()
- func (r *AssertResponse) IsCreated()
- func (r *AssertResponse) IsForbidden()
- func (r *AssertResponse) IsInternalServerError()
- func (r *AssertResponse) IsMethodNotAllowed()
- func (r *AssertResponse) IsNotFound()
- func (r *AssertResponse) IsOK()
- func (r *AssertResponse) IsUnauthorized()
- func (r *AssertResponse) IsUnprocessableEntity()
- func (r *AssertResponse) IsUnsupportedMediaType()
- func (r *AssertResponse) Print()
- func (r *AssertResponse) PrintJSON()
- func (r *AssertResponse) Recorder() *httptest.ResponseRecorder
- type RequestOption
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AssertResponse ¶
type AssertResponse struct {
// contains filtered or unexported fields
}
AssertResponse is used to build assertions around httptest.ResponseRecorder.
func HandleDELETE ¶
func HandleDELETE(tb testing.TB, handler http.Handler, url string, options ...RequestOption) *AssertResponse
HandleDELETE is an alias for HandleRequest that builds the DELETE request from url and options.
func HandleGET ¶
func HandleGET(tb testing.TB, handler http.Handler, url string, options ...RequestOption) *AssertResponse
HandleGET is an alias for HandleRequest that builds the GET request from url and options.
Example ¶
package main
import (
"fmt"
"net/http"
"testing"
"github.com/muonsoft/api-testing/apitest"
"github.com/muonsoft/api-testing/assertjson"
)
func main() {
handler := http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
fmt.Println("request method:", request.Method)
fmt.Println("request url:", request.URL.String())
writer.WriteHeader(http.StatusOK)
writer.Header().Set("Content-Type", "application/json")
writer.Write([]byte(`{"ok":true}`))
})
// HandleGET builds and sends GET request to handler
response := apitest.HandleGET(&testing.T{}, handler, "/example")
response.IsOK()
response.HasContentType("application/json")
response.HasJSON(func(json *assertjson.AssertJSON) {
json.Node("/ok").IsTrue()
})
}
Output: request method: GET request url: /example
func HandlePATCH ¶
func HandlePATCH(tb testing.TB, handler http.Handler, url string, body io.Reader, options ...RequestOption) *AssertResponse
HandlePATCH is an alias for HandleRequest that builds the PATCH request from url, body and options.
func HandlePOST ¶
func HandlePOST(tb testing.TB, handler http.Handler, url string, body io.Reader, options ...RequestOption) *AssertResponse
HandlePOST is an alias for HandleRequest that builds the POST request from url, body and options.
func HandlePUT ¶
func HandlePUT(tb testing.TB, handler http.Handler, url string, body io.Reader, options ...RequestOption) *AssertResponse
HandlePUT is an alias for HandleRequest that builds the PUT request from url, body and options.
func HandleRequest ¶
HandleRequest is used to test http.Handler by passing httptest.ResponseRecorder to it. This function returns AssertResponse struct as a helper to build assertions on the response.
func (*AssertResponse) Code ¶
func (r *AssertResponse) Code() int
Code returns HTTP status code of the response.
func (*AssertResponse) HasCode ¶
func (r *AssertResponse) HasCode(code int)
HasCode asserts that the response has specific HTTP status code.
func (*AssertResponse) HasContentType ¶
func (r *AssertResponse) HasContentType(contentType string)
HasContentType asserts that the response contains Content-Type header with specific value.
func (*AssertResponse) HasHeader ¶
func (r *AssertResponse) HasHeader(key, value string)
HasHeader asserts that the response contains specific header with key and value.
func (*AssertResponse) HasJSON ¶
func (r *AssertResponse) HasJSON(jsonAssert assertjson.JSONAssertFunc)
HasJSON asserts that the response body contains JSON and runs JSON assertions by callback function.
func (*AssertResponse) HasNoContent ¶
func (r *AssertResponse) HasNoContent()
HasNoContent asserts that the response has an 204 No Content HTTP status code and also checks that body is empty.
func (*AssertResponse) HasXML ¶
func (r *AssertResponse) HasXML(xmlAssert assertxml.XMLAssertFunc)
HasXML asserts that the response body contains XML and runs XML assertions by callback function.
func (*AssertResponse) IsAccepted ¶
func (r *AssertResponse) IsAccepted()
IsAccepted asserts that the response has an 202 Accepted HTTP status code.
func (*AssertResponse) IsBadRequest ¶
func (r *AssertResponse) IsBadRequest()
IsBadRequest asserts that the response has an 400 Bad Request HTTP status code.
func (*AssertResponse) IsConflict ¶
func (r *AssertResponse) IsConflict()
IsConflict asserts that the response has an 409 Conflict HTTP status code.
func (*AssertResponse) IsCreated ¶
func (r *AssertResponse) IsCreated()
IsCreated asserts that the response has an 201 Created HTTP status code.
func (*AssertResponse) IsForbidden ¶
func (r *AssertResponse) IsForbidden()
IsForbidden asserts that the response has an 403 Forbidden HTTP status code.
func (*AssertResponse) IsInternalServerError ¶
func (r *AssertResponse) IsInternalServerError()
IsInternalServerError asserts that the response has an 500 Internal Server Error HTTP status code.
func (*AssertResponse) IsMethodNotAllowed ¶
func (r *AssertResponse) IsMethodNotAllowed()
IsMethodNotAllowed asserts that the response has an 405 Method Not Allowed HTTP status code.
func (*AssertResponse) IsNotFound ¶
func (r *AssertResponse) IsNotFound()
IsNotFound asserts that the response has an 404 Not Found HTTP status code.
func (*AssertResponse) IsOK ¶
func (r *AssertResponse) IsOK()
IsOK asserts that the response has an 200 Ok HTTP status code.
func (*AssertResponse) IsUnauthorized ¶
func (r *AssertResponse) IsUnauthorized()
IsUnauthorized asserts that the response has an 401 Unauthorized HTTP status code.
func (*AssertResponse) IsUnprocessableEntity ¶
func (r *AssertResponse) IsUnprocessableEntity()
IsUnprocessableEntity asserts that the response has an 422 Unprocessable Entity HTTP status code.
func (*AssertResponse) IsUnsupportedMediaType ¶
func (r *AssertResponse) IsUnsupportedMediaType()
IsUnsupportedMediaType asserts that the response has an 415 Unsupported Media Type HTTP status code.
func (*AssertResponse) Print ¶ added in v0.4.0
func (r *AssertResponse) Print()
Print prints response headers and body to console. Use it for debug purposes.
func (*AssertResponse) PrintJSON ¶ added in v0.4.0
func (r *AssertResponse) PrintJSON()
PrintJSON prints response headers and indented JSON body to console. Use it for debug purposes.
func (*AssertResponse) Recorder ¶
func (r *AssertResponse) Recorder() *httptest.ResponseRecorder
Recorder returns underlying httptest.ResponseRecorder.
type RequestOption ¶
RequestOption can be used to tune up http.Request.
func WithHeader ¶
func WithHeader(key, value string) RequestOption
WithHeader options adds specific header to the request.