mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-23 18:22:23 +08:00
Merge branch 'master' into context-stream-tests
This commit is contained in:
commit
2cc80d631c
16
context.go
16
context.go
@ -159,16 +159,15 @@ func (c *Context) Error(err error) *Error {
|
|||||||
if err == nil {
|
if err == nil {
|
||||||
panic("err is nil")
|
panic("err is nil")
|
||||||
}
|
}
|
||||||
var parsedError *Error
|
|
||||||
switch err.(type) {
|
parsedError, ok := err.(*Error)
|
||||||
case *Error:
|
if !ok {
|
||||||
parsedError = err.(*Error)
|
|
||||||
default:
|
|
||||||
parsedError = &Error{
|
parsedError = &Error{
|
||||||
Err: err,
|
Err: err,
|
||||||
Type: ErrorTypePrivate,
|
Type: ErrorTypePrivate,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
c.Errors = append(c.Errors, parsedError)
|
c.Errors = append(c.Errors, parsedError)
|
||||||
return parsedError
|
return parsedError
|
||||||
}
|
}
|
||||||
@ -695,7 +694,12 @@ func (c *Context) SecureJSON(code int, obj interface{}) {
|
|||||||
// It add padding to response body to request data from a server residing in a different domain than the client.
|
// It add padding to response body to request data from a server residing in a different domain than the client.
|
||||||
// It also sets the Content-Type as "application/javascript".
|
// It also sets the Content-Type as "application/javascript".
|
||||||
func (c *Context) JSONP(code int, obj interface{}) {
|
func (c *Context) JSONP(code int, obj interface{}) {
|
||||||
c.Render(code, render.JsonpJSON{Callback: c.DefaultQuery("callback", ""), Data: obj})
|
callback := c.DefaultQuery("callback", "")
|
||||||
|
if callback == "" {
|
||||||
|
c.Render(code, render.JSON{Data: obj})
|
||||||
|
} else {
|
||||||
|
c.Render(code, render.JsonpJSON{Callback: callback, Data: obj})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// JSON serializes the given struct as JSON into the response body.
|
// JSON serializes the given struct as JSON into the response body.
|
||||||
|
@ -597,6 +597,20 @@ func TestContextRenderJSONP(t *testing.T) {
|
|||||||
assert.Equal(t, "application/javascript; charset=utf-8", w.HeaderMap.Get("Content-Type"))
|
assert.Equal(t, "application/javascript; charset=utf-8", w.HeaderMap.Get("Content-Type"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Tests that the response is serialized as JSONP
|
||||||
|
// and Content-Type is set to application/json
|
||||||
|
func TestContextRenderJSONPWithoutCallback(t *testing.T) {
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
c, _ := CreateTestContext(w)
|
||||||
|
c.Request, _ = http.NewRequest("GET", "http://example.com", nil)
|
||||||
|
|
||||||
|
c.JSONP(201, H{"foo": "bar"})
|
||||||
|
|
||||||
|
assert.Equal(t, 201, w.Code)
|
||||||
|
assert.Equal(t, "{\"foo\":\"bar\"}", w.Body.String())
|
||||||
|
assert.Equal(t, "application/json; charset=utf-8", w.HeaderMap.Get("Content-Type"))
|
||||||
|
}
|
||||||
|
|
||||||
// Tests that no JSON is rendered if code is 204
|
// Tests that no JSON is rendered if code is 204
|
||||||
func TestContextRenderNoContentJSON(t *testing.T) {
|
func TestContextRenderNoContentJSON(t *testing.T) {
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
|
@ -128,7 +128,7 @@ func Run(addr ...string) (err error) {
|
|||||||
// RunTLS : The router is attached to a http.Server and starts listening and serving HTTPS requests.
|
// RunTLS : The router is attached to a http.Server and starts listening and serving HTTPS requests.
|
||||||
// It is a shortcut for http.ListenAndServeTLS(addr, certFile, keyFile, router)
|
// It is a shortcut for http.ListenAndServeTLS(addr, certFile, keyFile, router)
|
||||||
// Note: this method will block the calling goroutine undefinitelly unless an error happens.
|
// Note: this method will block the calling goroutine undefinitelly unless an error happens.
|
||||||
func RunTLS(addr string, certFile string, keyFile string) (err error) {
|
func RunTLS(addr, certFile, keyFile string) (err error) {
|
||||||
return engine().RunTLS(addr, certFile, keyFile)
|
return engine().RunTLS(addr, certFile, keyFile)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user