WireMock and Go

Testcontainers module for Go #

The WireMock community provides a Testcontainers for Go module module which allows using WireMock single-shot containers within Golang tests. This module can run any WireMock Docker compatible images, see the documentation for detailed usage guidelines and examples.

Example:

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))
	}
}

References:

Go WireMock - WireMock REST API client #

The Golang client library to stub API resources in WireMock using its Administrative REST API. The project connects to the instance and allows setting up stubs and response templating, or using administrative API to extract observability data.

References:

Example:

func TestSome(t *testing.T) {
    wiremockClient := wiremock.NewClient("http://0.0.0.0:8080")
    defer wiremockClient.Reset()

    wiremockClient.StubFor(wiremock.Post(wiremock.URLPathEqualTo("/user")).
    WithQueryParam("name", wiremock.EqualTo("John Doe")).
    WillReturnResponse(
        wiremock.NewResponse().
            WithJSONBody(map[string]interface{}{
                "code":   400,
                "detail": "detail",
            }).
            WithHeader("Content-Type", "application/json").
            WithStatus(http.StatusBadRequest),
    ))
}

Useful pages #