testcontainers_wiremock

package module
v1.0.0-alpha-9 Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2024 License: Apache-2.0 Imports: 10 Imported by: 1

README

WireMock Module for Testcontainers Go

GoDoc GitHub release (latest by date) Slack GitHub contributors

This module allows provisioning the WireMock API mock server as a standalone container within your unit tests, based on the official WireMock Docker images (2.35.0-1 or above) or compatible custom images.

You can learn more about WireMock and Golang on this WireMock solutions page.

Supported features

The following features are now explicitly included in the module's API:

  • Passing API Mapping and Resource files
  • Sending HTTP requests to the mocked container
  • Embedded Go WireMock client for interacting with the WireMock container REST API

More features will be added over time.

Quick Start

See the Quick Start Guide. Just a teaser of how it feels at the real speed!

Quickstart demo GIF

Requirements

  • Golang version 1.17 or above, so all modern Golang projects should be compatible with it.
  • The module supports the official WireMock Docker images 2.35.0-1 or above.
  • Custom images are supported too as long as they follow the same CLI and API structure.

Usage

import (
  "context"
  . "github.com/wiremock/wiremock-testcontainers-go"
  "testing"
)

func TestWireMock(t *testing.T) {
	// Create Container
	ctx := context.Background()
	container, err := RunContainerAndStopOnCleanup(ctx,
		WithMappingFile("hello", "hello-world.json"),
	)
	if err != nil {
		t.Fatal(err)
	}

	// Send the HTTP GET request to the mocked API
	statusCode, out, err := SendHttpGet(container, "/hello", nil)
	if err != nil {
		t.Fatal(err, "Failed to get a response")
	}

	// Verify the response
	if statusCode != 200 {
		t.Fatalf("expected HTTP-200 but got %d", statusCode)
	}

	if string(out) != "Hello, world!" {
		t.Fatalf("expected 'Hello, world!' but got %v", string(out))
	}
}

Examples

License

The module is licensed under Apache License v.2

References

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetURI

func GetURI(ctx context.Context, container testcontainers.Container) (string, error)

func SendHttpDelete

func SendHttpDelete(container testcontainers.Container, endpoint string) (int, string, error)

SendHttpDelete sends Http DELETE request to the container passed as an argument.

func SendHttpGet

func SendHttpGet(container testcontainers.Container, endpoint string, queryParams map[string]string) (int, string, error)

SendHttpGet sends Http GET request to the container passed as an argument. 'queryParams' parameter is optional and can be passed as a nil. Query parameters also work when hardcoded in the endpoint argument.

func SendHttpPatch

func SendHttpPatch(container testcontainers.Container, endpoint string, body io.Reader) (int, string, error)

SendHttpPatch sends Http PATCH request to the container passed as an argument.

func SendHttpPost

func SendHttpPost(container testcontainers.Container, endpoint string, body io.Reader) (int, string, error)

SendHttpPost sends Http POST request to the container passed as an argument.

func SendHttpPut

func SendHttpPut(container testcontainers.Container, endpoint string, body io.Reader) (int, string, error)

SendHttpPut sends Http PUT request to the container passed as an argument.

func WithFile

func WithFile(name string, filePath string) testcontainers.CustomizeRequestOption

func WithImage

func WithImage(image string) testcontainers.CustomizeRequestOption

func WithMappingFile

func WithMappingFile(id string, filePath string) testcontainers.CustomizeRequestOption

Types

type WireMockContainer

type WireMockContainer struct {
	testcontainers.Container

	Client *wiremock.Client
	// contains filtered or unexported fields
}

func RunContainer

func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomizer) (*WireMockContainer, error)

RunContainer creates an instance of the WireMockContainer type

func RunContainerAndStopOnCleanup

func RunContainerAndStopOnCleanup(ctx context.Context, t *testing.T, opts ...testcontainers.ContainerCustomizer) (*WireMockContainer, error)

Creates an instance of the WireMockContainer type that is automatically terminated upon test completion

func RunDefaultContainerAndStopOnCleanup

func RunDefaultContainerAndStopOnCleanup(ctx context.Context, t *testing.T) (*WireMockContainer, error)

Creates a default instance of the WireMockContainer type that is automatically terminated upon test completion

type WireMockExtension

type WireMockExtension struct {
	testcontainers.Container
	// contains filtered or unexported fields
}

Jump to

Keyboard shortcuts

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