handlertest

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jan 1, 2023 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package handlertest implements support for testing implementations of HTTP handlers.

This is inspired by fstest.TestFS, but implemented differently, notably using a testing.T parameter for better reporting.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HostTest

func HostTest(t *testing.T, h handler.Host, newCtx func(handler.Features) (context.Context, handler.Features)) error

HostTest tests a handler.Host by testing default property values and ability to change them.

To use this, pass your host and the context which allows access to the request and response objects. This should be configured for "HTTP/1.1".

Here's an example of a net/http host which supports no features:

newCtx := func(features handler.Features) (context.Context, handler.Features) {
	if features != 0 {
		return testCtx, 0 // unsupported
	}

	r, _ := http.NewRequest("GET", "", nil)
	w := &httptest.ResponseRecorder{HeaderMap: map[string][]string{}}
	return context.WithValue(testCtx, requestStateKey{}, &requestState{r: r, w: w}), features
}

// Run all the tests
handlertest.HostTest(t, host{}, newCtx)

Types

This section is empty.

Jump to

Keyboard shortcuts

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