Documentation ¶
Overview ¶
Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT License.
Index ¶
- type ResponseOption
- type ResponsePredicate
- type Server
- func (s *Server) AppendError(err error)
- func (s *Server) AppendResponse(opts ...ResponseOption)
- func (s *Server) Do(req *http.Request) (*http.Response, error)
- func (s *Server) RepeatError(n int, err error)
- func (s *Server) RepeatResponse(n int, opts ...ResponseOption)
- func (s *Server) Requests() int
- func (s *Server) ServerConfig() *http.Server
- func (s *Server) SetError(err error)
- func (s *Server) SetResponse(opts ...ResponseOption)
- func (s *Server) URL() string
- type ServerOption
- type TrackedClose
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
Requests returns the number of times an HTTP request was made. NOTE: this is *not* goroutine safe to be called while requests are being served.
func (*Server) ServerConfig ¶
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 ¶
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.
type ServerOption ¶
type ServerOption interface {
// contains filtered or unexported methods
}
ServerOption is an abstraction for configuring a mock Server.
func WithHTTP2Enabled ¶
func WithHTTP2Enabled(enabled bool) ServerOption
WithHTTP2Enabled sets the HTTP2Enabled field on the testserver to the boolean value provided.
func WithTLSConfig ¶
func WithTLSConfig(cfg *tls.Config) ServerOption
WithTLSConfig sets the given TLS config on server.
func WithTransformAllRequestsToTestServerUrl ¶
func WithTransformAllRequestsToTestServerUrl() ServerOption
type TrackedClose ¶
type TrackedClose func() bool
TrackedClose, when invoked, returns true if Close was called.
func NewTrackedCloser ¶
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.