mock

package
v1.6.0 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT License.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ResponseOption

type ResponseOption interface {
	// contains filtered or unexported methods
}

ResponseOption is an abstraction for configuring a mock HTTP response.

func WithBody

func WithBody(b []byte) ResponseOption

WithBody sets the HTTP response's body to the specified value.

func WithBodyReadError added in v0.1.1

func WithBodyReadError() ResponseOption

WithBodyReadError returns a response that will fail when reading the body.

func WithHeader

func WithHeader(k, v string) ResponseOption

WithHeader adds the specified header and value to the HTTP response.

func WithPredicate added in v0.2.3

func WithPredicate(p ResponsePredicate) ResponseOption

WithPredicate invokes the specified predicate func on the HTTP request. If the predicate returns true, the response associated with the predicate is returned and the next response is removed from the queue. When false, the associated response is ignored and the next one is returned. NOTE: not supported for static responses, will cause a panic.

func WithSlowResponse added in v0.1.1

func WithSlowResponse(d time.Duration) ResponseOption

WithSlowResponse will sleep for the specified duration before returning the HTTP response.

func WithStatusCode

func WithStatusCode(c int) ResponseOption

WithStatusCode sets the HTTP response's status code to the specified value.

type ResponsePredicate added in v0.2.3

type ResponsePredicate func(*http.Request) bool

ResponsePredicate is a predicate that's invoked in response to an HTTP request.

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server is a wrapper around an httptest.Server. The serving of requests is not safe for concurrent use which is ok for right now as each test creates is own server and doesn't create additional go routines.

func NewServer

func NewServer(cfg ...ServerOption) (*Server, func())

NewServer creates a new Server object. The returned close func must be called when the server is no longer needed.

func NewTLSServer

func NewTLSServer(cfg ...ServerOption) (*Server, func())

NewTLSServer creates a new Server object and applies any additional ServerOption configurations provided on the Server. The returned close func must be called when the server is no longer needed. It will return nil for both the server and close func if it encountered an error when configuring HTTP2 for the new TLS server.

func (*Server) AppendError

func (s *Server) AppendError(err error)

AppendError appends the error to the end of the response queue.

func (*Server) AppendResponse

func (s *Server) AppendResponse(opts ...ResponseOption)

AppendResponse appends the response to the end of the response queue. If no options are provided the default response is an http.StatusOK.

func (*Server) Do

func (s *Server) Do(req *http.Request) (*http.Response, error)

Do implements the azcore.Transport interface on Server. Calling this when the response queue is empty and no static response has been set will cause a panic.

func (*Server) RepeatError

func (s *Server) RepeatError(n int, err error)

RepeatError appends the error n number of times to the end of the response queue.

func (*Server) RepeatResponse

func (s *Server) RepeatResponse(n int, opts ...ResponseOption)

RepeatResponse appends the response n number of times to the end of the response queue. If no options are provided the default response is an http.StatusOK.

func (*Server) Requests

func (s *Server) Requests() int

Requests returns the number of times an HTTP request was made.

func (*Server) ServerConfig added in v0.4.0

func (s *Server) ServerConfig() *http.Server

ServerConfig returns the server config. Please note this should not be modified since Start or StartTLS has already been called on the server.

func (*Server) SetError

func (s *Server) SetError(err error)

SetError indicates the same error should always be returned. Any responses set via other methods will be ignored.

func (*Server) SetResponse

func (s *Server) SetResponse(opts ...ResponseOption)

SetResponse indicates the same response should always be returned. Any responses set via other methods will be ignored. If no options are provided the default response is an http.StatusOK. NOTE: does not support WithPredicate(), will cause a panic.

func (*Server) URL

func (s *Server) URL() string

URL returns the endpoint of the test server in URL format.

type ServerOption added in v0.4.0

type ServerOption interface {
	// contains filtered or unexported methods
}

ServerOption is an abstraction for configuring a mock Server.

func WithHTTP2Enabled added in v0.5.0

func WithHTTP2Enabled(enabled bool) ServerOption

WithHTTP2Enabled sets the HTTP2Enabled field on the testserver to the boolean value provided.

func WithTLSConfig added in v0.4.0

func WithTLSConfig(cfg *tls.Config) ServerOption

WithTLSConfig sets the given TLS config on server.

func WithTransformAllRequestsToTestServerUrl added in v0.9.0

func WithTransformAllRequestsToTestServerUrl() ServerOption

type TrackedClose added in v0.8.3

type TrackedClose func() bool

TrackedClose, when invoked, returns true if Close was called.

func NewTrackedCloser added in v0.8.3

func NewTrackedCloser(r io.Reader) (io.ReadCloser, TrackedClose)

NewTrackedCloser is similar to io.NopCloser but tracks that Close was called. Call TrackedClose to check if Close has been called.

Jump to

Keyboard shortcuts

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