reqtest

package
v0.9.1 Latest Latest
Warning

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

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

Documentation

Overview

Package reqtest provides utilities for testing requests and responses.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewHandler

func NewHandler(g router.Group) http.Handler

NewHandler creates a handler from an endpoint group for testing.

func NewRequestJSON

func NewRequestJSON(method, target string, body any) *http.Request

NewRequestJSON wraps around httptest.NewRequest to return a new incoming server Request containing a JSON encoded request body. NewRequestJSON panics on error for ease of use in testing, where a panic is acceptable.

Types

type ResponseRecord

type ResponseRecord struct {
	StatusCode int
	Header     http.Header
	Body       []byte
	// contains filtered or unexported fields
}

ResponseRecord contains the status code, header, and body of the recorded response. More fields may be populated in the future, so callers should not DeepEqual the result in tests. Use the provided assertion methods instead.

func Record

func Record(t *testing.T, h http.Handler, req *http.Request) *ResponseRecord

Record the response generated by the handler for testing.

func (*ResponseRecord) AssertBodyContains

func (r *ResponseRecord) AssertBodyContains(v string)

AssertBodyContains marks the test as failed if the response body does not contain the provided substring.

func (*ResponseRecord) AssertHeaderContains

func (r *ResponseRecord) AssertHeaderContains(field string, v string)

AssertHeaderContains marks the test as failed if the response header field does not contain the provided substring.

func (*ResponseRecord) AssertStatusCode

func (r *ResponseRecord) AssertStatusCode(v int)

AssertStatusCode marks the test as failed if the response status code does not match the expected value.

type StubGroup

type StubGroup struct{}

StubGroup is an endpoint group that returns hard-coded values for testing.

func (*StubGroup) Mount

func (s *StubGroup) Mount(r *gin.RouterGroup)

Mount initializes group-level middlewares and mounts the endpoints.

func (*StubGroup) Prefixes

func (s *StubGroup) Prefixes() []string

Prefixes returns the common path prefixes for endpoints in the group.

Jump to

Keyboard shortcuts

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