Categorygithub.com/j0s/go-wiremock
modulepackage
1.4.1
Repository: https://github.com/j0s/go-wiremock.git
Documentation: pkg.go.dev

# README

go-wiremock

Actions Status Go Report Card

The simple package to stub HTTP resource using WireMock admin

Documentation

GoDoc

Usage

docker run -it --rm -p 8080:8080 rodolpheche/wiremock
package main

import (
    "testing"

    "github.com/walkerus/go-wiremock"
)

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

    // stubbing POST http://0.0.0.0:8080/example
    wiremockClient.StubFor(wiremock.Post(wiremock.URLPathEqualTo("/example")).
            WithQueryParam("firstName", wiremock.EqualTo("Jhon")).
            WithQueryParam("lastName", wiremock.NotMatching("Black")).
            WithBodyPattern(wiremock.EqualToJson(`{"meta": "information"}`)).
            WithHeader("x-session", wiremock.Matching("^\\S+fingerprint\\S+$")).
            WillReturn(
                `{"code": 400, "detail": "detail"}`,
                map[string]string{"Content-Type": "application/json"},
                400,
            ).
            AtPriority(1))

    // scenario
    defer wiremockClient.ResetAllScenarios()
    wiremockClient.StubFor(wiremock.Get(wiremock.URLPathEqualTo("/status")).
        WillReturn(
            `{"status": null}`,
            map[string]string{"Content-Type": "application/json"},
            200,
        ).
        InScenario("Set status").
        WhenScenarioStateIs(wiremock.ScenarioStateStarted))

    wiremockClient.StubFor(wiremock.Post(wiremock.URLPathEqualTo("/state")).
            WithBodyPattern(wiremock.EqualToJson(`{"status": "started"}`)).
            InScenario("Set status").
            WillSetStateTo("Status started"))

    statusStub := wiremock.Get(wiremock.URLPathEqualTo("/status")).
        WillReturn(
            `{"status": "started"}`,
            map[string]string{"Content-Type": "application/json"},
            200,
        ).
        InScenario("Set status").
        WhenScenarioStateIs("Status started")
    wiremockClient.StubFor(statusStub)

    //testing code...

    verifyResult, _ := wiremockClient.Verify(statusStub.Request(), 1)
    if !verifyResult {
        //...
    }

    wiremockClient.DeleteStub(statusStub)
}

# Functions

Contains returns ParamMatcher with ParamContains matching strategy.
Delete returns *StubRule for DELETE method.
EqualTo returns ParamMatcher with ParamEqualTo matching strategy.
EqualToJson returns ParamMatcher with ParamEqualToJson matching strategy.
EqualToXml returns ParamMatcher with ParamEqualToXml matching strategy.
Get returns *StubRule for GET method.
Matching returns ParamMatcher with ParamMatches matching strategy.
MatchingJsonPath returns ParamMatcher with ParamMatchesJsonPath matching strategy.
MatchingXPath returns ParamMatcher with ParamMatchesXPath matching strategy.
NewClient returns *Client.
NewRequest constructs minimum possible Request.
NewStubRule returns a new *StubRule.
NotMatching returns ParamMatcher with ParamDoesNotMatch matching strategy.
Patch returns *StubRule for PATCH method.
Post
Post returns *StubRule for POST method.
Put returns *StubRule for PUT method.
URLEqualTo returns URLMatcher with URLEqualToRule matching strategy.
URLMatching returns URLMatcher with URLMatchingRule matching strategy.
URLPathEqualTo returns URLMatcher with URLPathEqualToRule matching strategy.
URLPathMatching returns URLMatcher with URLPathMatchingRule matching strategy.

# Constants

Types of params matching.
Types of params matching.
Types of params matching.
Types of params matching.
Types of params matching.
Types of params matching.
Types of params matching.
Types of params matching.
Types of params matching.
No description provided by the author
Types of url matching.
Types of url matching.
Types of url matching.
Types of url matching.

# Structs

A Client implements requests to the wiremock server.
ParamMatcher is structure for defining the type of params.
A Request is the part of StubRule describing the matching of the http request.
StubRule is struct of http Request body to WireMock.
URLMatcher is structure for defining the type of url matching.

# Interfaces

ParamMatcherInterface is pair ParamMatchingStrategy and string matched value.
URLMatcherInterface is pair URLMatchingStrategy and string matched value.

# Type aliases

ParamMatchingStrategy is enum params matching type.
URLMatchingStrategy is enum url matching type.