-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest_body_test.go
187 lines (175 loc) · 4.52 KB
/
request_body_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 openapi_test
import (
"testing"
"github.com/MarkRosemaker/openapi"
)
func TestRequestBody_JSON(t *testing.T) {
t.Parallel()
// A request body with a referenced model definition.
testJSON(t, []byte(`{
"description": "user to add to the system",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/User"
},
"examples": {
"user" : {
"summary": "User Example",
"externalValue": "https://foo.bar/examples/user-example.json"
}
}
},
"application/xml": {
"schema": {
"$ref": "#/components/schemas/User"
},
"examples": {
"user" : {
"summary": "User example in XML",
"externalValue": "https://foo.bar/examples/user-example.xml"
}
}
},
"text/plain": {
"examples": {
"user" : {
"summary": "User example in Plain text",
"externalValue": "https://foo.bar/examples/user-example.txt"
}
}
},
"*/*": {
"examples": {
"user" : {
"summary": "User example in other format",
"externalValue": "https://foo.bar/examples/user-example.whatever"
}
}
}
}
}`), &openapi.RequestBody{})
// A body parameter that is an array of string values:
testJSON(t, []byte(`{
"description": "user to add to the system",
"required": true,
"content": {
"text/plain": {
"schema": {
"type": "array",
"items": {
"type": "string"
}
}
}
}
}`), &openapi.RequestBody{})
// A `requestBody` for submitting a file in a `POST` operation may look like the following example:
testJSON(t, []byte(`{
"content": {
"application/octet-stream": {}
}
}`), &openapi.RequestBody{})
// In addition, specific media types MAY be specified:
testJSON(t, []byte(`{
"content": {
"image/jpeg": {},
"image/png": {}
}
}`), &openapi.RequestBody{})
// To upload multiple files, a `multipart` media type MUST be used:
testJSON(t, []byte(`{
"content": {
"multipart/form-data": {
"schema": {
"type": "object",
"properties": {
"file": {
"type": "array",
"items": {}
}
}
}
}
}
}`), &openapi.RequestBody{})
// To submit content using form url encoding via RFC1866, the following definition may be used:
testJSON(t, []byte(`{
"content": {
"application/x-www-form-urlencoded": {
"schema": {
"type": "object",
"properties": {
"id": {
"type": "string",
"format": "uuid"
},
"address": {
"type": "object",
"properties": {}
}
}
}
}
}
}`), &openapi.RequestBody{})
// `contentMediaType` and `contentEncoding`
testJSON(t, []byte(`{
"content": {
"multipart/form-data": {
"schema": {
"type": "object",
"properties": {
"id": {
"type": "string",
"format": "uuid"
},
"address": {
"type": "object",
"properties": {}
},
"profileImage": {
"type": "string",
"contentMediaType": "image/png",
"contentEncoding": "base64"
},
"children": {
"type": "array",
"items": {
"type": "string"
}
},
"addresses": {
"type": "array",
"items": {
"$ref": "#/components/schemas/Address"
}
}
}
}
}
}
}`), &openapi.RequestBody{}) // TODO in the spec, it has type: object in "items" besides the "$ref"
}
func TestRequestBody_Validate_Error(t *testing.T) {
t.Parallel()
for _, tc := range []struct {
p openapi.RequestBody
err string
}{
{openapi.RequestBody{}, "content is required"},
{openapi.RequestBody{
Content: openapi.Content{"foo; bar": &openapi.MediaType{}},
}, `content["foo; bar"]: mime: invalid media parameter`},
{openapi.RequestBody{
Content: openapi.Content{"application/json": &openapi.MediaType{}},
Extensions: []byte(`{"foo": "bar"}`),
}, "foo: " + openapi.ErrUnknownField.Error()},
} {
t.Run(tc.err, func(t *testing.T) {
if err := tc.p.Validate(); err == nil || err.Error() != tc.err {
t.Fatalf("expected %q, got %q", tc.err, err)
}
})
}
}