httpmock

package module
v0.0.0-...-9883be9 Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2023 License: MIT Imports: 22 Imported by: 0

README

httpmock Build Status GitHub release GoDoc Coverage Status Go Report Card license

Versatile HTTP mocking made easy in Go that works with any net/http based stdlib implementation.

Heavily inspired by gock. There is also its Python port, pook.

To get started, take a look to the examples.

Features

  • Simple, expressive, fluent API.
  • Semantic API DSL for declarative HTTP mock declarations.
  • Built-in helpers for easy JSON/XML mocking.
  • Supports persistent and volatile TTL-limited mocks.
  • Full regular expressions capable HTTP request mock matching.
  • Designed for both testing and runtime scenarios.
  • Match request by method, URL params, headers and bodies.
  • Extensible and pluggable HTTP matching rules.
  • Ability to switch between mock and real networking modes.
  • Ability to filter/map HTTP requests for accurate mock matching.
  • Supports map and filters to handle mocks easily.
  • Works with any net/http compatible client, such as gentleman.
  • Network timeout/cancelation delay simulation.
  • Extensible and hackable API.
  • Dependency free.

Installation

go get -u github.com/empire/go-httpmock

API

See godoc reference for detailed API documentation.

How it mocks (TODO refine the following items)

  1. Intercepts any HTTP outgoing request via http.DefaultTransport or custom http.Transport used by any http.Client.
  2. Matches outgoing HTTP requests against a pool of defined HTTP mock expectations in FIFO declaration order.
  3. If at least one mock matches, it will be used in order to compose the mock HTTP response.
  4. If no mock can be matched, it will resolve the request with an error, unless real networking mode is enable, in which case a real HTTP request will be performed.

Tips

Testing

Declare your mocks before you start declaring the concrete test logic:

func TestFoo(t *testing.T) {
  s := httpmock.Server()

  httpmock.New(s.URL).
    Get("/bar").
    Reply(200).
    JSON(map[string]string{"foo": "bar"})

  // Your test code starts here...
}
Race conditions

If you're running concurrent code, be aware that your mocks are declared first to avoid unexpected race conditions while configuring httpmock or intercepting custom HTTP clients.

httpmock is not fully thread-safe, but sensible parts are. Any help making httpmock more reliable in this sense is appreciated.

Define complex mocks first

If you're mocking a bunch of mocks in the same test suite, it's recommended to define the more concrete mocks first, and then the generic ones.

This approach usually avoids matching unexpected generic mocks (e.g: specific header, body payload...) instead of the generic ones that performs less complex matches.

Examples

See examples directory for more featured use cases.

Simple mocking via tests
package test

import (
  "io/ioutil"
  "net/http"
  "testing"

  "github.com/empire/go-httpmock"
  "github.com/stretchr/testify/require"
)

func TestSimple(t *testing.T) {
  s := httpmock.Server(t)
  httpmock.New(s.URL).
    Get("/bar").
    Reply(200).
    JSON(map[string]string{"foo": "bar"})

  res, err := http.Get(s.URL + "/bar")
  require.Equal(t, err, nil)
  require.Equal(t, res.StatusCode, 200)

  body, _ := ioutil.ReadAll(res.Body)
  require.Equal(t, string(body)[:13], `{"foo":"bar"}`)

  // Verify that we don't have pending mocks
  require.True(t, httpmock.IsDone(t))
}
Request headers matching
package test

import (
  "io/ioutil"
  "net/http"
  "testing"

  "github.com/empire/go-httpmock"
  "github.com/stretchr/testify/require"
)

func TestMatchHeaders(t *testing.T) {
  s := httpmock.Server(t)

  httpmock.New(s.URL).
    MatchHeader("Authorization", "^foo bar$").
    MatchHeader("API", "1.[0-9]+").
    HeaderPresent("Accept").
    Reply(200).
    BodyString("foo foo")

  req, err := http.NewRequest("GET", s.URL, nil)
  req.Header.Set("Authorization", "foo bar")
  req.Header.Set("API", "1.0")
  req.Header.Set("Accept", "text/plain")

  res, err := (&http.Client{}).Do(req)
  require.Equal(t, err, nil)
  require.Equal(t, res.StatusCode, 200)
  body, _ := ioutil.ReadAll(res.Body)
  require.Equal(t, string(body), "foo foo")

  // Verify that we don't have pending mocks
  require.True(t, httpmock.IsDone(t))
}
Request param matching
package test

