contract

package module
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2020 License: MIT Imports: 12 Imported by: 0

README

Contract

go.dev reference

Go Contract testing using Pact and Mock Service.

Usage

package integration_test

import (
	"net/http"
	"testing"

	"github.com/jacoelho/contract"
)

func TestIntegration(t *testing.T) {
	pact := contract.TestingMockService(t, contract.WithContracts([]string{"fixtures/simple.json"}))

	resp, err := http.Get(pact.URL() + "/path")
	if err != nil {
		t.Error(err)
	}
	defer func() { _ = resp.Body.Close() }()
}

Interactions will be verified when test is clean-up (requires go 1.14).

Every test runs in a different container making t.Parallel() safe.

License

The MIT License (MIT)

See LICENSE to see the full text.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrBackendNotRunning = errors.New("backend not running")

Functions

This section is empty.

Types

type Backend

type Backend interface {
	Runnable
	Stoppable
	BaseURL() (string, error)
}

type Container added in v0.0.4

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

func NewContractContainer

func NewContractContainer(cfg ContainerConfig) (*Container, error)

func (*Container) BaseURL added in v0.0.4

func (c *Container) BaseURL() (string, error)

func (*Container) Run added in v0.0.4

func (c *Container) Run() error

func (*Container) Stop added in v0.0.4

func (c *Container) Stop() error

type ContainerConfig added in v0.0.4

type ContainerConfig struct {
	Repository string
	Tag        string
}

type MockService

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

MockService represents a HTTP mock/stub implementation of Pact

func TestingMockService

func TestingMockService(t *testing.T, settings ...Option) *MockService

TestingMockService creates a testing mock service test will fail if a call to verification return errors

func (*MockService) Create

func (m *MockService) Create(ctx context.Context, r io.Reader) error

Create creates an interaction to be tested

func (*MockService) Delete

func (m *MockService) Delete(ctx context.Context) error

Delete deletes all interactions

func (*MockService) URL

func (m *MockService) URL() string

URL returns a URL ready to be interacted, URL will be have the form `http://localhost:port`

func (*MockService) Verify

func (m *MockService) Verify(ctx context.Context) error

Verify verifies interactions

type Option

type Option func(*options)

func WithBackend

func WithBackend(b Backend) Option

WithBackend allows to use a custom backend by default a container will be used

func WithClient

func WithClient(client *http.Client) Option

WithClient allows to specify a custom *http.Client

func WithContracts

func WithContracts(filenames []string) Option

WithContracts allows to specify which interactions should be loaded

func WithTimeout added in v0.0.7

func WithTimeout(duration time.Duration) Option

WithTimeout specifies how long to wait for an http request to the mock service

type Runnable

type Runnable interface {
	Run() error
}

type Stoppable

type Stoppable interface {
	Stop() error
}

Jump to

Keyboard shortcuts

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