2015-04-08 13:32:50 +00:00
|
|
|
// 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 (
|
2018-08-12 13:38:07 +00:00
|
|
|
"bytes"
|
|
|
|
"encoding/xml"
|
2015-05-19 20:18:58 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2015-04-08 13:32:50 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
SetMode(TestMode)
|
|
|
|
}
|
|
|
|
|
2015-05-19 22:39:52 +00:00
|
|
|
type testStruct struct {
|
|
|
|
T *testing.T
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *testStruct) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
2017-11-21 13:18:45 +00:00
|
|
|
assert.Equal(t.T, "POST", req.Method)
|
|
|
|
assert.Equal(t.T, "/path", req.URL.Path)
|
2018-08-14 01:51:56 +00:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
2015-05-19 22:39:52 +00:00
|
|
|
fmt.Fprint(w, "hello")
|
|
|
|
}
|
|
|
|
|
2015-05-19 20:18:58 +00:00
|
|
|
func TestWrap(t *testing.T) {
|
|
|
|
router := New()
|
2015-05-19 22:39:52 +00:00
|
|
|
router.POST("/path", WrapH(&testStruct{t}))
|
|
|
|
router.GET("/path2", WrapF(func(w http.ResponseWriter, req *http.Request) {
|
2017-11-21 13:18:45 +00:00
|
|
|
assert.Equal(t, "GET", req.Method)
|
|
|
|
assert.Equal(t, "/path2", req.URL.Path)
|
2018-08-14 01:51:56 +00:00
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
2015-05-19 20:18:58 +00:00
|
|
|
fmt.Fprint(w, "hola!")
|
|
|
|
}))
|
|
|
|
|
2015-05-19 22:39:52 +00:00
|
|
|
w := performRequest(router, "POST", "/path")
|
2018-08-14 01:51:56 +00:00
|
|
|
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
2017-11-21 13:18:45 +00:00
|
|
|
assert.Equal(t, "hello", w.Body.String())
|
2015-05-19 20:18:58 +00:00
|
|
|
|
2015-05-19 22:39:52 +00:00
|
|
|
w = performRequest(router, "GET", "/path2")
|
2018-08-14 01:51:56 +00:00
|
|
|
assert.Equal(t, http.StatusBadRequest, w.Code)
|
2017-11-21 13:18:45 +00:00
|
|
|
assert.Equal(t, "hola!", w.Body.String())
|
2015-05-19 20:18:58 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 13:32:50 +00:00
|
|
|
func TestLastChar(t *testing.T) {
|
2017-11-21 13:18:45 +00:00
|
|
|
assert.Equal(t, uint8('a'), lastChar("hola"))
|
|
|
|
assert.Equal(t, uint8('s'), lastChar("adios"))
|
2015-04-08 13:32:50 +00:00
|
|
|
assert.Panics(t, func() { lastChar("") })
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseAccept(t *testing.T) {
|
|
|
|
parts := parseAccept("text/html , application/xhtml+xml,application/xml;q=0.9, */* ;q=0.8")
|
|
|
|
assert.Len(t, parts, 4)
|
2017-11-21 13:18:45 +00:00
|
|
|
assert.Equal(t, "text/html", parts[0])
|
|
|
|
assert.Equal(t, "application/xhtml+xml", parts[1])
|
|
|
|
assert.Equal(t, "application/xml", parts[2])
|
|
|
|
assert.Equal(t, "*/*", parts[3])
|
2015-04-08 13:32:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestChooseData(t *testing.T) {
|
|
|
|
A := "a"
|
|
|
|
B := "b"
|
2017-11-21 13:18:45 +00:00
|
|
|
assert.Equal(t, A, chooseData(A, B))
|
|
|
|
assert.Equal(t, B, chooseData(nil, B))
|
2015-04-08 13:32:50 +00:00
|
|
|
assert.Panics(t, func() { chooseData(nil, nil) })
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFilterFlags(t *testing.T) {
|
|
|
|
result := filterFlags("text/html ")
|
2017-11-21 13:18:45 +00:00
|
|
|
assert.Equal(t, "text/html", result)
|
2015-04-08 13:32:50 +00:00
|
|
|
|
|
|
|
result = filterFlags("text/html;")
|
2017-11-21 13:18:45 +00:00
|
|
|
assert.Equal(t, "text/html", result)
|
2015-04-08 13:32:50 +00:00
|
|
|
}
|
|
|
|
|
2015-04-09 10:15:02 +00:00
|
|
|
func TestFunctionName(t *testing.T) {
|
2015-08-21 18:57:53 +00:00
|
|
|
assert.Regexp(t, `^(.*/vendor/)?github.com/gin-gonic/gin.somefunction$`, nameOfFunction(somefunction))
|
2015-04-09 10:15:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func somefunction() {
|
2015-05-12 13:22:13 +00:00
|
|
|
// this empty function is used by TestFunctionName()
|
2015-04-09 10:15:02 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 13:32:50 +00:00
|
|
|
func TestJoinPaths(t *testing.T) {
|
2017-11-21 13:18:45 +00:00
|
|
|
assert.Equal(t, "", joinPaths("", ""))
|
|
|
|
assert.Equal(t, "/", joinPaths("", "/"))
|
|
|
|
assert.Equal(t, "/a", joinPaths("/a", ""))
|
|
|
|
assert.Equal(t, "/a/", joinPaths("/a/", ""))
|
|
|
|
assert.Equal(t, "/a/", joinPaths("/a/", "/"))
|
|
|
|
assert.Equal(t, "/a/", joinPaths("/a", "/"))
|
|
|
|
assert.Equal(t, "/a/hola", joinPaths("/a", "/hola"))
|
|
|
|
assert.Equal(t, "/a/hola", joinPaths("/a/", "/hola"))
|
|
|
|
assert.Equal(t, "/a/hola/", joinPaths("/a/", "/hola/"))
|
|
|
|
assert.Equal(t, "/a/hola/", joinPaths("/a/", "/hola//"))
|
2015-04-08 13:32:50 +00:00
|
|
|
}
|
2015-07-04 17:56:43 +00:00
|
|
|
|
|
|
|
type bindTestStruct struct {
|
|
|
|
Foo string `form:"foo" binding:"required"`
|
|
|
|
Bar int `form:"bar" binding:"min=4"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBindMiddleware(t *testing.T) {
|
|
|
|
var value *bindTestStruct
|
|
|
|
var called bool
|
|
|
|
router := New()
|
|
|
|
router.GET("/", Bind(bindTestStruct{}), func(c *Context) {
|
|
|
|
called = true
|
|
|
|
value = c.MustGet(BindKey).(*bindTestStruct)
|
|
|
|
})
|
|
|
|
performRequest(router, "GET", "/?foo=hola&bar=10")
|
|
|
|
assert.True(t, called)
|
2017-11-21 13:18:45 +00:00
|
|
|
assert.Equal(t, "hola", value.Foo)
|
|
|
|
assert.Equal(t, 10, value.Bar)
|
2015-07-04 17:56:43 +00:00
|
|
|
|
|
|
|
called = false
|
|
|
|
performRequest(router, "GET", "/?foo=hola&bar=1")
|
|
|
|
assert.False(t, called)
|
|
|
|
|
|
|
|
assert.Panics(t, func() {
|
|
|
|
Bind(&bindTestStruct{})
|
|
|
|
})
|
|
|
|
}
|
2018-08-12 13:38:07 +00:00
|
|
|
|
|
|
|
func TestMarshalXMLforH(t *testing.T) {
|
|
|
|
h := H{
|
|
|
|
"": "test",
|
|
|
|
}
|
|
|
|
var b bytes.Buffer
|
|
|
|
enc := xml.NewEncoder(&b)
|
|
|
|
var x xml.StartElement
|
|
|
|
e := h.MarshalXML(enc, x)
|
|
|
|
assert.Error(t, e)
|
|
|
|
}
|