httputils

package module
v0.0.0-...-49c4daf Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2024 License: MIT Imports: 6 Imported by: 0

README

httputils

httputils is a library for testing http.Handler in unit tests without starting the actual web service.

go get github.com/jxsl13/httputils@latest

Example test


func TestRoundtrip(t *testing.T) {

	for _, ct := range pickle.ContentTypes {
		mux := http.NewServeMux()
		mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
			if !strings.EqualFold(r.Method, http.MethodPost) {
				w.WriteHeader(http.StatusMethodNotAllowed)
				return
			}
			w.Header().Add("Content-Type", ct)
			w.WriteHeader(http.StatusOK)
			_, err := io.Copy(w, r.Body)
			if err != nil {
				w.WriteHeader(http.StatusInternalServerError)
			}
		})

		h := httputils.New(mux)

		var (
			body   = "peter porker"
			result = ""
		)
		err := h.Post("/", body, &result, ct)
		require.NoError(t, err)
		require.Equal(t, body, result)

	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CONNECT

func CONNECT(handler http.Handler, pathQuery string, requestOptions ...RequestOption) *httptest.ResponseRecorder

func DELETE

func DELETE(handler http.Handler, pathQuery string, requestOptions ...RequestOption) *httptest.ResponseRecorder

func Exec

func Exec(handler http.Handler, method, pathQuery string, body io.Reader, requestOptions ...RequestOption) *httptest.ResponseRecorder

func GET

func GET(handler http.Handler, pathQuery string, requestOptions ...RequestOption) *httptest.ResponseRecorder

GET simulates a Get request

func HEAD(handler http.Handler, pathQuery string, requestOptions ...RequestOption) *httptest.ResponseRecorder

func OPTIONS

func OPTIONS(handler http.Handler, pathQuery string, requestOptions ...RequestOption) *httptest.ResponseRecorder

func PATCH

func PATCH(handler http.Handler, pathQuery string, body io.Reader, requestOptions ...RequestOption) *httptest.ResponseRecorder

func POST

func POST(handler http.Handler, pathQuery string, body io.Reader, requestOptions ...RequestOption) *httptest.ResponseRecorder

Post simulates a POST request

func PUT

func PUT(handler http.Handler, pathQuery string, body io.Reader, requestOptions ...RequestOption) *httptest.ResponseRecorder

func TRACE

func TRACE(handler http.Handler, pathQuery string, requestOptions ...RequestOption) *httptest.ResponseRecorder

Types

type HTTPTester

type HTTPTester struct {
	// contains filtered or unexported fields
}

func New

func New(handler http.Handler, options ...TesterOption) *HTTPTester

New creates an extended http tester object that is able to encode and decode bodys

func (*HTTPTester) Connect

func (t *HTTPTester) Connect(pathQuery string, requestOptions ...RequestOption) (http.Header, error)

func (*HTTPTester) Delete

func (t *HTTPTester) Delete(pathQuery string, result any, requestOptions ...RequestOption) error

func (*HTTPTester) Get

func (t *HTTPTester) Get(pathQuery string, result any, requestOptions ...RequestOption) error

func (*HTTPTester) Head

func (t *HTTPTester) Head(pathQuery string, requestOptions ...RequestOption) (http.Header, error)

func (*HTTPTester) Options

func (t *HTTPTester) Options(pathQuery string, requestOptions ...RequestOption) (http.Header, error)

func (*HTTPTester) Patch

func (t *HTTPTester) Patch(pathQuery string, body, result any, contentType string, requestOptions ...RequestOption) error

func (*HTTPTester) Post

func (t *HTTPTester) Post(pathQuery string, body, result any, contentType string, requestOptions ...RequestOption) error

func (*HTTPTester) Put

func (t *HTTPTester) Put(pathQuery string, body, result any, contentType string, requestOptions ...RequestOption) error

func (*HTTPTester) Trace

func (t *HTTPTester) Trace(pathQuery string, requestOptions ...RequestOption) (*httptest.ResponseRecorder, error)

type RequestOption

type RequestOption func(*http.Request)

func WithAddHeader

func WithAddHeader(key, value string) RequestOption

WithAddHeader adds a header without overwriting an existing header

func WithContentType

func WithContentType(contentType string) RequestOption

WithAddHeader adds a header without overwriting an existing header

func WithHeaders

func WithHeaders(headers map[string]string) RequestOption

WithHeaders overwrites existing headers with the key value pars from the map

func WithMultiHeaders

func WithMultiHeaders(headers http.Header) RequestOption

WithMultiHeaders allows to set multi value headers

func WithSetHeader

func WithSetHeader(key, value string) RequestOption

WithAddHeader sets a header overwriting an existing header

type TesterOption

type TesterOption func(*HTTPTester)

func WithFallbackPickle

func WithFallbackPickle(pickle pickle.Pickle) TesterOption

func WithPickle

func WithPickle(pickle pickle.Pickle) TesterOption

func WithPickleJar

func WithPickleJar(pickle map[string]pickle.Pickle) TesterOption

func WithRequestOptions

func WithRequestOptions(options ...RequestOption) TesterOption

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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