Skip to content

Audibene-GMBH/go-wiremock

 
 

Repository files navigation

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("https://0.0.0.0:8080")
    defer wiremockClient.Reset()
    
    // stubbing POST https://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)
}

About

Golang WireMock admin client

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 100.0%