mirror of
https://github.com/labstack/echo.git
synced 2024-11-28 08:38:39 +02:00
Dropped json and xml indent, added jsonblob.
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
parent
94e5936287
commit
6bb871fe3a
53
context.go
53
context.go
@ -38,10 +38,10 @@ type (
|
||||
HTML(int, string) error
|
||||
String(int, string) error
|
||||
JSON(int, interface{}) error
|
||||
JSONIndent(int, interface{}, string, string) error
|
||||
JSONBlob(int, []byte) error
|
||||
JSONP(int, string, interface{}) error
|
||||
XML(int, interface{}) error
|
||||
XMLIndent(int, interface{}, string, string) error
|
||||
XMLBlob(int, []byte) error
|
||||
File(string, string, bool) error
|
||||
NoContent(int) error
|
||||
Redirect(int, string) error
|
||||
@ -197,31 +197,23 @@ func (c *context) String(code int, s string) (err error) {
|
||||
// JSON sends a JSON response with status code.
|
||||
func (c *context) JSON(code int, i interface{}) (err error) {
|
||||
b, err := json.Marshal(i)
|
||||
if c.echo.Debug() {
|
||||
b, err = json.MarshalIndent(i, "", " ")
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return c.JSONBlob(code, b)
|
||||
}
|
||||
|
||||
// JSONBlob sends a JSON blob response with status code.
|
||||
func (c *context) JSONBlob(code int, b []byte) (err error) {
|
||||
c.response.Header().Set(ContentType, ApplicationJSONCharsetUTF8)
|
||||
c.response.WriteHeader(code)
|
||||
c.response.Write(b)
|
||||
return
|
||||
}
|
||||
|
||||
// JSONIndent sends a JSON response with status code, but it applies prefix and indent to format the output.
|
||||
func (c *context) JSONIndent(code int, i interface{}, prefix string, indent string) (err error) {
|
||||
b, err := json.MarshalIndent(i, prefix, indent)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
c.json(code, b)
|
||||
return
|
||||
}
|
||||
|
||||
func (c *context) json(code int, b []byte) {
|
||||
c.response.Header().Set(ContentType, ApplicationJSONCharsetUTF8)
|
||||
c.response.WriteHeader(code)
|
||||
c.response.Write(b)
|
||||
}
|
||||
|
||||
// JSONP sends a JSONP response with status code. It uses `callback` to construct
|
||||
// the JSONP payload.
|
||||
func (c *context) JSONP(code int, callback string, i interface{}) (err error) {
|
||||
@ -240,9 +232,17 @@ func (c *context) JSONP(code int, callback string, i interface{}) (err error) {
|
||||
// XML sends an XML response with status code.
|
||||
func (c *context) XML(code int, i interface{}) (err error) {
|
||||
b, err := xml.Marshal(i)
|
||||
if c.echo.Debug() {
|
||||
b, err = xml.MarshalIndent(i, "", " ")
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return c.XMLBlob(code, b)
|
||||
}
|
||||
|
||||
// XMLBlob sends a XML blob response with status code.
|
||||
func (c *context) XMLBlob(code int, b []byte) (err error) {
|
||||
c.response.Header().Set(ContentType, ApplicationXMLCharsetUTF8)
|
||||
c.response.WriteHeader(code)
|
||||
c.response.Write([]byte(xml.Header))
|
||||
@ -250,23 +250,6 @@ func (c *context) XML(code int, i interface{}) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
// XMLIndent sends an XML response with status code, but it applies prefix and indent to format the output.
|
||||
func (c *context) XMLIndent(code int, i interface{}, prefix string, indent string) (err error) {
|
||||
b, err := xml.MarshalIndent(i, prefix, indent)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
c.xml(code, b)
|
||||
return
|
||||
}
|
||||
|
||||
func (c *context) xml(code int, b []byte) {
|
||||
c.response.Header().Set(ContentType, ApplicationXMLCharsetUTF8)
|
||||
c.response.WriteHeader(code)
|
||||
c.response.Write([]byte(xml.Header))
|
||||
c.response.Write(b)
|
||||
}
|
||||
|
||||
// File sends a response with the content of the file. If `attachment` is set
|
||||
// to true, the client is prompted to save the file with provided `name`,
|
||||
// name can be empty, in that case name of the file is used.
|
||||
|
@ -29,12 +29,8 @@ func (t *Template) Render(w io.Writer, name string, data interface{}) error {
|
||||
|
||||
func TestContext(t *testing.T) {
|
||||
userJSON := `{"id":"1","name":"Joe"}`
|
||||
userJSONIndent := "{\n_?\"id\": \"1\",\n_?\"name\": \"Joe\"\n_}"
|
||||
userXML := `<user><id>1</id><name>Joe</name></user>`
|
||||
userXMLIndent := "_<user>\n_?<id>1</id>\n_?<name>Joe</name>\n_</user>"
|
||||
incorrectContent := "this is incorrect content"
|
||||
|
||||
var nonMarshallableChannel chan bool
|
||||
invalidContent := "invalid content"
|
||||
|
||||
e := New()
|
||||
req := test.NewRequest(POST, "/", strings.NewReader(userJSON))
|
||||
@ -68,13 +64,13 @@ func TestContext(t *testing.T) {
|
||||
|
||||
// JSON
|
||||
testBindOk(t, c, ApplicationJSON)
|
||||
c.Object().request = test.NewRequest(POST, "/", strings.NewReader(incorrectContent))
|
||||
c.Object().request = test.NewRequest(POST, "/", strings.NewReader(invalidContent))
|
||||
testBindError(t, c, ApplicationJSON)
|
||||
|
||||
// XML
|
||||
c.Object().request = test.NewRequest(POST, "/", strings.NewReader(userXML))
|
||||
testBindOk(t, c, ApplicationXML)
|
||||
c.Object().request = test.NewRequest(POST, "/", strings.NewReader(incorrectContent))
|
||||
c.Object().request = test.NewRequest(POST, "/", strings.NewReader(invalidContent))
|
||||
testBindError(t, c, ApplicationXML)
|
||||
|
||||
// Unsupported
|
||||
@ -111,24 +107,7 @@ func TestContext(t *testing.T) {
|
||||
// JSON (error)
|
||||
rec = test.NewResponseRecorder()
|
||||
c = NewContext(req, rec, e)
|
||||
val := make(chan bool)
|
||||
err = c.JSON(http.StatusOK, val)
|
||||
assert.Error(t, err)
|
||||
|
||||
// JSONIndent
|
||||
rec = test.NewResponseRecorder()
|
||||
c = NewContext(req, rec, e)
|
||||
err = c.JSONIndent(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, userJSONIndent, rec.Body.String())
|
||||
}
|
||||
|
||||
// JSONIndent (error)
|
||||
rec = test.NewResponseRecorder()
|
||||
c = NewContext(req, rec, e)
|
||||
err = c.JSONIndent(http.StatusOK, nonMarshallableChannel, "_", "?")
|
||||
err = c.JSON(http.StatusOK, make(chan bool))
|
||||
assert.Error(t, err)
|
||||
|
||||
// JSONP
|
||||
@ -155,23 +134,7 @@ func TestContext(t *testing.T) {
|
||||
// XML (error)
|
||||
rec = test.NewResponseRecorder()
|
||||
c = NewContext(req, rec, e)
|
||||
err = c.XML(http.StatusOK, nonMarshallableChannel)
|
||||
assert.Error(t, err)
|
||||
|
||||
// XMLIndent
|
||||
rec = test.NewResponseRecorder()
|
||||
c = NewContext(req, rec, e)
|
||||
err = c.XMLIndent(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, xml.Header+userXMLIndent, rec.Body.String())
|
||||
}
|
||||
|
||||
// XMLIndent (error)
|
||||
rec = test.NewResponseRecorder()
|
||||
c = NewContext(req, rec, e)
|
||||
err = c.XMLIndent(http.StatusOK, nonMarshallableChannel, "_", "?")
|
||||
err = c.XML(http.StatusOK, make(chan bool))
|
||||
assert.Error(t, err)
|
||||
|
||||
// String
|
||||
|
@ -23,8 +23,7 @@ func (h *RequestHeader) Del(key string) {
|
||||
}
|
||||
|
||||
func (h *RequestHeader) Get(key string) string {
|
||||
// return h.header.Peek(key)
|
||||
return ""
|
||||
return string(h.header.Peek(key))
|
||||
}
|
||||
|
||||
func (h *RequestHeader) Set(key, val string) {
|
||||
@ -40,8 +39,7 @@ func (h *ResponseHeader) Del(key string) {
|
||||
}
|
||||
|
||||
func (h *ResponseHeader) Get(key string) string {
|
||||
// return h.ResponseHeader.Get(key)
|
||||
return ""
|
||||
return string(h.header.Peek(key))
|
||||
}
|
||||
|
||||
func (h *ResponseHeader) Set(key, val string) {
|
||||
|
@ -23,7 +23,7 @@ type (
|
||||
}
|
||||
)
|
||||
|
||||
func _NewResponse(c *fasthttp.RequestCtx) *Response {
|
||||
func NewResponse(c *fasthttp.RequestCtx) *Response {
|
||||
return &Response{
|
||||
context: c,
|
||||
header: &ResponseHeader{c.Response.Header},
|
||||
|
@ -35,6 +35,18 @@ func (r *Request) Header() engine.Header {
|
||||
return r.header
|
||||
}
|
||||
|
||||
// func Proto() string {
|
||||
// return r.request.Proto()
|
||||
// }
|
||||
//
|
||||
// func ProtoMajor() int {
|
||||
// return r.request.ProtoMajor()
|
||||
// }
|
||||
//
|
||||
// func ProtoMinor() int {
|
||||
// return r.request.ProtoMinor()
|
||||
// }
|
||||
|
||||
func (r *Request) RemoteAddress() string {
|
||||
return r.request.RemoteAddr
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user