import (
  "io/ioutil"
  "net/http"
  "testing"

  "github.com/empire/go-httpmock"
  "github.com/stretchr/testify/require"
)

func TestMatchParams(t *testing.T) {
  s := httpmock.Server(t)
  httpmock.New(s.URL).
    MatchParam("page", "1").
    MatchParam("per_page", "10").
    Reply(200).
    BodyString("foo foo")

  req, err := http.NewRequest("GET", s.URL+"?page=1&per_page=10", nil)

  res, err := (&http.Client{}).Do(req)
  require.Equal(t, err, nil)
  require.Equal(t, res.StatusCode, 200)
  body, _ := ioutil.ReadAll(res.Body)
  require.Equal(t, string(body), "foo foo")

  // Verify that we don't have pending mocks
  require.True(t, httpmock.IsDone(t))
}
JSON body matching and response
package test

import (
  "bytes"
  "io/ioutil"
  "net/http"
  "testing"

  "github.com/empire/go-httpmock"
  "github.com/stretchr/testify/require"
)

func TestMockSimple(t *testing.T) {
  s := httpmock.Server(t)
  httpmock.New(s.URL).
    Post("/bar").
    MatchType("json").
    JSON(map[string]string{"foo": "bar"}).
    Reply(201).
    JSON(map[string]string{"bar": "foo"})

  body := bytes.NewBuffer([]byte(`{"foo":"bar"}`))
  res, err := http.Post(s.URL+"/bar", "application/json", body)
  require.Equal(t, err, nil)
  require.Equal(t, res.StatusCode, 201)

  resBody, _ := ioutil.ReadAll(res.Body)
  require.Equal(t, string(resBody)[:13], `{"bar":"foo"}`)

  // Verify that we don't have pending mocks
  require.True(t, httpmock.IsDone(t))
}
Mocking a custom http.Client and http.RoundTripper
package test

import (
  "io/ioutil"
  "net/http"
  "testing"

  "github.com/empire/go-httpmock"
  "github.com/stretchr/testify/require"
)

func TestClient(t *testing.T) {
  s := httpmock.Server(t)
  httpmock.New(s.URL).
    Reply(200).
    BodyString("foo foo")

  req, err := http.NewRequest("GET", s.URL, nil)
  client := &http.Client{Transport: &http.Transport{}}

  res, err := client.Do(req)
  require.Equal(t, err, nil)
  require.Equal(t, res.StatusCode, 200)
  body, _ := ioutil.ReadAll(res.Body)
  require.Equal(t, string(body), "foo foo")

  // Verify that we don't have pending mocks
  require.True(t, httpmock.IsDone(t))
}
Debug intercepted http requests
// TODO check the following example code
package main

import (
  "bytes"
  "github.com/empire/go-httpmock"
  "net/http"
)

func main() {
  defer httpmock.Off()
  httpmock.Observe(httpmock.DumpRequest)

  httpmock.New("http://foo.com").
    Post("/bar").
    MatchType("json").
    JSON(map[string]string{"foo": "bar"}).
    Reply(200)

  body := bytes.NewBuffer([]byte(`{"foo":"bar"}`))
  http.Post("http://foo.com/bar", "application/json", body)
}

Hacking it!

You can easily hack httpmock defining custom matcher functions with own matching rules.

See add matcher functions and custom matching layer examples for further details.

License

MIT - Tomas Aparicio

Documentation

Index

Constants

View Source
const EOL = 0xa

EOL represents the end of line character.

View Source
const Version = "1.1.2"

Version defines the current package semantic version.

Variables

View Source
var BodyTypeAliases = map[string]string{
	"html": "text/html",
	"text": "text/plain",
	"json": "application/json",
	"xml":  "application/xml",
	"form": "multipart/form-data",
	"url":  "application/x-www-form-urlencoded",
}

BodyTypeAliases stores a generic MIME type by alias.

View Source
var BodyTypes = []string{
	"text/html",
	"text/plain",
	"application/json",
	"application/xml",
	"multipart/form-data",
	"application/x-www-form-urlencoded",
}

BodyTypes stores the supported MIME body types for matching. Currently only text-based types.

View Source
var CompressionSchemes = []string{
	"gzip",
}

CompressionSchemes stores the supported Content-Encoding types for decompression.

View Source
var DefaultMatcher = NewMatcher()

DefaultMatcher stores the default Matcher instance used to match mocks.

View Source
var ErrCannotMatch = errors.New("gock: cannot match any request")

ErrCannotMatch store the error returned in case of no matches.

Matchers stores all the built-in mock matchers.

