...

Source file src/github.com/gin-gonic/gin/utils_test.go

Documentation: github.com/gin-gonic/gin

     1  // Copyright 2014 Manu Martinez-Almeida. All rights reserved.
     2  // Use of this source code is governed by a MIT style
     3  // license that can be found in the LICENSE file.
     4  
     5  package gin
     6  
     7  import (
     8  	"bytes"
     9  	"encoding/xml"
    10  	"fmt"
    11  	"net/http"
    12  	"testing"
    13  
    14  	"github.com/stretchr/testify/assert"
    15  )
    16  
    17  func init() {
    18  	SetMode(TestMode)
    19  }
    20  
    21  func BenchmarkParseAccept(b *testing.B) {
    22  	for i := 0; i < b.N; i++ {
    23  		parseAccept("text/html , application/xhtml+xml,application/xml;q=0.9,  */* ;q=0.8")
    24  	}
    25  }
    26  
    27  type testStruct struct {
    28  	T *testing.T
    29  }
    30  
    31  func (t *testStruct) ServeHTTP(w http.ResponseWriter, req *http.Request) {
    32  	assert.Equal(t.T, "POST", req.Method)
    33  	assert.Equal(t.T, "/path", req.URL.Path)
    34  	w.WriteHeader(http.StatusInternalServerError)
    35  	fmt.Fprint(w, "hello")
    36  }
    37  
    38  func TestWrap(t *testing.T) {
    39  	router := New()
    40  	router.POST("/path", WrapH(&testStruct{t}))
    41  	router.GET("/path2", WrapF(func(w http.ResponseWriter, req *http.Request) {
    42  		assert.Equal(t, "GET", req.Method)
    43  		assert.Equal(t, "/path2", req.URL.Path)
    44  		w.WriteHeader(http.StatusBadRequest)
    45  		fmt.Fprint(w, "hola!")
    46  	}))
    47  
    48  	w := PerformRequest(router, "POST", "/path")
    49  	assert.Equal(t, http.StatusInternalServerError, w.Code)
    50  	assert.Equal(t, "hello", w.Body.String())
    51  
    52  	w = PerformRequest(router, "GET", "/path2")
    53  	assert.Equal(t, http.StatusBadRequest, w.Code)
    54  	assert.Equal(t, "hola!", w.Body.String())
    55  }
    56  
    57  func TestLastChar(t *testing.T) {
    58  	assert.Equal(t, uint8('a'), lastChar("hola"))
    59  	assert.Equal(t, uint8('s'), lastChar("adios"))
    60  	assert.Panics(t, func() { lastChar("") })
    61  }
    62  
    63  func TestParseAccept(t *testing.T) {
    64  	parts := parseAccept("text/html , application/xhtml+xml,application/xml;q=0.9,  */* ;q=0.8")
    65  	assert.Len(t, parts, 4)
    66  	assert.Equal(t, "text/html", parts[0])
    67  	assert.Equal(t, "application/xhtml+xml", parts[1])
    68  	assert.Equal(t, "application/xml", parts[2])
    69  	assert.Equal(t, "*/*", parts[3])
    70  }
    71  
    72  func TestChooseData(t *testing.T) {
    73  	A := "a"
    74  	B := "b"
    75  	assert.Equal(t, A, chooseData(A, B))
    76  	assert.Equal(t, B, chooseData(nil, B))
    77  	assert.Panics(t, func() { chooseData(nil, nil) })
    78  }
    79  
    80  func TestFilterFlags(t *testing.T) {
    81  	result := filterFlags("text/html ")
    82  	assert.Equal(t, "text/html", result)
    83  
    84  	result = filterFlags("text/html;")
    85  	assert.Equal(t, "text/html", result)
    86  }
    87  
    88  func TestFunctionName(t *testing.T) {
    89  	assert.Regexp(t, `^(.*/vendor/)?github.com/gin-gonic/gin.somefunction$`, nameOfFunction(somefunction))
    90  }
    91  
    92  func somefunction() {
    93  	// this empty function is used by TestFunctionName()
    94  }
    95  
    96  func TestJoinPaths(t *testing.T) {
    97  	assert.Equal(t, "", joinPaths("", ""))
    98  	assert.Equal(t, "/", joinPaths("", "/"))
    99  	assert.Equal(t, "/a", joinPaths("/a", ""))
   100  	assert.Equal(t, "/a/", joinPaths("/a/", ""))
   101  	assert.Equal(t, "/a/", joinPaths("/a/", "/"))
   102  	assert.Equal(t, "/a/", joinPaths("/a", "/"))
   103  	assert.Equal(t, "/a/hola", joinPaths("/a", "/hola"))
   104  	assert.Equal(t, "/a/hola", joinPaths("/a/", "/hola"))
   105  	assert.Equal(t, "/a/hola/", joinPaths("/a/", "/hola/"))
   106  	assert.Equal(t, "/a/hola/", joinPaths("/a/", "/hola//"))
   107  }
   108  
   109  type bindTestStruct struct {
   110  	Foo string `form:"foo" binding:"required"`
   111  	Bar int    `form:"bar" binding:"min=4"`
   112  }
   113  
   114  func TestBindMiddleware(t *testing.T) {
   115  	var value *bindTestStruct
   116  	var called bool
   117  	router := New()
   118  	router.GET("/", Bind(bindTestStruct{}), func(c *Context) {
   119  		called = true
   120  		value = c.MustGet(BindKey).(*bindTestStruct)
   121  	})
   122  	PerformRequest(router, "GET", "/?foo=hola&bar=10")
   123  	assert.True(t, called)
   124  	assert.Equal(t, "hola", value.Foo)
   125  	assert.Equal(t, 10, value.Bar)
   126  
   127  	called = false
   128  	PerformRequest(router, "GET", "/?foo=hola&bar=1")
   129  	assert.False(t, called)
   130  
   131  	assert.Panics(t, func() {
   132  		Bind(&bindTestStruct{})
   133  	})
   134  }
   135  
   136  func TestMarshalXMLforH(t *testing.T) {
   137  	h := H{
   138  		"": "test",
   139  	}
   140  	var b bytes.Buffer
   141  	enc := xml.NewEncoder(&b)
   142  	var x xml.StartElement
   143  	e := h.MarshalXML(enc, x)
   144  	assert.Error(t, e)
   145  }
   146  
   147  func TestIsASCII(t *testing.T) {
   148  	assert.Equal(t, isASCII("test"), true)
   149  	assert.Equal(t, isASCII("๐Ÿงก๐Ÿ’›๐Ÿ’š๐Ÿ’™๐Ÿ’œ"), false)
   150  }
   151  

View as plain text