Skip to content

Commit 9afe214

Browse files
committed
reproduce #267
Signed-off-by: Pierre Fenoll <[email protected]>
1 parent 582e6d0 commit 9afe214

File tree

1 file changed

+156
-0
lines changed

1 file changed

+156
-0
lines changed

openapi3filter/issue267_test.go

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
package openapi3filter
2+
3+
import (
4+
"net/http"
5+
"strings"
6+
"testing"
7+
8+
"github.com/stretchr/testify/require"
9+
10+
"github.com/getkin/kin-openapi/openapi3"
11+
"github.com/getkin/kin-openapi/routers/gorillamux"
12+
)
13+
14+
func TestIssue267(t *testing.T) {
15+
spec := `
16+
openapi: 3.0.0
17+
info:
18+
description: This is a sample of the API
19+
version: 1.0.0
20+
title: sample API
21+
tags:
22+
- name: authorization
23+
description: Create and validate authorization tokens using oauth
24+
paths:
25+
/oauth2/token:
26+
post:
27+
tags:
28+
- authorization
29+
requestBody:
30+
content:
31+
application/json:
32+
schema:
33+
$ref: '#/components/schemas/AccessTokenRequest'
34+
examples:
35+
ClientCredentialsTokenRequest:
36+
$ref: '#/components/examples/ClientCredentialsTokenRequest'
37+
RefreshTokenRequest:
38+
$ref: '#/components/examples/RefreshTokenRequest'
39+
application/x-www-form-urlencoded:
40+
schema:
41+
$ref: '#/components/schemas/AccessTokenRequest'
42+
examples:
43+
ClientCredentialsTokenRequest:
44+
$ref: '#/components/examples/ClientCredentialsTokenRequest'
45+
RefreshTokenRequest:
46+
$ref: '#/components/examples/RefreshTokenRequest'
47+
responses:
48+
'200':
49+
description: 'The request was successful and a token was issued.'
50+
51+
components:
52+
examples:
53+
ClientCredentialsTokenRequest:
54+
value:
55+
grant_type: client_credentials
56+
scope: 'member:read member:write'
57+
RefreshTokenRequest:
58+
value:
59+
grant_type: refresh_token
60+
client_id: '3fa85f64-5717-4562-b3fc-2c963f66afa6'
61+
refresh_token: '2fbd6ad96acc4fa99ef36a3e803b010b'
62+
schemas:
63+
AccessTokenRequest:
64+
description: 'Describes all of the potential access token requests that can be received'
65+
type: object
66+
oneOf:
67+
- $ref: '#/components/schemas/ClientCredentialsTokenRequest'
68+
- $ref: '#/components/schemas/RefreshTokenRequest'
69+
ClientCredentialsTokenRequest:
70+
description: 'The client_id and client_secret properties should only be sent in form data if the client does not support basic authentication for sending client credentials.'
71+
properties:
72+
grant_type:
73+
type: string
74+
enum:
75+
- client_credentials
76+
example: 'client_credentials'
77+
scope:
78+
description: 'A space separated list of scopes requested for the token'
79+
type: string
80+
example: 'member:read member:write'
81+
client_id:
82+
description: 'The ID provided when the client application was registered'
83+
type: string
84+
example: '3fa85f64-5717-4562-b3fc-2c963f66afa6'
85+
client_secret:
86+
description: 'A secret code that would be setup for the client to exchange for an access token.'
87+
type: string
88+
example: 'fac663c0-e8b5-4c02-9ad3-ddbd1bbc6964'
89+
required:
90+
- grant_type
91+
- scope
92+
RefreshTokenRequest:
93+
type: object
94+
properties:
95+
grant_type:
96+
type: string
97+
enum:
98+
- refresh_token
99+
example: 'refresh_token'
100+
client_id:
101+
description: 'The ID provided when the client application was registered'
102+
type: string
103+
example: '3fa85f64-5717-4562-b3fc-2c963f66afa6'
104+
refresh_token:
105+
description: 'A long lived one time use token that is issued only in cases where the client can be offline or restarted and where the authorization should persist.'
106+
type: string
107+
minLength: 32
108+
example: '2fbd6ad96acc4fa99ef36a3e803b010b'
109+
required:
110+
- grant_type
111+
- client_id
112+
- refresh_token
113+
`[1:]
114+
115+
loader := openapi3.NewLoader()
116+
117+
doc, err := loader.LoadFromData([]byte(spec))
118+
require.NoError(t, err)
119+
120+
err = doc.Validate(loader.Context)
121+
require.NoError(t, err)
122+
123+
router, err := gorillamux.NewRouter(doc)
124+
require.NoError(t, err)
125+
126+
for _, testcase := range []struct {
127+
ct, data string
128+
}{
129+
{
130+
ct: "application/json",
131+
data: `{"grant_type":"client_credentials", "scope":"testscope", "client_id":"myclient", "client_secret":"mypass"}`,
132+
},
133+
{
134+
ct: "application/x-www-form-urlencoded",
135+
data: "grant_type=client_credentials&scope=testscope&client_id=myclient&client_secret=mypass",
136+
},
137+
} {
138+
t.Run(testcase.ct, func(t *testing.T) {
139+
data := strings.NewReader(testcase.data)
140+
req, err := http.NewRequest("POST", "/oauth2/token", data)
141+
require.NoError(t, err)
142+
req.Header.Add("Content-Type", testcase.ct)
143+
144+
route, pathParams, err := router.FindRoute(req)
145+
require.NoError(t, err)
146+
147+
validationInput := &RequestValidationInput{
148+
Request: req,
149+
PathParams: pathParams,
150+
Route: route,
151+
}
152+
err = ValidateRequest(loader.Context, validationInput)
153+
require.NoError(t, err)
154+
})
155+
}
156+
}

0 commit comments

Comments
 (0)