dsl

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2016 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package dsl contains the main Pact DSL used in the Consumer collaboration test cases, and Provider contract test verification.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (

	// ErrNoConsumers is returned when no consumer are not found for a provider.
	ErrNoConsumers = errors.New("no consumers found")

	// ErrUnauthorized represents a Forbidden (403).
	ErrUnauthorized = errors.New("unauthorized")
)

Functions

func EachLike

func EachLike(content interface{}, minRequired int) string

EachLike specifies that a given element in a JSON body can be repeated "minRequired" times. Number needs to be 1 or greater

Example
match := EachLike(`[1,2,3]`, 1)
fmt.Println(formatJSON(match))
Output:

{
	"json_class": "Pact::ArrayLike",
	"contents": [
		1,
		2,
		3
	],
	"min": 1
}
Example (Nested)
jumper := Like(`"jumper"`)
shirt := Like(`"shirt"`)
tag := EachLike(fmt.Sprintf(`[%s, %s]`, jumper, shirt), 2)
size := Like(10)
colour := Term("red", "red|green|blue")

match := EachLike(
	EachLike(
		fmt.Sprintf(
			`{
							"size": %s,
							"colour": %s,
							"tag": %s
						}`, size, colour, tag),
		1),
	1)
fmt.Println(formatJSON(match))
Output:

{
	"json_class": "Pact::ArrayLike",
	"contents": {
		"json_class": "Pact::ArrayLike",
		"contents": {
			"size": {
				"json_class": "Pact::SomethingLike",
				"contents": 10
			},
			"colour": {
				"json_class": "Pact::Term",
				"data": {
					"generate": "red",
					"matcher": {
						"json_class": "Regexp",
						"o": 0,
						"s": "red|green|blue"
					}
				}
			},
			"tag": {
				"json_class": "Pact::ArrayLike",
				"contents": [
					{
						"json_class": "Pact::SomethingLike",
						"contents": "jumper"
					},
					{
						"json_class": "Pact::SomethingLike",
						"contents": "shirt"
					}
				],
				"min": 2
			}
		},
		"min": 1
	},
	"min": 1
}

func Like

func Like(content interface{}) string

Like specifies that the given content type should be matched based on type (int, string etc.) instead of a verbatim match.

Example (Number)
match := Like(37)
fmt.Println(formatJSON(match))
Output:

{
	"json_class": "Pact::SomethingLike",
	"contents": 37
}
Example (Object)
match := Like(`{"baz":"bat"}`)
fmt.Println(formatJSON(match))
Output:

{
	"json_class": "Pact::SomethingLike",
	"contents": {
		"baz": "bat"
	}
}
Example (String)
match := Like(`"myspecialvalue"`)
fmt.Println(formatJSON(match))
Output:

{
	"json_class": "Pact::SomethingLike",
	"contents": "myspecialvalue"
}

func Term

func Term(generate string, matcher string) string

Term specifies that the matching should generate a value and also match using a regular expression.

Example
match := Term("myawesomeword", `\\w+`)
fmt.Println(formatJSON(match))
Output:

{
	"json_class": "Pact::Term",
	"data": {
		"generate": "myawesomeword",
		"matcher": {
			"json_class": "Regexp",
			"o": 0,
			"s": "\\w+"
		}
	}
}

Types

type Client

type Client interface {
	StartServer() *types.MockServer
}

Client is the simplified remote interface to the Pact Daemon.

type HalDoc added in v0.0.2

type HalDoc struct {
	Links HalLinks `json:"_links"`
}

HalDoc is a simple representation of the HAL response from a Pact Broker.

type HalLinks struct {
	Pacts []PactLink `json:"pacts"`
}

HalLinks represents the _links key in a HAL document.

type Interaction

type Interaction struct {
	// Request
	Request Request `json:"request"`

	// Response
	Response Response `json:"response"`

	// Description to be written into the Pact file
	Description string `json:"description"`

	// Provider state to be written into the Pact file
	State string `json:"provider_state,omitempty"`
}

Interaction is the main implementation of the Pact interface.

func (*Interaction) Given

func (p *Interaction) Given(state string) *Interaction

Given specifies a provider state. Optional.

func (*Interaction) UponReceiving

func (p *Interaction) UponReceiving(description string) *Interaction

UponReceiving specifies the name of the test case. This becomes the name of the consumer/provider pair in the Pact file. Mandatory.

func (*Interaction) WillRespondWith

func (p *Interaction) WillRespondWith(response Response) *Interaction

WillRespondWith specifies the details of the HTTP response that will be used to confirm that the Provider must satisfy. Mandatory.

func (*Interaction) WithRequest

func (p *Interaction) WithRequest(request Request) *Interaction

WithRequest specifies the details of the HTTP request that will be used to confirm that the Provider provides an API listening on the given interface. Mandatory.

type MockService added in v0.0.2

type MockService struct {
	// BaseURL is the base host for the Pact Mock Service.
	BaseURL string

	// Consumer name.
	Consumer string

	// Provider name.
	Provider string
}

