1
0
mirror of https://github.com/labstack/echo.git synced 2025-07-15 01:34:53 +02:00

Proper header and MIME constants

Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana
2016-04-06 07:28:53 -07:00
parent adad28012c
commit 8b5772cf65
10 changed files with 92 additions and 93 deletions

View File

@ -41,7 +41,7 @@ func BasicAuth(f BasicAuthFunc) echo.MiddlewareFunc {
func BasicAuthFromConfig(config BasicAuthConfig) echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
auth := c.Request().Header().Get(echo.Authorization)
auth := c.Request().Header().Get(echo.HeaderAuthorization)
l := len(basic)
if len(auth) > l+1 && auth[:l] == basic {
@ -58,7 +58,7 @@ func BasicAuthFromConfig(config BasicAuthConfig) echo.MiddlewareFunc {
}
}
}
c.Response().Header().Set(echo.WWWAuthenticate, basic+" realm=Restricted")
c.Response().Header().Set(echo.HeaderWWWAuthenticate, basic+" realm=Restricted")
return echo.ErrUnauthorized
}
}

View File

@ -27,7 +27,7 @@ func TestBasicAuth(t *testing.T) {
// Valid credentials
auth := basic + " " + base64.StdEncoding.EncodeToString([]byte("joe:secret"))
rq.Header().Set(echo.Authorization, auth)
rq.Header().Set(echo.HeaderAuthorization, auth)
assert.NoError(t, h(c))
//---------------------
@ -36,21 +36,21 @@ func TestBasicAuth(t *testing.T) {
// Incorrect password
auth = basic + " " + base64.StdEncoding.EncodeToString([]byte("joe:password"))
rq.Header().Set(echo.Authorization, auth)
rq.Header().Set(echo.HeaderAuthorization, auth)
he := h(c).(*echo.HTTPError)
assert.Equal(t, http.StatusUnauthorized, he.Code)
assert.Equal(t, basic+" realm=Restricted", rs.Header().Get(echo.WWWAuthenticate))
assert.Equal(t, basic+" realm=Restricted", rs.Header().Get(echo.HeaderWWWAuthenticate))
// Empty Authorization header
rq.Header().Set(echo.Authorization, "")
rq.Header().Set(echo.HeaderAuthorization, "")
he = h(c).(*echo.HTTPError)
assert.Equal(t, http.StatusUnauthorized, he.Code)
assert.Equal(t, basic+" realm=Restricted", rs.Header().Get(echo.WWWAuthenticate))
assert.Equal(t, basic+" realm=Restricted", rs.Header().Get(echo.HeaderWWWAuthenticate))
// Invalid Authorization header
auth = base64.StdEncoding.EncodeToString([]byte("invalid"))
rq.Header().Set(echo.Authorization, auth)
rq.Header().Set(echo.HeaderAuthorization, auth)
he = h(c).(*echo.HTTPError)
assert.Equal(t, http.StatusUnauthorized, he.Code)
assert.Equal(t, basic+" realm=Restricted", rs.Header().Get(echo.WWWAuthenticate))
assert.Equal(t, basic+" realm=Restricted", rs.Header().Get(echo.HeaderWWWAuthenticate))
}

View File

@ -53,8 +53,8 @@ func GzipFromConfig(config GzipConfig) echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
rs := c.Response()
rs.Header().Add(echo.Vary, echo.AcceptEncoding)
if strings.Contains(c.Request().Header().Get(echo.AcceptEncoding), scheme) {
rs.Header().Add(echo.HeaderVary, echo.HeaderAcceptEncoding)
if strings.Contains(c.Request().Header().Get(echo.HeaderAcceptEncoding), scheme) {
rw := rs.Writer()
gw := pool.Get().(*gzip.Writer)
gw.Reset(rw)
@ -64,14 +64,14 @@ func GzipFromConfig(config GzipConfig) echo.MiddlewareFunc {
// nothing is written to body or error is returned.
// See issue #424, #407.
rs.SetWriter(rw)
rs.Header().Del(echo.ContentEncoding)
rs.Header().Del(echo.HeaderContentEncoding)
gw.Reset(ioutil.Discard)
}
gw.Close()
pool.Put(gw)
}()
g := gzipResponseWriter{Response: rs, Writer: gw}
rs.Header().Set(echo.ContentEncoding, scheme)
rs.Header().Set(echo.HeaderContentEncoding, scheme)
rs.SetWriter(g)
}
return next(c)
@ -80,8 +80,8 @@ func GzipFromConfig(config GzipConfig) echo.MiddlewareFunc {
}
func (g gzipResponseWriter) Write(b []byte) (int, error) {
if g.Header().Get(echo.ContentType) == "" {
g.Header().Set(echo.ContentType, http.DetectContentType(b))
if g.Header().Get(echo.HeaderContentType) == "" {
g.Header().Set(echo.HeaderContentType, http.DetectContentType(b))
}
return g.Writer.Write(b)
}

View File

@ -27,14 +27,14 @@ func TestGzip(t *testing.T) {
assert.Equal(t, "test", rec.Body.String())
rq = test.NewRequest(echo.GET, "/", nil)
rq.Header().Set(echo.AcceptEncoding, "gzip")
rq.Header().Set(echo.HeaderAcceptEncoding, "gzip")
rec = test.NewResponseRecorder()
c = echo.NewContext(rq, rec, e)
// Gzip
h(c)
assert.Equal(t, "gzip", rec.Header().Get(echo.ContentEncoding))
assert.Contains(t, rec.Header().Get(echo.ContentType), echo.TextPlain)
assert.Equal(t, "gzip", rec.Header().Get(echo.HeaderContentEncoding))
assert.Contains(t, rec.Header().Get(echo.HeaderContentType), echo.MIMETextPlain)
r, err := gzip.NewReader(rec.Body)
defer r.Close()
if assert.NoError(t, err) {
@ -54,8 +54,8 @@ func TestGzipNoContent(t *testing.T) {
})
h(c)
assert.Empty(t, rec.Header().Get(echo.ContentEncoding))
assert.Empty(t, rec.Header().Get(echo.ContentType))
assert.Empty(t, rec.Header().Get(echo.HeaderContentEncoding))
assert.Empty(t, rec.Header().Get(echo.HeaderContentType))
b, err := ioutil.ReadAll(rec.Body)
if assert.NoError(t, err) {
assert.Equal(t, 0, len(b))
@ -72,7 +72,7 @@ func TestGzipErrorReturned(t *testing.T) {
rec := test.NewResponseRecorder()
e.ServeHTTP(rq, rec)
assert.Empty(t, rec.Header().Get(echo.ContentEncoding))
assert.Empty(t, rec.Header().Get(echo.HeaderContentEncoding))
b, err := ioutil.ReadAll(rec.Body)
if assert.NoError(t, err) {
assert.Equal(t, "error", string(b))

View File

@ -90,9 +90,9 @@ func LoggerFromConfig(config LoggerConfig) echo.MiddlewareFunc {
return w.Write([]byte(time.Now().Format(time.RFC3339)))
case "remote_ip":
ra := rq.RemoteAddress()
if ip := rq.Header().Get(echo.XRealIP); ip != "" {
if ip := rq.Header().Get(echo.HeaderXRealIP); ip != "" {
ra = ip
} else if ip = rq.Header().Get(echo.XForwardedFor); ip != "" {
} else if ip = rq.Header().Get(echo.HeaderXForwardedFor); ip != "" {
ra = ip
} else {
ra, _, _ = net.SplitHostPort(ra)

View File

@ -63,14 +63,14 @@ func TestLoggerIPAddress(t *testing.T) {
})
// With X-Real-IP
rq.Header().Add(echo.XRealIP, ip)
rq.Header().Add(echo.HeaderXRealIP, ip)
h(c)
assert.Contains(t, ip, buf.String())
// With X-Forwarded-For
buf.Reset()
rq.Header().Del(echo.XRealIP)
rq.Header().Add(echo.XForwardedFor, ip)
rq.Header().Del(echo.HeaderXRealIP)
rq.Header().Add(echo.HeaderXForwardedFor, ip)
h(c)
assert.Contains(t, ip, buf.String())

View File

@ -90,7 +90,7 @@ func StaticFromConfig(config StaticConfig) echo.MiddlewareFunc {
// Create a directory index
rs := c.Response()
rs.Header().Set(echo.ContentType, echo.TextHTMLCharsetUTF8)
rs.Header().Set(echo.HeaderContentType, echo.MIMETextHTMLCharsetUTF8)
if _, err = fmt.Fprintf(rs, "<pre>\n"); err != nil {
return err
}