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 fileTake it
The walk stopped after 200 commits: lines older than cf32d2d point at it.
chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d
lines 1–10why, one step back1// 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"refactor(utils): move util functions to utils.go (#4467)OHZEKI Naokibefore adoptiond1a1534
line 11why, one step back11 "math"chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d
lines 12–15why, one step back12 "net/http"13 "testing"1415 "github.com/stretchr/testify/assert"test(context): use http.StatusContinue constant instead of magic number 100 (#4542)Mehrdad Banikianbefore adoptionfb25834
line 16why, one step back16 "github.com/stretchr/testify/require"chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d
lines 17–23why, one step back17)1819func init() {20 SetMode(TestMode)21}2223func BenchmarkParseAccept(b *testing.B) {refactor: use b.Loop() to simplify the code and improve performance (#4432)efckingbefore adoptiona85ef5c
line 24why, one step back24 for b.Loop() {chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d
lines 25–98why, one step back25 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) {ci(golangci-lint): update configuration and fix lint issues (#4247)Flc゛before adoptionc4287b1
line 99why, one step back99 assert.Empty(t, joinPaths("", ""))chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d
lines 100–146why, one step back100 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)test(context): use http.StatusContinue constant instead of magic number 100 (#4542)Mehrdad Banikianbefore adoptionfb25834
lines 147–157why, one step back147}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>")chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d
lines 158–162why, one step back158}159160func TestIsASCII(t *testing.T) {161 assert.True(t, isASCII("test"))162 assert.False(t, isASCII("🧡💛💚💙💜"))refactor(utils): move util functions to utils.go (#4467)OHZEKI Naokibefore adoptiond1a1534
lines 163–172why, one step back163}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))chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d
line 173why, one step back173}