Skip to content

Commit

Permalink
Fixup openAPI validation test
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcel Ludwig committed Dec 2, 2020
1 parent 7c2e4a8 commit dc41a8d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
27 changes: 24 additions & 3 deletions handler/openapi_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ import (
"io/ioutil"
"net/http"
"os"
"path/filepath"

"github.com/avenga/couper/config"
"github.com/getkin/kin-openapi/openapi3"
"github.com/getkin/kin-openapi/openapi3filter"

"github.com/avenga/couper/config"
)

type OpenAPIValidatorFactory struct {
Expand All @@ -24,11 +27,29 @@ func NewOpenAPIValidatorFactory(openapi *config.OpenAPI) (*OpenAPIValidatorFacto
if err != nil {
return nil, err
}
router := openapi3filter.NewRouter()
err = router.AddSwaggerFromFile(dir + "/" + openapi.File)

bytes, err := ioutil.ReadFile(filepath.Join(dir, openapi.File))
if err != nil {
return nil, err
}
return NewOpenAPIValidatorFactoryFromBytes(openapi, bytes)
}

func NewOpenAPIValidatorFactoryFromBytes(openapi *config.OpenAPI, bytes []byte) (*OpenAPIValidatorFactory, error) {
if openapi == nil || bytes == nil {
return nil, nil
}

swagger, err := openapi3.NewSwaggerLoader().LoadSwaggerFromData(bytes)
if err != nil {
return nil, err
}

router := openapi3filter.NewRouter()
if err = router.AddSwagger(swagger); err != nil {
return nil, err
}

return &OpenAPIValidatorFactory{
router: router,
ignoreRequestViolations: openapi.IgnoreRequestViolations,
Expand Down
1 change: 0 additions & 1 deletion handler/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"net"
"net/http"
"net/url"
"os"
"regexp"
"strconv"
"strings"
Expand Down
13 changes: 6 additions & 7 deletions handler/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ import (
"compress/gzip"
"context"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"reflect"
"strings"
"testing"
Expand Down Expand Up @@ -425,9 +423,7 @@ paths:
helper.Must(err)

openapiYAML := &bytes.Buffer{}
openapiYAMLTemplate.Execute(openapiYAML, map[string]string{"url": origin.URL})
helper.Must(ioutil.WriteFile("testdata/upstream.yaml", openapiYAML.Bytes(), 0644))
defer helper.Must(os.Remove("testdata/upstream.yaml"))
helper.Must(openapiYAMLTemplate.Execute(openapiYAML, map[string]string{"url": origin.URL}))

tests := []struct {
name string
Expand Down Expand Up @@ -482,11 +478,14 @@ paths:
for _, tt := range tests {
t.Run(tt.name, func(subT *testing.T) {
logger, hook := logrustest.NewNullLogger()
openapiValidatorFactory, err := handler.NewOpenAPIValidatorFactory(tt.openapi)
openapiValidatorFactory, err := handler.NewOpenAPIValidatorFactoryFromBytes(tt.openapi, openapiYAML.Bytes())
if err != nil {
subT.Fatal(err)
}
p, err := handler.NewProxy(&handler.ProxyOptions{Origin: origin.URL, OpenAPI: openapiValidatorFactory}, logger.WithContext(context.Background()), nil, eval.NewENVContext(nil))
content := helper.NewProxyContext(`
origin = "` + origin.URL + `"
`)
p, err := handler.NewProxy(&handler.ProxyOptions{Context: content, OpenAPI: openapiValidatorFactory}, logger.WithContext(context.Background()), nil, eval.NewENVContext(nil))
if err != nil {
subT.Fatal(err)
}
Expand Down

0 comments on commit dc41a8d

Please sign in to comment.