This repository has been archived by the owner on Nov 1, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The Echo Middleware assert the request using the openapi-assert package. Changed the travis file to build using the latest version of go.
- Loading branch information
Showing
5 changed files
with
215 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
language: go | ||
|
||
go: | ||
- "1.11" | ||
- "1.11.x" | ||
- "1.12.x" | ||
- master | ||
env: | ||
- GO111MODULE=on | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package echo | ||
|
||
import ( | ||
"net/http" | ||
|
||
assert "github.com/faabiosr/openapi-assert" | ||
"github.com/labstack/echo/v4" | ||
mw "github.com/labstack/echo/v4/middleware" | ||
) | ||
|
||
// AssertConfig defines the config for Assert Assert. | ||
type AssertConfig struct { | ||
// Skipper defines a function to skip middleware. | ||
Skipper mw.Skipper | ||
|
||
// OpenAPI Document | ||
Document assert.Document | ||
} | ||
|
||
// DefaultAssertConfig is the default Assert middleware config. | ||
var DefaultAssertConfig = AssertConfig{ | ||
Skipper: mw.DefaultSkipper, | ||
} | ||
|
||
// Assert returns middleware that uses the openapi-assert | ||
// package to assert echo HTTP requests. | ||
func Assert(doc assert.Document) echo.MiddlewareFunc { | ||
c := DefaultAssertConfig | ||
c.Document = doc | ||
|
||
return AssertWithConfig(c) | ||
} | ||
|
||
// AssertWithConfig returns an Assert middleware with config. | ||
func AssertWithConfig(cfg AssertConfig) echo.MiddlewareFunc { | ||
// Defaults | ||
if cfg.Skipper == nil { | ||
cfg.Skipper = DefaultAssertConfig.Skipper | ||
} | ||
|
||
if cfg.Document == nil { | ||
panic("echo: assert middleware requires an openapi-assert document") | ||
} | ||
|
||
assert := assert.New(cfg.Document) | ||
|
||
return func(next echo.HandlerFunc) echo.HandlerFunc { | ||
return func(ctx echo.Context) error { | ||
if cfg.Skipper(ctx) { | ||
return next(ctx) | ||
} | ||
|
||
if err := assert.Request(ctx.Request()); err != nil { | ||
return &echo.HTTPError{ | ||
Code: http.StatusBadRequest, | ||
Message: err.Error(), | ||
Internal: err, | ||
} | ||
} | ||
|
||
return next(ctx) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package echo | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"strings" | ||
"testing" | ||
|
||
oapi "github.com/faabiosr/openapi-assert" | ||
ec "github.com/labstack/echo/v4" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/suite" | ||
) | ||
|
||
type ( | ||
AssertTestSuite struct { | ||
suite.Suite | ||
assert *assert.Assertions | ||
|
||
doc oapi.Document | ||
|
||
srv *ec.Echo | ||
} | ||
) | ||
|
||
func (s *AssertTestSuite) SetupTest() { | ||
s.assert = assert.New(s.T()) | ||
s.doc, _ = oapi.LoadFromURI("../../fixtures/docs.json") | ||
s.srv = ec.New() | ||
} | ||
|
||
func (s *AssertTestSuite) TestMiddlewareWithConfig() { | ||
req := httptest.NewRequest(ec.PATCH, "/api/pets/1", nil) | ||
req.Header.Add(ec.HeaderContentType, ec.MIMEApplicationJSON) | ||
|
||
rec := httptest.NewRecorder() | ||
|
||
c := s.srv.NewContext(req, rec) | ||
|
||
cfg := AssertConfig{Document: s.doc} | ||
|
||
err := AssertWithConfig(cfg)(func(ctx ec.Context) error { | ||
return ctx.String(http.StatusOK, "test") | ||
})(c) | ||
|
||
s.assert.Error(err) | ||
} | ||
|
||
func (s *AssertTestSuite) TestMiddleware() { | ||
req := httptest.NewRequest( | ||
ec.POST, | ||
"/api/pets", | ||
strings.NewReader(`{"id": 1, "name": "doggo"}`), | ||
) | ||
|
||
req.Header.Add(ec.HeaderContentType, ec.MIMEApplicationJSON) | ||
|
||
rec := httptest.NewRecorder() | ||
|
||
c := s.srv.NewContext(req, rec) | ||
|
||
err := Assert(s.doc)(func(ctx ec.Context) error { | ||
return ctx.String(http.StatusOK, "test") | ||
})(c) | ||
|
||
s.assert.NoError(err) | ||
} | ||
|
||
func (s *AssertTestSuite) TestMiddlewareWithSkipper() { | ||
req := httptest.NewRequest(ec.PATCH, "/api/pets/1", nil) | ||
req.Header.Add(ec.HeaderContentType, ec.MIMEApplicationJSON) | ||
|
||
rec := httptest.NewRecorder() | ||
|
||
c := s.srv.NewContext(req, rec) | ||
|
||
cfg := AssertConfig{ | ||
Document: s.doc, | ||
Skipper: func(c ec.Context) bool { | ||
return true | ||
}, | ||
} | ||
|
||
err := AssertWithConfig(cfg)(func(ctx ec.Context) error { | ||
return ctx.String(http.StatusOK, "test") | ||
})(c) | ||
|
||
s.assert.NoError(err) | ||
} | ||
|
||
func (s *AssertTestSuite) TestMiddlewareWithoutDocument() { | ||
req := httptest.NewRequest(ec.PATCH, "/api/pets/1", nil) | ||
rec := httptest.NewRecorder() | ||
|
||
c := s.srv.NewContext(req, rec) | ||
|
||
cfg := AssertConfig{ | ||
Skipper: func(c ec.Context) bool { | ||
return true | ||
}, | ||
} | ||
|
||
caller := func() { | ||
AssertWithConfig(cfg)(func(ctx ec.Context) error { | ||
return ctx.String(http.StatusOK, "test") | ||
})(c) | ||
} | ||
|
||
s.assert.Panics(caller) | ||
} | ||
|
||
func TestAssertTestSuite(t *testing.T) { | ||
suite.Run(t, new(AssertTestSuite)) | ||
} |