middleware_test.go
This file
Path
middleware_test.go
253 lines · 5.2 KiB · Go
Last changed by
ci(golangci-lint): update configuration and fix lint issues (#4247)Flc゛before adoptionc4287b1
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–205why, 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 "errors"9 "net/http"10 "strings"11 "testing"1213 "github.com/gin-contrib/sse"14 "github.com/stretchr/testify/assert"15)1617func TestMiddlewareGeneralCase(t *testing.T) {18 signature := ""19 router := New()20 router.Use(func(c *Context) {21 signature += "A"22 c.Next()23 signature += "B"24 })25 router.Use(func(c *Context) {26 signature += "C"27 })28 router.GET("/", func(c *Context) {29 signature += "D"30 })31 router.NoRoute(func(c *Context) {32 signature += " X "33 })34 router.NoMethod(func(c *Context) {35 signature += " XX "36 })37 // RUN38 w := PerformRequest(router, http.MethodGet, "/")3940 // TEST41 assert.Equal(t, http.StatusOK, w.Code)42 assert.Equal(t, "ACDB", signature)43}4445func TestMiddlewareNoRoute(t *testing.T) {46 signature := ""47 router := New()48 router.Use(func(c *Context) {49 signature += "A"50 c.Next()51 signature += "B"52 })53 router.Use(func(c *Context) {54 signature += "C"55 c.Next()56 c.Next()57 c.Next()58 c.Next()59 signature += "D"60 })61 router.NoRoute(func(c *Context) {62 signature += "E"63 c.Next()64 signature += "F"65 }, func(c *Context) {66 signature += "G"67 c.Next()68 signature += "H"69 })70 router.NoMethod(func(c *Context) {71 signature += " X "72 })73 // RUN74 w := PerformRequest(router, http.MethodGet, "/")7576 // TEST77 assert.Equal(t, http.StatusNotFound, w.Code)78 assert.Equal(t, "ACEGHFDB", signature)79}8081func TestMiddlewareNoMethodEnabled(t *testing.T) {82 signature := ""83 router := New()84 router.HandleMethodNotAllowed = true85 router.Use(func(c *Context) {86 signature += "A"87 c.Next()88 signature += "B"89 })90 router.Use(func(c *Context) {91 signature += "C"92 c.Next()93 signature += "D"94 })95 router.NoMethod(func(c *Context) {96 signature += "E"97 c.Next()98 signature += "F"99 }, func(c *Context) {100 signature += "G"101 c.Next()102 signature += "H"103 })104 router.NoRoute(func(c *Context) {105 signature += " X "106 })107 router.POST("/", func(c *Context) {108 signature += " XX "109 })110 // RUN111 w := PerformRequest(router, http.MethodGet, "/")112113 // TEST114 assert.Equal(t, http.StatusMethodNotAllowed, w.Code)115 assert.Equal(t, "ACEGHFDB", signature)116}117118func TestMiddlewareNoMethodDisabled(t *testing.T) {119 signature := ""120 router := New()121122 // NoMethod disabled123 router.HandleMethodNotAllowed = false124125 router.Use(func(c *Context) {126 signature += "A"127 c.Next()128 signature += "B"129 })130 router.Use(func(c *Context) {131 signature += "C"132 c.Next()133 signature += "D"134 })135 router.NoMethod(func(c *Context) {136 signature += "E"137 c.Next()138 signature += "F"139 }, func(c *Context) {140 signature += "G"141 c.Next()142 signature += "H"143 })144 router.NoRoute(func(c *Context) {145 signature += " X "146 })147 router.POST("/", func(c *Context) {148 signature += " XX "149 })150151 // RUN152 w := PerformRequest(router, http.MethodGet, "/")153154 // TEST155 assert.Equal(t, http.StatusNotFound, w.Code)156 assert.Equal(t, "AC X DB", signature)157}158159func TestMiddlewareAbort(t *testing.T) {160 signature := ""161 router := New()162 router.Use(func(c *Context) {163 signature += "A"164 })165 router.Use(func(c *Context) {166 signature += "C"167 c.AbortWithStatus(http.StatusUnauthorized)168 c.Next()169 signature += "D"170 })171 router.GET("/", func(c *Context) {172 signature += " X "173 c.Next()174 signature += " XX "175 })176177 // RUN178 w := PerformRequest(router, http.MethodGet, "/")179180 // TEST181 assert.Equal(t, http.StatusUnauthorized, w.Code)182 assert.Equal(t, "ACD", signature)183}184185func TestMiddlewareAbortHandlersChainAndNext(t *testing.T) {186 signature := ""187 router := New()188 router.Use(func(c *Context) {189 signature += "A"190 c.Next()191 c.AbortWithStatus(http.StatusGone)192 signature += "B"193 })194 router.GET("/", func(c *Context) {195 signature += "C"196 c.Next()197 })198 // RUN199 w := PerformRequest(router, http.MethodGet, "/")200201 // TEST202 assert.Equal(t, http.StatusGone, w.Code)203 assert.Equal(t, "ACB", signature)204}205chore: fix some function names in comment (#4131)yangquanshibefore adoptiond00e6a5
line 206why, one step back206// TestMiddlewareFailHandlersChain - ensure that Fail interrupt used middleware in fifo order aschore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d
lines 207–251why, one step back207// as well as Abort208func TestMiddlewareFailHandlersChain(t *testing.T) {209 // SETUP210 signature := ""211 router := New()212 router.Use(func(context *Context) {213 signature += "A"214 context.AbortWithError(http.StatusInternalServerError, errors.New("foo")) //nolint: errcheck215 })216 router.Use(func(context *Context) {217 signature += "B"218 context.Next()219 signature += "C"220 })221 // RUN222 w := PerformRequest(router, http.MethodGet, "/")223224 // TEST225 assert.Equal(t, http.StatusInternalServerError, w.Code)226 assert.Equal(t, "A", signature)227}228229func TestMiddlewareWrite(t *testing.T) {230 router := New()231 router.Use(func(c *Context) {232 c.String(http.StatusBadRequest, "hola\n")233 })234 router.Use(func(c *Context) {235 c.XML(http.StatusBadRequest, H{"foo": "bar"})236 })237 router.Use(func(c *Context) {238 c.JSON(http.StatusBadRequest, H{"foo": "bar"})239 })240 router.GET("/", func(c *Context) {241 c.JSON(http.StatusBadRequest, H{"foo": "bar"})242 }, func(c *Context) {243 c.Render(http.StatusBadRequest, sse.Event{244 Event: "test",245 Data: "message",246 })247 })248249 w := PerformRequest(router, http.MethodGet, "/")250251 assert.Equal(t, http.StatusBadRequest, w.Code)ci(golangci-lint): update configuration and fix lint issues (#4247)Flc゛before adoptionc4287b1
line 252why, one step back252 assert.Equal(t, strings.ReplaceAll("hola\n<map><foo>bar</foo></map>{\"foo\":\"bar\"}{\"foo\":\"bar\"}event:test\ndata:message\n\n", " ", ""), strings.ReplaceAll(w.Body.String(), " ", ""))chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d
line 253why, one step back253}