tdhttp

package
v1.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 18, 2019 License: BSD-2-Clause Imports: 11 Imported by: 0

Documentation

Overview

Package tdhttp provides some functions to easily test HTTP handlers.

Index

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

func Delete(target string, body io.Reader, headers ...string) *http.Request

Delete is a shortcut for:

NewRequest(http.MethodDelete, target, body, headers...)

func DeleteJSON added in v1.1.1

func DeleteJSON(target string, body interface{}, headers ...string) *http.Request

DeleteJSON is a shortcut for:

NewJSONRequest(http.MethodDelete, target, body, headers...)

func DeleteXML added in v1.1.1

func DeleteXML(target string, body interface{}, headers ...string) *http.Request

DeleteXML is a shortcut for:

NewXMLRequest(http.MethodDelete, target, body, headers...)

func Get added in v1.1.1

func Get(target string, headers ...string) *http.Request

Get is shortcut for:

NewRequest(http.MethodGet, target, nil, headers...)

func NewJSONRequest

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

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

func NewRequest(method, target string, body io.Reader, headers ...string) *http.Request

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

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

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

func Patch(target string, body io.Reader, headers ...string) *http.Request

Patch is a shortcut for:

NewRequest(http.MethodPatch, target, body, headers...)

func PatchJSON added in v1.1.1

func PatchJSON(target string, body interface{}, headers ...string) *http.Request

PatchJSON is a shortcut for:

NewJSONRequest(http.MethodPatch, target, body, headers...)

func PatchXML added in v1.1.1

func PatchXML(target string, body interface{}, headers ...string) *http.Request

PatchXML is a shortcut for:

NewXMLRequest(http.MethodPatch, target, body, headers...)

func Post added in v1.1.1

func Post(target string, body io.Reader, headers ...string) *http.Request

Post is a shortcut for:

NewRequest(http.MethodPost, target, body, headers...)

func PostJSON added in v1.1.1

func PostJSON(target string, body interface{}, headers ...string) *http.Request

PostJSON is a shortcut for:

NewJSONRequest(http.MethodPost, target, body, headers...)

func PostXML added in v1.1.1

func PostXML(target string, body interface{}, headers ...string) *http.Request

PostXML is a shortcut for:

NewXMLRequest(http.MethodPost, target, body, headers...)

func Put added in v1.1.1

func Put(target string, body io.Reader, headers ...string) *http.Request

Put is a shortcut for:

NewRequest(http.MethodPut, target, body, headers...)

func PutJSON added in v1.1.1

func PutJSON(target string, body interface{}, headers ...string) *http.Request

PutJSON is a shortcut for:

NewJSONRequest(http.MethodPut, target, body, headers...)

func PutXML added in v1.1.1

func PutXML(target string, body interface{}, headers ...string) *http.Request

PutXML is a shortcut for:

NewXMLRequest(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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL