-
Notifications
You must be signed in to change notification settings - Fork 1
/
controller_test.go
216 lines (174 loc) · 7.32 KB
/
controller_test.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
package main
import (
"fmt"
"net/http"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestControllerGetExpectations_NoExpectations_ReturnEmptyList(t *testing.T) {
var exps = ControllerGetExpectations(Expectations{})
assert.Empty(t, exps)
}
func TestControllerAddExpectations_NoExpectations_ReturnOneItem(t *testing.T) {
var exp = Expectation{Key: "k"}
var exps = ControllerAddExpectation(exp.Key, exp, Expectations{})
assert.Contains(t, exps, exp.Key)
assert.Equal(t, exp, exps[exp.Key])
}
func TestControllerAddExpectations_ExistingKey_ReturnUpdatedOneItem(t *testing.T) {
var exp1 = Expectation{Key: "k", Delay: 1}
var exp2 = Expectation{Key: "k", Delay: 2}
var exps = ControllerAddExpectation(exp1.Key, exp1, Expectations{})
assert.Contains(t, exps, exp1.Key)
exps = ControllerAddExpectation(exp2.Key, exp2, exps)
assert.Contains(t, exps, exp2.Key)
assert.Equal(t, 1, len(exps))
assert.Equal(t, exp2, exps[exp2.Key])
}
func TestControllerAddExpectations_NewKey_ReturnTwoItems(t *testing.T) {
var exp1 = Expectation{Key: "k1", Delay: 1}
var exp2 = Expectation{Key: "k2", Delay: 2}
var exps = ControllerAddExpectation(exp1.Key, exp1, Expectations{})
assert.Contains(t, exps, exp1.Key)
exps = ControllerAddExpectation(exp2.Key, exp2, exps)
assert.Contains(t, exps, exp2.Key)
assert.Equal(t, 2, len(exps))
assert.Equal(t, exp1, exps[exp1.Key])
assert.Equal(t, exp2, exps[exp2.Key])
}
func TestControllerRemoveExpectations_OneExpectations_ReturnEmptyList(t *testing.T) {
var exp = Expectation{Key: "k"}
var exps = ControllerAddExpectation(exp.Key, exp, Expectations{})
assert.Contains(t, exps, exp.Key)
exps = ControllerRemoveExpectation(exp.Key, exps)
assert.Empty(t, exps)
}
func TestControllerRemoveWrongKeyExpectations_OneExpectations_NotReturnError(t *testing.T) {
var exp = Expectation{Key: "k"}
var exps = ControllerAddExpectation(exp.Key, exp, Expectations{})
assert.Contains(t, exps, exp.Key)
exps = ControllerRemoveExpectation("wrong_key", exps)
assert.Contains(t, exps, exp.Key)
}
func TestControllerTranslateRequestToExpectation_SimpleRequest_AllFieldsTranslated(t *testing.T) {
request, err := http.NewRequest("POST", "https://www.host.com/a/b?foo=bar#fr", strings.NewReader("body text"))
if err != nil {
t.Fatal(err)
}
request.Header.Add("h1", "hv1")
exp := ControllerTranslateRequestToExpectation(request)
assert.NotNil(t, exp)
assert.Equal(t, "POST", exp.Method)
assert.Equal(t, "/a/b?foo=bar#fr", exp.Path)
assert.Equal(t, "body text", exp.Body)
assert.NotNil(t, exp.Headers)
assert.Equal(t, 1, len(*exp.Headers))
assert.Equal(t, "hv1", (*exp.Headers)["H1"])
}
func TestControllerTranslateHTTPHeadersToExpHeaders_TwoHeaders_HeadersTranslated(t *testing.T) {
header := http.Header{}
header.Add("h1", "hv1")
header.Add("h1", "hv2")
expHeaders := ControllerTranslateHTTPHeadersToExpHeaders(header)
assert.NotNil(t, expHeaders)
assert.Equal(t, 1, len(*expHeaders))
assert.Equal(t, "hv1,hv2", (*expHeaders)["H1"])
}
func TestControllerStringPassesFilter_EmptyFilter_True(t *testing.T) {
assert.True(t, ControllerStringPassesFilter("abc", ""))
}
func TestControllerStringPassesFilter_ExistingSubstring_True(t *testing.T) {
assert.True(t, ControllerStringPassesFilter("abc", "ab"))
}
func TestControllerStringPassesFilter_ExistingRegex_True(t *testing.T) {
assert.True(t, ControllerStringPassesFilter("abc", ".b."))
}
func TestControllerStringPassesFilter_NotExistingSubstring_False(t *testing.T) {
assert.False(t, ControllerStringPassesFilter("abc", "zz"))
}
func TestControllerStringPassesFilter_NotExistingRegex_False(t *testing.T) {
assert.False(t, ControllerStringPassesFilter("abc", ".z."))
}
func TestControllerStringPassesFilter_MultilineBody_True(t *testing.T) {
assert.True(t, ControllerStringPassesFilter("a\nb", "a.b"))
}
func TestControllerRequestPassFilter_EmptyRequestEmptyFilter_True(t *testing.T) {
assert.True(t, ControllerRequestPassesFilter(
&ExpectationRequest{},
&ExpectationRequest{}))
}
func TestControllerRequestPassFilter_MethodsAreEq_True(t *testing.T) {
assert.True(t, ControllerRequestPassesFilter(
&ExpectationRequest{Method: "POST"},
&ExpectationRequest{Method: "POST"}))
}
func TestControllerRequestPassFilter_PathsAreEq_True(t *testing.T) {
assert.True(t, ControllerRequestPassesFilter(
&ExpectationRequest{Path: "/path"},
&ExpectationRequest{Path: "/path"}))
}
func TestControllerRequestPassFilter_MethodsNotEqAndPathsAreEq_False(t *testing.T) {
assert.False(t, ControllerRequestPassesFilter(
&ExpectationRequest{Method: "GET", Path: "/path"},
&ExpectationRequest{Method: "POST", Path: "/path"}))
}
func TestControllerRequestPassFilter_HeadersAreEq_True(t *testing.T) {
assert.True(t, ControllerRequestPassesFilter(
&ExpectationRequest{Headers: &Headers{"h1": "hv1"}},
&ExpectationRequest{Headers: &Headers{"h1": "hv1"}}))
}
func TestControllerRequestPassFilter_HeaderNotEq_False(t *testing.T) {
result := ControllerRequestPassesFilter(
&ExpectationRequest{Headers: &Headers{"h1": "hv1"}},
&ExpectationRequest{Headers: &Headers{"h2": "hv2"}})
assert.False(t, result)
}
func TestControllerRequestPassFilter_HeaderValueNotEq_False(t *testing.T) {
assert.False(t, ControllerRequestPassesFilter(
&ExpectationRequest{Headers: &Headers{"h1": "hv1"}},
&ExpectationRequest{Headers: &Headers{"h1": "hv2"}}))
}
func TestControllerRequestPassFilter_NoHeaderinReq_False(t *testing.T) {
assert.False(t, ControllerRequestPassesFilter(
&ExpectationRequest{},
&ExpectationRequest{Headers: &Headers{"h2": "hv2"}}))
}
func TestControllerRequestPassFilter_NoHeaderInFilter_True(t *testing.T) {
assert.True(t, ControllerRequestPassesFilter(
&ExpectationRequest{Headers: &Headers{"h1": "hv1"}},
&ExpectationRequest{}))
}
func TestControllerRequestPassFilter_BodysEq_True(t *testing.T) {
assert.True(t, ControllerRequestPassesFilter(
&ExpectationRequest{Body: "body"},
&ExpectationRequest{Body: "body"}))
}
func TestControllerSortExpectationsByPriority_EmptyExps_OK(t *testing.T) {
sortedMap := ControllerSortExpectationsByPriority(Expectations{})
assert.Equal(t, 0, len(sortedMap))
}
func TestControllerSortExpectationsByPriority_ListOfExpectations_OK(t *testing.T) {
exp1 := Expectation{Key: "k1", Priority: 1}
exp2 := Expectation{Key: "k0", Priority: 0}
exp3 := Expectation{Key: "k2", Priority: 2}
exps := ControllerAddExpectation(exp1.Key, exp1, Expectations{})
exps = ControllerAddExpectation(exp2.Key, exp2, exps)
exps = ControllerAddExpectation(exp3.Key, exp3, exps)
sortedMap := ControllerSortExpectationsByPriority(exps)
assert.Equal(t, 3, len(sortedMap))
assert.Equal(t, "k2", sortedMap[0].Key)
assert.Equal(t, "k1", sortedMap[1].Key)
assert.Equal(t, "k0", sortedMap[2].Key)
}
func TestControllerControllerCreateHTTPRequestWithHeaders(t *testing.T) {
expReq := &ExpectationRequest{Method: "GET", Path: "/request", Headers: &Headers{"h_req": "hv_req"}}
expFwd := &ExpectationForward{Scheme: "https", Host: "localhost_fwd", Headers: &Headers{"h_req": "hv_fwd", "h_fwd": "hv_fwd"}}
httpReq := ControllerCreateHTTPRequest(expReq, expFwd)
assert.NotNil(t, httpReq)
assert.Equal(t, expReq.Method, httpReq.Method)
assert.Equal(t, expFwd.Host, httpReq.Host)
assert.Equal(t, fmt.Sprintf("%s://%s%s", expFwd.Scheme, expFwd.Host, expReq.Path), httpReq.URL.String())
assert.Equal(t, "hv_fwd", httpReq.Header.Get("h_req"))
assert.Equal(t, "hv_fwd", httpReq.Header.Get("h_fwd"))
}