Repository navigation
Expand file tree
/
Copy pathoapi_validate_test.go
More file actions
261 lines (228 loc) · 7.07 KB
/
Copy pathoapi_validate_test.go
File metadata and controls
261 lines (228 loc) · 7.07 KB
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
package nethttpmiddleware
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"net/http/httptest"
"reflect"
"testing"
"github.com/getkin/kin-openapi/openapi3"
"github.com/getkin/kin-openapi/openapi3filter"
)
func Test_determineStatusCodeForMultiError(t *testing.T) {
t.Run("returns HTTP 400 Bad Request when only `RequestError`s", func(t *testing.T) {
errs := []error{
&openapi3filter.RequestError{},
&openapi3filter.RequestError{},
}
expected := 400
actual := determineStatusCodeForMultiError(errs)
if expected != actual {
t.Errorf("Expected an HTTP %d to be returned, but received %d", expected, actual)
}
})
t.Run("returns HTTP 401 Unauthorized when only `SecurityRequirementsError`s", func(t *testing.T) {
errs := []error{
&openapi3filter.SecurityRequirementsError{},
&openapi3filter.SecurityRequirementsError{},
}
expected := 401
actual := determineStatusCodeForMultiError(errs)
if expected != actual {
t.Errorf("Expected an HTTP %d to be returned, but received %d", expected, actual)
}
})
t.Run("returns HTTP 500 Internal Server Error when mixed error types", func(t *testing.T) {
errs := []error{
&openapi3filter.RequestError{},
&openapi3filter.SecurityRequirementsError{},
}
expected := 500
actual := determineStatusCodeForMultiError(errs)
if expected != actual {
t.Errorf("Expected an HTTP %d to be returned, but received %d", expected, actual)
}
})
t.Run("returns HTTP 500 Internal Server Error when unknown error type(s) are seen", func(t *testing.T) {
errs := []error{
fmt.Errorf("this isn't a known error type"),
}
expected := 500
actual := determineStatusCodeForMultiError(errs)
if expected != actual {
t.Errorf("Expected an HTTP %d to be returned, but received %d", expected, actual)
}
})
}
const validatedValueSpec = `
openapi: "3.0.0"
info:
version: 1.0.0
title: TestServer
paths:
/resource:
post:
operationId: createResource
responses:
'204':
description: No content
requestBody:
required: true
content:
application/json:
schema:
properties:
name:
type: string
`
// validatedValuer is the interface openapi3filter gives the request body when
// openapi3filter.Options.KeepValidatedValue is set.
type validatedValuer interface {
ValidatedValue() any
}
// bodySeen records what a handler behind the middleware saw on r.Body.
type bodySeen struct {
called bool
hasValue bool
validatedValue any
body []byte
}
// postThroughValidator sends a POST with body through the middleware built
// from options (nil means OapiRequestValidator) and records the r.Body the
// handler received.
func postThroughValidator(t *testing.T, options *Options, body string) (*httptest.ResponseRecorder, bodySeen) {
t.Helper()
spec, err := openapi3.NewLoader().LoadFromData([]byte(validatedValueSpec))
if err != nil {
t.Fatalf("failed to load spec: %v", err)
}
var seen bodySeen
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
seen.called = true
if vv, ok := r.Body.(validatedValuer); ok {
seen.hasValue = true
seen.validatedValue = vv.ValidatedValue()
}
data, err := io.ReadAll(r.Body)
if err != nil {
t.Errorf("failed to read request body in handler: %v", err)
}
seen.body = data
w.WriteHeader(http.StatusNoContent)
})
var mw func(http.Handler) http.Handler
if options == nil {
mw = OapiRequestValidator(spec)
} else {
mw = OapiRequestValidatorWithOptions(spec, options)
}
req := httptest.NewRequest(http.MethodPost, "/resource", bytes.NewReader([]byte(body)))
req.Header.Set("Content-Type", "application/json")
rec := httptest.NewRecorder()
mw(handler).ServeHTTP(rec, req)
return rec, seen
}
func TestKeepValidatedValue(t *testing.T) {
const body = `{"name":"Wilbur"}`
expectedValue := map[string]any{"name": "Wilbur"}
keptCases := []struct {
name string
options *Options
}{
{
name: "OapiRequestValidator without options",
options: nil,
},
{
name: "empty Options",
options: &Options{},
},
{
name: "ErrorHandlerWithOpts",
options: &Options{
ErrorHandlerWithOpts: func(_ context.Context, err error, w http.ResponseWriter, _ *http.Request, opts ErrorHandlerOpts) {
http.Error(w, err.Error(), opts.StatusCode)
},
},
},
}
for _, tc := range keptCases {
t.Run("keeps the validated value with "+tc.name, func(t *testing.T) {
rec, seen := postThroughValidator(t, tc.options, body)
if rec.Code != http.StatusNoContent {
t.Fatalf("expected HTTP %d, got %d: %s", http.StatusNoContent, rec.Code, rec.Body.String())
}
if !seen.called {
t.Fatal("handler was not called")
}
if !seen.hasValue {
t.Fatalf("expected r.Body (%T) to implement ValidatedValue() any", seen.validatedValue)
}
if !reflect.DeepEqual(expectedValue, seen.validatedValue) {
t.Errorf("expected validated value %#v, got %#v", expectedValue, seen.validatedValue)
}
if string(seen.body) != body {
t.Errorf("expected the body to still read %q, got %q", body, string(seen.body))
}
if tc.options != nil && tc.options.Options.KeepValidatedValue {
t.Error("the caller's Options.Options.KeepValidatedValue was modified")
}
})
}
t.Run("DiscardValidatedValue drops the validated value", func(t *testing.T) {
options := &Options{
DiscardValidatedValue: true,
Options: openapi3filter.Options{
// overridden by DiscardValidatedValue
KeepValidatedValue: true,
},
}
rec, seen := postThroughValidator(t, options, body)
if rec.Code != http.StatusNoContent {
t.Fatalf("expected HTTP %d, got %d: %s", http.StatusNoContent, rec.Code, rec.Body.String())
}
if !seen.called {
t.Fatal("handler was not called")
}
if seen.hasValue {
t.Error("expected r.Body not to implement ValidatedValue() any")
}
if string(seen.body) != body {
t.Errorf("expected the body to still read %q, got %q", body, string(seen.body))
}
if !options.Options.KeepValidatedValue {
t.Error("the caller's Options.Options.KeepValidatedValue was modified")
}
})
t.Run("ExcludeRequestBody still passes the body through", func(t *testing.T) {
options := &Options{
Options: openapi3filter.Options{
ExcludeRequestBody: true,
},
}
rec, seen := postThroughValidator(t, options, body)
if rec.Code != http.StatusNoContent {
t.Fatalf("expected HTTP %d, got %d: %s", http.StatusNoContent, rec.Code, rec.Body.String())
}
if !seen.called {
t.Fatal("handler was not called")
}
if seen.hasValue {
t.Error("expected r.Body not to implement ValidatedValue() any when the body is not validated")
}
if string(seen.body) != body {
t.Errorf("expected the body to still read %q, got %q", body, string(seen.body))
}
})
t.Run("an invalid body is still rejected", func(t *testing.T) {
rec, seen := postThroughValidator(t, nil, `{"name":1}`)
if rec.Code != http.StatusBadRequest {
t.Fatalf("expected HTTP %d, got %d: %s", http.StatusBadRequest, rec.Code, rec.Body.String())
}
if seen.called {
t.Error("handler should not have been called")
}
})
}