forked from testcontainers/testcontainers-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding mockserver module (testcontainers#2085)
* Adding mockserver module * Adding a verify in the example for completeness * Adding docs for mockserver * Using the module generator scripts for completeness * Apply suggestions from code review Co-authored-by: Manuel de la Peña <[email protected]> * Addressing PR review comments * fix: run make lint --------- Co-authored-by: Manuel de la Peña <[email protected]> Co-authored-by: Manuel de la Peña <[email protected]>
- Loading branch information
1 parent
2309c4e
commit 1934eb2
Showing
12 changed files
with
492 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# MockServer | ||
|
||
Not available until the next release of testcontainers-go <a href="https://github.com/testcontainers/testcontainers-go"><span class="tc-version">:material-tag: main</span></a> | ||
|
||
## Introduction | ||
|
||
The Testcontainers module for MockServer. MockServer can be used to mock HTTP services by matching requests against user-defined expectations. | ||
|
||
## Adding this module to your project dependencies | ||
|
||
Please run the following command to add the MockServer module to your Go dependencies: | ||
|
||
``` | ||
go get github.com/testcontainers/testcontainers-go/modules/mockserver | ||
``` | ||
|
||
## Usage example | ||
|
||
<!--codeinclude--> | ||
[Creating a MockServer container](../../modules/mockserver/examples_test.go) inside_block:runMockServerContainer | ||
<!--/codeinclude--> | ||
|
||
## Module reference | ||
|
||
The MockServer module exposes one entrypoint function to create the MockServer container, and this function receives two parameters: | ||
|
||
```golang | ||
func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomizer) (*MockServerContainer, error) | ||
``` | ||
|
||
- `context.Context`, the Go context. | ||
- `testcontainers.ContainerCustomizer`, a variadic argument for passing options. | ||
|
||
### Container Options | ||
|
||
When starting the MockServer container, you can pass options in a variadic way to configure it. | ||
|
||
#### Image | ||
|
||
If you need to set a different MockServer Docker image, you can use `testcontainers.WithImage` with a valid Docker image | ||
for MockServer. E.g. `testcontainers.WithImage("mockserver/mockserver:5.15.0")`. | ||
|
||
{% include "../features/common_functional_options.md" %} | ||
|
||
### Container Methods | ||
|
||
The MockServer container exposes the following methods: | ||
|
||
#### URL | ||
|
||
The `URL` method returns the url string to connect to the MockServer container. | ||
It returns a string with the format `http://<host>:<port>`. | ||
|
||
It can be use to configure a MockServer client (`github.com/BraspagDevelopers/mock-server-client`), e.g.: | ||
|
||
<!--codeinclude--> | ||
[Using URL with the MockServer client](../../modules/mockserver/examples_test.go) inside_block:connectToMockServer | ||
<!--/codeinclude--> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
include ../../commons-test.mk | ||
|
||
.PHONY: test | ||
test: | ||
$(MAKE) test-mockserver |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
mode: set |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package mockserver_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"strings" | ||
|
||
client "github.com/BraspagDevelopers/mock-server-client" | ||
|
||
"github.com/testcontainers/testcontainers-go" | ||
"github.com/testcontainers/testcontainers-go/modules/mockserver" | ||
) | ||
|
||
func ExampleRunContainer() { | ||
// runMockServerContainer { | ||
ctx := context.Background() | ||
|
||
mockserverContainer, err := mockserver.RunContainer(ctx, testcontainers.WithImage("mockserver/mockserver:5.15.0")) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// Clean up the container | ||
defer func() { | ||
if err := mockserverContainer.Terminate(ctx); err != nil { | ||
panic(err) | ||
} | ||
}() | ||
// } | ||
|
||
state, err := mockserverContainer.State(ctx) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
fmt.Println(state.Running) | ||
|
||
// Output: | ||
// true | ||
} | ||
|
||
func ExampleRunContainer_connect() { | ||
// connectToMockServer { | ||
ctx := context.Background() | ||
|
||
mockserverContainer, err := mockserver.RunContainer(ctx, testcontainers.WithImage("mockserver/mockserver:5.15.0")) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// Clean up the container | ||
defer func() { | ||
if err := mockserverContainer.Terminate(ctx); err != nil { | ||
panic(err) | ||
} | ||
}() | ||
|
||
url, err := mockserverContainer.URL(ctx) | ||
if err != nil { | ||
panic(err) | ||
} | ||
ms := client.NewClientURL(url) | ||
// } | ||
|
||
requestMatcher := client.RequestMatcher{ | ||
Method: http.MethodPost, | ||
Path: "/api/categories", | ||
} | ||
requestMatcher = requestMatcher.WithJSONFields(map[string]interface{}{"name": "Tools"}) | ||
err = ms.RegisterExpectation(client.NewExpectation(requestMatcher).WithResponse(client.NewResponseOK().WithJSONBody(map[string]any{"test": "value"}))) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
httpClient := &http.Client{} | ||
resp, err := httpClient.Post(url+"/api/categories", "application/json", strings.NewReader(`{"name": "Tools"}`)) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
buf, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
panic(err) | ||
} | ||
resp.Body.Close() | ||
|
||
fmt.Println(resp.StatusCode) | ||
fmt.Println(string(buf)) | ||
|
||
err = ms.Verify(requestMatcher, client.Once()) | ||
fmt.Println(err == nil) | ||
|
||
// Output: | ||
// 200 | ||
// { | ||
// "test" : "value" | ||
// } | ||
// true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
module github.com/testcontainers/testcontainers-go/modules/mockserver | ||
|
||
go 1.20 | ||
|
||
require ( | ||
github.com/BraspagDevelopers/mock-server-client v0.2.2 | ||
github.com/testcontainers/testcontainers-go v0.27.0 | ||
) | ||
|
||
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/Microsoft/hcsshim v0.11.4 // indirect | ||
github.com/cenkalti/backoff/v4 v4.2.1 // indirect | ||
github.com/containerd/containerd v1.7.11 // indirect | ||
github.com/containerd/log v0.1.0 // indirect | ||
github.com/cpuguy83/dockercfg v0.3.1 // indirect | ||
github.com/docker/distribution v2.8.2+incompatible // indirect | ||
github.com/docker/docker v24.0.7+incompatible // indirect | ||
github.com/docker/go-connections v0.4.0 // indirect | ||
github.com/docker/go-units v0.5.0 // indirect | ||
github.com/go-ole/go-ole v1.2.6 // indirect | ||
github.com/go-resty/resty/v2 v2.3.0 // indirect | ||
github.com/gogo/protobuf v1.3.2 // indirect | ||
github.com/golang/protobuf v1.5.3 // indirect | ||
github.com/google/uuid v1.5.0 // indirect | ||
github.com/klauspost/compress v1.16.0 // indirect | ||
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect | ||
github.com/magiconair/properties v1.8.7 // indirect | ||
github.com/moby/patternmatcher v0.6.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-rc5 // indirect | ||
github.com/opencontainers/runc v1.1.5 // indirect | ||
github.com/pkg/errors v0.9.1 // indirect | ||
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect | ||
github.com/shirou/gopsutil/v3 v3.23.12 // indirect | ||
github.com/shoenig/go-m1cpu v0.1.6 // indirect | ||
github.com/sirupsen/logrus v1.9.3 // indirect | ||
github.com/tklauser/go-sysconf v0.3.12 // indirect | ||
github.com/tklauser/numcpus v0.6.1 // indirect | ||
github.com/yusufpapurcu/wmi v1.2.3 // indirect | ||
golang.org/x/exp v0.0.0-20230510235704-dd950f8aeaea // indirect | ||
golang.org/x/mod v0.11.0 // indirect | ||
golang.org/x/net v0.17.0 // indirect | ||
golang.org/x/sys v0.15.0 // indirect | ||
golang.org/x/tools v0.10.0 // indirect | ||
google.golang.org/genproto/googleapis/rpc v0.0.0-20230711160842-782d3b101e98 // indirect | ||
google.golang.org/grpc v1.58.3 // indirect | ||
google.golang.org/protobuf v1.31.0 // indirect | ||
) | ||
|
||
replace github.com/testcontainers/testcontainers-go => ../.. |
Oops, something went wrong.