-
Notifications
You must be signed in to change notification settings - Fork 5
/
testing_test.go
328 lines (303 loc) · 7.1 KB
/
testing_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
package oas
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
"testing"
"github.com/go-openapi/loads"
"github.com/go-openapi/strfmt"
"github.com/go-openapi/swag"
"github.com/go-openapi/validate"
)
func assertNoError(err error) {
if err != nil {
panic(err)
}
}
func mustWriteBadRequest(w http.ResponseWriter, contentType string, b []byte) {
w.Header().Set("Content-Type", contentType)
w.WriteHeader(http.StatusBadRequest)
_, err := w.Write(b)
assertNoError(err)
}
// problemHandlerResponseWriter returns a ProblemHandlerFunc that writes to the
// response.
func problemHandlerResponseWriter() ProblemHandlerFunc {
return func(problem Problem) {
var b []byte
var err error
switch te := problem.Cause().(type) {
case MultiError:
b, err = json.Marshal(convertErrs(te.Errors()...))
default:
b, err = json.Marshal(convertErrs(te))
}
assertNoError(err)
mustWriteBadRequest(
problem.ResponseWriter(),
"application/json",
b,
)
}
}
// problemHandlerResponseWriter returns a ProblemHandlerFunc that logs to the
// buffer.
func problemHandlerBufferLogger(buf *bytes.Buffer) ProblemHandlerFunc {
l := log.New(buf, "", 0)
return func(problem Problem) {
switch te := problem.Cause().(type) {
case MultiError:
for _, e := range convertErrs(te.Errors()...).Errors {
l.Printf("problem handler: %s: %s", te.Message(), buildErrMessage(e))
}
default:
l.Printf("problem handler: %s", te.Error())
}
}
}
// ---
type (
errorItem struct {
Message string `json:"message"`
HasField bool `json:"-"`
Field string `json:"field,omitempty"`
HasValue bool `json:"-"`
Value interface{} `json:"value,omitempty"`
}
payload struct {
Errors []errorItem `json:"errors"`
}
)
func convertErrs(errs ...error) payload {
// This is an example of composing an error for response from validation
// errors.
type fielder interface {
Field() string
}
type valuer interface {
Value() interface{}
}
p := payload{Errors: make([]errorItem, 0)}
for _, e := range errs {
item := errorItem{Message: e.Error()}
if fe, ok := e.(fielder); ok {
item.Field = fe.Field()
item.HasField = true
}
if ve, ok := e.(valuer); ok {
item.Value = ve.Value()
item.HasValue = true
}
p.Errors = append(p.Errors, item)
}
return p
}
func buildErrMessage(err errorItem) string {
msg := ""
if err.HasField {
msg += "field=" + err.Field + " "
}
if err.HasValue {
msg += fmt.Sprintf("value=%v", err.Value) + " "
}
msg += "message=" + err.Message
return msg
}
func loadDocFile(t *testing.T, fpath string) *Document {
doc, err := LoadFile(fpath)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
return doc
}
func loadDocBytes(b []byte) *Document {
yml, err := swag.BytesToYAMLDoc(b)
if err != nil {
log.Fatalf("failed to convert spec to yaml: %v", err)
}
jsn, err := swag.YAMLToJSON(yml)
if err != nil {
log.Fatalf("failed to convert yaml to json: %v", err)
}
doc, err := loads.Analyzed(jsn, "2.0")
if err != nil {
log.Fatalf("failed to analyze spec: %v", err)
}
doc, err = doc.Expanded()
if err != nil {
log.Fatalf("failed to expand spec: %v", err)
}
if err := validate.Spec(doc, strfmt.Default); err != nil {
log.Fatalf("failed to validate spec: %v", err)
}
return wrapDocument(doc)
}
var petstore = []byte(`
swagger: "2.0"
info:
description: "This is a sample server Petstore server."
version: "1.0.0"
title: "Swagger Petstore"
termsOfService: "http://swagger.io/terms/"
contact:
email: "apiteam@swagger.io"
license:
name: "Apache 2.0"
url: "http://www.apache.org/licenses/LICENSE-2.0.html"
host: "petstore.swagger.io"
basePath: "/v2"
tags:
- name: "pet"
description: "Everything about your Pets"
externalDocs:
description: "Find out more"
url: "http://swagger.io"
schemes:
- "http"
paths:
/pet:
post:
tags:
- "pet"
summary: "Add a new pet to the store"
operationId: "addPet"
consumes:
- "application/json"
produces:
- "application/json"
parameters:
- in: "body"
name: "body"
description: "Pet object that needs to be added to the store"
required: true
schema:
$ref: "#/definitions/Pet"
- in: query
name: debug
type: boolean
responses:
405:
description: "Invalid input"
security:
- petstore_auth:
- "write:pets"
- "read:pets"
/pet/{petId}:
get:
tags:
- "pet"
summary: "Find pet by ID"
description: "Returns a single pet"
operationId: "getPetById"
produces:
- "application/json"
parameters:
- in: query
name: debug
type: boolean
responses:
200:
description: "successful operation"
schema:
$ref: "#/definitions/Pet"
400:
description: "Invalid ID supplied"
404:
description: "Pet not found"
security:
- api_key: []
parameters:
- name: "petId"
in: "path"
description: "ID of pet to return"
required: true
type: "integer"
format: "int64"
/user/login:
get:
tags:
- "user"
summary: "Logs user into the system"
description: ""
operationId: "loginUser"
produces:
- "application/json"
parameters:
- name: "username"
in: "query"
description: "The user name for login"
required: true
type: "string"
- name: "password"
in: "query"
description: "The password for login in clear text"
required: true
type: "string"
responses:
200:
description: "successful operation"
schema:
type: "string"
headers:
X-Rate-Limit:
type: "integer"
format: "int32"
description: "calls per hour allowed by the user"
X-Expires-After:
type: "string"
format: "date-time"
description: "date in UTC when token expires"
400:
description: "Invalid username/password supplied"
securityDefinitions:
petstore_auth:
type: "oauth2"
authorizationUrl: "http://petstore.swagger.io/oauth/dialog"
flow: "implicit"
scopes:
write:pets: "modify pets in your account"
read:pets: "read your pets"
api_key:
type: "apiKey"
name: "api_key"
in: "header"
definitions:
Pet:
type: "object"
required:
- "name"
- "age"
properties:
id:
type: "integer"
format: "int64"
name:
type: "string"
example: "doggie"
age:
type: "integer"
format: "int32"
example: 7
status:
type: "string"
description: "pet status in the store"
enum:
- "available"
- "pending"
- "sold"
ApiResponse:
type: "object"
properties:
code:
type: "integer"
format: "int32"
type:
type: "string"
message:
type: "string"
externalDocs:
description: "Find out more about Swagger"
url: "http://swagger.io"
`)