1
0
mirror of https://github.com/labstack/echo.git synced 2025-01-12 01:22:21 +02:00
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2015-06-09 09:16:52 -07:00
parent 0edb31b3bd
commit 1ac5425ec4
5 changed files with 23 additions and 9 deletions

View File

@ -382,7 +382,7 @@ func (e *Echo) URI(h Handler, params ...interface{}) string {
return uri.String()
}
// URL is an alias for URI
// URL is an alias for URI.
func (e *Echo) URL(h Handler, params ...interface{}) string {
return e.URI(h, params...)
}
@ -400,6 +400,7 @@ func (e *Echo) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
c.reset(r, w, e)
if h == nil {
c.response.status = http.StatusNotFound // Helpful to skip middleware
h = e.notFoundHandler
}

View File

@ -29,7 +29,8 @@ func Gzip() echo.MiddlewareFunc {
return func(h echo.HandlerFunc) echo.HandlerFunc {
return func(c *echo.Context) error {
if (c.Request().Header.Get(echo.Upgrade)) != echo.WebSocket && // Skip for WebSocket
strings.Contains(c.Request().Header.Get(echo.AcceptEncoding), scheme) {
strings.Contains(c.Request().Header.Get(echo.AcceptEncoding), scheme) &&
c.Response().Status() != http.StatusNotFound { // Skip for "404 - Not Found"
w := gzip.NewWriter(c.Response().Writer())
defer w.Close()
gw := gzipWriter{Writer: w, ResponseWriter: c.Response().Writer()}

View File

@ -1,30 +1,38 @@
package middleware
import (
"bytes"
"compress/gzip"
"net/http"
"net/http/httptest"
"testing"
"bytes"
"github.com/labstack/echo"
"github.com/stretchr/testify/assert"
)
func TestGzip(t *testing.T) {
// Empty Accept-Encoding header
req, _ := http.NewRequest(echo.GET, "/", nil)
rec := httptest.NewRecorder()
c := echo.NewContext(req, echo.NewResponse(rec), echo.New())
h := func(c *echo.Context) error {
return c.String(http.StatusOK, "test")
}
// Skip if no Accept-Encoding header
Gzip()(h)(c)
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, "test", rec.Body.String())
// With Accept-Encoding header
// Skip if WebSocket
rec = httptest.NewRecorder()
c = echo.NewContext(req, echo.NewResponse(rec), echo.New())
c.Request().Header.Set(echo.Upgrade, echo.WebSocket)
Gzip()(h)(c)
assert.Equal(t, http.StatusOK, rec.Code)
assert.NotEqual(t, "gzip", rec.Header().Get(echo.ContentEncoding))
// Gzip
req, _ = http.NewRequest(echo.GET, "/", nil)
req.Header.Set(echo.AcceptEncoding, "gzip")
rec = httptest.NewRecorder()

View File

@ -74,7 +74,7 @@ types of handlers.
Routes with common prefix can be grouped to define a new sub-router with optional
middleware. If middleware is passed to the function, it overrides parent middleware
- helpful if you want a completly new middleware stack for the group. To add middleware
- helpful if you want a completely new middleware stack for the group. To add middleware
later you can use `Group.Use(m ...Middleware)`. Groups can also be nested.
In the code below, we create an admin group which requires basic HTTP authentication
@ -254,7 +254,7 @@ RedirectToSlashOptions struct {
e.Use(mw.RedirectToSlash())
```
*Note*: StripTrailingSlash and RedirectToSlash should not be used together.
> StripTrailingSlash and RedirectToSlash middleware should not be used together.
[Examples](https://github.com/labstack/echo/tree/master/examples/middleware)

View File

@ -1,3 +1,7 @@
.social a {
text-decoration: none;
}
}
.social a:not(:first-child) {
margin-left: 20px;
}