Documentation ¶
Index ¶
Constants ¶
const ( // ANY matches with any HTTP method. ANY = Method("") CONNECT = Method("CONNECT") DELETE = Method("DELETE") GET = Method("GET") HEAD = Method("HEAD") OPTIONS = Method("OPTIONS") PATCH = Method("PATCH") POST = Method("POST") PUT = Method("PUT") TRACE = Method("TRACE") // AnyPath matches will all request paths. AnyPath = "!AnyPath!" )
Variables ¶
This section is empty.
Functions ¶
func FailureFunc ¶
FailureFunc is a round trip function that returns error. It can simulate connection error or timeouts.
Types ¶
type RoundTripFunc ¶
RoundTripFunc is a function to mock HTTP request-response round trip. It is loaded into standard http.Client as a mock transport.
type Server ¶
type Server struct { DefaultRoundTrip RoundTripFunc // contains filtered or unexported fields }
Server is a test HTTP server that is able to stack multiple round trips for any test case. Example usage:
s := httpt.NewServer(t) s.Push(StringResponse(http.StatusBadRequest, "really bad request")) ... // Make sure your component uses mocked http e.g passed in context: ctx = context.WithValue(ctx, oauth2.HTTPClient, s.HTTTPClient()) // Or used directly: resp, err := s.HTTPClient().Do(request)
func NewRawServer ¶
func NewRawServer() *Server
NewRawServer constructs Server without any default round trip function. This is used when someone needs to use Server without testing package.
func NewServer ¶
NewServer constructs Server with NotMockedFunc as default. Always use that when running within go test.
func (*Server) HTTPClient ¶
HTTPClient returns standrard http.Client to feed components the needs to be mocked.
func (*Server) Len ¶
Len returns number of round trip functions (requests) that are mocked. Useful example:
assert.Equal(t, 0, s.Len()) // at the end of your unit test with httpt.Server, to check if all mocked requests were actually used.
func (Server) On ¶
On specifies particular method and path for mocked round trip function. Example usage:
server.On(httpt.GET, "/path/test").Push(<any round trip function>)
func (*Server) Reset ¶
func (s *Server) Reset()
Reset resets stacked round trip functions. Nothing is mocked after that.
func (*Server) StillExpectedRTs ¶
NotDoneRTs returns string slice with concatenated [METHOD]path for Round trips which are still expected. Useful when after test Len != 0.