safehttptest

package
v0.0.0-...-f115076 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2024 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package safehttptest provides utilities for testing safehttp.Handler:s and safehttp.Interceptor:s.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewRequest

func NewRequest(method, target string, body io.Reader) *safehttp.IncomingRequest

NewRequest returns a new incoming server Request, suitable for passing to an http.Handler for testing.

The target is the RFC 7230 "request-target": it may be either a path or an absolute URL. If target is an absolute URL, the host name from the URL is used. Otherwise, "example.com" is used.

The TLS field is set to a non-nil dummy value if target has scheme "https".

The Request.Proto is always HTTP/1.1.

An empty method means "GET".

The provided body may be nil. If the body is of type *bytes.Reader, *strings.Reader, or *bytes.Buffer, the Request.ContentLength is set.

NewRequest panics on error for ease of use in testing, where a panic is acceptable.

To generate a client HTTP request instead of a server request, see the NewRequest function in the net/http package.

Types

type FakeDispatcher

type FakeDispatcher struct {
	Written    safehttp.Response
	Dispatcher safehttp.Dispatcher
}

FakeDispatcher provides a minimal implementation of the Dispatcher to be used for testing Interceptors.

func (*FakeDispatcher) Error

Error writes just the status code.

func (*FakeDispatcher) Write

Write records the written responses and calls Dispatcher.Write. If Dispatcher is nil, the default one is used.

type FakeResponseWriter

type FakeResponseWriter struct {
	// The Dispatcher implementation
	Dispatcher *FakeDispatcher

	// ResponseWriter is only used for calls to Dispatcher. Calls to AddCookie()
	// do not affect it.
	ResponseWriter http.ResponseWriter

	// Cookies coming from AddCookie() calls. Use the ResponseWriter to see what
	// cookies have been set by the Dispatcher.
	Cookies []*safehttp.Cookie

	// Response headers.
	Headers safehttp.Header
}

FakeResponseWriter creates a fake safehttp.ResponseWriter implementation.

It performs no error checking nor runs interceptors.

func NewFakeResponseWriter

func NewFakeResponseWriter() (*FakeResponseWriter, *httptest.ResponseRecorder)

NewFakeResponseWriter creates a new safehttp.ResponseWriter implementation and a httptest.ResponseRecorder, for testing purposes only.

func (*FakeResponseWriter) AddCookie

func (frw *FakeResponseWriter) AddCookie(c *safehttp.Cookie) error

AddCookie appends the given cookie to the Cookies field.

func (*FakeResponseWriter) Header

func (frw *FakeResponseWriter) Header() safehttp.Header

Header returns the Header.

func (*FakeResponseWriter) NoContent

func (frw *FakeResponseWriter) NoContent() safehttp.Result

NoContent writes just the NoContent status code.

func (*FakeResponseWriter) Write

Write forwards the response to Dispatcher.Write.

func (*FakeResponseWriter) WriteError

WriteError forwards the error response to Dispatcher.WriteError.

Jump to

Keyboard shortcuts

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