Browse Source

add deprecated test case (#1176)

* add deprecated test case

* swap assert param

* remove
田欧 8 years ago
parent
commit
9e895470dd
2 changed files with 56 additions and 0 deletions
  1. 25 0
      context_test.go
  2. 31 0
      deprecated_test.go

+ 25 - 0
context_test.go

@@ -676,6 +676,7 @@ func TestContextRenderNoContentSecureJSON(t *testing.T) {
 func TestContextRenderHTML(t *testing.T) {
 	w := httptest.NewRecorder()
 	c, router := CreateTestContext(w)
+
 	templ := template.Must(template.New("t").Parse(`Hello {{.name}}`))
 	router.SetHTMLTemplate(templ)
 
@@ -686,6 +687,30 @@ func TestContextRenderHTML(t *testing.T) {
 	assert.Equal(t, "text/html; charset=utf-8", w.HeaderMap.Get("Content-Type"))
 }
 
+func TestContextRenderHTML2(t *testing.T) {
+	w := httptest.NewRecorder()
+	c, router := CreateTestContext(w)
+
+	// print debug warning log when Engine.trees > 0
+	router.addRoute("GET", "/", HandlersChain{func(_ *Context) {}})
+	assert.Len(t, router.trees, 1)
+
+	var b bytes.Buffer
+	setup(&b)
+	defer teardown()
+
+	templ := template.Must(template.New("t").Parse(`Hello {{.name}}`))
+	router.SetHTMLTemplate(templ)
+
+	assert.Equal(t, "[GIN-debug] [WARNING] Since SetHTMLTemplate() is NOT thread-safe. It should only be called\nat initialization. ie. before any route is registered or the router is listening in a socket:\n\n\trouter := gin.Default()\n\trouter.SetHTMLTemplate(template) // << good place\n\n", b.String())
+
+	c.HTML(201, "t", H{"name": "alexandernyquist"})
+
+	assert.Equal(t, 201, w.Code)
+	assert.Equal(t, "Hello alexandernyquist", w.Body.String())
+	assert.Equal(t, "text/html; charset=utf-8", w.HeaderMap.Get("Content-Type"))
+}
+
 // Tests that no HTML is rendered if code is 204
 func TestContextRenderNoContentHTML(t *testing.T) {
 	w := httptest.NewRecorder()

+ 31 - 0
deprecated_test.go

@@ -0,0 +1,31 @@
+// Copyright 2014 Manu Martinez-Almeida.  All rights reserved.
+// Use of this source code is governed by a MIT style
+// license that can be found in the LICENSE file.
+
+package gin
+
+import (
+	"bytes"
+	"net/http"
+	"net/http/httptest"
+	"testing"
+
+	"github.com/gin-gonic/gin/binding"
+	"github.com/stretchr/testify/assert"
+)
+
+func TestBindWith(t *testing.T) {
+	w := httptest.NewRecorder()
+	c, _ := CreateTestContext(w)
+
+	c.Request, _ = http.NewRequest("POST", "/?foo=bar&bar=foo", bytes.NewBufferString("foo=unused"))
+
+	var obj struct {
+		Foo string `form:"foo"`
+		Bar string `form:"bar"`
+	}
+	assert.NoError(t, c.BindWith(&obj, binding.Form))
+	assert.Equal(t, "foo", obj.Bar)
+	assert.Equal(t, "bar", obj.Foo)
+	assert.Equal(t, 0, w.Body.Len())
+}