MockService is the HTTP interface to setup the Pact Mock Service See https://github.com/bethesque/pact-mock_service and https://gist.github.com/bethesque/9d81f21d6f77650811f4.

func (*MockService) AddInteraction added in v0.0.2

func (m *MockService) AddInteraction(interaction *Interaction) error

AddInteraction adds a new Pact Mock Service interaction.

func (*MockService) DeleteInteractions added in v0.0.2

func (m *MockService) DeleteInteractions() error

DeleteInteractions removes any previous Mock Service Interactions.

func (*MockService) Verify added in v0.0.2

func (m *MockService) Verify() error

Verify confirms that all interactions were called.

func (*MockService) WritePact added in v0.0.2

func (m *MockService) WritePact() error

WritePact writes the pact file to disk.

type Pact

type Pact struct {
	// Current server for the consumer.
	Server *types.MockServer

	// Port the Pact Daemon is running on.
	Port int

	// Consumer is the name of the Consumer/Client.
	Consumer string

	// Provider is the name of the Providing service.
	Provider string

	// Interactions contains all of the Mock Service Interactions to be setup.
	Interactions []*Interaction

	// Log levels.
	LogLevel string

	// Location of Pact external service invocation output logging.
	// Defaults to `<cwd>/logs`.
	LogDir string

	// Pact files will be saved in this folder.
	// Defaults to `<cwd>/pacts`.
	PactDir string

	// Specify which version of the Pact Specification should be used (1 or 2).
	// Defaults to 2.
	SpecificationVersion int
	// contains filtered or unexported fields
}

Pact is the container structure to run the Consumer Pact test cases.

func (*Pact) AddInteraction

func (p *Pact) AddInteraction() *Interaction

AddInteraction creates a new Pact interaction, initialising all required things. Will automatically start a Mock Service if none running.

func (*Pact) Setup

func (p *Pact) Setup() *Pact

Setup starts the Pact Mock Server. This is usually called before each test suite begins. AddInteraction() will automatically call this if no Mock Server has been started.

func (*Pact) Teardown

func (p *Pact) Teardown() *Pact

Teardown stops the Pact Mock Server. This usually is called on completion of each test suite.

func (*Pact) Verify

func (p *Pact) Verify(integrationTest func() error) error

Verify runs the current test case against a Mock Service. Will cleanup interactions between tests within a suite.

func (*Pact) VerifyProvider

func (p *Pact) VerifyProvider(request types.VerifyRequest) error

VerifyProvider reads the provided pact files and runs verification against a running Provider API.

func (*Pact) WritePact added in v0.0.2

func (p *Pact) WritePact() error

WritePact should be called writes when all tests have been performed for a given Consumer <-> Provider pair. It will write out the Pact to the configured file.

type PactClient

type PactClient struct {
	// Port the daemon is running on.
	Port int
}

PactClient is the default implementation of the Client interface.

func (*PactClient) ListServers

func (p *PactClient) ListServers() types.PactListResponse

ListServers lists all running Pact Mock Servers.

func (*PactClient) StartServer

func (p *PactClient) StartServer(args []string) *types.MockServer

StartServer starts a remote Pact Mock Server.

func (*PactClient) StopDaemon

func (p *PactClient) StopDaemon() error

StopDaemon remotely shuts down the Pact Daemon.

func (*PactClient) StopServer

func (p *PactClient) StopServer(server *types.MockServer) *types.MockServer

StopServer stops a remote Pact Mock Server.

func (*PactClient) VerifyProvider

func (p *PactClient) VerifyProvider(request types.VerifyRequest) (string, error)

VerifyProvider runs the verification process against a running Provider.

type PactFile added in v0.0.2

type PactFile struct {
	// The API Consumer name
	Consumer PactName `json:"consumer"`

	// The API Provider name
	Provider PactName `json:"provider"`
}

PactFile is a simple representation of a Pact file to be able to parse Consumer/Provider from the file.

type PactLink struct {
	Href  string `json:"href"`
	Title string `json:"title"`
	Name  string `json:"name"`
}

PactLink represents the Pact object in the HAL response.

type PactName added in v0.0.2

type PactName struct {
	Name string `json:"name"`
}

PactName represents the name fields in the PactFile.

type Publisher added in v0.0.2

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

Publisher is the API to send Pact files to a Pact Broker.

func (*Publisher) Publish added in v0.0.2

func (p *Publisher) Publish(request types.PublishRequest) error

Publish sends the Pacts to a broker, optionally tagging them

type Request

type Request struct {
	Method  string            `json:"method"`
	Path    string            `json:"path"`
	Query   string            `json:"query,omitempty"`
	Headers map[string]string `json:"headers,omitempty"`
	Body    interface{}       `json:"body,omitempty"`
}

Request is the default implementation of the Request interface.

type Response

type Response struct {
	Status  int               `json:"status"`
	Headers map[string]string `json:"headers,omitempty"`
	Body    interface{}       `json:"body,omitempty"`
}

Response is the default implementation of the Response interface.

Jump to

Keyboard shortcuts

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