View Source
var MatchersBody = []MatchFunc{
	MatchBody,
}

MatchersBody exposes an slice of HTTP body specific built-in mock matchers.

MatchersHeader exposes an slice of HTTP header specific mock matchers.

Functions

func CleanUnmatchedRequest

func CleanUnmatchedRequest()

CleanUnmatchedRequest cleans the unmatched requests internal registry.

func GetUnmatchedRequests

func GetUnmatchedRequests() []*http.Request

GetUnmatchedRequests returns all requests that have been received but haven't matched any mock

func HasUnmatchedRequest

func HasUnmatchedRequest() bool

HasUnmatchedRequest returns true if gock has received any requests that didn't match a mock

func IsDone

func IsDone(t *testing.T) bool

func IsPending

func IsPending(t *testing.T) bool

func MatchBody

func MatchBody(req *http.Request, ereq *Request) (bool, error)

MatchBody tries to match the request body. TODO: not too smart now, needs several improvements.

func MatchHeaders

func MatchHeaders(req *http.Request, ereq *Request) (bool, error)

MatchHeaders matches the headers fields of the given request.

func MatchHost

func MatchHost(req *http.Request, ereq *Request) (bool, error)

MatchHost matches the HTTP host header field of the given request.

func MatchMethod

func MatchMethod(req *http.Request, ereq *Request) (bool, error)

MatchMethod matches the HTTP method of the given request.

func MatchPath

func MatchPath(req *http.Request, ereq *Request) (bool, error)

MatchPath matches the HTTP URL path of the given request.

func MatchPathParams

func MatchPathParams(req *http.Request, ereq *Request) (bool, error)

MatchPathParams matches the URL path parameters of the given request.

func MatchQueryParams

func MatchQueryParams(req *http.Request, ereq *Request) (bool, error)

MatchQueryParams matches the URL query params fields of the given request.

func MatchScheme

func MatchScheme(req *http.Request, ereq *Request) (bool, error)

MatchScheme matches the request URL protocol scheme.

func Observe

func Observe(fn ObserverFunc)

Observe provides a hook to support inspection of the request and matched mock TODO is used as a global variable

func Responder

func Responder(req *http.Request, mock *Response, res *http.Response) (*http.Response, error)

Responder builds a mock http.Response based on the given Response mock.

func Server

func Server(t *testing.T) *httptest.Server

Types

type FilterRequestFunc

type FilterRequestFunc func(*http.Request) bool

FilterRequestFunc represents the required function interface for request filters.

type FilterResponseFunc

type FilterResponseFunc func(*http.Response) bool

FilterResponseFunc represents the required function interface impletemed by response filters.

type MapRequestFunc

type MapRequestFunc func(*http.Request) *http.Request

MapRequestFunc represents the required function interface for request mappers.

type MapResponseFunc

type MapResponseFunc func(*http.Response) *http.Response

MapResponseFunc represents the required function interface impletemed by response mappers.

type MatchFunc

type MatchFunc func(*http.Request, *Request) (bool, error)

MatchFunc represents the required function interface implemented by matchers.

type Matcher

type Matcher interface {
	// Get returns a slice of registered function matchers.
	Get() []MatchFunc

	// Add adds a new matcher function.
	Add(MatchFunc)

	// Set sets the matchers functions stack.
	Set([]MatchFunc)

	// Flush flushes the current matchers function stack.
	Flush()

	// Match matches the given http.Request with a mock Request.
	Match(*http.Request, *Request) (bool, error)
}

Matcher represents the required interface implemented by mock matchers.

type Mock

type Mock interface {
	// Disable disables the current mock manually.
	Disable()

	// Done returns true if the current mock is disabled.
	Done() bool

	// Request returns the mock Request instance.
	Request() *Request

	// Response returns the mock Response instance.
	Response() *Response

	// Match matches the given http.Request with the current mock.
	Match(*http.Request) (bool, error)

	// AddMatcher adds a new matcher function.
	AddMatcher(MatchFunc)

	// SetMatcher uses a new matcher implementation.
	SetMatcher(Matcher)
}

Mock represents the required interface that must be implemented by HTTP mock instances.

func Pending

func Pending(t *testing.T) []Mock

type MockMatcher

type MockMatcher struct {
	Matchers []MatchFunc
}

MockMatcher implements a mock matcher

func NewBasicMatcher

func NewBasicMatcher() *MockMatcher

NewBasicMatcher creates a new matcher with header only mock matchers.

func NewEmptyMatcher

