forked from lamoda/gonkey
/
reply_strategy.go
197 lines (167 loc) · 4.13 KB
/
reply_strategy.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
package mocks
import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httputil"
"strings"
"sync"
)
type replyStrategy interface {
HandleRequest(w http.ResponseWriter, r *http.Request) []error
}
type contextAwareStrategy interface {
ResetRunningContext()
EndRunningContext() []error
}
type constantReply struct {
replyStrategy
replyBody []byte
statusCode int
headers map[string]string
}
func unhandledRequestError(r *http.Request) []error {
requestContent, err := httputil.DumpRequest(r, true)
if err != nil {
return []error{fmt.Errorf("Gonkey internal error during request dump: %s\n", err)}
}
return []error{fmt.Errorf("unhandled request to mock:\n%s", requestContent)}
}
func newFileReplyWithCode(filename string, statusCode int, headers map[string]string) (replyStrategy, error) {
content, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
r := &constantReply{
replyBody: content,
statusCode: statusCode,
headers: headers,
}
return r, nil
}
func newConstantReplyWithCode(content []byte, statusCode int, headers map[string]string) replyStrategy {
return &constantReply{
replyBody: content,
statusCode: statusCode,
headers: headers,
}
}
func (s *constantReply) HandleRequest(w http.ResponseWriter, r *http.Request) []error {
for k, v := range s.headers {
w.Header().Add(k, v)
}
w.WriteHeader(s.statusCode)
w.Write(s.replyBody)
return nil
}
type failReply struct {
}
func (s *failReply) HandleRequest(w http.ResponseWriter, r *http.Request) []error {
return unhandledRequestError(r)
}
type nopReply struct {
replyStrategy
}
func (s *nopReply) HandleRequest(w http.ResponseWriter, r *http.Request) []error {
w.WriteHeader(http.StatusNoContent)
return nil
}
type uriVaryReply struct {
replyStrategy
contextAwareStrategy
basePath string
variants map[string]*definition
}
func newUriVaryReply(basePath string, variants map[string]*definition) replyStrategy {
return &uriVaryReply{
basePath: strings.TrimRight(basePath, "/") + "/",
variants: variants,
}
}
func (s *uriVaryReply) HandleRequest(w http.ResponseWriter, r *http.Request) []error {
for uri, def := range s.variants {
uri = strings.TrimLeft(uri, "/")
if s.basePath+uri == r.URL.Path {
return def.Execute(w, r)
}
}
return unhandledRequestError(r)
}
func (s *uriVaryReply) ResetRunningContext() {
for _, def := range s.variants {
def.ResetRunningContext()
}
}
func (s *uriVaryReply) EndRunningContext() []error {
var errs []error
for _, def := range s.variants {
errs = append(errs, def.EndRunningContext()...)
}
return errs
}
type methodVaryReply struct {
replyStrategy
contextAwareStrategy
variants map[string]*definition
}
func newMethodVaryReply(variants map[string]*definition) replyStrategy {
return &methodVaryReply{
variants: variants,
}
}
func (s *methodVaryReply) HandleRequest(w http.ResponseWriter, r *http.Request) []error {
for method, def := range s.variants {
if strings.EqualFold(r.Method, method) {
return def.Execute(w, r)
}
}
return unhandledRequestError(r)
}
func (s *methodVaryReply) ResetRunningContext() {
for _, def := range s.variants {
def.ResetRunningContext()
}
}
func (s *methodVaryReply) EndRunningContext() []error {
var errs []error
for _, def := range s.variants {
errs = append(errs, def.EndRunningContext()...)
}
return errs
}
func newSequentialReply(strategies []*definition) replyStrategy {
return &sequentialReply{
sequence: strategies,
}
}
type sequentialReply struct {
sync.Mutex
count int
sequence []*definition
}
func (s *sequentialReply) ResetRunningContext() {
s.Lock()
s.count = 0
s.Unlock()
for _, def := range s.sequence {
def.ResetRunningContext()
}
}
func (s *sequentialReply) EndRunningContext() []error {
var errs []error
for _, def := range s.sequence {
errs = append(errs, def.EndRunningContext()...)
}
return errs
}
func (s *sequentialReply) HandleRequest(w http.ResponseWriter, r *http.Request) []error {
s.Lock()
defer s.Unlock()
// out of bounds, url requested more times than sequence length
if s.count >= len(s.sequence) {
return unhandledRequestError(r)
}
def := s.sequence[s.count]
s.count++
return def.Execute(w, r)
}