-
Notifications
You must be signed in to change notification settings - Fork 12
/
tc-wiremock.go
222 lines (177 loc) · 6.17 KB
/
tc-wiremock.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package testcontainers_wiremock
import (
"context"
"io"
"net/http"
"net/url"
"path/filepath"
"testing"
"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"
const defaultWireMockVersion = "2.35.0-1"
const defaultPort = "8080"
type WireMockContainer struct {
testcontainers.Container
version string
Client *wiremock.Client
}
type WireMockExtension struct {
testcontainers.Container
id string
classname string
jarPath string
}
// RunContainer creates an instance of the WireMockContainer type
func RunContainer(ctx context.Context, opts ...testcontainers.ContainerCustomizer) (*WireMockContainer, error) {
req := testcontainers.ContainerRequest{
Image: defaultWireMockImage + ":" + defaultWireMockVersion,
ExposedPorts: []string{defaultPort + "/tcp"},
Cmd: []string{""},
WaitingFor: wait.ForHTTP("/__admin").WithPort(nat.Port(defaultPort)),
}
genericContainerReq := testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
}
for _, opt := range opts {
opt.Customize(&genericContainerReq)
}
req.Cmd = append(req.Cmd, "--disable-banner")
container, err := testcontainers.GenericContainer(ctx, genericContainerReq)
if err != nil {
return nil, err
}
uri, err := GetURI(ctx, container)
if err != nil {
return nil, err
}
return &WireMockContainer{
Container: container,
Client: wiremock.NewClient(uri),
}, 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) error {
cfgFile := testcontainers.ContainerFile{
HostFilePath: filePath,
ContainerFilePath: filepath.Join("/home/wiremock/mappings", id+".json"),
FileMode: 0755,
}
req.Files = append(req.Files, cfgFile)
return nil
}
}
func WithFile(name string, filePath string) testcontainers.CustomizeRequestOption {
return func(req *testcontainers.GenericContainerRequest) error {
cfgFile := testcontainers.ContainerFile{
HostFilePath: filePath,
ContainerFilePath: "/home/wiremock/__files/" + name,
FileMode: 0755,
}
req.Files = append(req.Files, cfgFile)
return nil
}
}
func WithImage(image string) testcontainers.CustomizeRequestOption {
return func(req *testcontainers.GenericContainerRequest) error {
req.Image = image
return nil
}
}
func GetURI(ctx context.Context, container testcontainers.Container) (string, error) {
hostIP, err := container.Host(ctx)
if err != nil {
return "", err
}
mappedPort, err := container.MappedPort(ctx, nat.Port(defaultPort))
if err != nil {
return "", err
}
return "http://" + hostIP + ":" + mappedPort.Port(), nil
}
// SendHttpGet sends Http GET request to the container passed as an argument.
// 'queryParams' parameter is optional and can be passed as a nil. Query parameters also work when hardcoded in the endpoint argument.
func SendHttpGet(container testcontainers.Container, endpoint string, queryParams map[string]string) (int, string, error) {
if queryParams != nil {
var err error
endpoint, err = addQueryParamsToURL(endpoint, queryParams)
if err != nil {
return -1, "", err
}
}
return sendHttpRequest(http.MethodGet, container, endpoint, nil)
}
// SendHttpDelete sends Http DELETE request to the container passed as an argument.
func SendHttpDelete(container testcontainers.Container, endpoint string) (int, string, error) {
return sendHttpRequest(http.MethodDelete, container, endpoint, nil)
}
// SendHttpPost sends Http POST request to the container passed as an argument.
func SendHttpPost(container testcontainers.Container, endpoint string, body io.Reader) (int, string, error) {
return sendHttpRequest(http.MethodPost, container, endpoint, body)
}
// SendHttpPatch sends Http PATCH request to the container passed as an argument.
func SendHttpPatch(container testcontainers.Container, endpoint string, body io.Reader) (int, string, error) {
return sendHttpRequest(http.MethodPatch, container, endpoint, body)
}
// SendHttpPut sends Http PUT request to the container passed as an argument.
func SendHttpPut(container testcontainers.Container, endpoint string, body io.Reader) (int, string, error) {
return sendHttpRequest(http.MethodPut, container, endpoint, body)
}
func sendHttpRequest(httpMethod string, container testcontainers.Container, endpoint string, body io.Reader) (int, string, error) {
ctx := context.Background()
uri, err := GetURI(ctx, container)
if err != nil {
return -1, "", err
}
req, err := http.NewRequest(httpMethod, uri+endpoint, body)
if err != nil {
return -1, "", err
}
res, err := http.DefaultClient.Do(req)
if err != nil {
return -1, "", err
}
out, err := io.ReadAll(res.Body)
if err != nil {
return -1, "", err
}
return res.StatusCode, string(out), nil
}
func addQueryParamsToURL(endpoint string, queryParams map[string]string) (string, error) {
parsedURL, err := url.Parse(endpoint)
if err != nil {
return "", err
}
existingQueryParams, err := url.ParseQuery(parsedURL.RawQuery)
if err != nil {
return "", err
}
for key, value := range queryParams {
existingQueryParams.Set(key, value)
}
parsedURL.RawQuery = existingQueryParams.Encode()
return parsedURL.String(), nil
}