-
-
Notifications
You must be signed in to change notification settings - Fork 564
/
Copy pathhttp_response_test.go
332 lines (310 loc) Β· 8.42 KB
/
http_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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package expr_test
import (
"testing"
. "goa.design/goa/v3/dsl"
"goa.design/goa/v3/expr"
)
func TestHTTPResponseValidation(t *testing.T) {
cases := []struct {
Name string
DSL func()
Error string
}{
{"empty", emptyResultEmptyResponseDSL, ""},
{"non empty result", nonEmptyResultEmptyResponseDSL, ""},
{"non empty response", emptyResultNonEmptyResponseDSL, ""},
{"header string result", stringResultResponseWithHeadersDSL, ""},
{"cookie string result", stringResultResponseWithCookiesDSL, ""},
{"string result text encoding", stringResultResponseWithTextContentTypeDSL, ""},
{"header object result", objectResultResponseWithHeadersDSL, ""},
{"cookie object result", objectResultResponseWithCookiesDSL, ""},
{"header array result", arrayResultResponseWithHeadersDSL, ""},
{"cookie array result", arrayResultResponseWithCookiesDSL, `service "ArrayResultResponseWithCookies" HTTP endpoint "Method": attribute "foo" used in HTTP cookies must be a primitive type.`},
{"header map result", mapResultResponseWithHeadersDSL, `service "MapResultResponseWithHeaders" HTTP endpoint "Method": attribute "foo" used in HTTP headers must be a primitive type or an array of primitive types.`},
{"cookie map result", mapResultResponseWithCookiesDSL, `service "MapResultResponseWithCookies" HTTP endpoint "Method": attribute "foo" used in HTTP cookies must be a primitive type.`},
{"invalid", emptyResultResponseWithHeadersDSL, `HTTP response of service "EmptyResultResponseWithHeaders" HTTP endpoint "Method": response defines headers but result is empty`},
{"implicit object in header", implicitObjectResultResponseWithHeadersDSL, `service "ArrayObjectResultResponseWithHeaders" HTTP endpoint "Method": attribute "foo" used in HTTP headers must be a primitive type or an array of primitive types.`},
{"array of object in header", arrayObjectResultResponseWithHeadersDSL, `service "ArrayObjectResultResponseWithHeaders" HTTP endpoint "Method": Array result is mapped to an HTTP header but is not an array of primitive types.`},
{"not string or []byte", intResultResponseWithTextContentTypeDSL, `HTTP response of service "StringResultResponseWithHeaders" HTTP endpoint "Method": Result type must be String or Bytes when ContentType is 'text/plain'`},
{"missing header result attribute", missingHeaderResultAttributeDSL, `HTTP response of service "MissingHeaderResultAttribute" HTTP endpoint "Method": header "bar" has no equivalent attribute in result type, use notation 'attribute_name:header_name' to identify corresponding result type attribute.`},
{"missing cookie result attribute", missingCookieResultAttributeDSL, `HTTP response of service "MissingCookieResultAttribute" HTTP endpoint "Method": cookie "bar" has no equivalent attribute in result type, use notation 'attribute_name:cookie_name' to identify corresponding result type attribute.
service "MissingCookieResultAttribute" HTTP endpoint "Method": attribute "bar" used in HTTP cookies must be a primitive type.`},
{"skip encode and gRPC", skipEncodeAndGRPCDSL, `service "SkipEncodeAndGRPC" HTTP endpoint "Method": Endpoint response cannot use SkipResponseBodyEncodeDecode and define a gRPC transport.`},
}
for _, c := range cases {
t.Run(c.Name, func(t *testing.T) {
if c.Error == "" {
expr.RunDSL(t, c.DSL)
} else {
err := expr.RunInvalidDSL(t, c.DSL)
if err.Error() != c.Error {
t.Errorf("got error %q, expected %q", err.Error(), c.Error)
}
}
})
}
}
var emptyResultEmptyResponseDSL = func() {
Service("EmptyResultEmptyResponse", func() {
Method("Method", func() {
HTTP(func() {
POST("/")
})
})
})
}
var nonEmptyResultEmptyResponseDSL = func() {
Service("NonEmptyResultEmptyResponse", func() {
Method("Method", func() {
Result(String)
HTTP(func() {
POST("/")
})
})
})
}
var emptyResultNonEmptyResponseDSL = func() {
Service("EmptyResultNonEmptyResponse", func() {
Method("Method", func() {
HTTP(func() {
POST("/")
Response(StatusOK)
})
})
})
}
var stringResultResponseWithHeadersDSL = func() {
Service("StringResultResponseWithHeaders", func() {
Method("Method", func() {
Result(String)
HTTP(func() {
POST("/")
Response(func() {
Header("Location")
})
})
})
})
}
var stringResultResponseWithCookiesDSL = func() {
Service("StringResultResponseWithCookies", func() {
Method("Method", func() {
Result(String)
HTTP(func() {
POST("/")
Response(func() {
Cookie("Location")
})
})
})
})
}
var stringResultResponseWithTextContentTypeDSL = func() {
Service("StringResultResponseWithHeaders", func() {
Method("Method", func() {
Result(String)
HTTP(func() {
POST("/")
Response(func() {
ContentType("text/plain")
})
})
})
})
}
var objectResultResponseWithHeadersDSL = func() {
Service("ObjectResultResponseWithHeaders", func() {
Method("Method", func() {
Result(func() {
Attribute("foo", String)
})
HTTP(func() {
POST("/")
Response(func() {
Header("foo:Location")
})
})
})
})
}
var objectResultResponseWithCookiesDSL = func() {
Service("ObjectResultResponseWithCookies", func() {
Method("Method", func() {
Result(func() {
Attribute("foo", String)
})
HTTP(func() {
POST("/")
Response(func() {
Cookie("foo:Location")
})
})
})
})
}
var implicitObjectResultResponseWithHeadersDSL = func() {
Service("ArrayObjectResultResponseWithHeaders", func() {
Method("Method", func() {
Result(func() {
Attribute("foo", func() {
Attribute("bar", String)
Attribute("baz", String)
})
})
HTTP(func() {
POST("/")
Response(func() {
Header("foo:Location")
})
})
})
})
}
var arrayObjectResultResponseWithHeadersDSL = func() {
var Obj = Type("Obj", func() {
Attribute("foo", String)
})
Service("ArrayObjectResultResponseWithHeaders", func() {
Method("Method", func() {
Result(ArrayOf(Obj))
HTTP(func() {
POST("/")
Response(func() {
Header("foo:Location")
})
})
})
})
}
var arrayResultResponseWithHeadersDSL = func() {
Service("ArrayResultResponseWithHeaders", func() {
Method("Method", func() {
Result(func() {
Attribute("foo", ArrayOf(String))
})
HTTP(func() {
POST("/")
Response(func() {
Header("foo:Location")
})
})
})
})
}
var arrayResultResponseWithCookiesDSL = func() {
Service("ArrayResultResponseWithCookies", func() {
Method("Method", func() {
Result(func() {
Attribute("foo", ArrayOf(String))
})
HTTP(func() {
POST("/")
Response(func() {
Cookie("foo:Location")
})
})
})
})
}
var mapResultResponseWithHeadersDSL = func() {
Service("MapResultResponseWithHeaders", func() {
Method("Method", func() {
Result(func() {
Attribute("foo", MapOf(String, String))
})
HTTP(func() {
POST("/")
Response(func() {
Header("foo:Location")
})
})
})
})
}
var mapResultResponseWithCookiesDSL = func() {
Service("MapResultResponseWithCookies", func() {
Method("Method", func() {
Result(func() {
Attribute("foo", MapOf(String, String))
})
HTTP(func() {
POST("/")
Response(func() {
Cookie("foo:Location")
})
})
})
})
}
var emptyResultResponseWithHeadersDSL = func() {
Service("EmptyResultResponseWithHeaders", func() {
Method("Method", func() {
HTTP(func() {
POST("/")
Response(func() {
Header("foo:Location")
})
})
})
})
}
var intResultResponseWithTextContentTypeDSL = func() {
Service("StringResultResponseWithHeaders", func() {
Method("Method", func() {
Result(Int)
HTTP(func() {
POST("/")
Response(func() {
ContentType("text/plain")
})
})
})
})
}
var missingHeaderResultAttributeDSL = func() {
Service("MissingHeaderResultAttribute", func() {
Method("Method", func() {
Result(func() {
Attribute("foo")
})
HTTP(func() {
POST("/")
Response(func() {
Header("bar")
})
})
})
})
}
var missingCookieResultAttributeDSL = func() {
Service("MissingCookieResultAttribute", func() {
Method("Method", func() {
Result(func() {
Attribute("foo")
})
HTTP(func() {
POST("/")
Response(func() {
Cookie("bar")
})
})
})
})
}
var skipEncodeAndGRPCDSL = func() {
Service("SkipEncodeAndGRPC", func() {
Method("Method", func() {
Result(func() {
Field(1, "foo")
})
HTTP(func() {
POST("/")
SkipResponseBodyEncodeDecode()
Response(func() {
Header("foo")
})
})
GRPC(func() {})
})
})
}