-
Notifications
You must be signed in to change notification settings - Fork 23
/
wiremock.go
342 lines (282 loc) · 9.34 KB
/
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// Copyright The Enterprise Contract Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
// Package wiremock does HTTP protocol stubbing using WireMock (https://wiremock.org/)
package wiremock
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"mime"
"net/http"
"os"
"path"
"cuelang.org/go/pkg/strings"
"github.com/cucumber/godog"
"github.com/otiai10/copy"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
"github.com/walkerus/go-wiremock"
"github.com/enterprise-contract/ec-cli/acceptance/log"
"github.com/enterprise-contract/ec-cli/acceptance/testenv"
)
type key int
const (
wireMockStateKey key = iota // The state of the wiremock persisted between runs and in Context
wireMockImage = "docker.io/wiremock/wiremock:2.33.2" // container image used to run WireMock
)
// to make it simpler on imports in the clients of this package,
// we re-expose functions from the wiremock package, add others
// as needed
var Get = wiremock.Get
var Post = wiremock.Post
var URLPathEqualTo = wiremock.URLPathEqualTo
var MatchingJsonPath = wiremock.MatchingJsonPath
var NewResponse = wiremock.NewResponse
type client struct {
*wiremock.Client
unmatchedURL string
}
// newClient creates a new WireMock client, delegating to the wiremock.Client
// with additional methods we require not present there
func newClient(url string) *client {
w := *wiremock.NewClient(url)
return &client{unmatchedURL: fmt.Sprintf("%s/__admin/requests/unmatched", url), Client: &w}
}
// The result of /__admin/requests/unmatched endpoint
type unmatchedRequest struct {
URL string `json:"url"`
AbsoluteURL string `json:"absoluteUrl"`
Method string
Headers map[string]interface{}
Body string
BrowserProxyRequest bool
LoggedDate int64
LoggedDateString string
}
type wiremockState struct {
URL string
}
func (g wiremockState) Key() any {
return wireMockStateKey
}
func (g wiremockState) Up() bool {
return g.URL != ""
}
// contentTypeFromString returns the content-type part of a MIME media type
// for example given "type/subtype;parameter=value;..." returns "type/subtype"
func contentTypeFromString(s string) string {
for _, part := range strings.Split(s, ",") {
contentType, _, err := mime.ParseMediaType(part)
if err != nil {
continue
}
// first that parses without error
return contentType
}
return "unknown/unknown"
}
// contentTypeFrom returns the content-type based on the "Accept" HTTP
// header of a HTTP request, given that HTTP headers can be multivalued
// it either returns the single value or the first value of many
// "<unknown>" is returned when the "Accept" HTTP header is not
// present, or it contains no values
func contentTypeFrom(u unmatchedRequest) string {
for key, value := range u.Headers {
if strings.ToLower(key) == "accept" {
switch v := value.(type) {
case string:
return contentTypeFromString(v)
case []interface{}:
if len(v) >= 1 {
return contentTypeFromString(fmt.Sprintf("%s", v[0]))
}
}
}
}
return "<unknown>"
}
// String formats the unmatched request and generates a snippet to help
// stub the request, useful when reporting unmatched requests i.e. those
// that have been received but no stubbing was defined
func (u unmatchedRequest) String() string {
str := fmt.Sprintf("Found unmatched %s request to %s\n", u.Method, u.URL)
if u.Body != "" {
str += "The request contained this HTTP body:\n" + u.Body
}
str += fmt.Sprintf(`Stub it by adding:
wiremock.StubFor(ctx, wiremock.%s(wiremock.URLPathEqualTo("%s")).
WillReturnResponse(wiremock.NewResponse().WithBody("<body>").WithHeaders(
map[string]string{"Content-Type": "%s"},
).WithStatus(200)
))`, strings.ToTitle(strings.ToLower(u.Method)), u.URL, contentTypeFrom(u))
return str
}
// UnmatchedRequests queries the WireMock admin API for any unmatched requests
// i.e. those that have been received but no stubbing was defined and returns
// them
func (c *client) UnmatchedRequests() ([]unmatchedRequest, error) {
res, err := http.Get(c.unmatchedURL)
if err != nil {
return nil, fmt.Errorf("failed to fetch unmatched requests via `%s`: %s", c.unmatchedURL, err.Error())
}
defer res.Body.Close()
bodyBytes, err := io.ReadAll(res.Body)
if err != nil {
return nil, fmt.Errorf("failed to fetch unmatched requests via `%s`: failed to read the response, error: %s", c.unmatchedURL, err.Error())
}
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("failed to fetch unmatched requests via `%s`: bad response status: %d, response: %s", c.unmatchedURL, res.StatusCode, string(bodyBytes))
}
var unmatchedRequestsResponse struct {
Requests []unmatchedRequest `json:"requests"`
}
err = json.Unmarshal(bodyBytes, &unmatchedRequestsResponse)
if err != nil {
return nil, fmt.Errorf("failed to fetch unmatched requests via `%s`: unable to unmarshal JSON error: %s, given JSON: `%s`", c.unmatchedURL, err.Error(), string(bodyBytes))
}
return unmatchedRequestsResponse.Requests, nil
}
// StartWiremock starts the WireMock instance if one is not already running
func StartWiremock(ctx context.Context) (context.Context, error) {
var state *wiremockState
ctx, err := testenv.SetupState(ctx, &state)
if err != nil {
return ctx, err
}
if state.Up() {
// we already stored the key in this Context, a WireMock instance
// must be running already
return ctx, nil
}
recordings, err := recordingsDir()
if err != nil {
return ctx, err
}
req := testenv.TestContainersRequest(ctx, testcontainers.ContainerRequest{
Image: wireMockImage,
ExposedPorts: []string{"0.0.0.0::8080/tcp", "0.0.0.0::8443/tcp"},
WaitingFor: wait.ForHTTP("/__admin/mappings").WithPort("8080/tcp"),
Binds: []string{fmt.Sprintf("%s:/recordings:z", recordings)},
Cmd: []string{
"--root-dir=/recordings",
"--verbose",
},
})
logger, ctx := log.LoggerFor(ctx)
w, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
ContainerRequest: req,
Started: true,
Logger: logger,
})
if err != nil {
return ctx, fmt.Errorf("unable to run GenericContainer: %v", err)
}
port, err := w.MappedPort(ctx, "8080/tcp")
if err != nil {
return ctx, err
}
url := fmt.Sprintf("http://localhost:%d", port.Int())
state.URL = url
return ctx, nil
}
// wiremockFrom returns the client used to interact with the WireMock admin API
func wiremockFrom(ctx context.Context) (*client, error) {
state := testenv.FetchState[wiremockState](ctx)
if !state.Up() {
return nil, errors.New("wireMock is not up, did you start it first")
}
return newClient(state.URL), nil
}
// StubFor delegates to the wiremock.StubFor of the WireMock instance assigned
// to the provided Context
func StubFor(ctx context.Context, stubRule *wiremock.StubRule) error {
w, err := wiremockFrom(ctx)
if err != nil {
return err
}
return w.StubFor(stubRule)
}
// Endpoint returns the URL of the WireMock instance
func Endpoint(ctx context.Context) (string, error) {
state := testenv.FetchState[wiremockState](ctx)
if !state.Up() {
return "", errors.New("wireMock is not up, did you start it first")
}
return state.URL, nil
}
func IsRunning(ctx context.Context) bool {
if !testenv.HasState[wiremockState](ctx) {
return false
}
state := testenv.FetchState[wiremockState](ctx)
return state.Up()
}
// AddStepsTo makes sure that nay unmatched requests, i.e. requests that are not
// stubbed get reported at the end of a scenario run
// TODO: reset stub state after the scenario (given not persisted flag is set)
func AddStepsTo(sc *godog.ScenarioContext) {
sc.After(func(ctx context.Context, finished *godog.Scenario, scenarioErr error) (context.Context, error) {
if !IsRunning(ctx) {
return ctx, nil
}
w, err := wiremockFrom(ctx)
if err != nil {
// wiremock wasn't launched, we don't need to proceed
return ctx, err
}
unmatched, err := w.UnmatchedRequests()
if err != nil {
return ctx, err
}
if len(unmatched) == 0 {
return ctx, nil
}
logger, ctx := log.LoggerFor(ctx)
logger.Log("Found unmatched WireMock requests:")
for i, u := range unmatched {
logger.Logf("[%d]: %s", i, u)
}
return ctx, nil
})
}
func recordingsDir() (string, error) {
// cwd is the directory where the test is run from i.e. $GITROOT
cwd, err := os.Getwd()
if err != nil {
return "", err
}
aggregate, err := os.MkdirTemp("", "ec-acceptance-wiremock.*")
if err != nil {
return "", err
}
sourcePath := path.Join(cwd, "acceptance", "wiremock", "recordings")
services, err := os.ReadDir(sourcePath)
if err != nil {
return "", err
}
for _, s := range services {
if !s.IsDir() {
continue
}
src := path.Join(sourcePath, s.Name())
if err := copy.Copy(src, aggregate); err != nil {
return "", err
}
}
return aggregate, nil
}