func NewEmptyMatcher() *MockMatcher

NewEmptyMatcher creates a new empty matcher without default matchers.

func NewMatcher

func NewMatcher() *MockMatcher

NewMatcher creates a new mock matcher using the default matcher functions.

func (*MockMatcher) Add

func (m *MockMatcher) Add(fn MatchFunc)

Add adds a new function matcher.

func (*MockMatcher) Clone

func (m *MockMatcher) Clone() *MockMatcher

Clone returns a separate MockMatcher instance that has a copy of the same MatcherFuncs

func (*MockMatcher) Flush

func (m *MockMatcher) Flush()

Flush flushes the current matcher

func (*MockMatcher) Get

func (m *MockMatcher) Get() []MatchFunc

Get returns a slice of registered function matchers.

func (*MockMatcher) Match

func (m *MockMatcher) Match(req *http.Request, ereq *Request) (bool, error)

Match matches the given http.Request with a mock request returning true in case that the request matches, otherwise false.

func (*MockMatcher) Set

func (m *MockMatcher) Set(stack []MatchFunc)

Set sets a new stack of matchers functions.

type Mocker

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

Mocker implements a Mock capable interface providing a default mock configuration used internally to store mocks.

func NewMock

func NewMock(req *Request, res *Response) *Mocker

NewMock creates a new HTTP mock based on the given request and response instances. It's mostly used internally.

func (*Mocker) AddMatcher

func (m *Mocker) AddMatcher(fn MatchFunc)

AddMatcher adds a new matcher function for the current mock expectation.

func (*Mocker) Disable

func (m *Mocker) Disable()

Disable disables the current mock manually.

func (*Mocker) Done

func (m *Mocker) Done() bool

Done returns true in case that the current mock instance is disabled and therefore must be removed.

func (*Mocker) Match

func (m *Mocker) Match(req *http.Request) (bool, error)

Match matches the given http.Request with the current Request mock expectation, returning true if matches.

func (*Mocker) Request

func (m *Mocker) Request() *Request

Request returns the Request instance configured for the current HTTP mock.

func (*Mocker) Response

func (m *Mocker) Response() *Response

Response returns the Response instance configured for the current HTTP mock.

func (*Mocker) SetMatcher

func (m *Mocker) SetMatcher(matcher Matcher)

SetMatcher sets a new matcher implementation for the current mock expectation.

type ObserverFunc

type ObserverFunc func(*http.Request, Mock)

ObserverFunc is implemented by users to inspect the outgoing intercepted HTTP traffic

var DumpNoMatchersRequest ObserverFunc = func(request *http.Request, mock Mock) {
	if mock != nil && mock.Response().StatusCode != http.StatusNotImplemented {
		return
	}

	bytes, _ := httputil.DumpRequestOut(request, true)
	fmt.Println(string(bytes))
	fmt.Printf("\nMatches: false\n---\n")
}

Observe(DumpNoMatchersRequest)

var DumpRequest ObserverFunc = func(request *http.Request, mock Mock) {
	bytes, _ := httputil.DumpRequestOut(request, true)
	fmt.Println(string(bytes))
	fmt.Printf("\nMatches: %v\n---\n", mock != nil)
}

DumpRequest is a default implementation of ObserverFunc that dumps the HTTP/1.x wire representation of the http request

type Options

type Options struct {
	// DisableRegexpHost stores if the host is only a plain string rather than regular expression,
	// if DisableRegexpHost is true, host sets in gock.New(...) will be treated as plain string
	DisableRegexpHost bool
}

Options represents customized option for gock

type Request

type Request struct {
	// Mock stores the parent mock reference for the current request mock used for method delegation.
	Mock Mock

	// Response stores the current Response instance for the current matches Request.
	Response *Response

	// Error stores the latest mock request configuration error.
	Error error

	// Counter stores the pending times that the current mock should be active.
	Counter int

	// Persisted stores if the current mock should be always active.
	Persisted bool

	// Options stores options for current Request.
	Options Options

	// URLStruct stores the parsed URL as *url.URL struct.
	URLStruct *url.URL

	// Method stores the Request HTTP method to match.
	Method string

	// CompressionScheme stores the Request Compression scheme to match and use for decompression.
	CompressionScheme string

	// Header stores the HTTP header fields to match.
	Header http.Header

	// Cookies stores the Request HTTP cookies values to match.
	Cookies []*http.Cookie

	// PathParams stores the path parameters to match.
	PathParams map[string]string

	// BodyBuffer stores the body data to match.
	BodyBuffer []byte

	// Mappers stores the request functions mappers used for matching.
	Mappers []MapRequestFunc

	// Filters stores the request functions filters used for matching.
	Filters []FilterRequestFunc
}

