Documentation ¶
Overview ¶
Utility functions to help writing tests for a Go-Json-Rest app
Go comes with net/http/httptest to help writing test for an http server. When this http server implements a JSON REST API, some basic checks end up to be always the same. This test package tries to save some typing by providing helpers for this particular use case.
package main import ( "github.com/viwiilover/go-json-rest/rest" "github.com/viwiilover/go-json-rest/rest/test" "testing" ) func TestSimpleRequest(t *testing.T) { api := rest.NewApi() api.Use(rest.DefaultDevStack...) router, err := rest.MakeRouter( rest.Get("/r", func(w rest.ResponseWriter, r *rest.Request) { w.WriteJson(map[string]string{"Id": "123"}) }), ) if err != nil { log.Fatal(err) } api.SetApp(router) recorded := test.RunRequest(t, api.MakeHandler(), test.MakeSimpleRequest("GET", "http://1.2.3.4/r", nil)) recorded.CodeIs(200) recorded.ContentTypeIsJson() }
Index ¶
- func BodyIs(t *testing.T, r *httptest.ResponseRecorder, expectedBody string)
- func CodeIs(t *testing.T, r *httptest.ResponseRecorder, expectedCode int)
- func ContentEncodingIsGzip(t *testing.T, r *httptest.ResponseRecorder)
- func ContentTypeIsJson(t *testing.T, r *httptest.ResponseRecorder)
- func DecodeJsonPayload(r *httptest.ResponseRecorder, v interface{}) error
- func DecodedBody(r *httptest.ResponseRecorder) ([]byte, error)
- func HeaderIs(t *testing.T, r *httptest.ResponseRecorder, headerKey, expectedValue string)
- func MakeSimpleRequest(method string, urlStr string, payload interface{}) *http.Request
- type Recorded
- func (rd *Recorded) BodyIs(expectedBody string)
- func (rd *Recorded) CodeIs(expectedCode int)
- func (rd *Recorded) ContentEncodingIsGzip()
- func (rd *Recorded) ContentTypeIsJson()
- func (rd *Recorded) DecodeJsonPayload(v interface{}) error
- func (rd *Recorded) DecodedBody() ([]byte, error)
- func (rd *Recorded) HeaderIs(headerKey, expectedValue string)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CodeIs ¶
func CodeIs(t *testing.T, r *httptest.ResponseRecorder, expectedCode int)
CodeIs compares the rescorded status code
func ContentEncodingIsGzip ¶
func ContentEncodingIsGzip(t *testing.T, r *httptest.ResponseRecorder)
func ContentTypeIsJson ¶
func ContentTypeIsJson(t *testing.T, r *httptest.ResponseRecorder)
func DecodeJsonPayload ¶
func DecodeJsonPayload(r *httptest.ResponseRecorder, v interface{}) error
func DecodedBody ¶
func DecodedBody(r *httptest.ResponseRecorder) ([]byte, error)
DecodedBody returns the entire body read from r.Body, with it gunzipped if Content-Encoding is set to gzip
Types ¶
type Recorded ¶
type Recorded struct { T *testing.T Recorder *httptest.ResponseRecorder }
func RunRequest ¶
RunRequest runs a HTTP request through the given handler
func (*Recorded) ContentEncodingIsGzip ¶
func (rd *Recorded) ContentEncodingIsGzip()
func (*Recorded) ContentTypeIsJson ¶
func (rd *Recorded) ContentTypeIsJson()
func (*Recorded) DecodeJsonPayload ¶
func (*Recorded) DecodedBody ¶
Click to show internal directories.
Click to hide internal directories.