forked from infobloxopen/atlas-app-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponse_test.go
237 lines (197 loc) · 7.02 KB
/
response_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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package gateway
import (
"context"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"testing"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes/wrappers"
"google.golang.org/grpc/metadata"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
)
type user struct {
Name string `json:"user"`
Age int `json:"age"`
}
type result struct {
Users []*user `json"users"`
}
type userWithPtr struct {
PtrValue *wrappers.Int64Value `json:"ptr_value"`
}
func (m *userWithPtr) Reset() {}
func (m *userWithPtr) ProtoMessage() {}
func (m *userWithPtr) String() string { return "" }
type userWithPtrResult struct {
Results *userWithPtr `json:"results"`
}
func (m *userWithPtrResult) Reset() {}
func (m *userWithPtrResult) ProtoMessage() {}
func (m *userWithPtrResult) String() string { return "" }
func (m *result) Reset() {}
func (m *result) ProtoMessage() {}
func (m *result) String() string { return "" }
type badresult struct {
Success []*user `json:"success"`
}
func (m *badresult) Reset() {}
func (m *badresult) ProtoMessage() {}
func (m *badresult) String() string { return "" }
type response struct {
Status *RestStatus `json:"success"`
Result []*user `json:"users"`
}
func TestForwardResponseMessage(t *testing.T) {
md := runtime.ServerMetadata{
HeaderMD: metadata.Pairs(
runtime.MetadataPrefix+"status-code", CodeName(Created),
runtime.MetadataPrefix+"status-message", "created 1 item",
),
}
ctx := runtime.NewServerMetadataContext(context.Background(), md)
rw := httptest.NewRecorder()
ForwardResponseMessage(ctx, nil, &runtime.JSONBuiltin{}, rw, nil, &result{Users: []*user{{"Poe", 209}, {"Hemingway", 119}}})
if rw.Code != http.StatusCreated {
t.Errorf("invalid http status code: %d - expected: %d", rw.Code, http.StatusCreated)
}
if ct := rw.Header().Get("Content-Type"); ct != "application/json" {
t.Errorf("invalid content-type: %s - expected: %s", ct, "application/json")
}
v := &response{}
if err := json.Unmarshal(rw.Body.Bytes(), v); err != nil {
t.Fatalf("failed to unmarshal JSON response: %s", err)
}
if v.Status.Code != CodeName(Created) {
t.Errorf("invalid status code: %s - expected: %s", v.Status.Code, CodeName(Created))
}
if v.Status.HTTPStatus != http.StatusCreated {
t.Errorf("invalid http status code: %d - expected: %d", v.Status.HTTPStatus, http.StatusCreated)
}
if v.Status.Message != "created 1 item" {
t.Errorf("invalid status message: %s - expected: %s", v.Status.Message, "created 1 item")
}
if l := len(v.Result); l != 2 {
t.Fatalf("invalid number of items in response result: %d - expected: %d", l, 2)
}
poe, hemingway := v.Result[0], v.Result[1]
if poe.Name != "Poe" || poe.Age != 209 {
t.Errorf("invalid result item: %+v - expected: %+v", poe, &user{"Poe", 209})
}
if hemingway.Name != "Hemingway" || hemingway.Age != 119 {
t.Errorf("invalid result item: %+v - expected: %+v", hemingway, &user{"Hemingway", 119})
}
}
func TestForwardResponseMessageWithNil(t *testing.T) {
ctx := runtime.NewServerMetadataContext(context.Background(), runtime.ServerMetadata{})
rw := httptest.NewRecorder()
ForwardResponseMessage(
ctx, nil, &runtime.JSONPb{OrigName: true, EmitDefaults: true}, rw, nil,
&userWithPtrResult{Results: &userWithPtr{PtrValue: nil}},
)
var v map[string]interface{}
if err := json.Unmarshal(rw.Body.Bytes(), &v); err != nil {
t.Fatalf("failed to unmarshal JSON response: %s", err)
}
if len(v["Results"].(map[string]interface{})) != 1 {
t.Errorf("invalid result item: %+v - expected %+v", v["Results"], map[string]interface{}{})
}
}
func TestForwardResponseMessageWithSuccessField(t *testing.T) {
ctx := runtime.NewServerMetadataContext(context.Background(), runtime.ServerMetadata{})
rw := httptest.NewRecorder()
ForwardResponseMessage(
ctx, nil, &runtime.JSONBuiltin{}, rw, nil,
&badresult{Success: []*user{{"Poe", 209}, {"Hemingway", 119}}},
)
var v map[string][]*user
if err := json.Unmarshal(rw.Body.Bytes(), &v); err != nil {
t.Fatalf("failed to unmarshal response: %s", err)
}
l, ok := v["success"]
if !ok {
t.Fatal("invalid response: missing 'success' field")
}
if len(l) != 2 {
t.Fatalf("invalid number of items in response: %d - expected: %d", len(l), 2)
}
if u := l[0]; u.Name != "Poe" || u.Age != 209 {
t.Errorf("invalid response item: %+v - expected: %+v", u, &user{"Poe", 209})
}
if u := l[1]; u.Name != "Hemingway" || u.Age != 119 {
t.Errorf("invalid response item: %+v - expected: %+v", u, &user{"Hemingway", 119})
}
}
func TestForwardResponseStream(t *testing.T) {
md := runtime.ServerMetadata{
HeaderMD: metadata.Pairs(
runtime.MetadataPrefix+"status-message", "returned 1 item",
),
}
ctx := runtime.NewServerMetadataContext(context.Background(), md)
rw := httptest.NewRecorder()
count := 0
items := []*result{
{[]*user{{"Poe", 209}}},
{[]*user{{"Hemingway", 119}}},
}
recv := func() (proto.Message, error) {
if count < len(items) {
i := items[count]
count++
return i, nil
}
return nil, io.EOF
}
ForwardResponseStream(ctx, nil, &runtime.JSONBuiltin{}, rw, nil, recv)
// if not set explicitly should be set by default
if rw.Code != http.StatusPartialContent {
t.Errorf("invalid http status code:%d - expected: %d", rw.Code, http.StatusPartialContent)
}
if ct := rw.Header().Get("Content-Type"); ct != "application/json" {
t.Errorf("invalid content-type: %s - expected: %s", ct, "application/json")
}
if te := rw.Header().Get("Transfer-Encoding"); te != "chunked" {
t.Errorf("invalid transfer-encoding: %s - expected: %s", te, "chunked")
}
dec := json.NewDecoder(rw.Body)
var sv map[string]*RestStatus
if err := dec.Decode(&sv); err != nil {
t.Fatalf("failed to unmarshal response status: %s", err)
}
if s, ok := sv["success"]; !ok {
t.Fatalf("invalid status response: %v (%v)", s, sv)
}
rst := sv["success"]
if rst.Code != CodeName(PartialContent) {
t.Errorf("invalid status code: %s - expected: %s", rst.Code, CodeName(PartialContent))
}
if rst.HTTPStatus != http.StatusPartialContent {
t.Errorf("invalid http status code: %d - expected: %d", rst.HTTPStatus, http.StatusPartialContent)
}
if rst.Message != "returned 1 item" {
t.Errorf("invalid status message: %s - expected: %s", rst.Message, "returned 1 item")
}
var rv *result
// test Poe
if err := dec.Decode(&rv); err != nil {
t.Fatalf("failed to unmarshal response chunked result: %s", err)
}
if len(rv.Users) != 1 {
t.Fatalf("invalid number of items in chuncked result: %d - expected: %d", len(rv.Users), 1)
}
if u := rv.Users[0]; u.Name != "Poe" || u.Age != 209 {
t.Errorf("invalid item from chuncked result: %+v - expected: %+v", u, &user{"Poe", 209})
}
// test Hemingway
if err := dec.Decode(&rv); err != nil {
t.Fatalf("failed to unmarshal response chunked result: %s", err)
}
if len(rv.Users) != 1 {
t.Fatalf("invalid number of items in chuncked result: %d - expected: %d", len(rv.Users), 1)
}
if u := rv.Users[0]; u.Name != "Hemingway" || u.Age != 119 {
t.Errorf("invalid item from chuncked result: %+v - expected: %+v", u, &user{"Hemingway", 119})
}
}