Skip to content
Myr

utils_test.go

This file

Path

utils_test.go

173 lines · 4.5 KiB · Go

Last changed by

test(context): use http.StatusContinue constant instead of magic number 100 (#4542)Mehrdad Banikianbefore adoptionfb25834

History of this file

Take it

1// Copyright 2014 Manu Martinez-Almeida. All rights reserved.2// Use of this source code is governed by a MIT style3// license that can be found in the LICENSE file.45package gin67import (8	"bytes"9	"encoding/xml"10	"fmt"11	"math"12	"net/http"13	"testing"1415	"github.com/stretchr/testify/assert"16	"github.com/stretchr/testify/require"17)1819func init() {20	SetMode(TestMode)21}2223func BenchmarkParseAccept(b *testing.B) {24	for b.Loop() {25		parseAccept("text/html , application/xhtml+xml,application/xml;q=0.9,  */* ;q=0.8")26	}27}2829type testStruct struct {30	T *testing.T31}3233func (t *testStruct) ServeHTTP(w http.ResponseWriter, req *http.Request) {34	assert.Equal(t.T, http.MethodPost, req.Method)35	assert.Equal(t.T, "/path", req.URL.Path)36	w.WriteHeader(http.StatusInternalServerError)37	fmt.Fprint(w, "hello")38}3940func TestWrap(t *testing.T) {41	router := New()42	router.POST("/path", WrapH(&testStruct{t}))43	router.GET("/path2", WrapF(func(w http.ResponseWriter, req *http.Request) {44		assert.Equal(t, http.MethodGet, req.Method)45		assert.Equal(t, "/path2", req.URL.Path)46		w.WriteHeader(http.StatusBadRequest)47		fmt.Fprint(w, "hola!")48	}))4950	w := PerformRequest(router, http.MethodPost, "/path")51	assert.Equal(t, http.StatusInternalServerError, w.Code)52	assert.Equal(t, "hello", w.Body.String())5354	w = PerformRequest(router, http.MethodGet, "/path2")55	assert.Equal(t, http.StatusBadRequest, w.Code)56	assert.Equal(t, "hola!", w.Body.String())57}5859func TestLastChar(t *testing.T) {60	assert.Equal(t, uint8('a'), lastChar("hola"))61	assert.Equal(t, uint8('s'), lastChar("adios"))62	assert.Panics(t, func() { lastChar("") })63}6465func TestParseAccept(t *testing.T) {66	parts := parseAccept("text/html , application/xhtml+xml,application/xml;q=0.9,  */* ;q=0.8")67	assert.Len(t, parts, 4)68	assert.Equal(t, "text/html", parts[0])69	assert.Equal(t, "application/xhtml+xml", parts[1])70	assert.Equal(t, "application/xml", parts[2])71	assert.Equal(t, "*/*", parts[3])72}7374func TestChooseData(t *testing.T) {75	A := "a"76	B := "b"77	assert.Equal(t, A, chooseData(A, B))78	assert.Equal(t, B, chooseData(nil, B))79	assert.Panics(t, func() { chooseData(nil, nil) })80}8182func TestFilterFlags(t *testing.T) {83	result := filterFlags("text/html ")84	assert.Equal(t, "text/html", result)8586	result = filterFlags("text/html;")87	assert.Equal(t, "text/html", result)88}8990func TestFunctionName(t *testing.T) {91	assert.Regexp(t, `^(.*/vendor/)?github.com/gin-gonic/gin.somefunction$`, nameOfFunction(somefunction))92}9394func somefunction() {95	// this empty function is used by TestFunctionName()96}9798func TestJoinPaths(t *testing.T) {99	assert.Empty(t, joinPaths("", ""))100	assert.Equal(t, "/", joinPaths("", "/"))101	assert.Equal(t, "/a", joinPaths("/a", ""))102	assert.Equal(t, "/a/", joinPaths("/a/", ""))103	assert.Equal(t, "/a/", joinPaths("/a/", "/"))104	assert.Equal(t, "/a/", joinPaths("/a", "/"))105	assert.Equal(t, "/a/hola", joinPaths("/a", "/hola"))106	assert.Equal(t, "/a/hola", joinPaths("/a/", "/hola"))107	assert.Equal(t, "/a/hola/", joinPaths("/a/", "/hola/"))108	assert.Equal(t, "/a/hola/", joinPaths("/a/", "/hola//"))109}110111type bindTestStruct struct {112	Foo string `form:"foo" binding:"required"`113	Bar int    `form:"bar" binding:"min=4"`114}115116func TestBindMiddleware(t *testing.T) {117	var value *bindTestStruct118	var called bool119	router := New()120	router.GET("/", Bind(bindTestStruct{}), func(c *Context) {121		called = true122		value = c.MustGet(BindKey).(*bindTestStruct)123	})124	PerformRequest(router, http.MethodGet, "/?foo=hola&bar=10")125	assert.True(t, called)126	assert.Equal(t, "hola", value.Foo)127	assert.Equal(t, 10, value.Bar)128129	called = false130	PerformRequest(router, http.MethodGet, "/?foo=hola&bar=1")131	assert.False(t, called)132133	assert.Panics(t, func() {134		Bind(&bindTestStruct{})135	})136}137138func TestMarshalXMLforH(t *testing.T) {139	h := H{140		"": "test",141	}142	var b bytes.Buffer143	enc := xml.NewEncoder(&b)144	var x xml.StartElement145	e := h.MarshalXML(enc, x)146	assert.Error(t, e)147}148149func TestMarshalXMLforHSuccess(t *testing.T) {150	h := H{151		"key1": "value1",152		"key2": 123,153	}154	data, err := xml.Marshal(h)155	require.NoError(t, err)156	assert.Contains(t, string(data), "<key1>value1</key1>")157	assert.Contains(t, string(data), "<key2>123</key2>")158}159160func TestIsASCII(t *testing.T) {161	assert.True(t, isASCII("test"))162	assert.False(t, isASCII("🧡💛💚💙💜"))163}164165func TestSafeInt8(t *testing.T) {166	assert.Equal(t, int8(100), safeInt8(100))167	assert.Equal(t, int8(math.MaxInt8), safeInt8(int(math.MaxInt8)+123))168}169170func TestSafeUint16(t *testing.T) {171	assert.Equal(t, uint16(100), safeUint16(100))172	assert.Equal(t, uint16(math.MaxUint16), safeUint16(int(math.MaxUint16)+123))173}