gock

package
v3.15.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Jan 25, 2018 License: Apache-2.0, Apache-2.0 Imports: 17 Imported by: 0

Documentation

Overview

Out-of-box Gock/Gentleman functions.

Initializing a gock

The "GockConfigBuilder.NewConfigByRandom()" function would set-up a random configuration for gock testing. And you could use "*GockConfig.NewClient()" to build client object of Gentleman with defined URL.

gockConfig := GockConfigBuilder.NewConfigByRandom()

gockConfig.New().Get("/your-resource").
	Reply(http.StatusOK).
	JSON(
		map[string]interface{} {
			"v1": "hello",
			"v2": 20,
		},
	)

client := gockConfig.NewClient()

// ... testing by gentleman ...

Bridge of httptest

"*GockConfig" has supports for interface of "testing/http/HttpTest", which you could use it to start a "real" web server by mocked implementation.

server := gockConfig.HttpTest.NewServer(&FakeServerConfig{ Host: "127.0.0.1", Port: 10401 })

server.Start()
defer server.Stop()

// The URL http://127.0.0.1:10401/agent/33 would be intercepted by this mock configuration.
gockConfig.New().Get("/agent/33").
	Reply(http.StatusOK).
	JSON(
		map[string]interface{} {
			"v1": "hello",
			"v2": 20,
		},
	)

// testing...

Index

Constants

This section is empty.

Variables

View Source
var GockConfigBuilder = &struct {
	NewConfig         func(host string, port uint16) *GockConfig
	NewConfigByRandom func() *GockConfig
}{
	NewConfig: newGockConfig,
	NewConfigByRandom: func() *GockConfig {
		rand.Seed(time.Now().Unix())
		port := rand.Int31n(1000) + 30000
		host := fmt.Sprintf("test-pc%03d.gock.kordan.asshole", rand.Int31n(999)+1)

		return newGockConfig(host, uint16(port))
	},
}

Functions in namespace for building of *GockConfig

NewConfig(string, string) *GockConfig:

Constructs a new configuration with your host and port

NewConfigByRandom() *GockConfig:

Constructs a new configuration with generated(randomly) host and port
Port is range from 30000 ~ 30999.
Host would have naming of suffixing by random number from "001" ~ "999".

Functions

This section is empty.

Types

type GentlemanT

type GentlemanT interface {
	// Initializes a client object
	NewClient() *gentleman.Client
	// Sets-up client object
	SetupClient(*gentleman.Client) *gentleman.Client
	// Gets the plugin should be used in client object
	Plugin() plugin.Plugin
}

Defines the interface used to ease testing by Gentleman library.

type GockConfig

type GockConfig struct {
	// The host of mocked URL
	Host string
	// The port of mocked URL
	Port uint16
	// Supporting of out-of-box utility for Gentleman library.
	GentlemanT GentlemanT
	// Supporting of out-of-box utility for "net/http/httptest.Server" object.
	HttpTest tHttp.HttpTest
}

Facade object, which could be used to:

  1. Mock-up web service with simple configuration
  2. Constructs a Gentleman client with configuration of mock
  3. Initialize a new *httptest.Server, which could be used to start real server on mock-setup.

func (*GockConfig) GetUrl

func (c *GockConfig) GetUrl() string

Gets the URL(by "http://") of this configuration.

func (*GockConfig) New

func (c *GockConfig) New() *gock.Request

Initialize a "*gock.Request* object with defined URL.

func (*GockConfig) NewHttpConfig

func (c *GockConfig) NewHttpConfig() *client.HttpClientConfig

Constructs a configuration for HTTP client.

func (*GockConfig) NewRestfulClientConfig

func (c *GockConfig) NewRestfulClientConfig() *oHttp.RestfulClientConfig

Constructs a configuration for RESTful client.

func (*GockConfig) Off

func (c *GockConfig) Off()

Calls gock.Off()

func (*GockConfig) StartRealNetwork

func (c *GockConfig) StartRealNetwork()

Calls gock.EnableNetworking()

func (*GockConfig) StopRealNetwork

func (c *GockConfig) StopRealNetwork()

Calls:

  1. gock.Off()
  2. gock.DisableNetworking()

Jump to

Keyboard shortcuts

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