Request represents the high-level HTTP request used to store request fields used to match intercepted requests.

func New

func New(uri string) *Request

New creates and registers a new HTTP mock with default settings and returns the Request DSL for HTTP mock definition and set up.

func NewRequest

func NewRequest() *Request

NewRequest creates a new Request instance.

func (*Request) AddMatcher

func (r *Request) AddMatcher(fn MatchFunc) *Request

AddMatcher adds a new matcher function to match the request.

func (*Request) BasicAuth

func (r *Request) BasicAuth(username, password string) *Request

BasicAuth defines a username and password for HTTP Basic Authentication

func (*Request) Body

func (r *Request) Body(body io.Reader) *Request

Body defines the body data to match based on a io.Reader interface.

func (*Request) BodyString

func (r *Request) BodyString(body string) *Request

BodyString defines the body to match based on a given string.

func (*Request) Compression

func (r *Request) Compression(scheme string) *Request

Compression defines the request compression scheme, and enables automatic body decompression. Supports only the "gzip" scheme so far.

func (*Request) Delete

func (r *Request) Delete(path string) *Request

Delete specifies the DELETE method and the given URL path to match.

func (*Request) File

func (r *Request) File(path string) *Request

File defines the body to match based on the given file path string.

func (*Request) Filter

func (r *Request) Filter(fn FilterRequestFunc) *Request

Filter filters a new request filter function to filter http.Request before the matching process.

func (*Request) Get

func (r *Request) Get(path string) *Request

Get specifies the GET method and the given URL path to match.

func (*Request) Head

func (r *Request) Head(path string) *Request

Head specifies the HEAD method and the given URL path to match.

func (*Request) HeaderPresent

func (r *Request) HeaderPresent(key string) *Request

HeaderPresent defines that a header field must be present in the request.

func (*Request) JSON

func (r *Request) JSON(data interface{}) *Request

JSON defines the JSON body to match based on a given structure.

func (*Request) Map

func (r *Request) Map(fn MapRequestFunc) *Request

Map adds a new request mapper function to map http.Request before the matching process.

func (*Request) MatchHeader

func (r *Request) MatchHeader(key, value string) *Request

MatchHeader defines a new key and value header to match.

func (*Request) MatchHeaders

func (r *Request) MatchHeaders(headers map[string]string) *Request

MatchHeaders defines a map of key-value headers to match.

func (*Request) MatchParam

func (r *Request) MatchParam(key, value string) *Request

MatchParam defines a new key and value URL query param to match.

func (*Request) MatchParams

func (r *Request) MatchParams(params map[string]string) *Request

MatchParams defines a map of URL query param key-value to match.

func (*Request) MatchType

func (r *Request) MatchType(kind string) *Request

MatchType defines the request Content-Type MIME header field. Supports custom MIME types and type aliases. E.g: json, xml, form, text...

func (*Request) ParamPresent

func (r *Request) ParamPresent(key string) *Request

ParamPresent matches if the given query param key is present in the URL.

func (*Request) Patch

func (r *Request) Patch(path string) *Request

Patch specifies the PATCH method and the given URL path to match.

func (*Request) Path

func (r *Request) Path(path string) *Request

Path defines the mock URL path value to match.

func (*Request) PathParam

func (r *Request) PathParam(key, val string) *Request

PathParam matches if a given path parameter key is present in the URL.

The value is representative of the restful resource the key defines, e.g.

// /users/123/name
r.PathParam("users", "123")

would match.

func (*Request) Persist

func (r *Request) Persist() *Request

Persist defines the current HTTP mock as persistent and won't be removed after intercepting it.

func (*Request) Post

func (r *Request) Post(path string) *Request

Post specifies the POST method and the given URL path to match.

func (*Request) Put

func (r *Request) Put(path string) *Request

Put specifies the PUT method and the given URL path to match.

func (*Request) Reply

func (r *Request) Reply(status int) *Response

Reply defines the Response status code and returns the mock Response DSL.

func (*Request) ReplyError

func (r *Request) ReplyError(err error) *Response

ReplyError defines the Response simulated error.

func (*Request) ReplyFunc

func (r *Request) ReplyFunc(replier func(*Response)) *Response

ReplyFunc allows the developer to define the mock response via a custom function.

func (*Request) SetMatcher

