From 68f278607490eeeffa225d5528015f4335301749 Mon Sep 17 00:00:00 2001 From: thinkerou Date: Wed, 7 Nov 2018 11:18:02 +0800 Subject: [PATCH 01/12] remove go1.6 support --- .travis.yml | 1 - README.md | 2 +- context.go | 6 ++++++ context_17.go | 17 ----------------- context_17_test.go | 27 --------------------------- context_test.go | 12 ++++++++++++ debug.go | 2 +- render/json.go | 18 ++++++++++++++++++ render/json_17.go | 31 ------------------------------- render/render_17_test.go | 26 -------------------------- render/render_test.go | 12 ++++++++++++ 11 files changed, 50 insertions(+), 104 deletions(-) delete mode 100644 context_17.go delete mode 100644 context_17_test.go delete mode 100644 render/json_17.go delete mode 100644 render/render_17_test.go diff --git a/.travis.yml b/.travis.yml index 2eeb0b3d..5f4e8037 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,6 @@ language: go sudo: false go: - - 1.6.x - 1.7.x - 1.8.x - 1.9.x diff --git a/README.md b/README.md index 96becfc7..d251d2b2 100644 --- a/README.md +++ b/README.md @@ -120,7 +120,7 @@ $ go run main.go ## Prerequisite -Now Gin requires Go 1.6 or later and Go 1.7 will be required soon. +Now Gin requires Go 1.7 or later and Go 1.8 will be required soon. ## Quick start diff --git a/context.go b/context.go index 887e716d..d80c04ac 100644 --- a/context.go +++ b/context.go @@ -787,6 +787,12 @@ func (c *Context) AsciiJSON(code int, obj interface{}) { c.Render(code, render.AsciiJSON{Data: obj}) } +// PureJSON serializes the given struct as JSON into the response body. +// PureJSON, unlike JSON, does not replace special html characters with their unicode entities. +func (c *Context) PureJSON(code int, obj interface{}) { + c.Render(code, render.PureJSON{Data: obj}) +} + // XML serializes the given struct as XML into the response body. // It also sets the Content-Type as "application/xml". func (c *Context) XML(code int, obj interface{}) { diff --git a/context_17.go b/context_17.go deleted file mode 100644 index 8e9f75ad..00000000 --- a/context_17.go +++ /dev/null @@ -1,17 +0,0 @@ -// Copyright 2018 Gin Core Team. All rights reserved. -// Use of this source code is governed by a MIT style -// license that can be found in the LICENSE file. - -// +build go1.7 - -package gin - -import ( - "github.com/gin-gonic/gin/render" -) - -// PureJSON serializes the given struct as JSON into the response body. -// PureJSON, unlike JSON, does not replace special html characters with their unicode entities. -func (c *Context) PureJSON(code int, obj interface{}) { - c.Render(code, render.PureJSON{Data: obj}) -} diff --git a/context_17_test.go b/context_17_test.go deleted file mode 100644 index 5b9ebcdc..00000000 --- a/context_17_test.go +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright 2018 Gin Core Team. All rights reserved. -// Use of this source code is governed by a MIT style -// license that can be found in the LICENSE file. - -// +build go1.7 - -package gin - -import ( - "net/http" - "net/http/httptest" - "testing" - - "github.com/stretchr/testify/assert" -) - -// Tests that the response is serialized as JSON -// and Content-Type is set to application/json -// and special HTML characters are preserved -func TestContextRenderPureJSON(t *testing.T) { - w := httptest.NewRecorder() - c, _ := CreateTestContext(w) - c.PureJSON(http.StatusCreated, H{"foo": "bar", "html": ""}) - assert.Equal(t, http.StatusCreated, w.Code) - assert.Equal(t, "{\"foo\":\"bar\",\"html\":\"\"}\n", w.Body.String()) - assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type")) -} diff --git a/context_test.go b/context_test.go index dced73fd..83a68470 100644 --- a/context_test.go +++ b/context_test.go @@ -773,6 +773,18 @@ func TestContextRenderNoContentAsciiJSON(t *testing.T) { assert.Equal(t, "application/json", w.Header().Get("Content-Type")) } +// Tests that the response is serialized as JSON +// and Content-Type is set to application/json +// and special HTML characters are preserved +func TestContextRenderPureJSON(t *testing.T) { + w := httptest.NewRecorder() + c, _ := CreateTestContext(w) + c.PureJSON(http.StatusCreated, H{"foo": "bar", "html": ""}) + assert.Equal(t, http.StatusCreated, w.Code) + assert.Equal(t, "{\"foo\":\"bar\",\"html\":\"\"}\n", w.Body.String()) + assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type")) +} + // Tests that the response executes the templates // and responds with Content-Type set to text/html func TestContextRenderHTML(t *testing.T) { diff --git a/debug.go b/debug.go index c5e65b22..1c500b0b 100644 --- a/debug.go +++ b/debug.go @@ -14,7 +14,7 @@ import ( "strings" ) -const ginSupportMinGoVer = 6 +const ginSupportMinGoVer = 7 // IsDebugging returns true if the framework is running in debug mode. // Use SetMode(gin.ReleaseMode) to disable debug mode. diff --git a/render/json.go b/render/json.go index 32d0fc42..84a3724b 100644 --- a/render/json.go +++ b/render/json.go @@ -43,6 +43,11 @@ type AsciiJSON struct { // SecureJSONPrefix is a string which represents SecureJSON prefix. type SecureJSONPrefix string +// PureJSON contains the given interface object. +type PureJSON struct { + Data interface{} +} + var jsonContentType = []string{"application/json; charset=utf-8"} var jsonpContentType = []string{"application/javascript; charset=utf-8"} var jsonAsciiContentType = []string{"application/json"} @@ -159,3 +164,16 @@ func (r AsciiJSON) Render(w http.ResponseWriter) (err error) { func (r AsciiJSON) WriteContentType(w http.ResponseWriter) { writeContentType(w, jsonAsciiContentType) } + +// Render (PureJSON) writes custom ContentType and encodes the given interface object. +func (r PureJSON) Render(w http.ResponseWriter) error { + r.WriteContentType(w) + encoder := json.NewEncoder(w) + encoder.SetEscapeHTML(false) + return encoder.Encode(r.Data) +} + +// WriteContentType (PureJSON) writes custom ContentType. +func (r PureJSON) WriteContentType(w http.ResponseWriter) { + writeContentType(w, jsonContentType) +} diff --git a/render/json_17.go b/render/json_17.go deleted file mode 100644 index 208193c7..00000000 --- a/render/json_17.go +++ /dev/null @@ -1,31 +0,0 @@ -// Copyright 2018 Gin Core Team. All rights reserved. -// Use of this source code is governed by a MIT style -// license that can be found in the LICENSE file. - -// +build go1.7 - -package render - -import ( - "net/http" - - "github.com/gin-gonic/gin/internal/json" -) - -// PureJSON contains the given interface object. -type PureJSON struct { - Data interface{} -} - -// Render (PureJSON) writes custom ContentType and encodes the given interface object. -func (r PureJSON) Render(w http.ResponseWriter) error { - r.WriteContentType(w) - encoder := json.NewEncoder(w) - encoder.SetEscapeHTML(false) - return encoder.Encode(r.Data) -} - -// WriteContentType (PureJSON) writes custom ContentType. -func (r PureJSON) WriteContentType(w http.ResponseWriter) { - writeContentType(w, jsonContentType) -} diff --git a/render/render_17_test.go b/render/render_17_test.go deleted file mode 100644 index 68330090..00000000 --- a/render/render_17_test.go +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright 2018 Gin Core Team. All rights reserved. -// Use of this source code is governed by a MIT style -// license that can be found in the LICENSE file. - -// +build go1.7 - -package render - -import ( - "net/http/httptest" - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestRenderPureJSON(t *testing.T) { - w := httptest.NewRecorder() - data := map[string]interface{}{ - "foo": "bar", - "html": "", - } - err := (PureJSON{data}).Render(w) - assert.NoError(t, err) - assert.Equal(t, "{\"foo\":\"bar\",\"html\":\"\"}\n", w.Body.String()) - assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type")) -} diff --git a/render/render_test.go b/render/render_test.go index 4c9b180d..74b4ddfa 100644 --- a/render/render_test.go +++ b/render/render_test.go @@ -215,6 +215,18 @@ func TestRenderAsciiJSONFail(t *testing.T) { assert.Error(t, (AsciiJSON{data}).Render(w)) } +func TestRenderPureJSON(t *testing.T) { + w := httptest.NewRecorder() + data := map[string]interface{}{ + "foo": "bar", + "html": "", + } + err := (PureJSON{data}).Render(w) + assert.NoError(t, err) + assert.Equal(t, "{\"foo\":\"bar\",\"html\":\"\"}\n", w.Body.String()) + assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type")) +} + type xmlmap map[string]interface{} // Allows type H to be used with xml.Marshal From f5542958adae6e3281fea674030a3f9c5856f3c2 Mon Sep 17 00:00:00 2001 From: thinkerou Date: Wed, 7 Nov 2018 18:59:59 +0800 Subject: [PATCH 02/12] remove build tag --- recovery_test.go | 2 -- response_writer_1.7.go | 4 ++-- response_writer_1.8.go | 4 ++-- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/recovery_test.go b/recovery_test.go index cafaee91..7b56d401 100644 --- a/recovery_test.go +++ b/recovery_test.go @@ -2,8 +2,6 @@ // Use of this source code is governed by a MIT style // license that can be found in the LICENSE file. -// +build go1.7 - package gin import ( diff --git a/response_writer_1.7.go b/response_writer_1.7.go index 801d196b..d8023497 100644 --- a/response_writer_1.7.go +++ b/response_writer_1.7.go @@ -1,9 +1,9 @@ -// +build !go1.8 - // Copyright 2018 Gin Core Team. All rights reserved. // Use of this source code is governed by a MIT style // license that can be found in the LICENSE file. +// +build !go1.8 + package gin // ResponseWriter ... diff --git a/response_writer_1.8.go b/response_writer_1.8.go index 527c0038..47e2f3c1 100644 --- a/response_writer_1.8.go +++ b/response_writer_1.8.go @@ -1,9 +1,9 @@ -// +build go1.8 - // Copyright 2018 Gin Core Team. All rights reserved. // Use of this source code is governed by a MIT style // license that can be found in the LICENSE file. +// +build go1.8 + package gin import ( From 2b7203f2fec66b4be1088ac191e543bc124fe953 Mon Sep 17 00:00:00 2001 From: thinkerou Date: Wed, 7 Nov 2018 19:50:56 +0800 Subject: [PATCH 03/12] remove todo --- context_test.go | 7 ++----- debug.go | 2 +- debug_test.go | 2 +- render/redirect.go | 4 +--- 4 files changed, 5 insertions(+), 10 deletions(-) diff --git a/context_test.go b/context_test.go index 83a68470..641a00c2 100644 --- a/context_test.go +++ b/context_test.go @@ -601,8 +601,7 @@ func TestContextGetCookie(t *testing.T) { } func TestContextBodyAllowedForStatus(t *testing.T) { - // todo(thinkerou): go1.6 not support StatusProcessing - assert.False(t, false, bodyAllowedForStatus(102)) + assert.False(t, false, bodyAllowedForStatus(http.StatusProcessing)) assert.False(t, false, bodyAllowedForStatus(http.StatusNoContent)) assert.False(t, false, bodyAllowedForStatus(http.StatusNotModified)) assert.True(t, true, bodyAllowedForStatus(http.StatusInternalServerError)) @@ -1070,9 +1069,7 @@ func TestContextRenderRedirectAll(t *testing.T) { assert.Panics(t, func() { c.Redirect(299, "/resource") }) assert.Panics(t, func() { c.Redirect(309, "/resource") }) assert.NotPanics(t, func() { c.Redirect(http.StatusMultipleChoices, "/resource") }) - // todo(thinkerou): go1.6 not support StatusPermanentRedirect(308) - // when we upgrade go version we can use http.StatusPermanentRedirect - assert.NotPanics(t, func() { c.Redirect(308, "/resource") }) + assert.NotPanics(t, func() { c.Redirect(http.StatusPermanentRedirect, "/resource") }) } func TestContextNegotiationWithJSON(t *testing.T) { diff --git a/debug.go b/debug.go index 1c500b0b..f0a23794 100644 --- a/debug.go +++ b/debug.go @@ -66,7 +66,7 @@ func getMinVer(v string) (uint64, error) { func debugPrintWARNINGDefault() { if v, e := getMinVer(runtime.Version()); e == nil && v <= ginSupportMinGoVer { - debugPrint(`[WARNING] Now Gin requires Go 1.6 or later and Go 1.7 will be required soon. + debugPrint(`[WARNING] Now Gin requires Go 1.7 or later and Go 1.8 will be required soon. `) } diff --git a/debug_test.go b/debug_test.go index 97ff166b..6c18e6de 100644 --- a/debug_test.go +++ b/debug_test.go @@ -91,7 +91,7 @@ func TestDebugPrintWARNINGDefault(t *testing.T) { }) m, e := getMinVer(runtime.Version()) if e == nil && m <= ginSupportMinGoVer { - assert.Equal(t, "[GIN-debug] [WARNING] Now Gin requires Go 1.6 or later and Go 1.7 will be required soon.\n\n[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.\n\n", re) + assert.Equal(t, "[GIN-debug] [WARNING] Now Gin requires Go 1.7 or later and Go 1.8 will be required soon.\n\n[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.\n\n", re) } else { assert.Equal(t, "[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.\n\n", re) } diff --git a/render/redirect.go b/render/redirect.go index 9c145fe2..c006691c 100644 --- a/render/redirect.go +++ b/render/redirect.go @@ -18,9 +18,7 @@ type Redirect struct { // Render (Redirect) redirects the http request to new location and writes redirect response. func (r Redirect) Render(w http.ResponseWriter) error { - // todo(thinkerou): go1.6 not support StatusPermanentRedirect(308) - // when we upgrade go version we can use http.StatusPermanentRedirect - if (r.Code < 300 || r.Code > 308) && r.Code != 201 { + if (r.Code < http.StatusMultipleChoices || r.Code > http.StatusPermanentRedirect) && r.Code != http.StatusCreated { panic(fmt.Sprintf("Cannot redirect with status code %d", r.Code)) } http.Redirect(w, r.Request, r.Location, r.Code) From 883edea2c10a1b7527e07bbada7c5eb06592b8ab Mon Sep 17 00:00:00 2001 From: thinkerou Date: Wed, 7 Nov 2018 20:02:30 +0800 Subject: [PATCH 04/12] remove go1.6 support: https://github.com/gin-gonic/gin/pull/1383/commits --- context.go | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/context.go b/context.go index d80c04ac..95797056 100644 --- a/context.go +++ b/context.go @@ -438,13 +438,7 @@ func (c *Context) PostFormMap(key string) map[string]string { func (c *Context) GetPostFormMap(key string) (map[string]string, bool) { req := c.Request req.ParseMultipartForm(c.engine.MaxMultipartMemory) - dicts, exist := c.get(req.PostForm, key) - - if !exist && req.MultipartForm != nil && req.MultipartForm.File != nil { - dicts, exist = c.get(req.MultipartForm.Value, key) - } - - return dicts, exist + return c.get(req.PostForm, key) } // get is an internal method and returns a map which satisfy conditions. From bb486e7e21d88677cb655b886f09385841742cb8 Mon Sep 17 00:00:00 2001 From: thinkerou Date: Wed, 7 Nov 2018 20:05:18 +0800 Subject: [PATCH 05/12] update readme --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index d251d2b2..f4ee0fa8 100644 --- a/README.md +++ b/README.md @@ -1001,7 +1001,6 @@ func main() { #### PureJSON Normally, JSON replaces special HTML characters with their unicode entities, e.g. `<` becomes `\u003c`. If you want to encode such characters literally, you can use PureJSON instead. -This feature is unavailable in Go 1.6 and lower. ```go func main() { From a37abf2c0505dca5863f4b52c500ebcb26ca7f94 Mon Sep 17 00:00:00 2001 From: thinkerou Date: Tue, 20 Nov 2018 18:55:50 +0800 Subject: [PATCH 06/12] remove go1.7 support --- .travis.yml | 1 - README.md | 2 +- debug.go | 4 +-- examples/graceful-shutdown/close/server.go | 2 -- .../graceful-shutdown/server.go | 2 -- response_writer.go | 13 +++++++++- response_writer_1.7.go | 12 --------- response_writer_1.8.go | 25 ------------------- 8 files changed, 15 insertions(+), 46 deletions(-) delete mode 100644 response_writer_1.7.go delete mode 100644 response_writer_1.8.go diff --git a/.travis.yml b/.travis.yml index 5f4e8037..4bfd170e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,6 @@ language: go sudo: false go: - - 1.7.x - 1.8.x - 1.9.x - 1.10.x diff --git a/README.md b/README.md index f4ee0fa8..bd710ad7 100644 --- a/README.md +++ b/README.md @@ -120,7 +120,7 @@ $ go run main.go ## Prerequisite -Now Gin requires Go 1.7 or later and Go 1.8 will be required soon. +Now Gin requires Go 1.8 or later and Go 1.9 will be required soon. ## Quick start diff --git a/debug.go b/debug.go index f0a23794..0fb60e53 100644 --- a/debug.go +++ b/debug.go @@ -14,7 +14,7 @@ import ( "strings" ) -const ginSupportMinGoVer = 7 +const ginSupportMinGoVer = 8 // IsDebugging returns true if the framework is running in debug mode. // Use SetMode(gin.ReleaseMode) to disable debug mode. @@ -66,7 +66,7 @@ func getMinVer(v string) (uint64, error) { func debugPrintWARNINGDefault() { if v, e := getMinVer(runtime.Version()); e == nil && v <= ginSupportMinGoVer { - debugPrint(`[WARNING] Now Gin requires Go 1.7 or later and Go 1.8 will be required soon. + debugPrint(`[WARNING] Now Gin requires Go 1.8 or later and Go 1.9 will be required soon. `) } diff --git a/examples/graceful-shutdown/close/server.go b/examples/graceful-shutdown/close/server.go index 9c4e90fa..9fd5bed6 100644 --- a/examples/graceful-shutdown/close/server.go +++ b/examples/graceful-shutdown/close/server.go @@ -1,5 +1,3 @@ -// +build go1.8 - package main import ( diff --git a/examples/graceful-shutdown/graceful-shutdown/server.go b/examples/graceful-shutdown/graceful-shutdown/server.go index af4f2146..8a5b8faf 100644 --- a/examples/graceful-shutdown/graceful-shutdown/server.go +++ b/examples/graceful-shutdown/graceful-shutdown/server.go @@ -1,5 +1,3 @@ -// +build go1.8 - package main import ( diff --git a/response_writer.go b/response_writer.go index 923b53f8..26826689 100644 --- a/response_writer.go +++ b/response_writer.go @@ -16,7 +16,8 @@ const ( defaultStatus = http.StatusOK ) -type responseWriterBase interface { +// ResponseWriter ... +type ResponseWriter interface { http.ResponseWriter http.Hijacker http.Flusher @@ -37,6 +38,9 @@ type responseWriterBase interface { // Forces to write the http header (status code + headers). WriteHeaderNow() + + // get the http.Pusher for server push + Pusher() http.Pusher } type responseWriter struct { @@ -113,3 +117,10 @@ func (w *responseWriter) Flush() { w.WriteHeaderNow() w.ResponseWriter.(http.Flusher).Flush() } + +func (w *responseWriter) Pusher() (pusher http.Pusher) { + if pusher, ok := w.ResponseWriter.(http.Pusher); ok { + return pusher + } + return nil +} diff --git a/response_writer_1.7.go b/response_writer_1.7.go deleted file mode 100644 index d8023497..00000000 --- a/response_writer_1.7.go +++ /dev/null @@ -1,12 +0,0 @@ -// Copyright 2018 Gin Core Team. All rights reserved. -// Use of this source code is governed by a MIT style -// license that can be found in the LICENSE file. - -// +build !go1.8 - -package gin - -// ResponseWriter ... -type ResponseWriter interface { - responseWriterBase -} diff --git a/response_writer_1.8.go b/response_writer_1.8.go deleted file mode 100644 index 47e2f3c1..00000000 --- a/response_writer_1.8.go +++ /dev/null @@ -1,25 +0,0 @@ -// Copyright 2018 Gin Core Team. All rights reserved. -// Use of this source code is governed by a MIT style -// license that can be found in the LICENSE file. - -// +build go1.8 - -package gin - -import ( - "net/http" -) - -// ResponseWriter ... -type ResponseWriter interface { - responseWriterBase - // get the http.Pusher for server push - Pusher() http.Pusher -} - -func (w *responseWriter) Pusher() (pusher http.Pusher) { - if pusher, ok := w.ResponseWriter.(http.Pusher); ok { - return pusher - } - return nil -} From 4fe7ad9d79174049e5336f6883a38369230007df Mon Sep 17 00:00:00 2001 From: thinkerou Date: Tue, 20 Nov 2018 19:01:24 +0800 Subject: [PATCH 07/12] fix embedmd error --- README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/README.md b/README.md index bd710ad7..dd34afda 100644 --- a/README.md +++ b/README.md @@ -1545,8 +1545,6 @@ If you are using Go 1.8, you may not need to use this library! Consider using ht [embedmd]:# (examples/graceful-shutdown/graceful-shutdown/server.go go) ```go -// +build go1.8 - package main import ( From 976263d3793e1025b026490086200f351e255a65 Mon Sep 17 00:00:00 2001 From: thinkerou Date: Wed, 19 Dec 2018 22:59:47 +0800 Subject: [PATCH 08/12] test --- .travis.yml | 2 ++ context.go | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 4bfd170e..2eeb0b3d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,8 @@ language: go sudo: false go: + - 1.6.x + - 1.7.x - 1.8.x - 1.9.x - 1.10.x diff --git a/context.go b/context.go index 6b2d7c87..cf037d0a 100644 --- a/context.go +++ b/context.go @@ -419,11 +419,11 @@ func (c *Context) GetPostFormArray(key string) ([]string, bool) { if values := req.PostForm[key]; len(values) > 0 { return values, true } - if req.MultipartForm != nil && req.MultipartForm.File != nil { + /*if req.MultipartForm != nil && req.MultipartForm.File != nil { if values := req.MultipartForm.Value[key]; len(values) > 0 { return values, true } - } + }*/ return []string{}, false } From 87ae880c8efd9d892e98bc6f8c8a0c73f8e6441b Mon Sep 17 00:00:00 2001 From: thinkerou Date: Wed, 19 Dec 2018 23:06:52 +0800 Subject: [PATCH 09/12] revert it --- context.go | 5 ----- 1 file changed, 5 deletions(-) diff --git a/context.go b/context.go index cf037d0a..e000c53e 100644 --- a/context.go +++ b/context.go @@ -419,11 +419,6 @@ func (c *Context) GetPostFormArray(key string) ([]string, bool) { if values := req.PostForm[key]; len(values) > 0 { return values, true } - /*if req.MultipartForm != nil && req.MultipartForm.File != nil { - if values := req.MultipartForm.Value[key]; len(values) > 0 { - return values, true - } - }*/ return []string{}, false } From 36b0ffc00615d7dddce855a97d66251dce2feef8 Mon Sep 17 00:00:00 2001 From: thinkerou Date: Wed, 19 Dec 2018 23:09:31 +0800 Subject: [PATCH 10/12] revert it --- .travis.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 2eeb0b3d..4bfd170e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,6 @@ language: go sudo: false go: - - 1.6.x - - 1.7.x - 1.8.x - 1.9.x - 1.10.x From 3a5e3c013e94292b8a3a858a15821d99738bbc41 Mon Sep 17 00:00:00 2001 From: thinkerou Date: Fri, 18 Jan 2019 11:57:09 +0800 Subject: [PATCH 11/12] remove context_17 --- context_17.go | 17 ----------------- context_17_test.go | 27 --------------------------- 2 files changed, 44 deletions(-) delete mode 100644 context_17.go delete mode 100644 context_17_test.go diff --git a/context_17.go b/context_17.go deleted file mode 100644 index 8e9f75ad..00000000 --- a/context_17.go +++ /dev/null @@ -1,17 +0,0 @@ -// Copyright 2018 Gin Core Team. All rights reserved. -// Use of this source code is governed by a MIT style -// license that can be found in the LICENSE file. - -// +build go1.7 - -package gin - -import ( - "github.com/gin-gonic/gin/render" -) - -// PureJSON serializes the given struct as JSON into the response body. -// PureJSON, unlike JSON, does not replace special html characters with their unicode entities. -func (c *Context) PureJSON(code int, obj interface{}) { - c.Render(code, render.PureJSON{Data: obj}) -} diff --git a/context_17_test.go b/context_17_test.go deleted file mode 100644 index 5b9ebcdc..00000000 --- a/context_17_test.go +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright 2018 Gin Core Team. All rights reserved. -// Use of this source code is governed by a MIT style -// license that can be found in the LICENSE file. - -// +build go1.7 - -package gin - -import ( - "net/http" - "net/http/httptest" - "testing" - - "github.com/stretchr/testify/assert" -) - -// Tests that the response is serialized as JSON -// and Content-Type is set to application/json -// and special HTML characters are preserved -func TestContextRenderPureJSON(t *testing.T) { - w := httptest.NewRecorder() - c, _ := CreateTestContext(w) - c.PureJSON(http.StatusCreated, H{"foo": "bar", "html": ""}) - assert.Equal(t, http.StatusCreated, w.Code) - assert.Equal(t, "{\"foo\":\"bar\",\"html\":\"\"}\n", w.Body.String()) - assert.Equal(t, "application/json; charset=utf-8", w.Header().Get("Content-Type")) -} From 77d2d3576b2e25188818750a0c5347ca50d39b9c Mon Sep 17 00:00:00 2001 From: thinkerou Date: Sun, 3 Mar 2019 12:16:57 +0800 Subject: [PATCH 12/12] add pusher test --- gin_integration_test.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/gin_integration_test.go b/gin_integration_test.go index b80cbb24..9beec14d 100644 --- a/gin_integration_test.go +++ b/gin_integration_test.go @@ -8,6 +8,7 @@ import ( "bufio" "crypto/tls" "fmt" + "html/template" "io/ioutil" "net" "net/http" @@ -69,6 +70,42 @@ func TestRunTLS(t *testing.T) { testRequest(t, "https://localhost:8443/example") } +func TestPusher(t *testing.T) { + var html = template.Must(template.New("https").Parse(` + + + Https Test + + + +

Welcome, Ginner!

+ + +`)) + + router := New() + router.Static("./assets", "./assets") + router.SetHTMLTemplate(html) + + go func() { + router.GET("/pusher", func(c *Context) { + if pusher := c.Writer.Pusher(); pusher != nil { + pusher.Push("/assets/app.js", nil) + } + c.String(http.StatusOK, "it worked") + }) + + assert.NoError(t, router.RunTLS(":8449", "./testdata/certificate/cert.pem", "./testdata/certificate/key.pem")) + }() + + // have to wait for the goroutine to start and run the server + // otherwise the main thread will complete + time.Sleep(5 * time.Millisecond) + + assert.Error(t, router.RunTLS(":8449", "./testdata/certificate/cert.pem", "./testdata/certificate/key.pem")) + testRequest(t, "https://localhost:8449/pusher") +} + func TestRunEmptyWithEnv(t *testing.T) { os.Setenv("PORT", "3123") router := New()