"Fossies" - the Fresh Open Source Software Archive

Member "gin-1.7.7/middleware_test.go" (24 Nov 2021, 5294 Bytes) of package /linux/www/gin-1.7.7.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Go source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "middleware_test.go": 1.7.6_vs_1.7.7.

    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     "errors"
    9     "net/http"
   10     "strings"
   11     "testing"
   12 
   13     "github.com/gin-contrib/sse"
   14     "github.com/stretchr/testify/assert"
   15 )
   16 
   17 func 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     // RUN
   38     w := performRequest(router, "GET", "/")
   39 
   40     // TEST
   41     assert.Equal(t, http.StatusOK, w.Code)
   42     assert.Equal(t, "ACDB", signature)
   43 }
   44 
   45 func 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     // RUN
   74     w := performRequest(router, "GET", "/")
   75 
   76     // TEST
   77     assert.Equal(t, http.StatusNotFound, w.Code)
   78     assert.Equal(t, "ACEGHFDB", signature)
   79 }
   80 
   81 func TestMiddlewareNoMethodEnabled(t *testing.T) {
   82     signature := ""
   83     router := New()
   84     router.HandleMethodNotAllowed = true
   85     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     // RUN
  111     w := performRequest(router, "GET", "/")
  112 
  113     // TEST
  114     assert.Equal(t, http.StatusMethodNotAllowed, w.Code)
  115     assert.Equal(t, "ACEGHFDB", signature)
  116 }
  117 
  118 func TestMiddlewareNoMethodDisabled(t *testing.T) {
  119     signature := ""
  120     router := New()
  121 
  122     // NoMethod disabled
  123     router.HandleMethodNotAllowed = false
  124 
  125     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     })
  150 
  151     // RUN
  152     w := performRequest(router, "GET", "/")
  153 
  154     // TEST
  155     assert.Equal(t, http.StatusNotFound, w.Code)
  156     assert.Equal(t, "AC X DB", signature)
  157 }
  158 
  159 func 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     })
  176 
  177     // RUN
  178     w := performRequest(router, "GET", "/")
  179 
  180     // TEST
  181     assert.Equal(t, http.StatusUnauthorized, w.Code)
  182     assert.Equal(t, "ACD", signature)
  183 }
  184 
  185 func 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     })
  195     router.GET("/", func(c *Context) {
  196         signature += "C"
  197         c.Next()
  198     })
  199     // RUN
  200     w := performRequest(router, "GET", "/")
  201 
  202     // TEST
  203     assert.Equal(t, http.StatusGone, w.Code)
  204     assert.Equal(t, "ACB", signature)
  205 }
  206 
  207 // TestFailHandlersChain - ensure that Fail interrupt used middleware in fifo order as
  208 // as well as Abort
  209 func TestMiddlewareFailHandlersChain(t *testing.T) {
  210     // SETUP
  211     signature := ""
  212     router := New()
  213     router.Use(func(context *Context) {
  214         signature += "A"
  215         context.AbortWithError(http.StatusInternalServerError, errors.New("foo")) // nolint: errcheck
  216     })
  217     router.Use(func(context *Context) {
  218         signature += "B"
  219         context.Next()
  220         signature += "C"
  221     })
  222     // RUN
  223     w := performRequest(router, "GET", "/")
  224 
  225     // TEST
  226     assert.Equal(t, http.StatusInternalServerError, w.Code)
  227     assert.Equal(t, "A", signature)
  228 }
  229 
  230 func TestMiddlewareWrite(t *testing.T) {
  231     router := New()
  232     router.Use(func(c *Context) {
  233         c.String(http.StatusBadRequest, "hola\n")
  234     })
  235     router.Use(func(c *Context) {
  236         c.XML(http.StatusBadRequest, H{"foo": "bar"})
  237     })
  238     router.Use(func(c *Context) {
  239         c.JSON(http.StatusBadRequest, H{"foo": "bar"})
  240     })
  241     router.GET("/", func(c *Context) {
  242         c.JSON(http.StatusBadRequest, H{"foo": "bar"})
  243     }, func(c *Context) {
  244         c.Render(http.StatusBadRequest, sse.Event{
  245             Event: "test",
  246             Data:  "message",
  247         })
  248     })
  249 
  250     w := performRequest(router, "GET", "/")
  251 
  252     assert.Equal(t, http.StatusBadRequest, w.Code)
  253     assert.Equal(t, strings.Replace("hola\n<map><foo>bar</foo></map>{\"foo\":\"bar\"}{\"foo\":\"bar\"}event:test\ndata:message\n\n", " ", "", -1), strings.Replace(w.Body.String(), " ", "", -1))
  254 }