From 6bb871fe3a34734eae761b8777bebe2d72f6279e Mon Sep 17 00:00:00 2001 From: Vishal Rana Date: Wed, 10 Feb 2016 16:51:43 -0800 Subject: [PATCH] Dropped json and xml indent, added jsonblob. Signed-off-by: Vishal Rana --- context.go | 53 +++++++++++++------------------------ context_test.go | 47 ++++---------------------------- engine/fasthttp/header.go | 6 ++--- engine/fasthttp/response.go | 2 +- engine/standard/request.go | 12 +++++++++ 5 files changed, 38 insertions(+), 82 deletions(-) diff --git a/context.go b/context.go index df7cf076..27057139 100644 --- a/context.go +++ b/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. diff --git a/context_test.go b/context_test.go index 6614286f..778b7c01 100644 --- a/context_test.go +++ b/context_test.go @@ -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 := `1Joe` - userXMLIndent := "_\n_?1\n_?Joe\n_" - 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 diff --git a/engine/fasthttp/header.go b/engine/fasthttp/header.go index 4a978703..93c0b0d6 100644 --- a/engine/fasthttp/header.go +++ b/engine/fasthttp/header.go @@ -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) { diff --git a/engine/fasthttp/response.go b/engine/fasthttp/response.go index 37ef2ea1..dba2b12e 100644 --- a/engine/fasthttp/response.go +++ b/engine/fasthttp/response.go @@ -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}, diff --git a/engine/standard/request.go b/engine/standard/request.go index 0a41c52b..0e5c11f8 100644 --- a/engine/standard/request.go +++ b/engine/standard/request.go @@ -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 }