Skip to content

Commit 9d0eda5

Browse files
authored
Add test for #112 (#209)
1 parent 6e85965 commit 9d0eda5

3 files changed

Lines changed: 53 additions & 14 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package openapi3_test
2+
3+
import (
4+
"strings"
5+
"testing"
6+
7+
"github.com/getkin/kin-openapi/openapi3"
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func TestPathsMustStartWithSlash(t *testing.T) {
12+
spec := `
13+
openapi: "3.0"
14+
info:
15+
version: "1.0"
16+
title: sample
17+
basePath: /adc/v1
18+
paths:
19+
PATH:
20+
get:
21+
responses:
22+
200:
23+
description: description
24+
`
25+
26+
for path, expectedErr := range map[string]string{
27+
"foo/bar": "Error when validating Paths: Path 'foo/bar' does not start with '/'",
28+
"/foo/bar": "",
29+
} {
30+
loader := openapi3.NewSwaggerLoader()
31+
doc, err := loader.LoadSwaggerFromData([]byte(strings.Replace(spec, "PATH", path, 1)))
32+
require.NoError(t, err)
33+
err = doc.Validate(loader.Context)
34+
if expectedErr != "" {
35+
require.EqualError(t, err, expectedErr)
36+
} else {
37+
require.NoError(t, err)
38+
}
39+
}
40+
}

openapi3/swagger_loader_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"net"
66
"net/http"
77
"net/http/httptest"
8-
98
"net/url"
109
"testing"
1110

@@ -490,6 +489,7 @@ paths:
490489
require.Equal(t, "getUserById", link.OperationID)
491490
require.Equal(t, "link to to the father", link.Description)
492491
}
492+
493493
func TestResolveNonComponentsRef(t *testing.T) {
494494
spec := []byte(`
495495
openapi: 3.0.0

openapi3filter/validation_test.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -593,9 +593,9 @@ func TestOperationOrSwaggerSecurity(t *testing.T) {
593593

594594
// Create the request
595595
emptyBody := bytes.NewReader(make([]byte, 0))
596-
pathUrl, err := url.Parse(path.name)
596+
pathURL, err := url.Parse(path.name)
597597
require.NoError(t, err)
598-
route, _, err := router.FindRoute(http.MethodGet, pathUrl)
598+
route, _, err := router.FindRoute(http.MethodGet, pathURL)
599599
require.NoError(t, err)
600600
req := openapi3filter.RequestValidationInput{
601601
Request: httptest.NewRequest(http.MethodGet, path.name, emptyBody),
@@ -605,15 +605,15 @@ func TestOperationOrSwaggerSecurity(t *testing.T) {
605605
if schemesValidated != nil {
606606
if validated, ok := (*schemesValidated)[input.SecurityScheme]; ok {
607607
if validated {
608-
t.Fatalf("The path \"%s\" had the schemes %v named \"%s\" validated more than once",
608+
t.Fatalf("The path %q had the schemes %v named %q validated more than once",
609609
path.name, input.SecurityScheme, input.SecuritySchemeName)
610610
}
611611
(*schemesValidated)[input.SecurityScheme] = true
612612
return nil
613613
}
614614
}
615615

616-
t.Fatalf("The path \"%s\" had the schemes %v named \"%s\"",
616+
t.Fatalf("The path %q had the schemes %v named %q",
617617
path.name, input.SecurityScheme, input.SecuritySchemeName)
618618

619619
return nil
@@ -717,9 +717,9 @@ func TestAnySecurityRequirementMet(t *testing.T) {
717717

718718
for _, tc := range tc {
719719
// Create the request input for the path
720-
tcUrl, err := url.Parse(tc.name)
720+
tcURL, err := url.Parse(tc.name)
721721
require.NoError(t, err)
722-
route, _, err := router.FindRoute(http.MethodGet, tcUrl)
722+
route, _, err := router.FindRoute(http.MethodGet, tcURL)
723723
require.NoError(t, err)
724724
req := openapi3filter.RequestValidationInput{
725725
Route: route,
@@ -733,9 +733,9 @@ func TestAnySecurityRequirementMet(t *testing.T) {
733733

734734
// If there should have been an error
735735
if tc.error {
736-
require.Errorf(t, err, "an error is expected for path \"%s\"", tc.name)
736+
require.Errorf(t, err, "an error is expected for path %q", tc.name)
737737
} else {
738-
require.NoErrorf(t, err, "an error wasn't expected for path \"%s\"", tc.name)
738+
require.NoErrorf(t, err, "an error wasn't expected for path %q", tc.name)
739739
}
740740
}
741741
}
@@ -817,9 +817,9 @@ func TestAllSchemesMet(t *testing.T) {
817817

818818
for _, tc := range tc {
819819
// Create the request input for the path
820-
tcUrl, err := url.Parse(tc.name)
820+
tcURL, err := url.Parse(tc.name)
821821
require.NoError(t, err)
822-
route, _, err := router.FindRoute(http.MethodGet, tcUrl)
822+
route, _, err := router.FindRoute(http.MethodGet, tcURL)
823823
require.NoError(t, err)
824824
req := openapi3filter.RequestValidationInput{
825825
Route: route,
@@ -855,9 +855,8 @@ func makeAuthFunc(schemes map[string]bool) func(c context.Context, input *openap
855855
if present {
856856
// Return an unmet scheme error
857857
return fmt.Errorf("security scheme for %q wasn't met", input.SecuritySchemeName)
858-
} else {
859-
// Return an unknown scheme error
860-
return fmt.Errorf("security scheme for %q is unknown", input.SecuritySchemeName)
861858
}
859+
// Return an unknown scheme error
860+
return fmt.Errorf("security scheme for %q is unknown", input.SecuritySchemeName)
862861
}
863862
}

0 commit comments

Comments
 (0)