-
Notifications
You must be signed in to change notification settings - Fork 19
/
doc.go
66 lines (52 loc) · 2.04 KB
/
doc.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
// Copyright 2020 go-wiremock maintainers
// license that can be found in the LICENSE file.
/*
Package wiremock is client for WireMock API.
WireMock is a simulator for HTTP-based APIs.
Some might consider it a service virtualization tool or a mock server.
HTTP request:
POST /example?firstName=John&lastName=Any string other than "Gray" HTTP/1.1
Host: 0.0.0.0:8080
x-session: somefingerprintsome
Content-Type: application/json
Content-Length: 23
{"meta": "information"}
With response:
Status: 400 Bad Request
"Content-Type": "application/json"
{"code": 400, "detail": "detail"}
Stub:
client := wiremock.NewClient("http://0.0.0.0:8080")
client.StubFor(wiremock.Post(wiremock.URLPathEqualTo("/example")).
WithQueryParam("firstName", wiremock.EqualTo("John")).
WithQueryParam("lastName", wiremock.NotMatching("Gray")).
WithBodyPattern(wiremock.EqualToJson(`{"meta": "information"}`)).
WithHeader("x-session", wiremock.Matching("^\\S+fingerprint\\S+$")).
WillReturnResponse(
wiremock.NewResponse().
WithStatus(http.StatusBadRequest).
WithHeader("Content-Type", "application/json").
WithBody(`{"code": 400, "detail": "detail"}`),
).
AtPriority(1))
The client should reset all made stubs after tests:
client := wiremock.NewClient("http://0.0.0.0:8080")
defer wiremock.Reset()
client.StubFor(wiremock.Get(wiremock.URLPathEqualTo("/example")))
client.StubFor(wiremock.Get(wiremock.URLPathEqualTo("/example/1")))
// ...
To avoid conflicts, you can delete individual rules:
client := wiremock.NewClient("http://0.0.0.0:8080")
exampleStubRule := wiremock.Get(wiremock.URLPathEqualTo("/example"))
client.StubFor(exampleStubRule)
client.StubFor(wiremock.Get(wiremock.URLPathEqualTo("/example/1")))
// ...
client.DeleteStub(exampleStubRule)
You can verify if a request has been made that matches the mapping.
client := wiremock.NewClient("http://0.0.0.0:8080")
exampleStubRule := wiremock.Get(wiremock.URLPathEqualTo("/example"))
client.StubFor(exampleStubRule)
// ...
client.Verify(exampleStubRule.Request(), 1)
*/
package wiremock