Documentation
¶
Overview ¶
Package dsl contains the main Pact DSL used in the Consumer collaboration test cases, and Provider contract test verification.
Index ¶
- Variables
- func EachLike(content interface{}, minRequired int) string
- func Like(content interface{}) string
- func Term(generate string, matcher string) string
- type Client
- type HalDoc
- type HalLinks
- type Interaction
- type MockService
- type Pact
- type PactClient
- func (p *PactClient) ListServers() *types.PactListResponse
- func (p *PactClient) StartServer(args []string, port int) *types.MockServer
- func (p *PactClient) StopDaemon() error
- func (p *PactClient) StopServer(server *types.MockServer) *types.MockServer
- func (p *PactClient) VerifyProvider(request types.VerifyRequest) (string, error)
- type PactFile
- type PactLink
- type PactName
- type Publisher
- type Request
- type Response
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNoConsumers is returned when no consumer are not found for a provider. ErrNoConsumers = errors.New("no consumers found") ErrUnauthorized = errors.New("unauthorized") )
Functions ¶
func EachLike ¶
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 ¶
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 ¶ added in v0.0.2
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:"providerState,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
// PactFileWriteMode specifies how to write to the Pact file, for the life
// of a Mock Service.
// "overwrite" will always truncate and replace the pact after each run
// "update" will append to the pact file, which is useful if your tests
// are split over multiple files and instantiations of a Mock Server
// See https://github.com/realestate-com-au/pact/blob/master/documentation/configuration.md#pactfile_write_mode
PactFileWriteMode 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
// PactFileWriteMode specifies how to write to the Pact file, for the life
// of a Mock Service.
// "overwrite" will always truncate and replace the pact after each run
// "update" will append to the pact file, which is useful if your tests
// are split over multiple files and instantiations of a Mock Server
// See https://github.com/realestate-com-au/pact/blob/master/documentation/configuration.md#pactfile_write_mode
PactFileWriteMode string
// Specify which version of the Pact Specification should be used (1 or 2).
// Defaults to 2.
SpecificationVersion int
// Host is the address of the Daemon, Mock and Verification Service runs on
// Examples include 'localhost', '127.0.0.1', '[::1]'
// Defaults to 'localhost'
Host string
// Network is the network of the Daemon, Mock and Verification Service
// Examples include 'tcp', 'tcp4', 'tcp6'
// Defaults to 'tcp'
Network string
// Ports MockServer can be deployed to, can be CSV or Range with a dash
// Example "1234", "12324,5667", "1234-5667"
AllowedMockServerPorts string
// 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 ¶
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 ¶
Teardown stops the Pact Mock Server. This usually is called on completion of each test suite.
func (*Pact) Verify ¶
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.
type PactClient ¶
type PactClient struct {
// Port the daemon is running on.
Port int
// Network Daemon is listening on
Network string
// Address the Daemon is listening on
Address string
}
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, port int) *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 ¶ added in v0.0.2
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.