Skip to content

Commit

Permalink
Merge pull request #32 from walkerus/admin-client
Browse files Browse the repository at this point in the history
Add wiremock admin client
  • Loading branch information
oleg-nenashev authored Aug 16, 2023
2 parents 1d5371e + 04a37d1 commit b31889e
Show file tree
Hide file tree
Showing 9 changed files with 225 additions and 151 deletions.
34 changes: 18 additions & 16 deletions examples/quickstart/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,43 @@ module wiremock.org/testcontainers-go-quickstart

go 1.19

require (
github.com/wiremock/wiremock-testcontainers-go v1.0.0-alpha-6
)
require github.com/wiremock/wiremock-testcontainers-go v1.0.0-alpha-6

require (
dario.cat/mergo v1.0.0 // indirect
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
github.com/Microsoft/go-winio v0.5.2 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/cenkalti/backoff/v4 v4.2.0 // indirect
github.com/containerd/containerd v1.6.19 // indirect
github.com/containerd/containerd v1.7.3 // indirect
github.com/cpuguy83/dockercfg v0.3.1 // indirect
github.com/docker/distribution v2.8.2+incompatible // indirect
github.com/docker/docker v23.0.5+incompatible // indirect
github.com/docker/docker v24.0.5+incompatible // indirect
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/imdario/mergo v0.3.15 // indirect
github.com/klauspost/compress v1.15.9 // indirect
github.com/klauspost/compress v1.16.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/moby/patternmatcher v0.5.0 // indirect
github.com/moby/sys/sequential v0.5.0 // indirect
github.com/moby/term v0.5.0 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0-rc2 // indirect
github.com/opencontainers/image-spec v1.1.0-rc4 // indirect
github.com/opencontainers/runc v1.1.5 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
github.com/testcontainers/testcontainers-go v0.21.0 // indirect
github.com/testcontainers/testcontainers-go v0.22.0 // indirect
github.com/wiremock/go-wiremock v1.7.1 // indirect
golang.org/x/exp v0.0.0-20230510235704-dd950f8aeaea // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/mod v0.9.0 // indirect
golang.org/x/net v0.9.0 // indirect
golang.org/x/sys v0.9.0 // indirect
google.golang.org/genproto v0.0.0-20220617124728-180714bec0ad // indirect
google.golang.org/grpc v1.47.0 // indirect
google.golang.org/protobuf v1.28.0 // indirect
golang.org/x/tools v0.7.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230525234030-28d5490b6b19 // indirect
google.golang.org/grpc v1.57.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
)

replace github.com/wiremock/wiremock-testcontainers-go => ../../../wiremock-testcontainers-go // needed to use new method written localy in the quickstart_test.go
replace github.com/wiremock/wiremock-testcontainers-go => ../../../wiremock-testcontainers-go // needed to use new method written localy in the quickstart_test.go
159 changes: 35 additions & 124 deletions examples/quickstart/go.sum

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions examples/quickstart/quickstart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package testcontainers_wiremock_quickstart

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

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

func TestWireMock(t *testing.T) {
Expand Down Expand Up @@ -35,6 +36,6 @@ func TestWireMock(t *testing.T) {
}

if string(out) != "Hello, world!" {
t.Fatalf("expected 'Hello, world!' but got %v", string(out))
t.Fatalf("expected 'Hello, world!' but got %s", out)
}
}
46 changes: 46 additions & 0 deletions examples/using_api_client/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
module wiremock.org/testcontainers-go-quickstart

go 1.19

require (
github.com/wiremock/go-wiremock v1.7.1
github.com/wiremock/wiremock-testcontainers-go v1.0.0-alpha-6
)

