-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreaders_test.go
209 lines (175 loc) · 8.34 KB
/
readers_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
package shuttle
import (
"errors"
"io"
"net/http"
"net/http/httptest"
"testing"
)
func TestAcceptReader_AcceptTypeProvided_NotFound_ReturnFailure(t *testing.T) {
assertAcceptReader(t, "fail", []string{"not-found"}, []string{"not-found"}, false, -1)
}
func TestAcceptReader_NoAcceptTypesProvided_NoError(t *testing.T) {
assertAcceptReader(t, "", nil, nil, false, -1)
}
func TestAcceptReader_MultipleAcceptTypesProvided_Found_OverwriteAccept(t *testing.T) {
assertAcceptReader(t, "", []string{"not-found-1, found"}, []string{"found"}, false, -1)
}
func TestAcceptReader_MultipleComplexAcceptTypesProvided_Found_OverwriteAccept(t *testing.T) {
assertAcceptReader(t, "", []string{"not-found-1, found;q=0, not-found-2"}, []string{"found"}, false, -1)
}
func TestAcceptReader_WildcardAcceptTypeProvided_Found_OverwriteAccept(t *testing.T) {
assertAcceptReader(t, "", []string{"*/*"}, nil, false, -1)
}
func TestAcceptReader_RealWorldExampleWithWildcard_Found(t *testing.T) {
assertAcceptReader(t, "", []string{"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"}, nil, false, -1)
}
func TestAcceptReader_UseDefaultIfNotFound_True(t *testing.T) {
assertAcceptReader(t, "", []string{"text/html,application/xhtml+xml,application/xml;q=0.9,"}, nil, true, -1)
}
func TestAcceptReader_UseDefaultIfNotFound_False(t *testing.T) {
assertAcceptReader(t, "fail", []string{"text/html,application/xhtml+xml,application/xml;q=0.9,"}, nil, false, -1)
}
func TestAcceptReader_UseDefaultIfNotFound_MaxAcceptTypes(t *testing.T) {
assertAcceptReader(t, "", []string{"text/html,application/xhtml+xml,application/xml;q=0.9,"}, nil, true, 1)
}
func TestAcceptReader_MaxAcceptTypes_Single(t *testing.T) {
assertAcceptReader(t, "", []string{"text/html,application/xhtml+xml,application/xml;q=0.9,"}, nil, false, 1)
}
func TestAcceptReader_MaxAcceptTypes_Limit_NotFound(t *testing.T) {
assertAcceptReader(t, "", []string{"text/html,application/xhtml+xml,found,application/xml;q=0.9,"}, nil, false, 2)
}
func TestAcceptReader_MaxAcceptTypes_Limit_Found(t *testing.T) {
assertAcceptReader(t, "", []string{"text/html,application/xhtml+xml,found,application/xml;q=0.9,"}, []string{"found"}, false, 3)
}
func TestAcceptReader_MaxAcceptTypes_All(t *testing.T) {
assertAcceptReader(t, "fail", []string{"text/html,application/xhtml+xml,application/xml;q=0.9,"}, nil, false, -1)
}
func TestAcceptReader_MaxAcceptTypes_All_Found(t *testing.T) {
assertAcceptReader(t, "", []string{"text/html,application/xhtml+xml,application/xml;q=0.9,found"}, []string{"found"}, false, -1)
}
func assertAcceptReader(t *testing.T, expectedResult string, acceptTypes, acceptTypesWhenSuccessful []string, useDefaultIfNotFound bool, maxTypes int) {
request := httptest.NewRequest("GET", "/", nil)
request.Header["Accept"] = acceptTypes
notAcceptable := &TextResult{Content: expectedResult}
serializers := map[string]func() Serializer{
"found": func() Serializer { return nil },
}
result := newAcceptReader(serializers, notAcceptable, useDefaultIfNotFound, maxTypes, &nopMonitor{}).Read(nil, request)
if len(expectedResult) == 0 {
Assert(t).That(result).IsNil()
} else {
Assert(t).That(result).Equals(notAcceptable)
}
if result == nil {
Assert(t).That(request.Header["Accept"]).Equals(acceptTypesWhenSuccessful)
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
func TestDeserializeReader_NoContentType_ReturnFailure(t *testing.T) {
assertDeserializeReader(t, "unsupported-media-type", nil, nil)
}
func TestDeserializeReader_UnknownContentType_ReturnFailure(t *testing.T) {
assertDeserializeReader(t, "unsupported-media-type", []string{"application/garbage"}, nil)
}
func TestDeserializeReader_DeserializationFailure_ReturnFailure(t *testing.T) {
assertDeserializeReader(t, "unsupported-media-type", []string{"application/json"}, errors.New("fail"))
}
func TestDeserializeReader_XMLDeserializationFailure_ReturnFailure(t *testing.T) {
assertDeserializeReader(t, "unsupported-media-type", []string{"application/xml"}, errors.New("fail"))
}
func TestDeserializeReader_KnownContentType_Success(t *testing.T) {
assertDeserializeReader(t, nil, []string{"application/json"}, nil)
}
func TestDeserializeReader_XMLKnownContentType_Success(t *testing.T) {
assertDeserializeReader(t, nil, []string{"application/xml"}, nil)
}
func TestDeserializeReader_KnownAdvancedContentType_Success(t *testing.T) {
assertDeserializeReader(t, nil, []string{"application/json; charset=utf-8"}, nil)
}
func TestDeserializeReader_XMLKnownAdvancedContentType_Success(t *testing.T) {
assertDeserializeReader(t, nil, []string{"application/xml; charset=utf-8"}, nil)
}
func assertDeserializeReader(t *testing.T, expectedResult any, contentTypes []string, deserializeError error) {
input := &FakeInputModel{}
request := httptest.NewRequest("GET", "/", nil)
request.Header["Content-Type"] = contentTypes
fakeResult := &FakeContentResult{}
deserializer := &FakeDeserializer{err: deserializeError}
factories := map[string]func() Deserializer{
"application/json": func() Deserializer { return deserializer },
"application/xml": func() Deserializer { return deserializer },
}
reader := newDeserializeReader(factories, "unsupported-media-type", fakeResult, &nopMonitor{})
result := reader.Read(input, request)
if result != "unsupported-media-type" {
Assert(t).That(request.Body).Equals(deserializer.source)
Assert(t).That(input).Equals(deserializer.target)
}
// Assert(t).That(result).Equals(expectedResult) // TODO
Assert(t).That(fakeResult.value).Equals(deserializeError)
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
func TestBindReader_NoErrors(t *testing.T) {
input := &FakeInputModel{}
request := httptest.NewRequest("GET", "/", nil)
result := newBindReader(nil, &nopMonitor{}).Read(input, request)
Assert(t).That(result).IsNil()
Assert(t).That(input.boundRequest == request).IsTrue()
}
func TestBindReader_Error(t *testing.T) {
input := &FakeInputModel{bindError: errors.New("bind error")}
request := httptest.NewRequest("GET", "/", nil)
fakeBindErrorResult := &FakeContentResult{}
result := newBindReader(fakeBindErrorResult, &nopMonitor{}).Read(input, request)
Assert(t).That(result).Equals(fakeBindErrorResult)
Assert(t).That(fakeBindErrorResult.value).Equals(input.bindError)
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
func TestValidateReader_NoErrors(t *testing.T) {
input := &FakeInputModel{}
result := newValidateReader(nil, 4, &nopMonitor{}).Read(input, nil)
Assert(t).That(result).IsNil()
}
func TestValidateReader_ErrorResult(t *testing.T) {
input := &FakeInputModel{
validationErrors: []error{errors.New("1"), errors.New("2")},
}
fakeValidationErrorsResult := &FakeContentResult{}
result := newValidateReader(fakeValidationErrorsResult, 4, &nopMonitor{}).Read(input, nil)
Assert(t).That(result).Equals(fakeValidationErrorsResult)
Assert(t).That(fakeValidationErrorsResult.value).Equals(input.validationErrors)
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
type FakeInputModel struct {
boundRequest *http.Request
bindError error
validationErrors []error
}
func (this *FakeInputModel) Reset() {}
func (this *FakeInputModel) Bind(request *http.Request) error {
this.boundRequest = request
return this.bindError
}
func (this *FakeInputModel) Validate(errs []error) int {
for i := range this.validationErrors {
errs[i] = this.validationErrors[i]
}
return len(this.validationErrors)
}
func (this *FakeInputModel) Body() any { return this }
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
type FakeDeserializer struct {
source io.Reader
target any
err error
}
func (this *FakeDeserializer) Deserialize(target any, source io.Reader) error {
this.target = target
this.source = source
return this.err
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
type FakeContentResult struct{ value any }
func (this *FakeContentResult) SetContent(value any) { this.value = value }
func (this *FakeContentResult) Result() any { return this }