func (r *Request) SetMatcher(matcher Matcher) *Request

SetMatcher sets a new matcher function to match the request.

func (*Request) SetURL

func (r *Request) SetURL(u *url.URL) *Request

SetURL defines the url.URL struct to be used for matching.

func (*Request) Times

func (r *Request) Times(num int) *Request

Times defines the number of times that the current HTTP mock should remain active.

func (*Request) URL

func (r *Request) URL(uri string) *Request

URL defines the mock URL to match.

func (*Request) WithOptions

func (r *Request) WithOptions(options Options) *Request

WithOptions sets the options for the request.

func (*Request) XML

func (r *Request) XML(data interface{}) *Request

XML defines the XML body to match based on a given structure.

type Response

type Response struct {
	// Mock stores the parent mock reference for the current response mock used for method delegation.
	Mock Mock

	// Error stores the latest response configuration or injected error.
	Error error

	// StatusCode stores the response status code.
	StatusCode int

	// Headers stores the response headers.
	Header http.Header

	// Cookies stores the response cookie fields.
	Cookies []*http.Cookie

	// BodyBuffer stores the array of bytes to use as body.
	BodyBuffer []byte

	// ResponseDelay stores the simulated response delay.
	ResponseDelay time.Duration

	// Mappers stores the request functions mappers used for matching.
	Mappers []MapResponseFunc

	// Filters stores the request functions filters used for matching.
	Filters []FilterResponseFunc
}

Response represents high-level HTTP fields to configure and define HTTP responses intercepted by gock.

func NewResponse

func NewResponse() *Response

NewResponse creates a new Response.

func (*Response) AddHeader

func (r *Response) AddHeader(key, value string) *Response

AddHeader adds a new header field in the mock response with out removing an existent one.

func (*Response) Body

func (r *Response) Body(body io.Reader) *Response

Body sets the HTTP response body to be used.

func (*Response) BodyString

func (r *Response) BodyString(body string) *Response

BodyString defines the response body as string.

func (*Response) Delay

func (r *Response) Delay(delay time.Duration) *Response

Delay defines the response simulated delay. This feature is still experimental and will be improved in the future.

func (*Response) Done

func (r *Response) Done() bool

Done returns true if the mock was done and disabled.

func (*Response) File

func (r *Response) File(path string) *Response

File defines the response body reading the data from disk based on the file path string.

func (*Response) Filter

func (r *Response) Filter(fn FilterResponseFunc) *Response

Filter filters a new request filter function to filter http.Request before the matching process.

func (*Response) JSON

func (r *Response) JSON(data interface{}) *Response

JSON defines the response body based on a JSON based input.

func (*Response) Map

func (r *Response) Map(fn MapResponseFunc) *Response

Map adds a new response mapper function to map http.Response before the matching process.

func (*Response) SetError

func (r *Response) SetError(err error) *Response

SetError defines the response simulated error.

func (*Response) SetHeader

func (r *Response) SetHeader(key, value string) *Response

SetHeader sets a new header field in the mock response.

func (*Response) SetHeaders

func (r *Response) SetHeaders(headers map[string]string) *Response

SetHeaders sets a map of header fields in the mock response.

func (*Response) Status

func (r *Response) Status(code int) *Response

Status defines the desired HTTP status code to reply in the current response.

func (*Response) Type

func (r *Response) Type(kind string) *Response

Type defines the response Content-Type MIME header field. Supports type alias. E.g: json, xml, form, text...

func (*Response) XML

func (r *Response) XML(data interface{}) *Response

XML defines the response body based on a XML based input.

type Transport

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

Transport implements http.RoundTripper, which fulfills single http requests issued by an http.Client.

gock's Transport encapsulates a given or default http.Transport for further delegation, if needed.

func NewTransport

func NewTransport(mocks *_mocks) *Transport

NewTransport creates a new *Transport with no responders.

func (*Transport) CancelRequest

func (m *Transport) CancelRequest(req *http.Request)

CancelRequest is a no-op function.

func (*Transport) RoundTrip

func (m *Transport) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip receives HTTP requests and routes them to the appropriate responder. It is required to implement the http.RoundTripper interface. You will not interact with this directly, instead the *http.Client you are using will call it for you.

Directories

Path Synopsis
_examples
networking_partially_enabled
This example shows how to enable the networking for a request to a local server and mock a second request to a remote server.
This example shows how to enable the networking for a request to a local server and mock a second request to a remote server.

Jump to

Keyboard shortcuts

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