Skip to content

Commit 67e3b70

Browse files
author
marcinromaszewicz
committed
Move test specs to embedded files
Test specs as strings are annoying to work with, use go:embed and move them to yaml files.
1 parent 9879d43 commit 67e3b70

File tree

2 files changed

+80
-77
lines changed

2 files changed

+80
-77
lines changed

oapi_validate_test.go

Lines changed: 6 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -16,95 +16,24 @@ package middleware
1616

1717
import (
1818
"context"
19+
_ "embed"
1920
"errors"
2021
"net/http"
2122
"net/http/httptest"
2223
"net/url"
2324
"testing"
2425

25-
"github.com/deepmap/oapi-codegen/pkg/testutil"
2626
"github.com/getkin/kin-openapi/openapi3"
2727
"github.com/getkin/kin-openapi/openapi3filter"
2828
"github.com/gin-gonic/gin"
2929
"github.com/stretchr/testify/assert"
3030
"github.com/stretchr/testify/require"
31+
32+
"github.com/deepmap/oapi-codegen/pkg/testutil"
3133
)
3234

33-
var testSchema = `openapi: "3.0.0"
34-
info:
35-
version: 1.0.0
36-
title: TestServer
37-
servers:
38-
- url: http://deepmap.ai/
39-
paths:
40-
/resource:
41-
get:
42-
operationId: getResource
43-
parameters:
44-
- name: id
45-
in: query
46-
schema:
47-
type: integer
48-
minimum: 10
49-
maximum: 100
50-
responses:
51-
'200':
52-
description: success
53-
content:
54-
application/json:
55-
schema:
56-
properties:
57-
name:
58-
type: string
59-
id:
60-
type: integer
61-
post:
62-
operationId: createResource
63-
responses:
64-
'204':
65-
description: No content
66-
requestBody:
67-
required: true
68-
content:
69-
application/json:
70-
schema:
71-
properties:
72-
name:
73-
type: string
74-
/protected_resource:
75-
get:
76-
operationId: getProtectedResource
77-
security:
78-
- BearerAuth:
79-
- someScope
80-
responses:
81-
'204':
82-
description: no content
83-
/protected_resource2:
84-
get:
85-
operationId: getProtectedResource
86-
security:
87-
- BearerAuth:
88-
- otherScope
89-
responses:
90-
'204':
91-
description: no content
92-
/protected_resource_401:
93-
get:
94-
operationId: getProtectedResource
95-
security:
96-
- BearerAuth:
97-
- unauthorized
98-
responses:
99-
'401':
100-
description: no content
101-
components:
102-
securitySchemes:
103-
BearerAuth:
104-
type: http
105-
scheme: bearer
106-
bearerFormat: JWT
107-
`
35+
//go:embed test_spec.yaml
36+
var testSchema []byte
10837

10938
func doGet(t *testing.T, handler http.Handler, rawURL string) *httptest.ResponseRecorder {
11039
u, err := url.Parse(rawURL)
@@ -127,7 +56,7 @@ func doPost(t *testing.T, handler http.Handler, rawURL string, jsonBody interfac
12756
}
12857

12958
func TestOapiRequestValidator(t *testing.T) {
130-
swagger, err := openapi3.NewLoader().LoadFromData([]byte(testSchema))
59+
swagger, err := openapi3.NewLoader().LoadFromData(testSchema)
13160
require.NoError(t, err, "Error initializing swagger")
13261

13362
// Create a new echo router

test_spec.yaml

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
openapi: "3.0.0"
2+
info:
3+
version: 1.0.0
4+
title: TestServer
5+
servers:
6+
- url: http://deepmap.ai/
7+
paths:
8+
/resource:
9+
get:
10+
operationId: getResource
11+
parameters:
12+
- name: id
13+
in: query
14+
schema:
15+
type: integer
16+
minimum: 10
17+
maximum: 100
18+
responses:
19+
'200':
20+
description: success
21+
content:
22+
application/json:
23+
schema:
24+
properties:
25+
name:
26+
type: string
27+
id:
28+
type: integer
29+
post:
30+
operationId: createResource
31+
responses:
32+
'204':
33+
description: No content
34+
requestBody:
35+
required: true
36+
content:
37+
application/json:
38+
schema:
39+
properties:
40+
name:
41+
type: string
42+
/protected_resource:
43+
get:
44+
operationId: getProtectedResource
45+
security:
46+
- BearerAuth:
47+
- someScope
48+
responses:
49+
'204':
50+
description: no content
51+
/protected_resource2:
52+
get:
53+
operationId: getProtectedResource
54+
security:
55+
- BearerAuth:
56+
- otherScope
57+
responses:
58+
'204':
59+
description: no content
60+
/protected_resource_401:
61+
get:
62+
operationId: getProtectedResource
63+
security:
64+
- BearerAuth:
65+
- unauthorized
66+
responses:
67+
'401':
68+
description: no content
69+
components:
70+
securitySchemes:
71+
BearerAuth:
72+
type: http
73+
scheme: bearer
74+
bearerFormat: JWT

0 commit comments

Comments
 (0)