require (
dario.cat/mergo v1.0.0 // indirect
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/cenkalti/backoff/v4 v4.2.0 // indirect
github.com/containerd/containerd v1.7.3 // indirect
github.com/cpuguy83/dockercfg v0.3.1 // indirect
github.com/docker/distribution v2.8.2+incompatible // indirect
github.com/docker/docker v24.0.5+incompatible // indirect
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/klauspost/compress v1.16.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/moby/patternmatcher v0.5.0 // indirect
github.com/moby/sys/sequential v0.5.0 // indirect
github.com/moby/term v0.5.0 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0-rc4 // indirect
github.com/opencontainers/runc v1.1.5 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
github.com/testcontainers/testcontainers-go v0.22.0 // indirect
golang.org/x/exp v0.0.0-20230510235704-dd950f8aeaea // indirect
golang.org/x/mod v0.9.0 // indirect
golang.org/x/net v0.9.0 // indirect
golang.org/x/sys v0.9.0 // indirect
golang.org/x/tools v0.7.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230525234030-28d5490b6b19 // indirect
google.golang.org/grpc v1.57.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
)

replace github.com/wiremock/wiremock-testcontainers-go => ../../../wiremock-testcontainers-go // needed to use new method written localy in the quickstart_test.go
55 changes: 55 additions & 0 deletions examples/using_api_client/using_api_client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package testcontainers_wiremock_using_api_client

import (
"context"
"net/http"
"testing"

"github.com/wiremock/go-wiremock"

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

func TestWireMock(t *testing.T) {
// Create Container
ctx := context.Background()
container, err := RunContainer(ctx)
if err != nil {
t.Fatal(err)
}

// Clean up the container after the test is complete
t.Cleanup(func() {
if err := container.Terminate(ctx); err != nil {
t.Fatalf("failed to terminate container: %s", err)
}
})

// Use the WireMock client to stub a new endpoint manually
err = container.Client.StubFor(
wiremock.Get(wiremock.URLEqualTo("/hello")).
WillReturnResponse(
wiremock.NewResponse().
WithJSONBody(map[string]string{"result": "Hello, world!"}).
WithHeader("Content-Type", "application/json").
WithStatus(http.StatusOK),
),
)

if err != nil {
t.Fatal(err)
}

statusCode, out, err := SendHttpGet(container, "/hello", nil)
if err != nil {
t.Fatal(err, "Failed to get a response")
}

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

if string(out) != `{"result":"Hello, world!"}` {
t.Fatalf("expected 'Hello, world!' but got %v", out)
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.19
require (
github.com/docker/go-connections v0.4.0
github.com/testcontainers/testcontainers-go v0.22.0

github.com/wiremock/go-wiremock v1.7.1
)

require (
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ github.com/testcontainers/testcontainers-go v0.22.0/go.mod h1:k0YiPa26xJCRUbUkYq
github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
github.com/vishvananda/netlink v1.1.0/go.mod h1:cTgwzPIzzgDAYoQrMm0EdrjRUBkTqKYppBueQtXaqoE=
github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df/go.mod h1:JP3t17pCcGlemwknint6hfoeCVQrEMVwxRLRjXpq+BU=
github.com/wiremock/go-wiremock v1.7.1 h1:rwn8zRRI5hT7u+Yu+RzbMgfc1cgwnwwwbW2skO7h4QU=
github.com/wiremock/go-wiremock v1.7.1/go.mod h1:/uvO0XFheyy8XetvQqm4TbNQRsGPlByeNegzLzvXs0c=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
Expand Down
12 changes: 11 additions & 1 deletion tc-wiremock.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/docker/go-connections/nat"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
"github.com/wiremock/go-wiremock"
)

const defaultWireMockImage = "docker.io/wiremock/wiremock"
Expand All @@ -19,6 +20,7 @@ const defaultPort = "8080"
type WireMockContainer struct {
testcontainers.Container
version string
Client *wiremock.Client
}

type WireMockExtension struct {
Expand Down Expand Up @@ -53,7 +55,15 @@ func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomize
return nil, err
}

return &WireMockContainer{Container: container}, nil
uri, err := GetURI(ctx, container)
if err != nil {
return nil, err
}

return &WireMockContainer{
Container: container,
Client: wiremock.NewClient(uri),
}, nil
}

func WithMappingFile(id string, filePath string) testcontainers.CustomizeRequestOption {
Expand Down
61 changes: 54 additions & 7 deletions tc-wiremock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ package testcontainers_wiremock
import (
"bytes"
"context"
"net/http"
"path/filepath"
"strings"
"testing"

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

func TestWireMock(t *testing.T) {
Expand Down Expand Up @@ -33,7 +36,7 @@ func TestWireMock(t *testing.T) {
t.Fatalf("expected HTTP-200 but got %d", statusCode)
}
if string(out) != "Hello, world!" {
t.Fatalf("expected 'Hello, world!' but got %v", string(out))
t.Fatalf("expected 'Hello, world!' but got %s", out)
}
}

Expand Down Expand Up @@ -63,7 +66,7 @@ func TestWireMockWithResource(t *testing.T) {
t.Fatalf("expected HTTP-200 but got %d", statusCode)
}
if !strings.Contains(out, "Hello, world!") {
t.Fatalf("expected 'Hello, world!' but got %v", string(out))
t.Fatalf("expected 'Hello, world!' but got %s", out)
}
}

Expand Down Expand Up @@ -92,7 +95,7 @@ func TestSendHttpGetWorksWithQueryParamsPassedInArgument(t *testing.T) {
t.Fatalf("expected HTTP-200 but got %d", statusCode)
}
if out != "" {
t.Fatalf("expected 'Hello, world!' but got %v", string(out))
t.Fatalf("expected 'Hello, world!' but got %s", out)
}
}

Expand Down Expand Up @@ -121,7 +124,7 @@ func TestSendHttpGetWorksWithQueryParamsProvidedInURL(t *testing.T) {
t.Fatalf("expected HTTP-200 but got %d", statusCode)
}
if out != "" {
t.Fatalf("expected 'Hello, world!' but got %v", string(out))
t.Fatalf("expected 'Hello, world!' but got %s", out)
}
}

Expand Down Expand Up @@ -150,7 +153,7 @@ func TestSendHttpDelete(t *testing.T) {
t.Fatalf("expected HTTP-200 but got %d", statusCode)
}
if out != "" {
t.Fatalf("expected 'Hello, world!' but got %v", string(out))
t.Fatalf("expected 'Hello, world!' but got %s", out)
}
}

Expand Down Expand Up @@ -181,7 +184,7 @@ func TestSendHttpPatch(t *testing.T) {
t.Fatalf("expected HTTP-200 but got %d", statusCode)
}
if !strings.Contains(out, "sampleField1") {
t.Fatalf("expected 'Hello, world!' but got %v", string(out))
t.Fatalf("expected 'Hello, world!' but got %s", out)
}
}

Expand Down Expand Up @@ -212,6 +215,50 @@ func TestSendHttpPut(t *testing.T) {
t.Fatalf("expected HTTP-200 but got %d", statusCode)
}
if !strings.Contains(out, "sampleField1") {
t.Fatalf("expected 'Hello, world!' but got %v", string(out))
t.Fatalf("expected 'Hello, world!' but got %s", out)
}
}

func TestWireMockClient(t *testing.T) {
// Create Container
ctx := context.Background()
container, err := RunContainer(ctx)
if err != nil {
t.Fatal(err)
}

// Clean up the container after the test is complete
t.Cleanup(func() {
if err := container.Terminate(ctx); err != nil {
t.Fatalf("failed to terminate container: %s", err)
}
})

// Use the WireMock client to stub a new endpoint manually
err = container.Client.StubFor(
wiremock.Get(wiremock.URLEqualTo("/hello")).
WillReturnResponse(
wiremock.NewResponse().
WithJSONBody(map[string]string{"result": "Hello, world!"}).
WithHeader("Content-Type", "application/json").
WithStatus(http.StatusOK),
),
)

if err != nil {
t.Fatal(err)
}

statusCode, out, err := SendHttpGet(container, "/hello", nil)
if err != nil {
t.Fatal(err, "Failed to get a response")
}

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

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

0 comments on commit b31889e

Please sign in to comment.