testsrv

package module
v0.0.0-...-f4fc3fa Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2020 License: MIT Imports: 5 Imported by: 0

README

go-testsrv

Helper for the golang's httptest which spins up simple server with a http response or body and provides a request recorder which can be used in your assertions.

Example usage

func TestNewServerWithStatus(t *testing.T) {
	srv, rec := NewServerWithStatus(http.StatusNotFound)
	defer srv.Close()

	resp, err := c.Get(srv.URL + "/my/test-path")
	require.NoError(t, err)
	defer resp.Body.Close()

	assert.Equal(t, http.StatusNotFound, resp.StatusCode)
    require.NotNil(t, rec.Req)
	assert.Equal(t, http.MethodGet, rec.Req.Method)
	assert.Equal(t, "/my/test-path", rec.Req.URL.String())
	assert.Empty(t, rec.Body)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewHandler

func NewHandler(status int, body []byte) http.HandlerFunc

Types

type ReqRec

type ReqRec struct {
	Req  *http.Request
	Body []byte
}

func NewServer

func NewServer(f http.HandlerFunc) (*httptest.Server, *ReqRec)

func NewServerWithBody

func NewServerWithBody(body string) (*httptest.Server, *ReqRec)

func NewServerWithStatus

func NewServerWithStatus(status int) (*httptest.Server, *ReqRec)

Jump to

Keyboard shortcuts

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