-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwriters_test.go
187 lines (162 loc) · 9.73 KB
/
writers_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
package shuttle
import (
"bytes"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestWrite(t *testing.T) {
assertions := []struct {
Input any
Accept string
HTTPResponse
}{
{Input: nil,
HTTPResponse: HTTPResponse{StatusCode: 204, ContentType: nil, Body: ""}},
{Input: "body",
HTTPResponse: HTTPResponse{StatusCode: 200, ContentType: []string{"text/plain; charset=utf-8"}, Body: "body"}},
{Input: []byte("body"),
HTTPResponse: HTTPResponse{StatusCode: 200, ContentType: []string{"text/plain; charset=utf-8"}, Body: "body"}},
{Input: true,
HTTPResponse: HTTPResponse{StatusCode: 200, ContentType: []string{"text/plain; charset=utf-8"}, Body: "true"}},
{Input: false,
HTTPResponse: HTTPResponse{StatusCode: 200, ContentType: []string{"text/plain; charset=utf-8"}, Body: "false"}},
{Input: &TextResult{StatusCode: 201, ContentType: "no-expected-content-type-for-empty-body", Content: ""},
HTTPResponse: HTTPResponse{StatusCode: 201, ContentType: nil, Body: ""}},
{Input: TextResult{StatusCode: 201, ContentType: "no-expected-content-type-for-empty-body", Content: ""},
HTTPResponse: HTTPResponse{StatusCode: 201, ContentType: nil, Body: ""}},
{Input: &TextResult{StatusCode: 0, ContentType: "", Content: "body"},
HTTPResponse: HTTPResponse{StatusCode: 200, ContentType: []string{"text/plain; charset=utf-8"}, Body: "body"}},
{Input: &TextResult{StatusCode: 201, ContentType: "application/custom", Content: "body"},
HTTPResponse: HTTPResponse{StatusCode: 201, ContentType: []string{"application/custom"}, Body: "body"}},
{Input: TextResult{StatusCode: 0, ContentType: "application/custom", Content: "body"},
HTTPResponse: HTTPResponse{StatusCode: 200, ContentType: []string{"application/custom"}, Body: "body"}},
{Input: TextResult{StatusCode: 202, ContentType: "", Content: "body"},
HTTPResponse: HTTPResponse{StatusCode: 202, ContentType: nil, Body: "body"}},
{Input: &BinaryResult{StatusCode: 404, ContentType: "no-expected-content-type-for-empty-body", Content: nil},
HTTPResponse: HTTPResponse{StatusCode: 404, ContentType: nil, Body: ""}},
{Input: BinaryResult{StatusCode: 404, ContentType: "no-expected-content-type-for-empty-body", ContentDisposition: "no-expected-content-disposition-for-empty-body", Content: nil},
HTTPResponse: HTTPResponse{StatusCode: 404, ContentType: nil, Body: ""}},
{Input: &BinaryResult{StatusCode: 404, ContentType: "custom-type", Content: []byte("body")},
HTTPResponse: HTTPResponse{StatusCode: 404, ContentType: []string{"custom-type"}, Body: "body"}},
{Input: BinaryResult{StatusCode: 404, ContentType: "", ContentDisposition: "custom-disposition", Content: []byte("body")},
HTTPResponse: HTTPResponse{StatusCode: 404, ContentType: nil, ContentDisposition: []string{"custom-disposition"}, Body: "body"}},
{Input: BinaryResult{StatusCode: 404, ContentType: "", Content: []byte("body")},
HTTPResponse: HTTPResponse{StatusCode: 404, ContentType: nil, Body: "body"}},
{Input: &StreamResult{StatusCode: 404, ContentType: "no-expected-content-type-for-empty-body", Content: nil},
HTTPResponse: HTTPResponse{StatusCode: 404, ContentType: nil, Body: ""}},
{Input: StreamResult{StatusCode: 404, ContentType: "no-expected-content-type-for-empty-body", Content: nil},
HTTPResponse: HTTPResponse{StatusCode: 404, ContentType: nil, Body: ""}},
{Input: StreamResult{StatusCode: 404, ContentType: "no-expected-content-type-for-empty-body", ContentDisposition: "no-expected-content-disposition-for-empty-body", Content: nil},
HTTPResponse: HTTPResponse{StatusCode: 404, ContentType: nil, ContentDisposition: nil, Body: ""}},
{Input: &StreamResult{StatusCode: 404, ContentType: "", Content: bytes.NewBufferString("body")},
HTTPResponse: HTTPResponse{StatusCode: 404, ContentType: nil, Body: "body"}},
{Input: StreamResult{StatusCode: 422, ContentType: "application/custom", Content: bytes.NewBufferString("body")},
HTTPResponse: HTTPResponse{StatusCode: 422, ContentType: []string{"application/custom"}, Body: "body"}},
{Input: StreamResult{StatusCode: 422, ContentType: "application/custom", ContentDisposition: "custom-disposition", Content: bytes.NewBufferString("body")},
HTTPResponse: HTTPResponse{StatusCode: 422, ContentType: []string{"application/custom"}, ContentDisposition: []string{"custom-disposition"}, Body: "body"}},
{Input: &SerializeResult{StatusCode: 401, ContentType: "no-expected-content-type-for-empty-body", Content: nil},
HTTPResponse: HTTPResponse{StatusCode: 401, ContentType: nil, Body: ""}},
{Input: SerializeResult{StatusCode: 401, ContentType: "no-expected-content-type-for-empty-body", Content: nil},
HTTPResponse: HTTPResponse{StatusCode: 401, ContentType: nil, Body: ""}},
{Input: &SerializeResult{StatusCode: 422, ContentType: "", Content: "body"},
Accept: "", // default serializer
HTTPResponse: HTTPResponse{StatusCode: 422, ContentType: []string{"application/json; charset=utf-8"}, Body: "{body}"}},
{Input: &SerializeResult{StatusCode: 422, ContentType: "application/override-default", Content: "body"},
Accept: "", // default serializer
HTTPResponse: HTTPResponse{StatusCode: 422, ContentType: []string{"application/override-default"}, Body: "{body}"}},
{Input: &SerializeResult{StatusCode: 422, ContentType: "", Content: "body"},
Accept: "application/xml", // serializer matching this Accept value
HTTPResponse: HTTPResponse{StatusCode: 422, ContentType: []string{"application/xml; charset=utf-8"}, Body: string(xmlPrefix) + "{body}"}},
{Input: &SerializeResult{StatusCode: 422, ContentType: "application/override-default", Content: "body"},
Accept: "application/xml", // serializer matching this Accept value
HTTPResponse: HTTPResponse{StatusCode: 422, ContentType: []string{"application/override-default"}, Body: string(xmlPrefix) + "{body}"}},
{Input: &SerializeResult{StatusCode: 422, ContentType: "", Content: "body"},
Accept: "application/not-acceptable", // use default serializer
HTTPResponse: HTTPResponse{StatusCode: 422, ContentType: []string{"application/json; charset=utf-8"}, Body: "{body}"}},
{Input: &SerializeResult{StatusCode: 200, ContentType: "", Content: "body"},
Accept: "application/xml;q=0.8", // simplify and use correct serializer
HTTPResponse: HTTPResponse{StatusCode: 200, ContentType: []string{"application/xml; charset=utf-8"}, Body: string(xmlPrefix) + "{body}"}},
{Input: 42, // use serializer for unknown type
HTTPResponse: HTTPResponse{StatusCode: 200, ContentType: []string{"application/json; charset=utf-8"}, Body: "{42}"}},
}
for _, assertion := range assertions {
response := recordResponse(assertion.Input, assertion.Accept)
assertResponse(t, response, assertion.HTTPResponse)
}
}
func recordResponse(result any, acceptHeader string) *httptest.ResponseRecorder {
writer := newTestWriter()
response := httptest.NewRecorder()
request := httptest.NewRequest("GET", "/", nil)
if len(acceptHeader) > 0 {
request.Header["Accept"] = []string{acceptHeader}
}
writer.Write(response, request, result)
return response
}
func newTestWriter() Writer {
return newWriter(map[string]func() Serializer{
emptyContentType: func() Serializer { return newFakeWriteSerializer("application/json; charset=utf-8") },
"application/xml": func() Serializer { return newFakeWriteSerializer("application/xml; charset=utf-8") },
}, &nopMonitor{})
}
func assertResponse(t *testing.T, response *httptest.ResponseRecorder, expected HTTPResponse) {
Assert(t).That(response.Code).Equals(expected.StatusCode)
Assert(t).That(response.Header()["Content-Type"]).Equals(expected.ContentType)
Assert(t).That(response.Header()["Content-Disposition"]).Equals(expected.ContentDisposition)
Assert(t).That(response.Body.String()).Equals(expected.Body)
}
func TestWriteHTTPHandler(t *testing.T) {
handler := &FakeHTTPHandlerResult{}
response := httptest.NewRecorder()
request := httptest.NewRequest("GET", "/", nil)
newTestWriter().Write(response, request, handler)
Assert(t).That(handler.response == response).IsTrue()
Assert(t).That(handler.request == request).IsTrue()
}
func TestSerializeResultWriteHTTPHeaders(t *testing.T) {
headers := map[string][]string{
"Header-1": {"value1-a", "value1-b"},
"Header-2": {"value2-a", "value2-b"},
}
assertHTTPHeaders(t, SerializeResult{Headers: headers})
assertHTTPHeaders(t, TextResult{Headers: headers})
assertHTTPHeaders(t, BinaryResult{Headers: headers})
assertHTTPHeaders(t, StreamResult{Headers: headers})
}
func assertHTTPHeaders(t *testing.T, result any) {
response := recordResponse(result, "application/json")
headers := response.Header()
Assert(t).That(headers["Header-1"]).Equals([]string{"value1-a", "value1-b"})
Assert(t).That(headers["Header-2"]).Equals([]string{"value2-a", "value2-b"})
}
type HTTPResponse struct {
StatusCode int
ContentType []string
ContentDisposition []string
Body string
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
type FakeHTTPHandlerResult struct {
response http.ResponseWriter
request *http.Request
}
func (this *FakeHTTPHandlerResult) ServeHTTP(response http.ResponseWriter, request *http.Request) {
this.response = response
this.request = request
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
type FakeWriteSerializer string
func newFakeWriteSerializer(contentType string) Serializer {
return FakeWriteSerializer(contentType)
}
func (this FakeWriteSerializer) ContentType() string { return string(this) }
func (this FakeWriteSerializer) Serialize(writer io.Writer, value any) error {
raw, _ := json.Marshal(value)
_, _ = io.WriteString(writer, "{"+strings.ReplaceAll(string(raw), `"`, ``)+"}")
return nil
}