2022-05-28 05:42:28 +03:00
|
|
|
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
2015-04-08 03:58:35 +03:00
|
|
|
// Use of this source code is governed by a MIT style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package gin
|
|
|
|
|
|
|
|
import (
|
2015-05-05 17:37:33 +03:00
|
|
|
"fmt"
|
2015-04-08 03:58:35 +03:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"os"
|
2015-05-31 19:44:34 +03:00
|
|
|
"path/filepath"
|
2015-04-08 03:58:35 +03:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2024-07-14 15:33:08 +03:00
|
|
|
"github.com/stretchr/testify/require"
|
2015-04-08 03:58:35 +03:00
|
|
|
)
|
|
|
|
|
2019-02-27 14:56:29 +03:00
|
|
|
type header struct {
|
|
|
|
Key string
|
|
|
|
Value string
|
|
|
|
}
|
|
|
|
|
2022-01-02 14:07:44 +03:00
|
|
|
// PerformRequest for testing gin router.
|
|
|
|
func PerformRequest(r http.Handler, method, path string, headers ...header) *httptest.ResponseRecorder {
|
2019-05-07 14:32:35 +03:00
|
|
|
req := httptest.NewRequest(method, path, nil)
|
2019-02-27 14:56:29 +03:00
|
|
|
for _, h := range headers {
|
|
|
|
req.Header.Add(h.Key, h.Value)
|
|
|
|
}
|
2015-04-08 03:58:35 +03:00
|
|
|
w := httptest.NewRecorder()
|
|
|
|
r.ServeHTTP(w, req)
|
|
|
|
return w
|
|
|
|
}
|
|
|
|
|
|
|
|
func testRouteOK(method string, t *testing.T) {
|
|
|
|
passed := false
|
2015-05-20 00:00:55 +03:00
|
|
|
passedAny := false
|
2015-04-08 03:58:35 +03:00
|
|
|
r := New()
|
2015-05-20 00:00:55 +03:00
|
|
|
r.Any("/test2", func(c *Context) {
|
|
|
|
passedAny = true
|
|
|
|
})
|
2015-05-20 00:22:35 +03:00
|
|
|
r.Handle(method, "/test", func(c *Context) {
|
2015-04-08 03:58:35 +03:00
|
|
|
passed = true
|
2015-05-20 00:22:35 +03:00
|
|
|
})
|
2015-05-18 21:50:46 +03:00
|
|
|
|
2022-01-02 14:07:44 +03:00
|
|
|
w := PerformRequest(r, method, "/test")
|
2015-04-08 03:58:35 +03:00
|
|
|
assert.True(t, passed)
|
2017-11-21 16:18:45 +03:00
|
|
|
assert.Equal(t, http.StatusOK, w.Code)
|
2015-05-20 00:00:55 +03:00
|
|
|
|
2022-01-02 14:07:44 +03:00
|
|
|
PerformRequest(r, method, "/test2")
|
2015-05-20 00:00:55 +03:00
|
|
|
assert.True(t, passedAny)
|
2015-04-08 03:58:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestSingleRouteOK tests that POST route is correctly invoked.
|
|
|
|
func testRouteNotOK(method string, t *testing.T) {
|
|
|
|
passed := false
|
|
|
|
router := New()
|
2015-05-20 00:22:35 +03:00
|
|
|
router.Handle(method, "/test_2", func(c *Context) {
|
2015-04-08 03:58:35 +03:00
|
|
|
passed = true
|
2015-05-20 00:22:35 +03:00
|
|
|
})
|
2015-04-08 03:58:35 +03:00
|
|
|
|
2022-01-02 14:07:44 +03:00
|
|
|
w := PerformRequest(router, method, "/test")
|
2015-04-08 03:58:35 +03:00
|
|
|
|
|
|
|
assert.False(t, passed)
|
2017-11-21 16:18:45 +03:00
|
|
|
assert.Equal(t, http.StatusNotFound, w.Code)
|
2015-04-08 03:58:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestSingleRouteOK tests that POST route is correctly invoked.
|
|
|
|
func testRouteNotOK2(method string, t *testing.T) {
|
|
|
|
passed := false
|
|
|
|
router := New()
|
2015-05-30 15:45:13 +03:00
|
|
|
router.HandleMethodNotAllowed = true
|
2015-04-08 03:58:35 +03:00
|
|
|
var methodRoute string
|
2019-11-29 02:50:49 +03:00
|
|
|
if method == http.MethodPost {
|
|
|
|
methodRoute = http.MethodGet
|
2015-04-08 03:58:35 +03:00
|
|
|
} else {
|
2019-11-29 02:50:49 +03:00
|
|
|
methodRoute = http.MethodPost
|
2015-04-08 03:58:35 +03:00
|
|
|
}
|
2015-05-20 00:22:35 +03:00
|
|
|
router.Handle(methodRoute, "/test", func(c *Context) {
|
2015-04-08 03:58:35 +03:00
|
|
|
passed = true
|
2015-05-20 00:22:35 +03:00
|
|
|
})
|
2015-04-08 03:58:35 +03:00
|
|
|
|
2022-01-02 14:07:44 +03:00
|
|
|
w := PerformRequest(router, method, "/test")
|
2015-04-08 03:58:35 +03:00
|
|
|
|
|
|
|
assert.False(t, passed)
|
2017-11-21 16:18:45 +03:00
|
|
|
assert.Equal(t, http.StatusMethodNotAllowed, w.Code)
|
2015-04-08 03:58:35 +03:00
|
|
|
}
|
|
|
|
|
2015-06-04 02:54:36 +03:00
|
|
|
func TestRouterMethod(t *testing.T) {
|
|
|
|
router := New()
|
|
|
|
router.PUT("/hey2", func(c *Context) {
|
2018-08-14 04:51:56 +03:00
|
|
|
c.String(http.StatusOK, "sup2")
|
2015-06-04 02:54:36 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
router.PUT("/hey", func(c *Context) {
|
2018-08-14 04:51:56 +03:00
|
|
|
c.String(http.StatusOK, "called")
|
2015-06-04 02:54:36 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
router.PUT("/hey3", func(c *Context) {
|
2018-08-14 04:51:56 +03:00
|
|
|
c.String(http.StatusOK, "sup3")
|
2015-06-04 02:54:36 +03:00
|
|
|
})
|
|
|
|
|
2022-01-02 14:07:44 +03:00
|
|
|
w := PerformRequest(router, http.MethodPut, "/hey")
|
2015-06-04 02:54:36 +03:00
|
|
|
|
2018-08-14 04:51:56 +03:00
|
|
|
assert.Equal(t, http.StatusOK, w.Code)
|
2017-11-21 16:18:45 +03:00
|
|
|
assert.Equal(t, "called", w.Body.String())
|
2015-06-04 02:54:36 +03:00
|
|
|
}
|
|
|
|
|
2015-04-08 03:58:35 +03:00
|
|
|
func TestRouterGroupRouteOK(t *testing.T) {
|
2019-11-29 02:50:49 +03:00
|
|
|
testRouteOK(http.MethodGet, t)
|
|
|
|
testRouteOK(http.MethodPost, t)
|
|
|
|
testRouteOK(http.MethodPut, t)
|
|
|
|
testRouteOK(http.MethodPatch, t)
|
|
|
|
testRouteOK(http.MethodHead, t)
|
|
|
|
testRouteOK(http.MethodOptions, t)
|
|
|
|
testRouteOK(http.MethodDelete, t)
|
|
|
|
testRouteOK(http.MethodConnect, t)
|
|
|
|
testRouteOK(http.MethodTrace, t)
|
2015-04-08 03:58:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRouteNotOK(t *testing.T) {
|
2019-11-29 02:50:49 +03:00
|
|
|
testRouteNotOK(http.MethodGet, t)
|
|
|
|
testRouteNotOK(http.MethodPost, t)
|
|
|
|
testRouteNotOK(http.MethodPut, t)
|
|
|
|
testRouteNotOK(http.MethodPatch, t)
|
|
|
|
testRouteNotOK(http.MethodHead, t)
|
|
|
|
testRouteNotOK(http.MethodOptions, t)
|
|
|
|
testRouteNotOK(http.MethodDelete, t)
|
|
|
|
testRouteNotOK(http.MethodConnect, t)
|
|
|
|
testRouteNotOK(http.MethodTrace, t)
|
2015-04-08 03:58:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRouteNotOK2(t *testing.T) {
|
2019-11-29 02:50:49 +03:00
|
|
|
testRouteNotOK2(http.MethodGet, t)
|
|
|
|
testRouteNotOK2(http.MethodPost, t)
|
|
|
|
testRouteNotOK2(http.MethodPut, t)
|
|
|
|
testRouteNotOK2(http.MethodPatch, t)
|
|
|
|
testRouteNotOK2(http.MethodHead, t)
|
|
|
|
testRouteNotOK2(http.MethodOptions, t)
|
|
|
|
testRouteNotOK2(http.MethodDelete, t)
|
|
|
|
testRouteNotOK2(http.MethodConnect, t)
|
|
|
|
testRouteNotOK2(http.MethodTrace, t)
|
2015-04-08 03:58:35 +03:00
|
|
|
}
|
|
|
|
|
2015-07-04 15:13:25 +03:00
|
|
|
func TestRouteRedirectTrailingSlash(t *testing.T) {
|
|
|
|
router := New()
|
|
|
|
router.RedirectFixedPath = false
|
|
|
|
router.RedirectTrailingSlash = true
|
|
|
|
router.GET("/path", func(c *Context) {})
|
|
|
|
router.GET("/path2/", func(c *Context) {})
|
|
|
|
router.POST("/path3", func(c *Context) {})
|
|
|
|
router.PUT("/path4/", func(c *Context) {})
|
|
|
|
|
2022-01-02 14:07:44 +03:00
|
|
|
w := PerformRequest(router, http.MethodGet, "/path/")
|
2017-11-21 16:18:45 +03:00
|
|
|
assert.Equal(t, "/path", w.Header().Get("Location"))
|
2018-08-14 04:51:56 +03:00
|
|
|
assert.Equal(t, http.StatusMovedPermanently, w.Code)
|
2015-07-04 15:13:25 +03:00
|
|
|
|
2022-01-02 14:07:44 +03:00
|
|
|
w = PerformRequest(router, http.MethodGet, "/path2")
|
2017-11-21 16:18:45 +03:00
|
|
|
assert.Equal(t, "/path2/", w.Header().Get("Location"))
|
2018-08-14 04:51:56 +03:00
|
|
|
assert.Equal(t, http.StatusMovedPermanently, w.Code)
|
2015-07-04 15:13:25 +03:00
|
|
|
|
2022-01-02 14:07:44 +03:00
|
|
|
w = PerformRequest(router, http.MethodPost, "/path3/")
|
2017-11-21 16:18:45 +03:00
|
|
|
assert.Equal(t, "/path3", w.Header().Get("Location"))
|
2018-08-14 04:51:56 +03:00
|
|
|
assert.Equal(t, http.StatusTemporaryRedirect, w.Code)
|
2015-07-04 15:13:25 +03:00
|
|
|
|
2022-01-02 14:07:44 +03:00
|
|
|
w = PerformRequest(router, http.MethodPut, "/path4")
|
2017-11-21 16:18:45 +03:00
|
|
|
assert.Equal(t, "/path4/", w.Header().Get("Location"))
|
2018-08-14 04:51:56 +03:00
|
|
|
assert.Equal(t, http.StatusTemporaryRedirect, w.Code)
|
2015-07-04 15:13:25 +03:00
|
|
|
|
2022-01-02 14:07:44 +03:00
|
|
|
w = PerformRequest(router, http.MethodGet, "/path")
|
2018-08-14 04:51:56 +03:00
|
|
|
assert.Equal(t, http.StatusOK, w.Code)
|
2015-07-04 15:13:25 +03:00
|
|
|
|
2022-01-02 14:07:44 +03:00
|
|
|
w = PerformRequest(router, http.MethodGet, "/path2/")
|
2018-08-14 04:51:56 +03:00
|
|
|
assert.Equal(t, http.StatusOK, w.Code)
|
2015-07-04 15:13:25 +03:00
|
|
|
|
2022-01-02 14:07:44 +03:00
|
|
|
w = PerformRequest(router, http.MethodPost, "/path3")
|
2018-08-14 04:51:56 +03:00
|
|
|
assert.Equal(t, http.StatusOK, w.Code)
|
2015-07-04 15:13:25 +03:00
|
|
|
|
2022-01-02 14:07:44 +03:00
|
|
|
w = PerformRequest(router, http.MethodPut, "/path4/")
|
2018-08-14 04:51:56 +03:00
|
|
|
assert.Equal(t, http.StatusOK, w.Code)
|
2015-07-04 15:13:25 +03:00
|
|
|
|
2022-01-02 14:07:44 +03:00
|
|
|
w = PerformRequest(router, http.MethodGet, "/path2", header{Key: "X-Forwarded-Prefix", Value: "/api"})
|
2019-02-27 14:56:29 +03:00
|
|
|
assert.Equal(t, "/api/path2/", w.Header().Get("Location"))
|
2024-03-05 09:36:02 +03:00
|
|
|
assert.Equal(t, http.StatusMovedPermanently, w.Code)
|
2019-02-27 14:56:29 +03:00
|
|
|
|
2022-01-02 14:07:44 +03:00
|
|
|
w = PerformRequest(router, http.MethodGet, "/path2/", header{Key: "X-Forwarded-Prefix", Value: "/api/"})
|
2024-03-05 09:36:02 +03:00
|
|
|
assert.Equal(t, http.StatusOK, w.Code)
|
2019-02-27 14:56:29 +03:00
|
|
|
|
2023-02-19 16:25:48 +03:00
|
|
|
w = PerformRequest(router, http.MethodGet, "/path/", header{Key: "X-Forwarded-Prefix", Value: "../../api#?"})
|
|
|
|
assert.Equal(t, "/api/path", w.Header().Get("Location"))
|
2024-03-05 09:36:02 +03:00
|
|
|
assert.Equal(t, http.StatusMovedPermanently, w.Code)
|
2023-02-19 16:25:48 +03:00
|
|
|
|
|
|
|
w = PerformRequest(router, http.MethodGet, "/path/", header{Key: "X-Forwarded-Prefix", Value: "../../api"})
|
|
|
|
assert.Equal(t, "/api/path", w.Header().Get("Location"))
|
2024-03-05 09:36:02 +03:00
|
|
|
assert.Equal(t, http.StatusMovedPermanently, w.Code)
|
2023-02-19 16:25:48 +03:00
|
|
|
|
|
|
|
w = PerformRequest(router, http.MethodGet, "/path2", header{Key: "X-Forwarded-Prefix", Value: "../../api"})
|
|
|
|
assert.Equal(t, "/api/path2/", w.Header().Get("Location"))
|
2024-03-05 09:36:02 +03:00
|
|
|
assert.Equal(t, http.StatusMovedPermanently, w.Code)
|
2023-02-19 16:25:48 +03:00
|
|
|
|
|
|
|
w = PerformRequest(router, http.MethodGet, "/path2", header{Key: "X-Forwarded-Prefix", Value: "/../../api"})
|
|
|
|
assert.Equal(t, "/api/path2/", w.Header().Get("Location"))
|
2024-03-05 09:36:02 +03:00
|
|
|
assert.Equal(t, http.StatusMovedPermanently, w.Code)
|
2023-02-19 16:25:48 +03:00
|
|
|
|
|
|
|
w = PerformRequest(router, http.MethodGet, "/path/", header{Key: "X-Forwarded-Prefix", Value: "api/../../"})
|
|
|
|
assert.Equal(t, "//path", w.Header().Get("Location"))
|
2024-03-05 09:36:02 +03:00
|
|
|
assert.Equal(t, http.StatusMovedPermanently, w.Code)
|
2023-02-19 16:25:48 +03:00
|
|
|
|
|
|
|
w = PerformRequest(router, http.MethodGet, "/path/", header{Key: "X-Forwarded-Prefix", Value: "api/../../../"})
|
|
|
|
assert.Equal(t, "/path", w.Header().Get("Location"))
|
2024-03-05 09:36:02 +03:00
|
|
|
assert.Equal(t, http.StatusMovedPermanently, w.Code)
|
2023-02-19 16:25:48 +03:00
|
|
|
|
|
|
|
w = PerformRequest(router, http.MethodGet, "/path2", header{Key: "X-Forwarded-Prefix", Value: "../../gin-gonic.com"})
|
|
|
|
assert.Equal(t, "/gin-goniccom/path2/", w.Header().Get("Location"))
|
2024-03-05 09:36:02 +03:00
|
|
|
assert.Equal(t, http.StatusMovedPermanently, w.Code)
|
2023-02-19 16:25:48 +03:00
|
|
|
|
|
|
|
w = PerformRequest(router, http.MethodGet, "/path2", header{Key: "X-Forwarded-Prefix", Value: "/../../gin-gonic.com"})
|
|
|
|
assert.Equal(t, "/gin-goniccom/path2/", w.Header().Get("Location"))
|
2024-03-05 09:36:02 +03:00
|
|
|
assert.Equal(t, http.StatusMovedPermanently, w.Code)
|
2023-02-17 05:00:19 +03:00
|
|
|
|
|
|
|
w = PerformRequest(router, http.MethodGet, "/path/", header{Key: "X-Forwarded-Prefix", Value: "https://gin-gonic.com/#"})
|
2023-02-19 16:25:48 +03:00
|
|
|
assert.Equal(t, "https/gin-goniccom/https/gin-goniccom/path", w.Header().Get("Location"))
|
2024-03-05 09:36:02 +03:00
|
|
|
assert.Equal(t, http.StatusMovedPermanently, w.Code)
|
2023-02-19 16:25:48 +03:00
|
|
|
|
|
|
|
w = PerformRequest(router, http.MethodGet, "/path/", header{Key: "X-Forwarded-Prefix", Value: "#api"})
|
|
|
|
assert.Equal(t, "api/api/path", w.Header().Get("Location"))
|
2024-03-05 09:36:02 +03:00
|
|
|
assert.Equal(t, http.StatusMovedPermanently, w.Code)
|
2023-02-19 16:25:48 +03:00
|
|
|
|
|
|
|
w = PerformRequest(router, http.MethodGet, "/path/", header{Key: "X-Forwarded-Prefix", Value: "/nor-mal/#?a=1"})
|
|
|
|
assert.Equal(t, "/nor-mal/a1/path", w.Header().Get("Location"))
|
2024-03-05 09:36:02 +03:00
|
|
|
assert.Equal(t, http.StatusMovedPermanently, w.Code)
|
2023-02-17 05:00:19 +03:00
|
|
|
|
2023-02-19 16:25:48 +03:00
|
|
|
w = PerformRequest(router, http.MethodGet, "/path/", header{Key: "X-Forwarded-Prefix", Value: "/nor-mal/%2e%2e/"})
|
|
|
|
assert.Equal(t, "/nor-mal/2e2e/path", w.Header().Get("Location"))
|
2024-03-05 09:36:02 +03:00
|
|
|
assert.Equal(t, http.StatusMovedPermanently, w.Code)
|
2023-02-17 05:00:19 +03:00
|
|
|
|
2015-07-04 15:13:25 +03:00
|
|
|
router.RedirectTrailingSlash = false
|
|
|
|
|
2022-01-02 14:07:44 +03:00
|
|
|
w = PerformRequest(router, http.MethodGet, "/path/")
|
2018-08-14 04:51:56 +03:00
|
|
|
assert.Equal(t, http.StatusNotFound, w.Code)
|
2022-01-02 14:07:44 +03:00
|
|
|
w = PerformRequest(router, http.MethodGet, "/path2")
|
2018-08-14 04:51:56 +03:00
|
|
|
assert.Equal(t, http.StatusNotFound, w.Code)
|
2022-01-02 14:07:44 +03:00
|
|
|
w = PerformRequest(router, http.MethodPost, "/path3/")
|
2018-08-14 04:51:56 +03:00
|
|
|
assert.Equal(t, http.StatusNotFound, w.Code)
|
2022-01-02 14:07:44 +03:00
|
|
|
w = PerformRequest(router, http.MethodPut, "/path4")
|
2018-08-14 04:51:56 +03:00
|
|
|
assert.Equal(t, http.StatusNotFound, w.Code)
|
2015-07-04 15:13:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRouteRedirectFixedPath(t *testing.T) {
|
|
|
|
router := New()
|
|
|
|
router.RedirectFixedPath = true
|
|
|
|
router.RedirectTrailingSlash = false
|
|
|
|
|
|
|
|
router.GET("/path", func(c *Context) {})
|
|
|
|
router.GET("/Path2", func(c *Context) {})
|
|
|
|
router.POST("/PATH3", func(c *Context) {})
|
|
|
|
router.POST("/Path4/", func(c *Context) {})
|
|
|
|
|
2022-01-02 14:07:44 +03:00
|
|
|
w := PerformRequest(router, http.MethodGet, "/PATH")
|
2017-11-21 16:18:45 +03:00
|
|
|
assert.Equal(t, "/path", w.Header().Get("Location"))
|
2018-08-14 04:51:56 +03:00
|
|
|
assert.Equal(t, http.StatusMovedPermanently, w.Code)
|
2015-07-04 15:13:25 +03:00
|
|
|
|
2022-01-02 14:07:44 +03:00
|
|
|
w = PerformRequest(router, http.MethodGet, "/path2")
|
2017-11-21 16:18:45 +03:00
|
|
|
assert.Equal(t, "/Path2", w.Header().Get("Location"))
|
2018-08-14 04:51:56 +03:00
|
|
|
assert.Equal(t, http.StatusMovedPermanently, w.Code)
|
2015-07-04 15:13:25 +03:00
|
|
|
|
2022-01-02 14:07:44 +03:00
|
|
|
w = PerformRequest(router, http.MethodPost, "/path3")
|
2017-11-21 16:18:45 +03:00
|
|
|
assert.Equal(t, "/PATH3", w.Header().Get("Location"))
|
2018-08-14 04:51:56 +03:00
|
|
|
assert.Equal(t, http.StatusTemporaryRedirect, w.Code)
|
2015-07-04 15:13:25 +03:00
|
|
|
|
2022-01-02 14:07:44 +03:00
|
|
|
w = PerformRequest(router, http.MethodPost, "/path4")
|
2017-11-21 16:18:45 +03:00
|
|
|
assert.Equal(t, "/Path4/", w.Header().Get("Location"))
|
2018-08-14 04:51:56 +03:00
|
|
|
assert.Equal(t, http.StatusTemporaryRedirect, w.Code)
|
2015-07-04 15:13:25 +03:00
|
|
|
}
|
|
|
|
|
2015-05-05 16:06:38 +03:00
|
|
|
// TestContextParamsGet tests that a parameter can be parsed from the URL.
|
|
|
|
func TestRouteParamsByName(t *testing.T) {
|
|
|
|
name := ""
|
|
|
|
lastName := ""
|
2015-05-05 17:37:33 +03:00
|
|
|
wild := ""
|
2015-05-05 16:06:38 +03:00
|
|
|
router := New()
|
2015-05-05 17:37:33 +03:00
|
|
|
router.GET("/test/:name/:last_name/*wild", func(c *Context) {
|
2015-05-05 16:06:38 +03:00
|
|
|
name = c.Params.ByName("name")
|
|
|
|
lastName = c.Params.ByName("last_name")
|
2015-05-26 18:49:25 +03:00
|
|
|
var ok bool
|
|
|
|
wild, ok = c.Params.Get("wild")
|
2015-05-05 17:37:33 +03:00
|
|
|
|
2015-05-26 18:49:25 +03:00
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Equal(t, name, c.Param("name"))
|
2015-05-26 13:11:59 +03:00
|
|
|
assert.Equal(t, lastName, c.Param("last_name"))
|
2015-05-26 18:49:25 +03:00
|
|
|
|
|
|
|
assert.Empty(t, c.Param("wtf"))
|
|
|
|
assert.Empty(t, c.Params.ByName("wtf"))
|
|
|
|
|
|
|
|
wtf, ok := c.Params.Get("wtf")
|
|
|
|
assert.Empty(t, wtf)
|
|
|
|
assert.False(t, ok)
|
2015-05-05 16:06:38 +03:00
|
|
|
})
|
2015-05-18 21:50:46 +03:00
|
|
|
|
2022-01-02 14:07:44 +03:00
|
|
|
w := PerformRequest(router, http.MethodGet, "/test/john/smith/is/super/great")
|
2015-05-05 16:06:38 +03:00
|
|
|
|
2018-08-14 04:51:56 +03:00
|
|
|
assert.Equal(t, http.StatusOK, w.Code)
|
2017-11-21 16:18:45 +03:00
|
|
|
assert.Equal(t, "john", name)
|
|
|
|
assert.Equal(t, "smith", lastName)
|
|
|
|
assert.Equal(t, "/is/super/great", wild)
|
2015-05-05 16:06:38 +03:00
|
|
|
}
|
|
|
|
|
2019-05-07 14:32:35 +03:00
|
|
|
// TestContextParamsGet tests that a parameter can be parsed from the URL even with extra slashes.
|
|
|
|
func TestRouteParamsByNameWithExtraSlash(t *testing.T) {
|
|
|
|
name := ""
|
|
|
|
lastName := ""
|
|
|
|
wild := ""
|
|
|
|
router := New()
|
2019-11-28 19:02:02 +03:00
|
|
|
router.RemoveExtraSlash = true
|
2019-05-07 14:32:35 +03:00
|
|
|
router.GET("/test/:name/:last_name/*wild", func(c *Context) {
|
|
|
|
name = c.Params.ByName("name")
|
|
|
|
lastName = c.Params.ByName("last_name")
|
|
|
|
var ok bool
|
|
|
|
wild, ok = c.Params.Get("wild")
|
|
|
|
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Equal(t, name, c.Param("name"))
|
|
|
|
assert.Equal(t, lastName, c.Param("last_name"))
|
|
|
|
|
|
|
|
assert.Empty(t, c.Param("wtf"))
|
|
|
|
assert.Empty(t, c.Params.ByName("wtf"))
|
|
|
|
|
|
|
|
wtf, ok := c.Params.Get("wtf")
|
|
|
|
assert.Empty(t, wtf)
|
|
|
|
assert.False(t, ok)
|
|
|
|
})
|
|
|
|
|
2022-01-02 14:07:44 +03:00
|
|
|
w := PerformRequest(router, http.MethodGet, "//test//john//smith//is//super//great")
|
2019-05-07 14:32:35 +03:00
|
|
|
|
|
|
|
assert.Equal(t, http.StatusOK, w.Code)
|
|
|
|
assert.Equal(t, "john", name)
|
|
|
|
assert.Equal(t, "smith", lastName)
|
|
|
|
assert.Equal(t, "/is/super/great", wild)
|
|
|
|
}
|
|
|
|
|
2023-12-07 03:38:55 +03:00
|
|
|
// TestRouteParamsNotEmpty tests that context parameters will be set
|
|
|
|
// even if a route with params/wildcards is registered after the context
|
2024-03-05 16:55:25 +03:00
|
|
|
// initialisation (which happened in a previous requests).
|
2023-12-07 03:38:55 +03:00
|
|
|
func TestRouteParamsNotEmpty(t *testing.T) {
|
|
|
|
name := ""
|
|
|
|
lastName := ""
|
|
|
|
wild := ""
|
|
|
|
router := New()
|
|
|
|
|
|
|
|
w := PerformRequest(router, http.MethodGet, "/test/john/smith/is/super/great")
|
|
|
|
|
|
|
|
assert.Equal(t, http.StatusNotFound, w.Code)
|
|
|
|
|
|
|
|
router.GET("/test/:name/:last_name/*wild", func(c *Context) {
|
|
|
|
name = c.Params.ByName("name")
|
|
|
|
lastName = c.Params.ByName("last_name")
|
|
|
|
var ok bool
|
|
|
|
wild, ok = c.Params.Get("wild")
|
|
|
|
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Equal(t, name, c.Param("name"))
|
|
|
|
assert.Equal(t, lastName, c.Param("last_name"))
|
|
|
|
|
|
|
|
assert.Empty(t, c.Param("wtf"))
|
|
|
|
assert.Empty(t, c.Params.ByName("wtf"))
|
|
|
|
|
|
|
|
wtf, ok := c.Params.Get("wtf")
|
|
|
|
assert.Empty(t, wtf)
|
|
|
|
assert.False(t, ok)
|
|
|
|
})
|
|
|
|
|
|
|
|
w = PerformRequest(router, http.MethodGet, "/test/john/smith/is/super/great")
|
|
|
|
|
|
|
|
assert.Equal(t, http.StatusOK, w.Code)
|
|
|
|
assert.Equal(t, "john", name)
|
|
|
|
assert.Equal(t, "smith", lastName)
|
|
|
|
assert.Equal(t, "/is/super/great", wild)
|
|
|
|
}
|
|
|
|
|
2015-04-08 03:58:35 +03:00
|
|
|
// TestHandleStaticFile - ensure the static file handles properly
|
2015-05-05 16:06:38 +03:00
|
|
|
func TestRouteStaticFile(t *testing.T) {
|
2015-04-08 03:58:35 +03:00
|
|
|
// SETUP file
|
|
|
|
testRoot, _ := os.Getwd()
|
2022-11-17 17:35:55 +03:00
|
|
|
f, err := os.CreateTemp(testRoot, "")
|
2015-04-08 03:58:35 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
defer os.Remove(f.Name())
|
2019-01-18 04:32:53 +03:00
|
|
|
_, err = f.WriteString("Gin Web Framework")
|
2024-07-14 15:33:08 +03:00
|
|
|
require.NoError(t, err)
|
2015-04-08 03:58:35 +03:00
|
|
|
f.Close()
|
|
|
|
|
2015-05-31 19:44:34 +03:00
|
|
|
dir, filename := filepath.Split(f.Name())
|
2015-05-21 18:01:13 +03:00
|
|
|
|
2015-04-08 03:58:35 +03:00
|
|
|
// SETUP gin
|
2015-05-18 21:50:46 +03:00
|
|
|
router := New()
|
2015-05-21 18:01:13 +03:00
|
|
|
router.Static("/using_static", dir)
|
|
|
|
router.StaticFile("/result", f.Name())
|
2015-04-08 03:58:35 +03:00
|
|
|
|
2022-01-02 14:07:44 +03:00
|
|
|
w := PerformRequest(router, http.MethodGet, "/using_static/"+filename)
|
|
|
|
w2 := PerformRequest(router, http.MethodGet, "/result")
|
2015-04-08 03:58:35 +03:00
|
|
|
|
2015-05-21 18:01:13 +03:00
|
|
|
assert.Equal(t, w, w2)
|
2018-08-14 04:51:56 +03:00
|
|
|
assert.Equal(t, http.StatusOK, w.Code)
|
2017-11-21 16:18:45 +03:00
|
|
|
assert.Equal(t, "Gin Web Framework", w.Body.String())
|
2018-10-12 02:31:31 +03:00
|
|
|
assert.Equal(t, "text/plain; charset=utf-8", w.Header().Get("Content-Type"))
|
2015-05-21 18:01:13 +03:00
|
|
|
|
2022-01-02 14:07:44 +03:00
|
|
|
w3 := PerformRequest(router, http.MethodHead, "/using_static/"+filename)
|
|
|
|
w4 := PerformRequest(router, http.MethodHead, "/result")
|
2015-05-21 18:01:13 +03:00
|
|
|
|
|
|
|
assert.Equal(t, w3, w4)
|
2018-08-14 04:51:56 +03:00
|
|
|
assert.Equal(t, http.StatusOK, w3.Code)
|
2015-04-08 03:58:35 +03:00
|
|
|
}
|
|
|
|
|
2022-03-18 04:52:23 +03:00
|
|
|
// TestHandleStaticFile - ensure the static file handles properly
|
|
|
|
func TestRouteStaticFileFS(t *testing.T) {
|
|
|
|
// SETUP file
|
|
|
|
testRoot, _ := os.Getwd()
|
2022-11-17 17:35:55 +03:00
|
|
|
f, err := os.CreateTemp(testRoot, "")
|
2022-03-18 04:52:23 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
defer os.Remove(f.Name())
|
|
|
|
_, err = f.WriteString("Gin Web Framework")
|
2024-07-14 15:33:08 +03:00
|
|
|
require.NoError(t, err)
|
2022-03-18 04:52:23 +03:00
|
|
|
f.Close()
|
|
|
|
|
|
|
|
dir, filename := filepath.Split(f.Name())
|
|
|
|
// SETUP gin
|
|
|
|
router := New()
|
|
|
|
router.Static("/using_static", dir)
|
|
|
|
router.StaticFileFS("/result_fs", filename, Dir(dir, false))
|
|
|
|
|
2022-03-18 06:55:25 +03:00
|
|
|
w := PerformRequest(router, http.MethodGet, "/using_static/"+filename)
|
|
|
|
w2 := PerformRequest(router, http.MethodGet, "/result_fs")
|
2022-03-18 04:52:23 +03:00
|
|
|
|
|
|
|
assert.Equal(t, w, w2)
|
|
|
|
assert.Equal(t, http.StatusOK, w.Code)
|
|
|
|
assert.Equal(t, "Gin Web Framework", w.Body.String())
|
|
|
|
assert.Equal(t, "text/plain; charset=utf-8", w.Header().Get("Content-Type"))
|
|
|
|
|
2022-03-18 06:55:25 +03:00
|
|
|
w3 := PerformRequest(router, http.MethodHead, "/using_static/"+filename)
|
|
|
|
w4 := PerformRequest(router, http.MethodHead, "/result_fs")
|
2022-03-18 04:52:23 +03:00
|
|
|
|
|
|
|
assert.Equal(t, w3, w4)
|
|
|
|
assert.Equal(t, http.StatusOK, w3.Code)
|
|
|
|
}
|
|
|
|
|
2015-04-08 03:58:35 +03:00
|
|
|
// TestHandleStaticDir - ensure the root/sub dir handles properly
|
2015-05-21 18:01:13 +03:00
|
|
|
func TestRouteStaticListingDir(t *testing.T) {
|
2015-05-18 21:50:46 +03:00
|
|
|
router := New()
|
2015-05-24 17:29:55 +03:00
|
|
|
router.StaticFS("/", Dir("./", true))
|
2015-04-08 03:58:35 +03:00
|
|
|
|
2022-01-02 14:07:44 +03:00
|
|
|
w := PerformRequest(router, http.MethodGet, "/")
|
2015-04-08 03:58:35 +03:00
|
|
|
|
2018-08-14 04:51:56 +03:00
|
|
|
assert.Equal(t, http.StatusOK, w.Code)
|
2015-05-18 21:50:46 +03:00
|
|
|
assert.Contains(t, w.Body.String(), "gin.go")
|
2018-10-12 02:31:31 +03:00
|
|
|
assert.Equal(t, "text/html; charset=utf-8", w.Header().Get("Content-Type"))
|
2015-04-08 03:58:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestHandleHeadToDir - ensure the root/sub dir handles properly
|
2015-05-21 18:01:13 +03:00
|
|
|
func TestRouteStaticNoListing(t *testing.T) {
|
2015-04-08 03:58:35 +03:00
|
|
|
router := New()
|
|
|
|
router.Static("/", "./")
|
|
|
|
|
2022-01-02 14:07:44 +03:00
|
|
|
w := PerformRequest(router, http.MethodGet, "/")
|
2015-04-08 03:58:35 +03:00
|
|
|
|
2018-08-14 04:51:56 +03:00
|
|
|
assert.Equal(t, http.StatusNotFound, w.Code)
|
2015-05-21 18:01:13 +03:00
|
|
|
assert.NotContains(t, w.Body.String(), "gin.go")
|
2015-05-18 21:50:46 +03:00
|
|
|
}
|
2015-05-18 21:51:08 +03:00
|
|
|
|
|
|
|
func TestRouterMiddlewareAndStatic(t *testing.T) {
|
|
|
|
router := New()
|
|
|
|
static := router.Group("/", func(c *Context) {
|
|
|
|
c.Writer.Header().Add("Last-Modified", "Mon, 02 Jan 2006 15:04:05 MST")
|
|
|
|
c.Writer.Header().Add("Expires", "Mon, 02 Jan 2006 15:04:05 MST")
|
|
|
|
c.Writer.Header().Add("X-GIN", "Gin Framework")
|
|
|
|
})
|
|
|
|
static.Static("/", "./")
|
|
|
|
|
2022-01-02 14:07:44 +03:00
|
|
|
w := PerformRequest(router, http.MethodGet, "/gin.go")
|
2015-05-18 21:51:08 +03:00
|
|
|
|
2018-08-14 04:51:56 +03:00
|
|
|
assert.Equal(t, http.StatusOK, w.Code)
|
2015-05-21 18:01:13 +03:00
|
|
|
assert.Contains(t, w.Body.String(), "package gin")
|
2021-04-21 02:38:54 +03:00
|
|
|
// Content-Type='text/plain; charset=utf-8' when go version <= 1.16,
|
|
|
|
// else, Content-Type='text/x-go; charset=utf-8'
|
|
|
|
assert.NotEqual(t, "", w.Header().Get("Content-Type"))
|
2024-07-14 15:33:08 +03:00
|
|
|
assert.NotEqual(t, "Mon, 02 Jan 2006 15:04:05 MST", w.Header().Get("Last-Modified"))
|
2018-10-12 02:31:31 +03:00
|
|
|
assert.Equal(t, "Mon, 02 Jan 2006 15:04:05 MST", w.Header().Get("Expires"))
|
|
|
|
assert.Equal(t, "Gin Framework", w.Header().Get("x-GIN"))
|
2015-04-08 03:58:35 +03:00
|
|
|
}
|
2015-05-05 17:37:33 +03:00
|
|
|
|
2015-05-30 15:45:13 +03:00
|
|
|
func TestRouteNotAllowedEnabled(t *testing.T) {
|
2015-05-05 17:37:33 +03:00
|
|
|
router := New()
|
2015-05-30 15:45:13 +03:00
|
|
|
router.HandleMethodNotAllowed = true
|
2015-05-05 17:37:33 +03:00
|
|
|
router.POST("/path", func(c *Context) {})
|
2022-01-02 14:07:44 +03:00
|
|
|
w := PerformRequest(router, http.MethodGet, "/path")
|
2017-11-21 16:18:45 +03:00
|
|
|
assert.Equal(t, http.StatusMethodNotAllowed, w.Code)
|
2015-05-05 17:37:33 +03:00
|
|
|
|
|
|
|
router.NoMethod(func(c *Context) {
|
|
|
|
c.String(http.StatusTeapot, "responseText")
|
|
|
|
})
|
2022-01-02 14:07:44 +03:00
|
|
|
w = PerformRequest(router, http.MethodGet, "/path")
|
2017-11-21 16:18:45 +03:00
|
|
|
assert.Equal(t, "responseText", w.Body.String())
|
|
|
|
assert.Equal(t, http.StatusTeapot, w.Code)
|
2015-05-05 17:37:33 +03:00
|
|
|
}
|
|
|
|
|
2018-08-12 16:17:57 +03:00
|
|
|
func TestRouteNotAllowedEnabled2(t *testing.T) {
|
|
|
|
router := New()
|
|
|
|
router.HandleMethodNotAllowed = true
|
|
|
|
// add one methodTree to trees
|
2019-11-29 02:50:49 +03:00
|
|
|
router.addRoute(http.MethodPost, "/", HandlersChain{func(_ *Context) {}})
|
2018-08-12 16:17:57 +03:00
|
|
|
router.GET("/path2", func(c *Context) {})
|
2022-01-02 14:07:44 +03:00
|
|
|
w := PerformRequest(router, http.MethodPost, "/path2")
|
2018-08-12 16:17:57 +03:00
|
|
|
assert.Equal(t, http.StatusMethodNotAllowed, w.Code)
|
|
|
|
}
|
|
|
|
|
2024-02-06 06:08:56 +03:00
|
|
|
func TestRouteNotAllowedEnabled3(t *testing.T) {
|
|
|
|
router := New()
|
|
|
|
router.HandleMethodNotAllowed = true
|
|
|
|
router.GET("/path", func(c *Context) {})
|
|
|
|
router.POST("/path", func(c *Context) {})
|
|
|
|
w := PerformRequest(router, http.MethodPut, "/path")
|
|
|
|
assert.Equal(t, http.StatusMethodNotAllowed, w.Code)
|
|
|
|
allowed := w.Header().Get("Allow")
|
|
|
|
assert.Contains(t, allowed, "GET")
|
|
|
|
assert.Contains(t, allowed, "POST")
|
|
|
|
}
|
|
|
|
|
2015-05-30 15:45:13 +03:00
|
|
|
func TestRouteNotAllowedDisabled(t *testing.T) {
|
|
|
|
router := New()
|
|
|
|
router.HandleMethodNotAllowed = false
|
|
|
|
router.POST("/path", func(c *Context) {})
|
2022-01-02 14:07:44 +03:00
|
|
|
w := PerformRequest(router, http.MethodGet, "/path")
|
2018-08-14 04:51:56 +03:00
|
|
|
assert.Equal(t, http.StatusNotFound, w.Code)
|
2015-05-30 15:45:13 +03:00
|
|
|
|
|
|
|
router.NoMethod(func(c *Context) {
|
|
|
|
c.String(http.StatusTeapot, "responseText")
|
|
|
|
})
|
2022-01-02 14:07:44 +03:00
|
|
|
w = PerformRequest(router, http.MethodGet, "/path")
|
2017-11-21 16:18:45 +03:00
|
|
|
assert.Equal(t, "404 page not found", w.Body.String())
|
2018-08-14 04:51:56 +03:00
|
|
|
assert.Equal(t, http.StatusNotFound, w.Code)
|
2015-05-30 15:45:13 +03:00
|
|
|
}
|
|
|
|
|
2019-11-28 19:02:02 +03:00
|
|
|
func TestRouterNotFoundWithRemoveExtraSlash(t *testing.T) {
|
|
|
|
router := New()
|
|
|
|
router.RemoveExtraSlash = true
|
|
|
|
router.GET("/path", func(c *Context) {})
|
|
|
|
router.GET("/", func(c *Context) {})
|
|
|
|
|
|
|
|
testRoutes := []struct {
|
|
|
|
route string
|
|
|
|
code int
|
|
|
|
location string
|
|
|
|
}{
|
|
|
|
{"/../path", http.StatusOK, ""}, // CleanPath
|
|
|
|
{"/nope", http.StatusNotFound, ""}, // NotFound
|
|
|
|
}
|
|
|
|
for _, tr := range testRoutes {
|
2022-01-02 14:07:44 +03:00
|
|
|
w := PerformRequest(router, "GET", tr.route)
|
2019-11-28 19:02:02 +03:00
|
|
|
assert.Equal(t, tr.code, w.Code)
|
|
|
|
if w.Code != http.StatusNotFound {
|
2024-11-15 18:51:12 +03:00
|
|
|
assert.Equal(t, tr.location, w.Header().Get("Location"))
|
2019-11-28 19:02:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-05 17:37:33 +03:00
|
|
|
func TestRouterNotFound(t *testing.T) {
|
|
|
|
router := New()
|
2015-05-30 15:45:13 +03:00
|
|
|
router.RedirectFixedPath = true
|
2015-05-05 17:37:33 +03:00
|
|
|
router.GET("/path", func(c *Context) {})
|
|
|
|
router.GET("/dir/", func(c *Context) {})
|
|
|
|
router.GET("/", func(c *Context) {})
|
|
|
|
|
|
|
|
testRoutes := []struct {
|
2017-08-14 06:02:31 +03:00
|
|
|
route string
|
|
|
|
code int
|
|
|
|
location string
|
2015-05-05 17:37:33 +03:00
|
|
|
}{
|
2019-11-28 19:02:02 +03:00
|
|
|
{"/path/", http.StatusMovedPermanently, "/path"}, // TSR -/
|
|
|
|
{"/dir", http.StatusMovedPermanently, "/dir/"}, // TSR +/
|
|
|
|
{"/PATH", http.StatusMovedPermanently, "/path"}, // Fixed Case
|
|
|
|
{"/DIR/", http.StatusMovedPermanently, "/dir/"}, // Fixed Case
|
|
|
|
{"/PATH/", http.StatusMovedPermanently, "/path"}, // Fixed Case -/
|
|
|
|
{"/DIR", http.StatusMovedPermanently, "/dir/"}, // Fixed Case +/
|
|
|
|
{"/../path", http.StatusMovedPermanently, "/path"}, // Without CleanPath
|
|
|
|
{"/nope", http.StatusNotFound, ""}, // NotFound
|
2015-05-05 17:37:33 +03:00
|
|
|
}
|
|
|
|
for _, tr := range testRoutes {
|
2022-01-02 14:07:44 +03:00
|
|
|
w := PerformRequest(router, http.MethodGet, tr.route)
|
2017-11-21 16:18:45 +03:00
|
|
|
assert.Equal(t, tr.code, w.Code)
|
2018-08-14 04:51:56 +03:00
|
|
|
if w.Code != http.StatusNotFound {
|
2024-11-15 18:51:12 +03:00
|
|
|
assert.Equal(t, tr.location, w.Header().Get("Location"))
|
2015-05-05 17:37:33 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test custom not found handler
|
|
|
|
var notFound bool
|
|
|
|
router.NoRoute(func(c *Context) {
|
2018-08-14 04:51:56 +03:00
|
|
|
c.AbortWithStatus(http.StatusNotFound)
|
2015-05-05 17:37:33 +03:00
|
|
|
notFound = true
|
|
|
|
})
|
2022-01-02 14:07:44 +03:00
|
|
|
w := PerformRequest(router, http.MethodGet, "/nope")
|
2018-08-14 04:51:56 +03:00
|
|
|
assert.Equal(t, http.StatusNotFound, w.Code)
|
2015-05-05 17:37:33 +03:00
|
|
|
assert.True(t, notFound)
|
|
|
|
|
|
|
|
// Test other method than GET (want 307 instead of 301)
|
|
|
|
router.PATCH("/path", func(c *Context) {})
|
2022-01-02 14:07:44 +03:00
|
|
|
w = PerformRequest(router, http.MethodPatch, "/path/")
|
2018-08-14 04:51:56 +03:00
|
|
|
assert.Equal(t, http.StatusTemporaryRedirect, w.Code)
|
2017-11-21 16:18:45 +03:00
|
|
|
assert.Equal(t, "map[Location:[/path]]", fmt.Sprint(w.Header()))
|
2015-05-05 17:37:33 +03:00
|
|
|
|
|
|
|
// Test special case where no node for the prefix "/" exists
|
|
|
|
router = New()
|
|
|
|
router.GET("/a", func(c *Context) {})
|
2022-01-02 14:07:44 +03:00
|
|
|
w = PerformRequest(router, http.MethodGet, "/")
|
2018-08-14 04:51:56 +03:00
|
|
|
assert.Equal(t, http.StatusNotFound, w.Code)
|
2021-10-26 13:15:29 +03:00
|
|
|
|
|
|
|
// Reproduction test for the bug of issue #2843
|
|
|
|
router = New()
|
|
|
|
router.NoRoute(func(c *Context) {
|
|
|
|
if c.Request.RequestURI == "/login" {
|
2024-03-05 09:36:02 +03:00
|
|
|
c.String(http.StatusOK, "login")
|
2021-10-26 13:15:29 +03:00
|
|
|
}
|
|
|
|
})
|
|
|
|
router.GET("/logout", func(c *Context) {
|
2024-03-05 09:36:02 +03:00
|
|
|
c.String(http.StatusOK, "logout")
|
2021-10-26 13:15:29 +03:00
|
|
|
})
|
2022-01-02 14:07:44 +03:00
|
|
|
w = PerformRequest(router, http.MethodGet, "/login")
|
2021-10-26 13:15:29 +03:00
|
|
|
assert.Equal(t, "login", w.Body.String())
|
2022-01-02 14:07:44 +03:00
|
|
|
w = PerformRequest(router, http.MethodGet, "/logout")
|
2021-10-26 13:15:29 +03:00
|
|
|
assert.Equal(t, "logout", w.Body.String())
|
2015-05-05 17:37:33 +03:00
|
|
|
}
|
2017-02-28 13:29:41 +03:00
|
|
|
|
2018-11-26 18:01:51 +03:00
|
|
|
func TestRouterStaticFSNotFound(t *testing.T) {
|
|
|
|
router := New()
|
|
|
|
router.StaticFS("/", http.FileSystem(http.Dir("/thisreallydoesntexist/")))
|
|
|
|
router.NoRoute(func(c *Context) {
|
2024-03-05 09:36:02 +03:00
|
|
|
c.String(http.StatusNotFound, "non existent")
|
2018-11-26 18:01:51 +03:00
|
|
|
})
|
|
|
|
|
2022-01-02 14:07:44 +03:00
|
|
|
w := PerformRequest(router, http.MethodGet, "/nonexistent")
|
2018-11-26 18:01:51 +03:00
|
|
|
assert.Equal(t, "non existent", w.Body.String())
|
|
|
|
|
2022-01-02 14:07:44 +03:00
|
|
|
w = PerformRequest(router, http.MethodHead, "/nonexistent")
|
2018-11-26 18:01:51 +03:00
|
|
|
assert.Equal(t, "non existent", w.Body.String())
|
|
|
|
}
|
|
|
|
|
2019-01-18 04:57:06 +03:00
|
|
|
func TestRouterStaticFSFileNotFound(t *testing.T) {
|
|
|
|
router := New()
|
|
|
|
|
|
|
|
router.StaticFS("/", http.FileSystem(http.Dir(".")))
|
|
|
|
|
|
|
|
assert.NotPanics(t, func() {
|
2022-01-02 14:07:44 +03:00
|
|
|
PerformRequest(router, http.MethodGet, "/nonexistent")
|
2019-01-18 04:57:06 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-03-11 05:52:47 +03:00
|
|
|
// Reproduction test for the bug of issue #1805
|
|
|
|
func TestMiddlewareCalledOnceByRouterStaticFSNotFound(t *testing.T) {
|
|
|
|
router := New()
|
|
|
|
|
|
|
|
// Middleware must be called just only once by per request.
|
|
|
|
middlewareCalledNum := 0
|
|
|
|
router.Use(func(c *Context) {
|
2020-05-05 08:55:57 +03:00
|
|
|
middlewareCalledNum++
|
2019-03-11 05:52:47 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
router.StaticFS("/", http.FileSystem(http.Dir("/thisreallydoesntexist/")))
|
|
|
|
|
|
|
|
// First access
|
2022-01-02 14:07:44 +03:00
|
|
|
PerformRequest(router, http.MethodGet, "/nonexistent")
|
2019-03-11 05:52:47 +03:00
|
|
|
assert.Equal(t, 1, middlewareCalledNum)
|
|
|
|
|
|
|
|
// Second access
|
2022-01-02 14:07:44 +03:00
|
|
|
PerformRequest(router, http.MethodHead, "/nonexistent")
|
2019-03-11 05:52:47 +03:00
|
|
|
assert.Equal(t, 2, middlewareCalledNum)
|
|
|
|
}
|
|
|
|
|
2017-02-28 13:29:41 +03:00
|
|
|
func TestRouteRawPath(t *testing.T) {
|
|
|
|
route := New()
|
|
|
|
route.UseRawPath = true
|
|
|
|
|
|
|
|
route.POST("/project/:name/build/:num", func(c *Context) {
|
|
|
|
name := c.Params.ByName("name")
|
|
|
|
num := c.Params.ByName("num")
|
|
|
|
|
2017-11-21 16:18:45 +03:00
|
|
|
assert.Equal(t, name, c.Param("name"))
|
|
|
|
assert.Equal(t, num, c.Param("num"))
|
2017-02-28 13:29:41 +03:00
|
|
|
|
|
|
|
assert.Equal(t, "Some/Other/Project", name)
|
|
|
|
assert.Equal(t, "222", num)
|
|
|
|
})
|
|
|
|
|
2022-01-02 14:07:44 +03:00
|
|
|
w := PerformRequest(route, http.MethodPost, "/project/Some%2FOther%2FProject/build/222")
|
2018-08-14 04:51:56 +03:00
|
|
|
assert.Equal(t, http.StatusOK, w.Code)
|
2017-02-28 13:29:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRouteRawPathNoUnescape(t *testing.T) {
|
|
|
|
route := New()
|
|
|
|
route.UseRawPath = true
|
|
|
|
route.UnescapePathValues = false
|
|
|
|
|
|
|
|
route.POST("/project/:name/build/:num", func(c *Context) {
|
|
|
|
name := c.Params.ByName("name")
|
|
|
|
num := c.Params.ByName("num")
|
|
|
|
|
2017-11-21 16:18:45 +03:00
|
|
|
assert.Equal(t, name, c.Param("name"))
|
|
|
|
assert.Equal(t, num, c.Param("num"))
|
2017-02-28 13:29:41 +03:00
|
|
|
|
|
|
|
assert.Equal(t, "Some%2FOther%2FProject", name)
|
|
|
|
assert.Equal(t, "333", num)
|
|
|
|
})
|
|
|
|
|
2022-01-02 14:07:44 +03:00
|
|
|
w := PerformRequest(route, http.MethodPost, "/project/Some%2FOther%2FProject/build/333")
|
2018-08-14 04:51:56 +03:00
|
|
|
assert.Equal(t, http.StatusOK, w.Code)
|
2017-02-28 13:29:41 +03:00
|
|
|
}
|
2017-07-06 04:30:09 +03:00
|
|
|
|
|
|
|
func TestRouteServeErrorWithWriteHeader(t *testing.T) {
|
|
|
|
route := New()
|
|
|
|
route.Use(func(c *Context) {
|
2024-03-05 09:36:02 +03:00
|
|
|
c.Status(http.StatusMisdirectedRequest)
|
2017-07-06 04:30:09 +03:00
|
|
|
c.Next()
|
|
|
|
})
|
|
|
|
|
2022-01-02 14:07:44 +03:00
|
|
|
w := PerformRequest(route, http.MethodGet, "/NotFound")
|
2024-03-05 09:36:02 +03:00
|
|
|
assert.Equal(t, http.StatusMisdirectedRequest, w.Code)
|
2017-07-06 04:30:09 +03:00
|
|
|
assert.Equal(t, 0, w.Body.Len())
|
|
|
|
}
|
2019-05-26 03:20:21 +03:00
|
|
|
|
|
|
|
func TestRouteContextHoldsFullPath(t *testing.T) {
|
|
|
|
router := New()
|
|
|
|
|
|
|
|
// Test routes
|
|
|
|
routes := []string{
|
|
|
|
"/simple",
|
|
|
|
"/project/:name",
|
2019-06-28 18:54:52 +03:00
|
|
|
"/",
|
|
|
|
"/news/home",
|
|
|
|
"/news",
|
|
|
|
"/simple-two/one",
|
|
|
|
"/simple-two/one-two",
|
2019-05-26 03:20:21 +03:00
|
|
|
"/project/:name/build/*params",
|
2019-06-28 18:54:52 +03:00
|
|
|
"/project/:name/bui",
|
2020-05-11 08:25:49 +03:00
|
|
|
"/user/:id/status",
|
|
|
|
"/user/:id",
|
|
|
|
"/user/:id/profile",
|
2019-05-26 03:20:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, route := range routes {
|
|
|
|
actualRoute := route
|
|
|
|
router.GET(route, func(c *Context) {
|
|
|
|
// For each defined route context should contain its full path
|
|
|
|
assert.Equal(t, actualRoute, c.FullPath())
|
|
|
|
c.AbortWithStatus(http.StatusOK)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, route := range routes {
|
2022-01-02 14:07:44 +03:00
|
|
|
w := PerformRequest(router, http.MethodGet, route)
|
2019-05-26 03:20:21 +03:00
|
|
|
assert.Equal(t, http.StatusOK, w.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test not found
|
|
|
|
router.Use(func(c *Context) {
|
|
|
|
// For not found routes full path is empty
|
|
|
|
assert.Equal(t, "", c.FullPath())
|
|
|
|
})
|
|
|
|
|
2022-01-02 14:07:44 +03:00
|
|
|
w := PerformRequest(router, http.MethodGet, "/not-found")
|
2019-05-26 03:20:21 +03:00
|
|
|
assert.Equal(t, http.StatusNotFound, w.Code)
|
|
|
|
}
|
2023-01-07 03:57:54 +03:00
|
|
|
|
|
|
|
func TestEngineHandleMethodNotAllowedCornerCase(t *testing.T) {
|
|
|
|
r := New()
|
|
|
|
r.HandleMethodNotAllowed = true
|
|
|
|
|
|
|
|
base := r.Group("base")
|
|
|
|
base.GET("/metrics", handlerTest1)
|
|
|
|
|
|
|
|
v1 := base.Group("v1")
|
|
|
|
|
|
|
|
v1.GET("/:id/devices", handlerTest1)
|
|
|
|
v1.GET("/user/:id/groups", handlerTest1)
|
|
|
|
|
|
|
|
v1.GET("/orgs/:id", handlerTest1)
|
|
|
|
v1.DELETE("/orgs/:id", handlerTest1)
|
|
|
|
|
|
|
|
w := PerformRequest(r, "GET", "/base/v1/user/groups")
|
|
|
|
assert.Equal(t, http.StatusNotFound, w.Code)
|
|
|
|
}
|