This repository has been archived by the owner on Jul 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
wiremock.go
135 lines (105 loc) · 2.62 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
package wiremock
import (
"bytes"
"encoding/json"
"fmt"
"net"
"net/http"
"time"
)
type Wiremock struct {
URL string
}
func New(url string) *Wiremock {
return &Wiremock{URL: url}
}
func (w *Wiremock) Reset() error {
return w.post("reset", nil)
}
type Stub struct {
Request RequestMatcher `json:"request"`
Response Response `json:"response"`
}
type RequestMatcher struct {
Method string `json:"method"`
URL string `json:"url"`
}
type Response struct {
Status int `json:"status"`
}
func (w *Wiremock) AddStub(stub Stub) error {
return w.post("mappings", stub)
}
type Count struct {
Count int `json:"count"`
}
type Requests struct {
Requests []Request `json:"requests"`
}
type Request struct {
Body string `json:"body"`
}
func (w *Wiremock) GetRequestBody(requestMatcher RequestMatcher) (string, error) {
resp, err := w.postWithResponse("requests/find", requestMatcher)
if err != nil {
return "", err
}
defer resp.Body.Close()
responseDecoder := json.NewDecoder(resp.Body)
reqs := &Requests{}
err = responseDecoder.Decode(reqs)
if err != nil {
return "", err
}
if len(reqs.Requests) != 1 {
return "", fmt.Errorf("expected one request, but instead got %d", len(reqs.Requests))
}
return reqs.Requests[0].Body, nil
}
func (w *Wiremock) GetCount(requestMatcher RequestMatcher) (int, error) {
resp, err := w.postWithResponse("requests/count", requestMatcher)
if err != nil {
return 0, err
}
defer resp.Body.Close()
countDecoder := json.NewDecoder(resp.Body)
count := &Count{}
err = countDecoder.Decode(count)
if err != nil {
return 0, err
}
return count.Count, nil
}
func (w *Wiremock) GetCountFn(requestMatcher RequestMatcher) func() (int, error) {
return func() (int, error) {
return w.GetCount(requestMatcher)
}
}
func (w *Wiremock) post(path string, body interface{}) error {
resp, err := w.postWithResponse(path, body)
if err != nil {
return err
}
defer resp.Body.Close()
return nil
}
func (w *Wiremock) postWithResponse(path string, body interface{}) (*http.Response, error) {
bodyJSON, err := json.Marshal(body)
if err != nil {
return nil, err
}
return retryOnTemporaryError(func() (*http.Response, error) {
return http.Post(fmt.Sprintf("%s/__admin/%s", w.URL, path), "application/json", bytes.NewReader(bodyJSON))
}, 5, time.Second)
}
func retryOnTemporaryError(fn func() (*http.Response, error), times int, wait time.Duration) (*http.Response, error) {
resp, err := fn()
if err == nil || times < 1 {
return resp, err
}
if nerr, ok := err.(net.Error); ok && nerr.Temporary() {
time.Sleep(wait)
return retryOnTemporaryError(fn, times-1, wait)
}
return resp, err
}