Documentation ¶
Overview ¶
Package safehttptest provides utilities for testing safehttp.Handler:s and safehttp.Interceptor:s.
Index ¶
- func NewRequest(method, target string, body io.Reader) *safehttp.IncomingRequest
- type FakeDispatcher
- type FakeResponseWriter
- func (frw *FakeResponseWriter) AddCookie(c *safehttp.Cookie) error
- func (frw *FakeResponseWriter) Header() safehttp.Header
- func (frw *FakeResponseWriter) NoContent() safehttp.Result
- func (frw *FakeResponseWriter) Write(resp safehttp.Response) safehttp.Result
- func (frw *FakeResponseWriter) WriteError(resp safehttp.ErrorResponse) safehttp.Result
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 ¶
func (d *FakeDispatcher) Error(rw http.ResponseWriter, resp safehttp.ErrorResponse) error
Error writes just the status code.
func (*FakeDispatcher) Write ¶
func (d *FakeDispatcher) Write(rw http.ResponseWriter, resp safehttp.Response) error
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 ¶
func (frw *FakeResponseWriter) Write(resp safehttp.Response) safehttp.Result
Write forwards the response to Dispatcher.Write.
func (*FakeResponseWriter) WriteError ¶
func (frw *FakeResponseWriter) WriteError(resp safehttp.ErrorResponse) safehttp.Result
WriteError forwards the error response to Dispatcher.WriteError.