Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed dobule call of error handler (#21)
- Loading branch information
Showing
9 changed files
with
187 additions
and
27 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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
default: install build | ||
|
||
add: | ||
go get -u -v ${PKG} | ||
|
||
install-tools: | ||
go install honnef.co/go/tools/cmd/staticcheck@latest && \ | ||
go install golang.org/x/tools/cmd/goimports@latest | ||
|
||
|
||
install-deps: | ||
go mod tidy | ||
|
||
install: install-tools install-deps | ||
|
||
build: lint test | ||
|
||
test: | ||
go test -race ./... | ||
|
||
lint: | ||
go vet ./... && \ | ||
staticcheck -tests=false ./... | ||
|
||
fmt: | ||
go fmt ./... && \ | ||
goimports -w . |
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
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
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,72 @@ | ||
package lecho_test | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/labstack/echo/v4" | ||
"github.com/rs/zerolog" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/ziflex/lecho/v3" | ||
) | ||
|
||
func TestMiddleware(t *testing.T) { | ||
t.Run("should not trigger error handler", func(t *testing.T) { | ||
var called bool | ||
e := echo.New() | ||
e.HTTPErrorHandler = func(err error, c echo.Context) { | ||
called = true | ||
|
||
c.JSON(http.StatusInternalServerError, err.Error()) | ||
} | ||
req := httptest.NewRequest(http.MethodGet, "/", nil) | ||
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) | ||
rec := httptest.NewRecorder() | ||
c := e.NewContext(req, rec) | ||
|
||
m := lecho.Middleware(lecho.Config{}) | ||
|
||
next := func(c echo.Context) error { | ||
return errors.New("error") | ||
} | ||
|
||
handler := m(next) | ||
err := handler(c) | ||
|
||
assert.Error(t, err, "should return error") | ||
assert.False(t, called, "should not call error handler") | ||
}) | ||
|
||
t.Run("should use enricher", func(t *testing.T) { | ||
e := echo.New() | ||
req := httptest.NewRequest(http.MethodGet, "/", nil) | ||
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) | ||
rec := httptest.NewRecorder() | ||
c := e.NewContext(req, rec) | ||
|
||
b := &bytes.Buffer{} | ||
|
||
l := lecho.New(b) | ||
m := lecho.Middleware(lecho.Config{ | ||
Logger: l, | ||
Enricher: func(c echo.Context, logger zerolog.Context) zerolog.Context { | ||
return logger.Str("test", "test") | ||
}, | ||
}) | ||
|
||
next := func(c echo.Context) error { | ||
return nil | ||
} | ||
|
||
handler := m(next) | ||
err := handler(c) | ||
|
||
assert.NoError(t, err, "should not return error") | ||
|
||
str := b.String() | ||
assert.Contains(t, str, `"test":"test"`) | ||
}) | ||
} |
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