go-testdeep: github.com/maxatome/go-testdeep/helpers/tdhttp Index | Files

package tdhttp

import "github.com/maxatome/go-testdeep/helpers/tdhttp"

Package tdhttp provides some functions to easily test HTTP handlers.

Index

Package Files

http.go request.go

Variables

var NewRequest = httptest.NewRequest

NewRequest is an alias on net/http/httptest.NewRequest for convenience purpose.

func CmpJSONResponse Uses

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 Uses

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 Uses

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 Uses

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 Uses

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 Uses

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 Uses

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 Uses

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 Uses

func NewJSONRequest(method, target string, body interface{}) *http.Request

NewJSONRequest creates a new HTTP request with body marshaled to JSON.

func NewXMLRequest Uses

func NewXMLRequest(method, target string, body interface{}) *http.Request

NewXMLRequest creates a new HTTP request with body marshaled to XML.

type Response Uses

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.

Package tdhttp imports 10 packages (graph). Updated 2019-01-14. Refresh now. Tools for package owners.