diff --git a/README.md b/README.md index a512d35..1aa31f1 100644 --- a/README.md +++ b/README.md @@ -5,18 +5,19 @@ [![Slack](https://img.shields.io/badge/slack-slack.wiremock.org-brightgreen?style=flat&logo=slack)](https://slack.wiremock.org/) [![GitHub contributors](https://img.shields.io/github/contributors/wiremock/wiremock-testcontainers-go)](https://github.com/wiremock/wiremock-testcontainers-go/graphs/contributors) -This module allows provisioning the [WireMock API mock server](https://wiremock.org/) as a standalone container within your unit tests, +This module allows provisioning the [WireMock API mock server](https://wiremock.org/?utm_medium=referral&utm_campaign=wiremock-testcontainers) as a standalone container within your unit tests, based on the official [WireMock Docker](https://github.com/wiremock/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](https://wiremock.org/docs/solutions/golang/). +You can learn more about WireMock and Golang on this [WireMock solutions page](https://wiremock.org/docs/solutions/golang/?utm_medium=referral&utm_campaign=wiremock-testcontainers). ## Supported features The following features are now explicitly included in the module's API: -- Passing API Mapping files -- Passing Resource files +- Passing API Mapping and Resource files - Sending HTTP requests to the mocked container +- Embedded [Go WireMock](https://github.com/wiremock/go-wiremock/) client + for interacting with the WireMock container REST API More features will be added over time. @@ -45,20 +46,13 @@ import ( func TestWireMock(t *testing.T) { // Create Container ctx := context.Background() - container, err := RunContainer(ctx, + container, err := RunContainerAndStopOnCleanup(ctx, WithMappingFile("hello", "hello-world.json"), ) 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) - } - }) - // Send the HTTP GET request to the mocked API statusCode, out, err := SendHttpGet(container, "/hello", nil) if err != nil { @@ -76,13 +70,18 @@ func TestWireMock(t *testing.T) { } ``` +## Examples + +- [Quick Start Guide](./docs/quickstart.md) - [sources](./examples/quickstart/) +- [Using the REST API Client](./examples/using_api_client/) + ## License The module is licensed under [Apache License v.2](./LICENSE) ## References -- [WireMock Website](https://wiremock.org/) -- [WireMock and Golang Solutions page](https://wiremock.org/docs/solutions/golang/) +- [WireMock Website](https://wiremock.org/?utm_medium=referral&utm_campaign=wiremock-testcontainers) +- [WireMock and Golang Solutions page](https://wiremock.org/docs/solutions/golang/?utm_medium=referral&utm_campaign=wiremock-testcontainers) - [Testcontainers for Go](https://golang.testcontainers.org/) - [WireMock Module page on the Testcontainers marketplace](https://testcontainers.com/modules/wiremock/) diff --git a/docs/quickstart.md b/docs/quickstart.md index bf4dbd8..4b94e61 100644 --- a/docs/quickstart.md +++ b/docs/quickstart.md @@ -74,19 +74,12 @@ In `func TestWireMock(t *testing.T)`, add the following code: ```go // Create Container ctx := context.Background() -container, err := RunContainer(ctx, +container, err := RunDefaultContainerAndStopOnCleanup(ctx, WithMappingFile("hello", "hello-world.json"), ) 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) - } -}) ``` ## Add logic to send a request diff --git a/docs/using-api-client.md b/docs/using-api-client.md new file mode 100644 index 0000000..0f7a2a5 --- /dev/null +++ b/docs/using-api-client.md @@ -0,0 +1,30 @@ +# Using the REST API client + +The WireMock module includes [Go WireMock](https://github.com/wiremock/go-wiremock/) +client that you can retrieve from the `WireMockContainer` instance +and use for runtime configuration or observability data access. + +```golang + // Create Container + ctx := context.Background() + container, err := RunDefaultContainerAndStopOnCleanup(ctx) + if err != nil { + t.Fatal(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) + } +``` + +See the example [here](https://github.com/wiremock/wiremock-testcontainers-go/tree/main/examples/using_api_client). diff --git a/examples/quickstart/README.md b/examples/quickstart/README.md index 20c7776..3a1b7aa 100644 --- a/examples/quickstart/README.md +++ b/examples/quickstart/README.md @@ -11,7 +11,7 @@ See the [Quickstart Guide](../../docs/quickstart.md) ```bash git clone https://github.com/wiremock/wiremock-testcontainers-go repository -cd wiremock-testcontainers-go +cd wiremock-testcontainers-go/examples/quickstart go mod download go test ``` diff --git a/examples/quickstart/quickstart_test.go b/examples/quickstart/quickstart_test.go index b9fcaca..991acd6 100644 --- a/examples/quickstart/quickstart_test.go +++ b/examples/quickstart/quickstart_test.go @@ -11,9 +11,9 @@ func TestWireMock(t *testing.T) { ctx := context.Background() mappingFileName := "hello-world.json" - container, err := RunContainerAndStopOnCleanup(ctx, t, []testcontainers.ContainerCustomizer{ + container, err := RunContainerAndStopOnCleanup(ctx, t, WithMappingFile(mappingFileName), - }) + ) if err != nil { t.Fatal(err) } diff --git a/examples/using_api_client/README.md b/examples/using_api_client/README.md new file mode 100644 index 0000000..8144cb8 --- /dev/null +++ b/examples/using_api_client/README.md @@ -0,0 +1,17 @@ +# Example - Using the Go WireMock client + +See the [Using the API Client Guide](../../docs/using-api-client.md) + +## Pre-requisites + +- Go 1.19 or above +- Docker-API compatible container runtime ([more info](https://golang.testcontainers.org/system_requirements/docker/)) + +## TL;DR + +```bash +git clone https://github.com/wiremock/wiremock-testcontainers-go repository +cd wiremock-testcontainers-go/examples/using_api_client.md +go mod download +go test +``` diff --git a/examples/using_api_client/go.mod b/examples/using_api_client/go.mod index 07a3837..e04bcf0 100644 --- a/examples/using_api_client/go.mod +++ b/examples/using_api_client/go.mod @@ -1,4 +1,4 @@ -module wiremock.org/testcontainers-go-quickstart +module wiremock.org/testcontainers-go-using-api-client go 1.19 diff --git a/examples/using_api_client/go.sum b/examples/using_api_client/go.sum new file mode 100644 index 0000000..49e712c --- /dev/null +++ b/examples/using_api_client/go.sum @@ -0,0 +1,117 @@ +dario.cat/mergo v1.0.0/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk= +github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E= +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM= +github.com/cenkalti/backoff/v4 v4.2.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE= +github.com/checkpoint-restore/go-criu/v5 v5.3.0/go.mod h1:E/eQpaFtUKGOOSEBZgmKAcn+zUUwWxqcaKZlF54wK8E= +github.com/cilium/ebpf v0.7.0/go.mod h1:/oI2+1shJiTGAMgl6/RgJr36Eo1jzrRcAWbcXO2usCA= +github.com/containerd/console v1.0.3/go.mod h1:7LqA/THxQ86k76b8c/EMSiaJ3h1eZkMkXar0TQ1gf3U= +github.com/containerd/containerd v1.7.3/go.mod h1:32FOM4/O0RkNg7AjQj3hDzN9cUGtu+HMvaKUNiqCZB8= +github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= +github.com/cpuguy83/dockercfg v0.3.1/go.mod h1:sugsbF4//dDlL/i+S+rtpIWp+5h0BHJHfjj5/jFyUJc= +github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= +github.com/cyphar/filepath-securejoin v0.2.3/go.mod h1:aPGpWjXOXUn2NCNjFvBE6aRxGGx79pTxQpKOJNYHHl4= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/docker/distribution v2.8.2+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w= +github.com/docker/docker v24.0.5+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk= +github.com/docker/go-connections v0.4.0/go.mod h1:Gbd7IOopHjR8Iph03tsViu4nIes5XhDvyHbTtUxmeec= +github.com/docker/go-units v0.4.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk= +github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk= +github.com/frankban/quicktest v1.11.3/go.mod h1:wRf/ReqHper53s+kmmSZizM8NamnL3IM0I9ntUbOk+k= +github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= +github.com/godbus/dbus/v5 v5.0.6/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= +github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= +github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= +github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= +github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= +github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= +github.com/klauspost/compress v1.16.0/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE= +github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0= +github.com/moby/patternmatcher v0.5.0/go.mod h1:hDPoyOpDY7OrrMDLaYoY3hf52gNCR/YOUYxkhApJIxc= +github.com/moby/sys/mountinfo v0.5.0/go.mod h1:3bMD3Rg+zkqx8MRYPi7Pyb0Ie97QEBmdxbhnCLlSvSU= +github.com/moby/sys/sequential v0.5.0/go.mod h1:tH2cOOs5V9MlPiXcQzRC+eEyab644PWKGRYaaV5ZZlo= +github.com/moby/term v0.5.0/go.mod h1:8FzsFHVUBGZdbDsJw/ot+X+d5HLUbvklYLJ9uGfcI3Y= +github.com/morikuni/aec v1.0.0/go.mod h1:BbKIizmSmc5MMPqRYbxO4ZU0S0+P200+tUnFx7PXmsc= +github.com/mrunalp/fileutils v0.5.0/go.mod h1:M1WthSahJixYnrXQl/DFQuteStB1weuxD2QJNHXfbSQ= +github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM= +github.com/opencontainers/image-spec v1.1.0-rc4/go.mod h1:X4pATf0uXsnn3g5aiGIsVnJBR4mxhKzfwmvK/B2NTm8= +github.com/opencontainers/runc v1.1.5/go.mod h1:1J5XiS+vdZ3wCyZybsuxXZWGrgSr8fFJHLXuG2PsnNg= +github.com/opencontainers/runtime-spec v1.0.3-0.20210326190908-1c3f411f0417/go.mod h1:jwyrGlmzljRJv/Fgzds9SsS/C5hL+LL3ko9hs6T5lQ0= +github.com/opencontainers/selinux v1.10.0/go.mod h1:2i0OySw99QjzBBQByd1Gr9gSjvuho1lHsJxIJ3gGbJI= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/seccomp/libseccomp-golang v0.9.2-0.20220502022130-f33da4d89646/go.mod h1:JA8cRccbGaA1s33RQf7Y1+q9gHmZX1yB/z9WDN1C6fg= +github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= +github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= +github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww= +github.com/testcontainers/testcontainers-go v0.22.0/go.mod h1:k0YiPa26xJCRUbUkYqy5rY6NGvSbVCeUBXCvucscBR4= +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/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= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/exp v0.0.0-20230510235704-dd950f8aeaea/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w= +golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/mod v0.9.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= +golang.org/x/net v0.0.0-20201224014010-6772e930b67b/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190606203320-7fc4e5ec1444/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191115151921-52ab43148777/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210616094352-59db8d763f22/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210906170528-6f6e22806c34/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20211025201205-69cdffdb9359/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20211116061358-0a5406a5449c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.9.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= +golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= +golang.org/x/tools v0.7.0/go.mod h1:4pg6aUX35JBAogB10C9AtvVL+qowtN4pT3CGSQex14s= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/genproto/googleapis/rpc v0.0.0-20230525234030-28d5490b6b19/go.mod h1:66JfowdXAEgad5O9NnYcsNPLCPZJD++2L9X0PCMODrA= +google.golang.org/grpc v1.57.0/go.mod h1:Sd+9RMTACXwmub0zcNY2c4arhtrbBYD1AUHI/dt16Mo= +google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= +google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= +google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= +google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/examples/using_api_client/using_api_client_test.go b/examples/using_api_client/using_api_client_test.go index cc8c76b..6e00754 100644 --- a/examples/using_api_client/using_api_client_test.go +++ b/examples/using_api_client/using_api_client_test.go @@ -13,18 +13,11 @@ import ( func TestWireMock(t *testing.T) { // Create Container ctx := context.Background() - container, err := RunContainer(ctx) + container, err := RunDefaultContainerAndStopOnCleanup(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")). @@ -35,7 +28,6 @@ func TestWireMock(t *testing.T) { WithStatus(http.StatusOK), ), ) - if err != nil { t.Fatal(err) } diff --git a/tc-wiremock.go b/tc-wiremock.go index 2d26233..42768ce 100644 --- a/tc-wiremock.go +++ b/tc-wiremock.go @@ -67,6 +67,29 @@ func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomize }, nil } +// Creates an instance of the WireMockContainer type that is automatically terminated upon test completion +func RunContainerAndStopOnCleanup(ctx context.Context, t *testing.T, opts ...testcontainers.ContainerCustomizer) (*WireMockContainer, error) { + container, err := RunContainer(ctx, opts...) + if err != nil { + t.Fatal(err) + return nil, err + } + + t.Cleanup(func() { + if err := container.Terminate(ctx); err != nil { + t.Fatalf("failed to terminate container: %s", err) + } + }) + + return container, nil +} + +// Creates a default instance of the WireMockContainer type that is automatically terminated upon test completion +func RunDefaultContainerAndStopOnCleanup(ctx context.Context, t *testing.T) (*WireMockContainer, error) { + var emptyCustomizers []testcontainers.ContainerCustomizer + return RunContainerAndStopOnCleanup(ctx, t, emptyCustomizers...) +} + func WithMappingFile(id string, filePath string) testcontainers.CustomizeRequestOption { return func(req *testcontainers.GenericContainerRequest) { cfgFile := testcontainers.ContainerFile{ @@ -191,20 +214,3 @@ func addQueryParamsToURL(endpoint string, queryParams map[string]string) (string return parsedURL.String(), nil } - -func RunContainerAndStopOnCleanup(ctx context.Context, t *testing.T, customizers []testcontainers.ContainerCustomizer) (testcontainers.Container, error) { - container, err := RunContainer(ctx, customizers...) - if err != nil { - t.Fatal(err) - return nil, err - } - - t.Cleanup(func() { - if err := container.Terminate(ctx); err != nil { - t.Fatalf("failed to terminate container: %s", err) - } - }) - - return container, nil -} - diff --git a/tc-wiremock_test.go b/tc-wiremock_test.go index cee8d87..0d7c646 100644 --- a/tc-wiremock_test.go +++ b/tc-wiremock_test.go @@ -12,7 +12,7 @@ import ( ) func TestWireMock(t *testing.T) { - // Create Container + // Create Container, by deliberately using the RunContainer API ctx := context.Background() container, err := RunContainer(ctx, WithMappingFile("hello", filepath.Join("testdata", "hello-world.json")), @@ -43,7 +43,7 @@ func TestWireMock(t *testing.T) { func TestWireMockWithResource(t *testing.T) { // Create Container ctx := context.Background() - container, err := RunContainer(ctx, + container, err := RunContainerAndStopOnCleanup(ctx, t, WithMappingFile("hello", filepath.Join("testdata", "hello-world-resource.json")), WithFile("hello-world-resource-response.xml", filepath.Join("testdata", "hello-world-resource-response.xml")), ) @@ -51,13 +51,6 @@ func TestWireMockWithResource(t *testing.T) { 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) - } - }) - statusCode, out, err := SendHttpGet(container, "/hello-from-file", nil) if err != nil { t.Fatal(err, "Failed to get a response") @@ -73,20 +66,13 @@ func TestWireMockWithResource(t *testing.T) { func TestSendHttpGetWorksWithQueryParamsPassedInArgument(t *testing.T) { // Create Container ctx := context.Background() - container, err := RunContainer(ctx, + container, err := RunContainerAndStopOnCleanup(ctx, t, WithMappingFile("get", filepath.Join("testdata", "url-with-query-params.json")), ) 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) - } - }) - statusCode, out, err := SendHttpGet(container, "/get", map[string]string{"query": "test"}) if err != nil { t.Fatal(err, "Failed to get a response") @@ -102,20 +88,13 @@ func TestSendHttpGetWorksWithQueryParamsPassedInArgument(t *testing.T) { func TestSendHttpGetWorksWithQueryParamsProvidedInURL(t *testing.T) { // Create Container ctx := context.Background() - container, err := RunContainer(ctx, + container, err := RunContainerAndStopOnCleanup(ctx, t, WithMappingFile("get", filepath.Join("testdata", "url-with-query-params.json")), ) 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) - } - }) - statusCode, out, err := SendHttpGet(container, "/get?query=test", nil) if err != nil { t.Fatal(err, "Failed to get a response") @@ -131,20 +110,13 @@ func TestSendHttpGetWorksWithQueryParamsProvidedInURL(t *testing.T) { func TestSendHttpDelete(t *testing.T) { // Create Container ctx := context.Background() - container, err := RunContainer(ctx, + container, err := RunContainerAndStopOnCleanup(ctx, t, WithMappingFile("delete", filepath.Join("testdata", "204-no-content.json")), ) 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) - } - }) - statusCode, out, err := SendHttpDelete(container, "/delete") if err != nil { t.Fatal(err, "Failed to get a response") @@ -160,21 +132,13 @@ func TestSendHttpDelete(t *testing.T) { func TestSendHttpPatch(t *testing.T) { // Create Container ctx := context.Background() - container, err := RunContainer(ctx, + container, err := RunContainerAndStopOnCleanup(ctx, t, WithMappingFile("patch", filepath.Join("testdata", "200-patch.json")), WithFile("sample-model.json", filepath.Join("testdata", "sample-model.json")), ) 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) - } - }) - var jsonBody = []byte(`{"title":"Buy cheese and bread for breakfast."}`) statusCode, out, err := SendHttpPatch(container, "/patch", bytes.NewBuffer(jsonBody)) if err != nil { @@ -191,7 +155,7 @@ func TestSendHttpPatch(t *testing.T) { func TestSendHttpPut(t *testing.T) { // Create Container ctx := context.Background() - container, err := RunContainer(ctx, + container, err := RunContainerAndStopOnCleanup(ctx, t, WithMappingFile("put", filepath.Join("testdata", "200-put.json")), WithFile("sample-model.json", filepath.Join("testdata", "sample-model.json")), ) @@ -199,13 +163,6 @@ func TestSendHttpPut(t *testing.T) { 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) - } - }) - var jsonBody = []byte(`{"title":"Buy cheese and bread for breakfast."}`) statusCode, out, err := SendHttpPut(container, "/put", bytes.NewBuffer(jsonBody)) if err != nil { @@ -222,18 +179,11 @@ func TestSendHttpPut(t *testing.T) { func TestWireMockClient(t *testing.T) { // Create Container ctx := context.Background() - container, err := RunContainer(ctx) + container, err := RunDefaultContainerAndStopOnCleanup(ctx, t) 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")). @@ -244,7 +194,6 @@ func TestWireMockClient(t *testing.T) { WithStatus(http.StatusOK), ), ) - if err != nil { t.Fatal(err) }