mirror of
https://github.com/labstack/echo.git
synced 2024-12-24 20:14:31 +02:00
Proper header and MIME constants
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
parent
adad28012c
commit
8b5772cf65
30
context.go
30
context.go
@ -289,21 +289,21 @@ func (c *context) Render(code int, name string, data interface{}) (err error) {
|
||||
if err = c.echo.renderer.Render(buf, name, data, c); err != nil {
|
||||
return
|
||||
}
|
||||
c.response.Header().Set(ContentType, TextHTMLCharsetUTF8)
|
||||
c.response.Header().Set(HeaderContentType, MIMETextHTMLCharsetUTF8)
|
||||
c.response.WriteHeader(code)
|
||||
_, err = c.response.Write(buf.Bytes())
|
||||
return
|
||||
}
|
||||
|
||||
func (c *context) HTML(code int, html string) (err error) {
|
||||
c.response.Header().Set(ContentType, TextHTMLCharsetUTF8)
|
||||
c.response.Header().Set(HeaderContentType, MIMETextHTMLCharsetUTF8)
|
||||
c.response.WriteHeader(code)
|
||||
_, err = c.response.Write([]byte(html))
|
||||
return
|
||||
}
|
||||
|
||||
func (c *context) String(code int, s string) (err error) {
|
||||
c.response.Header().Set(ContentType, TextPlainCharsetUTF8)
|
||||
c.response.Header().Set(HeaderContentType, MIMETextPlainCharsetUTF8)
|
||||
c.response.WriteHeader(code)
|
||||
_, err = c.response.Write([]byte(s))
|
||||
return
|
||||
@ -321,7 +321,7 @@ func (c *context) JSON(code int, i interface{}) (err error) {
|
||||
}
|
||||
|
||||
func (c *context) JSONBlob(code int, b []byte) (err error) {
|
||||
c.response.Header().Set(ContentType, ApplicationJSONCharsetUTF8)
|
||||
c.response.Header().Set(HeaderContentType, MIMEApplicationJSONCharsetUTF8)
|
||||
c.response.WriteHeader(code)
|
||||
_, err = c.response.Write(b)
|
||||
return
|
||||
@ -332,7 +332,7 @@ func (c *context) JSONP(code int, callback string, i interface{}) (err error) {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
c.response.Header().Set(ContentType, ApplicationJavaScriptCharsetUTF8)
|
||||
c.response.Header().Set(HeaderContentType, MIMEApplicationJavaScriptCharsetUTF8)
|
||||
c.response.WriteHeader(code)
|
||||
if _, err = c.response.Write([]byte(callback + "(")); err != nil {
|
||||
return
|
||||
@ -356,7 +356,7 @@ func (c *context) XML(code int, i interface{}) (err error) {
|
||||
}
|
||||
|
||||
func (c *context) XMLBlob(code int, b []byte) (err error) {
|
||||
c.response.Header().Set(ContentType, ApplicationXMLCharsetUTF8)
|
||||
c.response.Header().Set(HeaderContentType, MIMEApplicationXMLCharsetUTF8)
|
||||
c.response.WriteHeader(code)
|
||||
if _, err = c.response.Write([]byte(xml.Header)); err != nil {
|
||||
return
|
||||
@ -385,8 +385,8 @@ func (c *context) File(file string) error {
|
||||
}
|
||||
|
||||
func (c *context) Attachment(r io.ReadSeeker, name string) (err error) {
|
||||
c.response.Header().Set(ContentType, ContentTypeByExtension(name))
|
||||
c.response.Header().Set(ContentDisposition, "attachment; filename="+name)
|
||||
c.response.Header().Set(HeaderContentType, ContentTypeByExtension(name))
|
||||
c.response.Header().Set(HeaderContentDisposition, "attachment; filename="+name)
|
||||
c.response.WriteHeader(http.StatusOK)
|
||||
_, err = io.Copy(c.response, r)
|
||||
return
|
||||
@ -401,7 +401,7 @@ func (c *context) Redirect(code int, url string) error {
|
||||
if code < http.StatusMultipleChoices || code > http.StatusTemporaryRedirect {
|
||||
return ErrInvalidRedirectCode
|
||||
}
|
||||
c.response.Header().Set(Location, url)
|
||||
c.response.Header().Set(HeaderLocation, url)
|
||||
c.response.WriteHeader(code)
|
||||
return nil
|
||||
}
|
||||
@ -426,14 +426,14 @@ func (c *context) ServeContent(content io.ReadSeeker, name string, modtime time.
|
||||
rq := c.Request()
|
||||
rs := c.Response()
|
||||
|
||||
if t, err := time.Parse(http.TimeFormat, rq.Header().Get(IfModifiedSince)); err == nil && modtime.Before(t.Add(1*time.Second)) {
|
||||
rs.Header().Del(ContentType)
|
||||
rs.Header().Del(ContentLength)
|
||||
if t, err := time.Parse(http.TimeFormat, rq.Header().Get(HeaderIfModifiedSince)); err == nil && modtime.Before(t.Add(1*time.Second)) {
|
||||
rs.Header().Del(HeaderContentType)
|
||||
rs.Header().Del(HeaderContentLength)
|
||||
return c.NoContent(http.StatusNotModified)
|
||||
}
|
||||
|
||||
rs.Header().Set(ContentType, ContentTypeByExtension(name))
|
||||
rs.Header().Set(LastModified, modtime.UTC().Format(http.TimeFormat))
|
||||
rs.Header().Set(HeaderContentType, ContentTypeByExtension(name))
|
||||
rs.Header().Set(HeaderLastModified, modtime.UTC().Format(http.TimeFormat))
|
||||
rs.WriteHeader(http.StatusOK)
|
||||
_, err := io.Copy(rs, content)
|
||||
return err
|
||||
@ -444,7 +444,7 @@ func (c *context) ServeContent(content io.ReadSeeker, name string, modtime time.
|
||||
// found.
|
||||
func ContentTypeByExtension(name string) (t string) {
|
||||
if t = mime.TypeByExtension(filepath.Ext(name)); t == "" {
|
||||
t = OctetStream
|
||||
t = MIMEOctetStream
|
||||
}
|
||||
return
|
||||
}
|
||||
|
@ -65,15 +65,15 @@ func TestContext(t *testing.T) {
|
||||
//------
|
||||
|
||||
// JSON
|
||||
testBindOk(t, c, ApplicationJSON)
|
||||
testBindOk(t, c, MIMEApplicationJSON)
|
||||
c.Object().request = test.NewRequest(POST, "/", strings.NewReader(invalidContent))
|
||||
testBindError(t, c, ApplicationJSON)
|
||||
testBindError(t, c, MIMEApplicationJSON)
|
||||
|
||||
// XML
|
||||
c.Object().request = test.NewRequest(POST, "/", strings.NewReader(userXML))
|
||||
testBindOk(t, c, ApplicationXML)
|
||||
testBindOk(t, c, MIMEApplicationXML)
|
||||
c.Object().request = test.NewRequest(POST, "/", strings.NewReader(invalidContent))
|
||||
testBindError(t, c, ApplicationXML)
|
||||
testBindError(t, c, MIMEApplicationXML)
|
||||
|
||||
// Unsupported
|
||||
testBindError(t, c, "")
|
||||
@ -102,7 +102,7 @@ func TestContext(t *testing.T) {
|
||||
err = c.JSON(http.StatusOK, user{"1", "Joe"})
|
||||
if assert.NoError(t, err) {
|
||||
assert.Equal(t, http.StatusOK, rec.Status())
|
||||
assert.Equal(t, ApplicationJSONCharsetUTF8, rec.Header().Get(ContentType))
|
||||
assert.Equal(t, MIMEApplicationJSONCharsetUTF8, rec.Header().Get(HeaderContentType))
|
||||
assert.Equal(t, userJSON, rec.Body.String())
|
||||
}
|
||||
|
||||
@ -119,7 +119,7 @@ func TestContext(t *testing.T) {
|
||||
err = c.JSONP(http.StatusOK, callback, user{"1", "Joe"})
|
||||
if assert.NoError(t, err) {
|
||||
assert.Equal(t, http.StatusOK, rec.Status())
|
||||
assert.Equal(t, ApplicationJavaScriptCharsetUTF8, rec.Header().Get(ContentType))
|
||||
assert.Equal(t, MIMEApplicationJavaScriptCharsetUTF8, rec.Header().Get(HeaderContentType))
|
||||
assert.Equal(t, callback+"("+userJSON+");", rec.Body.String())
|
||||
}
|
||||
|
||||
@ -129,7 +129,7 @@ func TestContext(t *testing.T) {
|
||||
err = c.XML(http.StatusOK, user{"1", "Joe"})
|
||||
if assert.NoError(t, err) {
|
||||
assert.Equal(t, http.StatusOK, rec.Status())
|
||||
assert.Equal(t, ApplicationXMLCharsetUTF8, rec.Header().Get(ContentType))
|
||||
assert.Equal(t, MIMEApplicationXMLCharsetUTF8, rec.Header().Get(HeaderContentType))
|
||||
assert.Equal(t, xml.Header+userXML, rec.Body.String())
|
||||
}
|
||||
|
||||
@ -145,7 +145,7 @@ func TestContext(t *testing.T) {
|
||||
err = c.String(http.StatusOK, "Hello, World!")
|
||||
if assert.NoError(t, err) {
|
||||
assert.Equal(t, http.StatusOK, rec.Status())
|
||||
assert.Equal(t, TextPlainCharsetUTF8, rec.Header().Get(ContentType))
|
||||
assert.Equal(t, MIMETextPlainCharsetUTF8, rec.Header().Get(HeaderContentType))
|
||||
assert.Equal(t, "Hello, World!", rec.Body.String())
|
||||
}
|
||||
|
||||
@ -155,7 +155,7 @@ func TestContext(t *testing.T) {
|
||||
err = c.HTML(http.StatusOK, "Hello, <strong>World!</strong>")
|
||||
if assert.NoError(t, err) {
|
||||
assert.Equal(t, http.StatusOK, rec.Status())
|
||||
assert.Equal(t, TextHTMLCharsetUTF8, rec.Header().Get(ContentType))
|
||||
assert.Equal(t, MIMETextHTMLCharsetUTF8, rec.Header().Get(HeaderContentType))
|
||||
assert.Equal(t, "Hello, <strong>World!</strong>", rec.Body.String())
|
||||
}
|
||||
|
||||
@ -167,7 +167,7 @@ func TestContext(t *testing.T) {
|
||||
err = c.Attachment(file, "walle.png")
|
||||
if assert.NoError(t, err) {
|
||||
assert.Equal(t, http.StatusOK, rec.Status())
|
||||
assert.Equal(t, "attachment; filename=walle.png", rec.Header().Get(ContentDisposition))
|
||||
assert.Equal(t, "attachment; filename=walle.png", rec.Header().Get(HeaderContentDisposition))
|
||||
assert.Equal(t, 219885, rec.Body.Len())
|
||||
}
|
||||
}
|
||||
@ -182,7 +182,7 @@ func TestContext(t *testing.T) {
|
||||
rec = test.NewResponseRecorder()
|
||||
c = NewContext(rq, rec, e)
|
||||
assert.Equal(t, nil, c.Redirect(http.StatusMovedPermanently, "http://labstack.github.io/echo"))
|
||||
assert.Equal(t, "http://labstack.github.io/echo", rec.Header().Get(Location))
|
||||
assert.Equal(t, "http://labstack.github.io/echo", rec.Header().Get(HeaderLocation))
|
||||
assert.Equal(t, http.StatusMovedPermanently, rec.Status())
|
||||
|
||||
// Error
|
||||
@ -226,7 +226,7 @@ func TestContextFormValue(t *testing.T) {
|
||||
f.Set("email", "joe@labstack.com")
|
||||
|
||||
rq := test.NewRequest(POST, "/", strings.NewReader(f.Encode()))
|
||||
rq.Header().Add(ContentType, ApplicationForm)
|
||||
rq.Header().Add(HeaderContentType, MIMEApplicationForm)
|
||||
|
||||
c := NewContext(rq, nil, New())
|
||||
assert.Equal(t, "joe", c.FormValue("name"))
|
||||
@ -258,7 +258,7 @@ func TestContextServeContent(t *testing.T) {
|
||||
// Cached
|
||||
rc = test.NewResponseRecorder()
|
||||
c = NewContext(rq, rc, e)
|
||||
rq.Header().Set(IfModifiedSince, fi.ModTime().UTC().Format(http.TimeFormat))
|
||||
rq.Header().Set(HeaderIfModifiedSince, fi.ModTime().UTC().Format(http.TimeFormat))
|
||||
if assert.NoError(t, c.ServeContent(f, fi.Name(), fi.ModTime())) {
|
||||
assert.Equal(t, http.StatusNotModified, rc.Status())
|
||||
}
|
||||
@ -267,7 +267,7 @@ func TestContextServeContent(t *testing.T) {
|
||||
}
|
||||
|
||||
func testBindOk(t *testing.T, c Context, ct string) {
|
||||
c.Request().Header().Set(ContentType, ct)
|
||||
c.Request().Header().Set(HeaderContentType, ct)
|
||||
u := new(user)
|
||||
err := c.Bind(u)
|
||||
if assert.NoError(t, err) {
|
||||
@ -277,12 +277,12 @@ func testBindOk(t *testing.T, c Context, ct string) {
|
||||
}
|
||||
|
||||
func testBindError(t *testing.T, c Context, ct string) {
|
||||
c.Request().Header().Set(ContentType, ct)
|
||||
c.Request().Header().Set(HeaderContentType, ct)
|
||||
u := new(user)
|
||||
err := c.Bind(u)
|
||||
|
||||
switch ct {
|
||||
case ApplicationJSON, ApplicationXML:
|
||||
case MIMEApplicationJSON, MIMEApplicationXML:
|
||||
if assert.IsType(t, new(HTTPError), err) {
|
||||
assert.Equal(t, http.StatusBadRequest, err.(*HTTPError).Code)
|
||||
}
|
||||
|
69
echo.go
69
echo.go
@ -127,46 +127,45 @@ const (
|
||||
TRACE = "TRACE"
|
||||
)
|
||||
|
||||
// Media types
|
||||
// MIME types
|
||||
const (
|
||||
ApplicationJSON = "application/json"
|
||||
ApplicationJSONCharsetUTF8 = ApplicationJSON + "; " + CharsetUTF8
|
||||
ApplicationJavaScript = "application/javascript"
|
||||
ApplicationJavaScriptCharsetUTF8 = ApplicationJavaScript + "; " + CharsetUTF8
|
||||
ApplicationXML = "application/xml"
|
||||
ApplicationXMLCharsetUTF8 = ApplicationXML + "; " + CharsetUTF8
|
||||
ApplicationForm = "application/x-www-form-urlencoded"
|
||||
ApplicationProtobuf = "application/protobuf"
|
||||
ApplicationMsgpack = "application/msgpack"
|
||||
TextHTML = "text/html"
|
||||
TextHTMLCharsetUTF8 = TextHTML + "; " + CharsetUTF8
|
||||
TextPlain = "text/plain"
|
||||
TextPlainCharsetUTF8 = TextPlain + "; " + CharsetUTF8
|
||||
MultipartForm = "multipart/form-data"
|
||||
OctetStream = "application/octet-stream"
|
||||
MIMEApplicationJSON = "application/json"
|
||||
MIMEApplicationJSONCharsetUTF8 = MIMEApplicationJSON + "; " + charsetUTF8
|
||||
MIMEApplicationJavaScript = "application/javascript"
|
||||
MIMEApplicationJavaScriptCharsetUTF8 = MIMEApplicationJavaScript + "; " + charsetUTF8
|
||||
MIMEApplicationXML = "application/xml"
|
||||
MIMEApplicationXMLCharsetUTF8 = MIMEApplicationXML + "; " + charsetUTF8
|
||||
MIMEApplicationForm = "application/x-www-form-urlencoded"
|
||||
MIMEApplicationProtobuf = "application/protobuf"
|
||||
MIMEApplicationMsgpack = "application/msgpack"
|
||||
MIMETextHTML = "text/html"
|
||||
MIMETextHTMLCharsetUTF8 = MIMETextHTML + "; " + charsetUTF8
|
||||
MIMETextPlain = "text/plain"
|
||||
MIMETextPlainCharsetUTF8 = MIMETextPlain + "; " + charsetUTF8
|
||||
MIMEMultipartForm = "multipart/form-data"
|
||||
MIMEOctetStream = "application/octet-stream"
|
||||
)
|
||||
|
||||
// Charset
|
||||
const (
|
||||
CharsetUTF8 = "charset=utf-8"
|
||||
charsetUTF8 = "charset=utf-8"
|
||||
)
|
||||
|
||||
// Headers
|
||||
const (
|
||||
AcceptEncoding = "Accept-Encoding"
|
||||
Authorization = "Authorization"
|
||||
ContentDisposition = "Content-Disposition"
|
||||
ContentEncoding = "Content-Encoding"
|
||||
ContentLength = "Content-Length"
|
||||
ContentType = "Content-Type"
|
||||
IfModifiedSince = "If-Modified-Since"
|
||||
LastModified = "Last-Modified"
|
||||
Location = "Location"
|
||||
Upgrade = "Upgrade"
|
||||
Vary = "Vary"
|
||||
WWWAuthenticate = "WWW-Authenticate"
|
||||
XForwardedFor = "X-Forwarded-For"
|
||||
XRealIP = "X-Real-IP"
|
||||
HeaderAcceptEncoding = "Accept-Encoding"
|
||||
HeaderAuthorization = "Authorization"
|
||||
HeaderContentDisposition = "Content-Disposition"
|
||||
HeaderContentEncoding = "Content-Encoding"
|
||||
HeaderContentLength = "Content-Length"
|
||||
HeaderContentType = "Content-Type"
|
||||
HeaderIfModifiedSince = "If-Modified-Since"
|
||||
HeaderLastModified = "Last-Modified"
|
||||
HeaderLocation = "Location"
|
||||
HeaderUpgrade = "Upgrade"
|
||||
HeaderVary = "Vary"
|
||||
HeaderWWWAuthenticate = "WWW-Authenticate"
|
||||
HeaderXForwardedFor = "X-Forwarded-For"
|
||||
HeaderXRealIP = "X-Real-IP"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -514,13 +513,13 @@ func (e *HTTPError) Error() string {
|
||||
|
||||
func (b *binder) Bind(i interface{}, c Context) (err error) {
|
||||
rq := c.Request()
|
||||
ct := rq.Header().Get(ContentType)
|
||||
ct := rq.Header().Get(HeaderContentType)
|
||||
err = ErrUnsupportedMediaType
|
||||
if strings.HasPrefix(ct, ApplicationJSON) {
|
||||
if strings.HasPrefix(ct, MIMEApplicationJSON) {
|
||||
if err = json.NewDecoder(rq.Body()).Decode(i); err != nil {
|
||||
err = NewHTTPError(http.StatusBadRequest, err.Error())
|
||||
}
|
||||
} else if strings.HasPrefix(ct, ApplicationXML) {
|
||||
} else if strings.HasPrefix(ct, MIMEApplicationXML) {
|
||||
if err = xml.NewDecoder(rq.Body()).Decode(i); err != nil {
|
||||
err = NewHTTPError(http.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
@ -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))
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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))
|
||||
|
@ -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)
|
||||
|
@ -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())
|
||||
|
||||
|
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user