Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…ck#653)

Signed-off-by: Vishal Rana <[email protected]>
  • Loading branch information
vishr committed Sep 9, 2016
1 parent 00699ed commit 03efe4d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
34 changes: 28 additions & 6 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,22 @@ type (
// the JSONP payload.
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(int, interface{}) error

// XMLBlob sends a XML blob response with status code.
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(string) error

Expand Down Expand Up @@ -356,17 +366,18 @@ func (c *echoContext) JSON(code int, i interface{}) (err error) {
}

func (c *echoContext) JSONBlob(code int, b []byte) (err error) {
c.response.Header().Set(HeaderContentType, MIMEApplicationJSONCharsetUTF8)
c.response.WriteHeader(code)
_, err = c.response.Write(b)
return
return c.Blob(code, MIMEApplicationJSONCharsetUTF8, b)
}

func (c *echoContext) JSONP(code int, callback string, i interface{}) (err error) {
b, err := json.Marshal(i)
if err != nil {
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.WriteHeader(code)
if _, err = c.response.Write([]byte(callback + "(")); err != nil {
Expand All @@ -391,15 +402,26 @@ func (c *echoContext) XML(code int, i interface{}) (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 {
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)
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 {
f, err := os.Open(file)
if err != nil {
Expand Down
11 changes: 11 additions & 0 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,17 @@ func TestContext(t *testing.T) {
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
rec = test.NewResponseRecorder()
c = e.NewContext(req, rec).(*echoContext)
Expand Down

0 comments on commit 03efe4d

Please sign in to comment.