mirror of
https://github.com/labstack/echo.git
synced 2025-04-27 12:32:09 +02:00
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
parent
00699ed0db
commit
03efe4d61b
34
context.go
34
context.go
@ -128,12 +128,22 @@ type (
|
|||||||
// the JSONP payload.
|
// the JSONP payload.
|
||||||
JSONP(int, string, interface{}) error
|
JSONP(int, string, interface{}) error
|
||||||
|
|
||||||
|
// JSONPBlob sends a JSONP blob response with status code. It uses `callback`
|
||||||
|
// to construct the JSONP payload.
|
||||||
|
JSONPBlob(int, string, []byte) error
|
||||||
|
|
||||||
// XML sends an XML response with status code.
|
// XML sends an XML response with status code.
|
||||||
XML(int, interface{}) error
|
XML(int, interface{}) error
|
||||||
|
|
||||||
// XMLBlob sends a XML blob response with status code.
|
// XMLBlob sends a XML blob response with status code.
|
||||||
XMLBlob(int, []byte) error
|
XMLBlob(int, []byte) error
|
||||||
|
|
||||||
|
// Blob sends a blob response with status code and content type.
|
||||||
|
Blob(int, string, []byte) error
|
||||||
|
|
||||||
|
// Stream sends a streaming response with status code and content type.
|
||||||
|
Stream(int, string, io.Reader) error
|
||||||
|
|
||||||
// File sends a response with the content of the file.
|
// File sends a response with the content of the file.
|
||||||
File(string) error
|
File(string) error
|
||||||
|
|
||||||
@ -356,10 +366,7 @@ func (c *echoContext) JSON(code int, i interface{}) (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *echoContext) JSONBlob(code int, b []byte) (err error) {
|
func (c *echoContext) JSONBlob(code int, b []byte) (err error) {
|
||||||
c.response.Header().Set(HeaderContentType, MIMEApplicationJSONCharsetUTF8)
|
return c.Blob(code, MIMEApplicationJSONCharsetUTF8, b)
|
||||||
c.response.WriteHeader(code)
|
|
||||||
_, err = c.response.Write(b)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *echoContext) JSONP(code int, callback string, i interface{}) (err error) {
|
func (c *echoContext) JSONP(code int, callback string, i interface{}) (err error) {
|
||||||
@ -367,6 +374,10 @@ func (c *echoContext) JSONP(code int, callback string, i interface{}) (err error
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
return c.JSONPBlob(code, callback, b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *echoContext) JSONPBlob(code int, callback string, b []byte) (err error) {
|
||||||
c.response.Header().Set(HeaderContentType, MIMEApplicationJavaScriptCharsetUTF8)
|
c.response.Header().Set(HeaderContentType, MIMEApplicationJavaScriptCharsetUTF8)
|
||||||
c.response.WriteHeader(code)
|
c.response.WriteHeader(code)
|
||||||
if _, err = c.response.Write([]byte(callback + "(")); err != nil {
|
if _, err = c.response.Write([]byte(callback + "(")); err != nil {
|
||||||
@ -391,15 +402,26 @@ func (c *echoContext) XML(code int, i interface{}) (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *echoContext) XMLBlob(code int, b []byte) (err error) {
|
func (c *echoContext) XMLBlob(code int, b []byte) (err error) {
|
||||||
c.response.Header().Set(HeaderContentType, MIMEApplicationXMLCharsetUTF8)
|
|
||||||
c.response.WriteHeader(code)
|
|
||||||
if _, err = c.response.Write([]byte(xml.Header)); err != nil {
|
if _, err = c.response.Write([]byte(xml.Header)); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
return c.Blob(code, MIMEApplicationXMLCharsetUTF8, b)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *echoContext) Blob(code int, contentType string, b []byte) (err error) {
|
||||||
|
c.response.Header().Set(HeaderContentType, contentType)
|
||||||
|
c.response.WriteHeader(code)
|
||||||
_, err = c.response.Write(b)
|
_, err = c.response.Write(b)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *echoContext) Stream(code int, contentType string, r io.Reader) (err error) {
|
||||||
|
c.response.Header().Set(HeaderContentType, contentType)
|
||||||
|
c.response.WriteHeader(code)
|
||||||
|
_, err = io.Copy(c.response, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func (c *echoContext) File(file string) error {
|
func (c *echoContext) File(file string) error {
|
||||||
f, err := os.Open(file)
|
f, err := os.Open(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -132,6 +132,17 @@ func TestContext(t *testing.T) {
|
|||||||
assert.Equal(t, "Hello, <strong>World!</strong>", rec.Body.String())
|
assert.Equal(t, "Hello, <strong>World!</strong>", rec.Body.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Stream
|
||||||
|
rec = test.NewResponseRecorder()
|
||||||
|
c = e.NewContext(req, rec).(*echoContext)
|
||||||
|
r := strings.NewReader("response from a stream")
|
||||||
|
err = c.Stream(http.StatusOK, "application/octet-stream", r)
|
||||||
|
if assert.NoError(t, err) {
|
||||||
|
assert.Equal(t, http.StatusOK, rec.Status())
|
||||||
|
assert.Equal(t, "application/octet-stream", rec.Header().Get(HeaderContentType))
|
||||||
|
assert.Equal(t, "response from a stream", rec.Body.String())
|
||||||
|
}
|
||||||
|
|
||||||
// Attachment
|
// Attachment
|
||||||
rec = test.NewResponseRecorder()
|
rec = test.NewResponseRecorder()
|
||||||
c = e.NewContext(req, rec).(*echoContext)
|
c = e.NewContext(req, rec).(*echoContext)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user