Skip to content

Commit a0bc170

Browse files
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 88623b8 commit a0bc170

3 files changed

Lines changed: 81 additions & 78 deletions

File tree

oapi_validate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ type Options struct {
7171
Skipper echomiddleware.Skipper
7272
}
7373

74-
// Create a validator from a swagger object, with validation options
74+
// OapiRequestValidatorWithOptions creates a validator from a swagger object, with validation options
7575
func OapiRequestValidatorWithOptions(swagger *openapi3.T, options *Options) echo.MiddlewareFunc {
7676
router, err := gorillamux.NewRouter(swagger)
7777
if err != nil {

oapi_validate_test.go

Lines changed: 6 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -16,96 +16,25 @@ 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/labstack/echo/v4"
2929
echomiddleware "github.com/labstack/echo/v4/middleware"
3030
"github.com/stretchr/testify/assert"
3131
"github.com/stretchr/testify/require"
32+
33+
"github.com/deepmap/oapi-codegen/pkg/testutil"
3234
)
3335

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

11039
func doGet(t *testing.T, e *echo.Echo, rawURL string) *httptest.ResponseRecorder {
11140
u, err := url.Parse(rawURL)
@@ -128,7 +57,7 @@ func doPost(t *testing.T, e *echo.Echo, rawURL string, jsonBody interface{}) *ht
12857
}
12958

13059
func TestOapiRequestValidator(t *testing.T) {
131-
swagger, err := openapi3.NewLoader().LoadFromData([]byte(testSchema))
60+
swagger, err := openapi3.NewLoader().LoadFromData(testSchema)
13261
require.NoError(t, err, "Error initializing swagger")
13362

13463
// 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)