2022-05-28 02:42:28 +00:00
|
|
|
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
2014-08-29 17:49:50 +00:00
|
|
|
// Use of this source code is governed by a MIT style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2014-07-04 08:12:28 +00:00
|
|
|
package gin
|
|
|
|
|
2014-07-05 15:04:11 +00:00
|
|
|
import (
|
2017-11-21 01:27:57 +00:00
|
|
|
"crypto/tls"
|
2017-05-29 08:03:49 +00:00
|
|
|
"fmt"
|
2017-07-02 04:38:05 +00:00
|
|
|
"html/template"
|
2022-11-17 14:35:55 +00:00
|
|
|
"io"
|
2021-04-06 03:37:25 +00:00
|
|
|
"net"
|
2017-05-29 08:03:49 +00:00
|
|
|
"net/http"
|
2018-10-16 00:48:41 +00:00
|
|
|
"net/http/httptest"
|
2015-05-31 20:35:49 +00:00
|
|
|
"reflect"
|
2019-02-18 01:35:08 +00:00
|
|
|
"strconv"
|
|
|
|
"sync/atomic"
|
2014-07-05 15:04:11 +00:00
|
|
|
"testing"
|
2017-05-29 08:03:49 +00:00
|
|
|
"time"
|
2015-04-08 00:58:35 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2022-02-05 13:13:20 +00:00
|
|
|
"golang.org/x/net/http2"
|
2014-07-04 08:12:28 +00:00
|
|
|
)
|
|
|
|
|
2017-07-02 04:38:05 +00:00
|
|
|
func formatAsDate(t time.Time) string {
|
|
|
|
year, month, day := t.Date()
|
|
|
|
return fmt.Sprintf("%d/%02d/%02d", year, month, day)
|
|
|
|
}
|
|
|
|
|
2018-10-16 00:48:41 +00:00
|
|
|
func setupHTMLFiles(t *testing.T, mode string, tls bool, loadMethod func(*Engine)) *httptest.Server {
|
|
|
|
SetMode(mode)
|
2019-02-18 02:10:45 +00:00
|
|
|
defer SetMode(TestMode)
|
|
|
|
|
|
|
|
var router *Engine
|
|
|
|
captureOutput(t, func() {
|
|
|
|
router = New()
|
|
|
|
router.Delims("{[{", "}]}")
|
|
|
|
router.SetFuncMap(template.FuncMap{
|
|
|
|
"formatAsDate": formatAsDate,
|
|
|
|
})
|
|
|
|
loadMethod(router)
|
|
|
|
router.GET("/test", func(c *Context) {
|
|
|
|
c.HTML(http.StatusOK, "hello.tmpl", map[string]string{"name": "world"})
|
|
|
|
})
|
|
|
|
router.GET("/raw", func(c *Context) {
|
2022-03-21 01:43:17 +00:00
|
|
|
c.HTML(http.StatusOK, "raw.tmpl", map[string]any{
|
2019-02-18 02:10:45 +00:00
|
|
|
"now": time.Date(2017, 07, 01, 0, 0, 0, 0, time.UTC),
|
|
|
|
})
|
2017-07-02 05:06:43 +00:00
|
|
|
})
|
2018-10-16 00:48:41 +00:00
|
|
|
})
|
2017-05-29 08:03:49 +00:00
|
|
|
|
2018-10-16 00:48:41 +00:00
|
|
|
var ts *httptest.Server
|
|
|
|
|
|
|
|
if tls {
|
|
|
|
ts = httptest.NewTLSServer(router)
|
|
|
|
} else {
|
|
|
|
ts = httptest.NewServer(router)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ts
|
2017-05-29 08:03:49 +00:00
|
|
|
}
|
|
|
|
|
2018-10-16 00:48:41 +00:00
|
|
|
func TestLoadHTMLGlobDebugMode(t *testing.T) {
|
|
|
|
ts := setupHTMLFiles(
|
|
|
|
t,
|
|
|
|
DebugMode,
|
|
|
|
false,
|
|
|
|
func(router *Engine) {
|
|
|
|
router.LoadHTMLGlob("./testdata/template/*")
|
|
|
|
},
|
|
|
|
)
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
res, err := http.Get(fmt.Sprintf("%s/test", ts.URL))
|
2017-05-29 08:03:49 +00:00
|
|
|
if err != nil {
|
2022-11-06 09:12:11 +00:00
|
|
|
t.Error(err)
|
2017-05-29 08:03:49 +00:00
|
|
|
}
|
|
|
|
|
2022-11-17 14:35:55 +00:00
|
|
|
resp, _ := io.ReadAll(res.Body)
|
2018-09-12 13:21:26 +00:00
|
|
|
assert.Equal(t, "<h1>Hello world</h1>", string(resp))
|
2017-05-29 08:03:49 +00:00
|
|
|
}
|
|
|
|
|
2022-02-05 13:13:20 +00:00
|
|
|
func TestH2c(t *testing.T) {
|
|
|
|
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
|
|
|
if err != nil {
|
2022-11-06 09:12:11 +00:00
|
|
|
t.Error(err)
|
2022-02-05 13:13:20 +00:00
|
|
|
}
|
|
|
|
r := Default()
|
|
|
|
r.UseH2C = true
|
|
|
|
r.GET("/", func(c *Context) {
|
|
|
|
c.String(200, "<h1>Hello world</h1>")
|
|
|
|
})
|
2022-02-07 15:15:44 +00:00
|
|
|
go func() {
|
|
|
|
err := http.Serve(ln, r.Handler())
|
|
|
|
if err != nil {
|
2022-11-06 09:12:11 +00:00
|
|
|
t.Log(err)
|
2022-02-07 15:15:44 +00:00
|
|
|
}
|
|
|
|
}()
|
2022-02-05 13:13:20 +00:00
|
|
|
defer ln.Close()
|
|
|
|
|
|
|
|
url := "http://" + ln.Addr().String() + "/"
|
|
|
|
|
2022-09-01 02:21:27 +00:00
|
|
|
httpClient := http.Client{
|
2022-02-05 13:13:20 +00:00
|
|
|
Transport: &http2.Transport{
|
|
|
|
AllowHTTP: true,
|
|
|
|
DialTLS: func(netw, addr string, cfg *tls.Config) (net.Conn, error) {
|
|
|
|
return net.Dial(netw, addr)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-09-01 02:21:27 +00:00
|
|
|
res, err := httpClient.Get(url)
|
2022-02-05 13:13:20 +00:00
|
|
|
if err != nil {
|
2022-11-06 09:12:11 +00:00
|
|
|
t.Error(err)
|
2022-02-05 13:13:20 +00:00
|
|
|
}
|
|
|
|
|
2022-11-17 14:35:55 +00:00
|
|
|
resp, _ := io.ReadAll(res.Body)
|
2022-02-05 13:13:20 +00:00
|
|
|
assert.Equal(t, "<h1>Hello world</h1>", string(resp))
|
|
|
|
}
|
|
|
|
|
2018-10-16 00:48:41 +00:00
|
|
|
func TestLoadHTMLGlobTestMode(t *testing.T) {
|
|
|
|
ts := setupHTMLFiles(
|
|
|
|
t,
|
|
|
|
TestMode,
|
|
|
|
false,
|
|
|
|
func(router *Engine) {
|
|
|
|
router.LoadHTMLGlob("./testdata/template/*")
|
|
|
|
},
|
|
|
|
)
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
res, err := http.Get(fmt.Sprintf("%s/test", ts.URL))
|
2017-11-21 01:27:57 +00:00
|
|
|
if err != nil {
|
2022-11-06 09:12:11 +00:00
|
|
|
t.Error(err)
|
2017-11-21 01:27:57 +00:00
|
|
|
}
|
|
|
|
|
2022-11-17 14:35:55 +00:00
|
|
|
resp, _ := io.ReadAll(res.Body)
|
2018-09-12 13:21:26 +00:00
|
|
|
assert.Equal(t, "<h1>Hello world</h1>", string(resp))
|
2017-11-21 01:27:57 +00:00
|
|
|
}
|
|
|
|
|
2018-10-16 00:48:41 +00:00
|
|
|
func TestLoadHTMLGlobReleaseMode(t *testing.T) {
|
|
|
|
ts := setupHTMLFiles(
|
|
|
|
t,
|
|
|
|
ReleaseMode,
|
|
|
|
false,
|
|
|
|
func(router *Engine) {
|
|
|
|
router.LoadHTMLGlob("./testdata/template/*")
|
|
|
|
},
|
|
|
|
)
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
res, err := http.Get(fmt.Sprintf("%s/test", ts.URL))
|
2017-11-21 01:27:57 +00:00
|
|
|
if err != nil {
|
2022-11-06 09:12:11 +00:00
|
|
|
t.Error(err)
|
2017-11-21 01:27:57 +00:00
|
|
|
}
|
|
|
|
|
2022-11-17 14:35:55 +00:00
|
|
|
resp, _ := io.ReadAll(res.Body)
|
2018-09-12 13:21:26 +00:00
|
|
|
assert.Equal(t, "<h1>Hello world</h1>", string(resp))
|
2017-11-21 01:27:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadHTMLGlobUsingTLS(t *testing.T) {
|
2018-10-16 00:48:41 +00:00
|
|
|
ts := setupHTMLFiles(
|
|
|
|
t,
|
|
|
|
DebugMode,
|
|
|
|
true,
|
|
|
|
func(router *Engine) {
|
|
|
|
router.LoadHTMLGlob("./testdata/template/*")
|
|
|
|
},
|
|
|
|
)
|
|
|
|
defer ts.Close()
|
|
|
|
|
2017-11-21 01:27:57 +00:00
|
|
|
// Use InsecureSkipVerify for avoiding `x509: certificate signed by unknown authority` error
|
|
|
|
tr := &http.Transport{
|
|
|
|
TLSClientConfig: &tls.Config{
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
client := &http.Client{Transport: tr}
|
2018-10-16 00:48:41 +00:00
|
|
|
res, err := client.Get(fmt.Sprintf("%s/test", ts.URL))
|
2017-11-21 01:27:57 +00:00
|
|
|
if err != nil {
|
2022-11-06 09:12:11 +00:00
|
|
|
t.Error(err)
|
2017-11-21 01:27:57 +00:00
|
|
|
}
|
|
|
|
|
2022-11-17 14:35:55 +00:00
|
|
|
resp, _ := io.ReadAll(res.Body)
|
2018-09-12 13:21:26 +00:00
|
|
|
assert.Equal(t, "<h1>Hello world</h1>", string(resp))
|
2017-11-21 01:27:57 +00:00
|
|
|
}
|
|
|
|
|
2017-07-02 05:06:43 +00:00
|
|
|
func TestLoadHTMLGlobFromFuncMap(t *testing.T) {
|
2018-10-16 00:48:41 +00:00
|
|
|
ts := setupHTMLFiles(
|
|
|
|
t,
|
|
|
|
DebugMode,
|
|
|
|
false,
|
|
|
|
func(router *Engine) {
|
|
|
|
router.LoadHTMLGlob("./testdata/template/*")
|
|
|
|
},
|
|
|
|
)
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
res, err := http.Get(fmt.Sprintf("%s/raw", ts.URL))
|
2017-07-02 04:38:05 +00:00
|
|
|
if err != nil {
|
2022-11-06 09:12:11 +00:00
|
|
|
t.Error(err)
|
2017-07-02 04:38:05 +00:00
|
|
|
}
|
|
|
|
|
2022-11-17 14:35:55 +00:00
|
|
|
resp, _ := io.ReadAll(res.Body)
|
2022-04-21 10:21:46 +00:00
|
|
|
assert.Equal(t, "Date: 2017/07/01", string(resp))
|
2017-07-02 04:38:05 +00:00
|
|
|
}
|
|
|
|
|
2014-08-20 23:01:42 +00:00
|
|
|
func init() {
|
|
|
|
SetMode(TestMode)
|
|
|
|
}
|
|
|
|
|
2015-04-08 00:58:35 +00:00
|
|
|
func TestCreateEngine(t *testing.T) {
|
|
|
|
router := New()
|
2015-07-08 02:27:23 +00:00
|
|
|
assert.Equal(t, "/", router.basePath)
|
2015-04-08 00:58:35 +00:00
|
|
|
assert.Equal(t, router.engine, router)
|
|
|
|
assert.Empty(t, router.Handlers)
|
2015-06-04 02:32:18 +00:00
|
|
|
}
|
|
|
|
|
2018-10-16 00:48:41 +00:00
|
|
|
func TestLoadHTMLFilesTestMode(t *testing.T) {
|
|
|
|
ts := setupHTMLFiles(
|
|
|
|
t,
|
|
|
|
TestMode,
|
|
|
|
false,
|
|
|
|
func(router *Engine) {
|
|
|
|
router.LoadHTMLFiles("./testdata/template/hello.tmpl", "./testdata/template/raw.tmpl")
|
|
|
|
},
|
|
|
|
)
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
res, err := http.Get(fmt.Sprintf("%s/test", ts.URL))
|
2017-05-29 08:03:49 +00:00
|
|
|
if err != nil {
|
2022-11-06 09:12:11 +00:00
|
|
|
t.Error(err)
|
2017-05-29 08:03:49 +00:00
|
|
|
}
|
|
|
|
|
2022-11-17 14:35:55 +00:00
|
|
|
resp, _ := io.ReadAll(res.Body)
|
2018-09-12 13:21:26 +00:00
|
|
|
assert.Equal(t, "<h1>Hello world</h1>", string(resp))
|
2017-05-29 08:03:49 +00:00
|
|
|
}
|
|
|
|
|
2018-10-16 00:48:41 +00:00
|
|
|
func TestLoadHTMLFilesDebugMode(t *testing.T) {
|
|
|
|
ts := setupHTMLFiles(
|
|
|
|
t,
|
|
|
|
DebugMode,
|
|
|
|
false,
|
|
|
|
func(router *Engine) {
|
|
|
|
router.LoadHTMLFiles("./testdata/template/hello.tmpl", "./testdata/template/raw.tmpl")
|
|
|
|
},
|
|
|
|
)
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
res, err := http.Get(fmt.Sprintf("%s/test", ts.URL))
|
2017-11-21 01:27:57 +00:00
|
|
|
if err != nil {
|
2022-11-06 09:12:11 +00:00
|
|
|
t.Error(err)
|
2017-11-21 01:27:57 +00:00
|
|
|
}
|
|
|
|
|
2022-11-17 14:35:55 +00:00
|
|
|
resp, _ := io.ReadAll(res.Body)
|
2018-09-12 13:21:26 +00:00
|
|
|
assert.Equal(t, "<h1>Hello world</h1>", string(resp))
|
2017-11-21 01:27:57 +00:00
|
|
|
}
|
|
|
|
|
2018-10-16 00:48:41 +00:00
|
|
|
func TestLoadHTMLFilesReleaseMode(t *testing.T) {
|
|
|
|
ts := setupHTMLFiles(
|
|
|
|
t,
|
|
|
|
ReleaseMode,
|
|
|
|
false,
|
|
|
|
func(router *Engine) {
|
|
|
|
router.LoadHTMLFiles("./testdata/template/hello.tmpl", "./testdata/template/raw.tmpl")
|
|
|
|
},
|
|
|
|
)
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
res, err := http.Get(fmt.Sprintf("%s/test", ts.URL))
|
2017-11-21 01:27:57 +00:00
|
|
|
if err != nil {
|
2022-11-06 09:12:11 +00:00
|
|
|
t.Error(err)
|
2017-11-21 01:27:57 +00:00
|
|
|
}
|
|
|
|
|
2022-11-17 14:35:55 +00:00
|
|
|
resp, _ := io.ReadAll(res.Body)
|
2018-09-12 13:21:26 +00:00
|
|
|
assert.Equal(t, "<h1>Hello world</h1>", string(resp))
|
2017-11-21 01:27:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoadHTMLFilesUsingTLS(t *testing.T) {
|
2018-10-16 00:48:41 +00:00
|
|
|
ts := setupHTMLFiles(
|
|
|
|
t,
|
|
|
|
TestMode,
|
|
|
|
true,
|
|
|
|
func(router *Engine) {
|
|
|
|
router.LoadHTMLFiles("./testdata/template/hello.tmpl", "./testdata/template/raw.tmpl")
|
|
|
|
},
|
|
|
|
)
|
|
|
|
defer ts.Close()
|
|
|
|
|
2017-11-21 01:27:57 +00:00
|
|
|
// Use InsecureSkipVerify for avoiding `x509: certificate signed by unknown authority` error
|
|
|
|
tr := &http.Transport{
|
|
|
|
TLSClientConfig: &tls.Config{
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
client := &http.Client{Transport: tr}
|
2018-10-16 00:48:41 +00:00
|
|
|
res, err := client.Get(fmt.Sprintf("%s/test", ts.URL))
|
2017-11-21 01:27:57 +00:00
|
|
|
if err != nil {
|
2022-11-06 09:12:11 +00:00
|
|
|
t.Error(err)
|
2017-11-21 01:27:57 +00:00
|
|
|
}
|
|
|
|
|
2022-11-17 14:35:55 +00:00
|
|
|
resp, _ := io.ReadAll(res.Body)
|
2018-09-12 13:21:26 +00:00
|
|
|
assert.Equal(t, "<h1>Hello world</h1>", string(resp))
|
2017-11-21 01:27:57 +00:00
|
|
|
}
|
|
|
|
|
2017-07-02 05:06:43 +00:00
|
|
|
func TestLoadHTMLFilesFuncMap(t *testing.T) {
|
2018-10-16 00:48:41 +00:00
|
|
|
ts := setupHTMLFiles(
|
|
|
|
t,
|
|
|
|
TestMode,
|
|
|
|
false,
|
|
|
|
func(router *Engine) {
|
|
|
|
router.LoadHTMLFiles("./testdata/template/hello.tmpl", "./testdata/template/raw.tmpl")
|
|
|
|
},
|
|
|
|
)
|
|
|
|
defer ts.Close()
|
|
|
|
|
|
|
|
res, err := http.Get(fmt.Sprintf("%s/raw", ts.URL))
|
2017-07-02 05:06:43 +00:00
|
|
|
if err != nil {
|
2022-11-06 09:12:11 +00:00
|
|
|
t.Error(err)
|
2017-07-02 05:06:43 +00:00
|
|
|
}
|
|
|
|
|
2022-11-17 14:35:55 +00:00
|
|
|
resp, _ := io.ReadAll(res.Body)
|
2022-04-21 10:21:46 +00:00
|
|
|
assert.Equal(t, "Date: 2017/07/01", string(resp))
|
2017-07-02 05:06:43 +00:00
|
|
|
}
|
|
|
|
|
2015-06-04 02:32:18 +00:00
|
|
|
func TestAddRoute(t *testing.T) {
|
|
|
|
router := New()
|
|
|
|
router.addRoute("GET", "/", HandlersChain{func(_ *Context) {}})
|
|
|
|
|
|
|
|
assert.Len(t, router.trees, 1)
|
|
|
|
assert.NotNil(t, router.trees.get("GET"))
|
|
|
|
assert.Nil(t, router.trees.get("POST"))
|
|
|
|
|
|
|
|
router.addRoute("POST", "/", HandlersChain{func(_ *Context) {}})
|
2014-07-04 08:12:28 +00:00
|
|
|
|
2015-06-04 02:32:18 +00:00
|
|
|
assert.Len(t, router.trees, 2)
|
|
|
|
assert.NotNil(t, router.trees.get("GET"))
|
|
|
|
assert.NotNil(t, router.trees.get("POST"))
|
|
|
|
|
|
|
|
router.addRoute("POST", "/post", HandlersChain{func(_ *Context) {}})
|
|
|
|
assert.Len(t, router.trees, 2)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddRouteFails(t *testing.T) {
|
|
|
|
router := New()
|
2015-05-19 21:22:35 +00:00
|
|
|
assert.Panics(t, func() { router.addRoute("", "/", HandlersChain{func(_ *Context) {}}) })
|
|
|
|
assert.Panics(t, func() { router.addRoute("GET", "a", HandlersChain{func(_ *Context) {}}) })
|
|
|
|
assert.Panics(t, func() { router.addRoute("GET", "/", HandlersChain{}) })
|
2015-06-04 02:32:18 +00:00
|
|
|
|
|
|
|
router.addRoute("POST", "/post", HandlersChain{func(_ *Context) {}})
|
|
|
|
assert.Panics(t, func() {
|
|
|
|
router.addRoute("POST", "/post", HandlersChain{func(_ *Context) {}})
|
|
|
|
})
|
2014-07-04 08:12:28 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 00:58:35 +00:00
|
|
|
func TestCreateDefaultRouter(t *testing.T) {
|
|
|
|
router := Default()
|
|
|
|
assert.Len(t, router.Handlers, 2)
|
2014-07-04 08:12:28 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 00:58:35 +00:00
|
|
|
func TestNoRouteWithoutGlobalHandlers(t *testing.T) {
|
2015-05-31 20:35:49 +00:00
|
|
|
var middleware0 HandlerFunc = func(c *Context) {}
|
|
|
|
var middleware1 HandlerFunc = func(c *Context) {}
|
2015-04-08 00:58:35 +00:00
|
|
|
|
|
|
|
router := New()
|
|
|
|
|
|
|
|
router.NoRoute(middleware0)
|
|
|
|
assert.Nil(t, router.Handlers)
|
|
|
|
assert.Len(t, router.noRoute, 1)
|
|
|
|
assert.Len(t, router.allNoRoute, 1)
|
2015-05-31 20:35:49 +00:00
|
|
|
compareFunc(t, router.noRoute[0], middleware0)
|
|
|
|
compareFunc(t, router.allNoRoute[0], middleware0)
|
2015-04-08 00:58:35 +00:00
|
|
|
|
|
|
|
router.NoRoute(middleware1, middleware0)
|
|
|
|
assert.Len(t, router.noRoute, 2)
|
|
|
|
assert.Len(t, router.allNoRoute, 2)
|
2015-05-31 20:35:49 +00:00
|
|
|
compareFunc(t, router.noRoute[0], middleware1)
|
|
|
|
compareFunc(t, router.allNoRoute[0], middleware1)
|
|
|
|
compareFunc(t, router.noRoute[1], middleware0)
|
|
|
|
compareFunc(t, router.allNoRoute[1], middleware0)
|
2014-07-04 08:12:28 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 00:58:35 +00:00
|
|
|
func TestNoRouteWithGlobalHandlers(t *testing.T) {
|
2015-05-31 20:35:49 +00:00
|
|
|
var middleware0 HandlerFunc = func(c *Context) {}
|
|
|
|
var middleware1 HandlerFunc = func(c *Context) {}
|
|
|
|
var middleware2 HandlerFunc = func(c *Context) {}
|
2015-04-08 00:58:35 +00:00
|
|
|
|
|
|
|
router := New()
|
|
|
|
router.Use(middleware2)
|
|
|
|
|
|
|
|
router.NoRoute(middleware0)
|
|
|
|
assert.Len(t, router.allNoRoute, 2)
|
|
|
|
assert.Len(t, router.Handlers, 1)
|
|
|
|
assert.Len(t, router.noRoute, 1)
|
|
|
|
|
2015-05-31 20:35:49 +00:00
|
|
|
compareFunc(t, router.Handlers[0], middleware2)
|
|
|
|
compareFunc(t, router.noRoute[0], middleware0)
|
|
|
|
compareFunc(t, router.allNoRoute[0], middleware2)
|
|
|
|
compareFunc(t, router.allNoRoute[1], middleware0)
|
2015-04-08 00:58:35 +00:00
|
|
|
|
|
|
|
router.Use(middleware1)
|
|
|
|
assert.Len(t, router.allNoRoute, 3)
|
|
|
|
assert.Len(t, router.Handlers, 2)
|
|
|
|
assert.Len(t, router.noRoute, 1)
|
|
|
|
|
2015-05-31 20:35:49 +00:00
|
|
|
compareFunc(t, router.Handlers[0], middleware2)
|
|
|
|
compareFunc(t, router.Handlers[1], middleware1)
|
|
|
|
compareFunc(t, router.noRoute[0], middleware0)
|
|
|
|
compareFunc(t, router.allNoRoute[0], middleware2)
|
|
|
|
compareFunc(t, router.allNoRoute[1], middleware1)
|
|
|
|
compareFunc(t, router.allNoRoute[2], middleware0)
|
2014-07-04 08:12:28 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 00:58:35 +00:00
|
|
|
func TestNoMethodWithoutGlobalHandlers(t *testing.T) {
|
2015-05-31 20:35:49 +00:00
|
|
|
var middleware0 HandlerFunc = func(c *Context) {}
|
|
|
|
var middleware1 HandlerFunc = func(c *Context) {}
|
2015-04-08 00:58:35 +00:00
|
|
|
|
|
|
|
router := New()
|
|
|
|
|
|
|
|
router.NoMethod(middleware0)
|
|
|
|
assert.Empty(t, router.Handlers)
|
|
|
|
assert.Len(t, router.noMethod, 1)
|
|
|
|
assert.Len(t, router.allNoMethod, 1)
|
2015-05-31 20:35:49 +00:00
|
|
|
compareFunc(t, router.noMethod[0], middleware0)
|
|
|
|
compareFunc(t, router.allNoMethod[0], middleware0)
|
2015-04-08 00:58:35 +00:00
|
|
|
|
|
|
|
router.NoMethod(middleware1, middleware0)
|
|
|
|
assert.Len(t, router.noMethod, 2)
|
|
|
|
assert.Len(t, router.allNoMethod, 2)
|
2015-05-31 20:35:49 +00:00
|
|
|
compareFunc(t, router.noMethod[0], middleware1)
|
|
|
|
compareFunc(t, router.allNoMethod[0], middleware1)
|
|
|
|
compareFunc(t, router.noMethod[1], middleware0)
|
|
|
|
compareFunc(t, router.allNoMethod[1], middleware0)
|
2014-07-04 08:12:28 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 00:58:35 +00:00
|
|
|
func TestRebuild404Handlers(t *testing.T) {
|
2014-07-05 15:04:11 +00:00
|
|
|
}
|
2014-07-28 10:05:23 +00:00
|
|
|
|
2015-04-08 00:58:35 +00:00
|
|
|
func TestNoMethodWithGlobalHandlers(t *testing.T) {
|
2015-05-31 20:35:49 +00:00
|
|
|
var middleware0 HandlerFunc = func(c *Context) {}
|
|
|
|
var middleware1 HandlerFunc = func(c *Context) {}
|
|
|
|
var middleware2 HandlerFunc = func(c *Context) {}
|
2015-04-08 00:58:35 +00:00
|
|
|
|
|
|
|
router := New()
|
|
|
|
router.Use(middleware2)
|
|
|
|
|
|
|
|
router.NoMethod(middleware0)
|
|
|
|
assert.Len(t, router.allNoMethod, 2)
|
|
|
|
assert.Len(t, router.Handlers, 1)
|
|
|
|
assert.Len(t, router.noMethod, 1)
|
|
|
|
|
2015-05-31 20:35:49 +00:00
|
|
|
compareFunc(t, router.Handlers[0], middleware2)
|
|
|
|
compareFunc(t, router.noMethod[0], middleware0)
|
|
|
|
compareFunc(t, router.allNoMethod[0], middleware2)
|
|
|
|
compareFunc(t, router.allNoMethod[1], middleware0)
|
2015-04-08 00:58:35 +00:00
|
|
|
|
|
|
|
router.Use(middleware1)
|
|
|
|
assert.Len(t, router.allNoMethod, 3)
|
|
|
|
assert.Len(t, router.Handlers, 2)
|
|
|
|
assert.Len(t, router.noMethod, 1)
|
|
|
|
|
2015-05-31 20:35:49 +00:00
|
|
|
compareFunc(t, router.Handlers[0], middleware2)
|
|
|
|
compareFunc(t, router.Handlers[1], middleware1)
|
|
|
|
compareFunc(t, router.noMethod[0], middleware0)
|
|
|
|
compareFunc(t, router.allNoMethod[0], middleware2)
|
|
|
|
compareFunc(t, router.allNoMethod[1], middleware1)
|
|
|
|
compareFunc(t, router.allNoMethod[2], middleware0)
|
|
|
|
}
|
|
|
|
|
2022-03-21 01:43:17 +00:00
|
|
|
func compareFunc(t *testing.T, a, b any) {
|
2015-05-31 20:35:49 +00:00
|
|
|
sf1 := reflect.ValueOf(a)
|
|
|
|
sf2 := reflect.ValueOf(b)
|
|
|
|
if sf1.Pointer() != sf2.Pointer() {
|
|
|
|
t.Error("different functions")
|
|
|
|
}
|
2014-07-28 10:05:23 +00:00
|
|
|
}
|
2015-06-07 02:20:39 +00:00
|
|
|
|
|
|
|
func TestListOfRoutes(t *testing.T) {
|
|
|
|
router := New()
|
2016-04-14 23:16:46 +00:00
|
|
|
router.GET("/favicon.ico", handlerTest1)
|
|
|
|
router.GET("/", handlerTest1)
|
2015-06-07 02:20:39 +00:00
|
|
|
group := router.Group("/users")
|
|
|
|
{
|
2016-04-14 23:16:46 +00:00
|
|
|
group.GET("/", handlerTest2)
|
|
|
|
group.GET("/:id", handlerTest1)
|
|
|
|
group.POST("/:id", handlerTest2)
|
2015-06-07 02:20:39 +00:00
|
|
|
}
|
|
|
|
router.Static("/static", ".")
|
|
|
|
|
|
|
|
list := router.Routes()
|
|
|
|
|
|
|
|
assert.Len(t, list, 7)
|
2015-08-21 18:57:53 +00:00
|
|
|
assertRoutePresent(t, list, RouteInfo{
|
2015-06-18 15:17:22 +00:00
|
|
|
Method: "GET",
|
|
|
|
Path: "/favicon.ico",
|
2016-04-14 23:35:22 +00:00
|
|
|
Handler: "^(.*/vendor/)?github.com/gin-gonic/gin.handlerTest1$",
|
2015-06-07 02:20:39 +00:00
|
|
|
})
|
2015-08-21 18:57:53 +00:00
|
|
|
assertRoutePresent(t, list, RouteInfo{
|
2015-06-18 15:17:22 +00:00
|
|
|
Method: "GET",
|
|
|
|
Path: "/",
|
2016-04-14 23:35:22 +00:00
|
|
|
Handler: "^(.*/vendor/)?github.com/gin-gonic/gin.handlerTest1$",
|
2015-06-07 02:20:39 +00:00
|
|
|
})
|
2015-08-21 18:57:53 +00:00
|
|
|
assertRoutePresent(t, list, RouteInfo{
|
2015-06-18 15:17:22 +00:00
|
|
|
Method: "GET",
|
|
|
|
Path: "/users/",
|
2016-04-14 23:35:22 +00:00
|
|
|
Handler: "^(.*/vendor/)?github.com/gin-gonic/gin.handlerTest2$",
|
2015-06-07 02:20:39 +00:00
|
|
|
})
|
2015-08-21 18:57:53 +00:00
|
|
|
assertRoutePresent(t, list, RouteInfo{
|
2015-06-18 15:17:22 +00:00
|
|
|
Method: "GET",
|
|
|
|
Path: "/users/:id",
|
2016-04-14 23:35:22 +00:00
|
|
|
Handler: "^(.*/vendor/)?github.com/gin-gonic/gin.handlerTest1$",
|
2015-06-07 02:20:39 +00:00
|
|
|
})
|
2015-08-21 18:57:53 +00:00
|
|
|
assertRoutePresent(t, list, RouteInfo{
|
2015-06-18 15:17:22 +00:00
|
|
|
Method: "POST",
|
|
|
|
Path: "/users/:id",
|
2016-04-14 23:35:22 +00:00
|
|
|
Handler: "^(.*/vendor/)?github.com/gin-gonic/gin.handlerTest2$",
|
2015-06-07 02:20:39 +00:00
|
|
|
})
|
|
|
|
}
|
2015-06-18 15:17:22 +00:00
|
|
|
|
2019-01-18 01:57:06 +00:00
|
|
|
func TestEngineHandleContext(t *testing.T) {
|
|
|
|
r := New()
|
|
|
|
r.GET("/", func(c *Context) {
|
|
|
|
c.Request.URL.Path = "/v2"
|
|
|
|
r.HandleContext(c)
|
|
|
|
})
|
|
|
|
v2 := r.Group("/v2")
|
|
|
|
{
|
|
|
|
v2.GET("/", func(c *Context) {})
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.NotPanics(t, func() {
|
2022-01-02 11:07:44 +00:00
|
|
|
w := PerformRequest(r, "GET", "/")
|
2019-01-18 01:57:06 +00:00
|
|
|
assert.Equal(t, 301, w.Code)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-02-18 01:35:08 +00:00
|
|
|
func TestEngineHandleContextManyReEntries(t *testing.T) {
|
|
|
|
expectValue := 10000
|
|
|
|
|
|
|
|
var handlerCounter, middlewareCounter int64
|
|
|
|
|
|
|
|
r := New()
|
|
|
|
r.Use(func(c *Context) {
|
|
|
|
atomic.AddInt64(&middlewareCounter, 1)
|
|
|
|
})
|
|
|
|
r.GET("/:count", func(c *Context) {
|
|
|
|
countStr := c.Param("count")
|
|
|
|
count, err := strconv.Atoi(countStr)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
n, err := c.Writer.Write([]byte("."))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 1, n)
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case count > 0:
|
|
|
|
c.Request.URL.Path = "/" + strconv.Itoa(count-1)
|
|
|
|
r.HandleContext(c)
|
|
|
|
}
|
|
|
|
}, func(c *Context) {
|
|
|
|
atomic.AddInt64(&handlerCounter, 1)
|
|
|
|
})
|
|
|
|
|
|
|
|
assert.NotPanics(t, func() {
|
2022-01-02 11:07:44 +00:00
|
|
|
w := PerformRequest(r, "GET", "/"+strconv.Itoa(expectValue-1)) // include 0 value
|
2019-02-18 01:35:08 +00:00
|
|
|
assert.Equal(t, 200, w.Code)
|
|
|
|
assert.Equal(t, expectValue, w.Body.Len())
|
|
|
|
})
|
|
|
|
|
|
|
|
assert.Equal(t, int64(expectValue), handlerCounter)
|
|
|
|
assert.Equal(t, int64(expectValue), middlewareCounter)
|
|
|
|
}
|
|
|
|
|
2021-04-06 03:37:25 +00:00
|
|
|
func TestPrepareTrustedCIRDsWith(t *testing.T) {
|
|
|
|
r := New()
|
|
|
|
|
|
|
|
// valid ipv4 cidr
|
|
|
|
{
|
|
|
|
expectedTrustedCIDRs := []*net.IPNet{parseCIDR("0.0.0.0/0")}
|
2021-10-06 01:37:25 +00:00
|
|
|
err := r.SetTrustedProxies([]string{"0.0.0.0/0"})
|
2021-04-06 03:37:25 +00:00
|
|
|
|
|
|
|
assert.NoError(t, err)
|
2021-10-06 01:37:25 +00:00
|
|
|
assert.Equal(t, expectedTrustedCIDRs, r.trustedCIDRs)
|
2021-04-06 03:37:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// invalid ipv4 cidr
|
|
|
|
{
|
2021-10-06 01:37:25 +00:00
|
|
|
err := r.SetTrustedProxies([]string{"192.168.1.33/33"})
|
2021-04-06 03:37:25 +00:00
|
|
|
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// valid ipv4 address
|
|
|
|
{
|
|
|
|
expectedTrustedCIDRs := []*net.IPNet{parseCIDR("192.168.1.33/32")}
|
|
|
|
|
2021-10-06 01:37:25 +00:00
|
|
|
err := r.SetTrustedProxies([]string{"192.168.1.33"})
|
2021-04-06 03:37:25 +00:00
|
|
|
|
|
|
|
assert.NoError(t, err)
|
2021-10-06 01:37:25 +00:00
|
|
|
assert.Equal(t, expectedTrustedCIDRs, r.trustedCIDRs)
|
2021-04-06 03:37:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// invalid ipv4 address
|
|
|
|
{
|
2021-10-06 01:37:25 +00:00
|
|
|
err := r.SetTrustedProxies([]string{"192.168.1.256"})
|
2021-04-06 03:37:25 +00:00
|
|
|
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// valid ipv6 address
|
|
|
|
{
|
|
|
|
expectedTrustedCIDRs := []*net.IPNet{parseCIDR("2002:0000:0000:1234:abcd:ffff:c0a8:0101/128")}
|
2021-10-06 01:37:25 +00:00
|
|
|
err := r.SetTrustedProxies([]string{"2002:0000:0000:1234:abcd:ffff:c0a8:0101"})
|
2021-04-06 03:37:25 +00:00
|
|
|
|
|
|
|
assert.NoError(t, err)
|
2021-10-06 01:37:25 +00:00
|
|
|
assert.Equal(t, expectedTrustedCIDRs, r.trustedCIDRs)
|
2021-04-06 03:37:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// invalid ipv6 address
|
|
|
|
{
|
2021-10-06 01:37:25 +00:00
|
|
|
err := r.SetTrustedProxies([]string{"gggg:0000:0000:1234:abcd:ffff:c0a8:0101"})
|
2021-04-06 03:37:25 +00:00
|
|
|
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// valid ipv6 cidr
|
|
|
|
{
|
|
|
|
expectedTrustedCIDRs := []*net.IPNet{parseCIDR("::/0")}
|
2021-10-06 01:37:25 +00:00
|
|
|
err := r.SetTrustedProxies([]string{"::/0"})
|
2021-04-06 03:37:25 +00:00
|
|
|
|
|
|
|
assert.NoError(t, err)
|
2021-10-06 01:37:25 +00:00
|
|
|
assert.Equal(t, expectedTrustedCIDRs, r.trustedCIDRs)
|
2021-04-06 03:37:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// invalid ipv6 cidr
|
|
|
|
{
|
2021-10-06 01:37:25 +00:00
|
|
|
err := r.SetTrustedProxies([]string{"gggg:0000:0000:1234:abcd:ffff:c0a8:0101/129"})
|
2021-04-06 03:37:25 +00:00
|
|
|
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// valid combination
|
|
|
|
{
|
|
|
|
expectedTrustedCIDRs := []*net.IPNet{
|
|
|
|
parseCIDR("::/0"),
|
|
|
|
parseCIDR("192.168.0.0/16"),
|
|
|
|
parseCIDR("172.16.0.1/32"),
|
|
|
|
}
|
2021-10-06 01:37:25 +00:00
|
|
|
err := r.SetTrustedProxies([]string{
|
2021-04-06 03:37:25 +00:00
|
|
|
"::/0",
|
|
|
|
"192.168.0.0/16",
|
|
|
|
"172.16.0.1",
|
2021-10-06 01:37:25 +00:00
|
|
|
})
|
2021-04-06 03:37:25 +00:00
|
|
|
|
|
|
|
assert.NoError(t, err)
|
2021-10-06 01:37:25 +00:00
|
|
|
assert.Equal(t, expectedTrustedCIDRs, r.trustedCIDRs)
|
2021-04-06 03:37:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// invalid combination
|
|
|
|
{
|
2021-10-06 01:37:25 +00:00
|
|
|
err := r.SetTrustedProxies([]string{
|
2021-04-06 03:37:25 +00:00
|
|
|
"::/0",
|
|
|
|
"192.168.0.0/16",
|
|
|
|
"172.16.0.256",
|
2021-10-06 01:37:25 +00:00
|
|
|
})
|
2021-04-06 03:37:25 +00:00
|
|
|
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// nil value
|
|
|
|
{
|
2021-10-06 01:37:25 +00:00
|
|
|
err := r.SetTrustedProxies(nil)
|
2021-04-06 03:37:25 +00:00
|
|
|
|
2021-10-06 01:37:25 +00:00
|
|
|
assert.Nil(t, r.trustedCIDRs)
|
2021-04-06 03:37:25 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseCIDR(cidr string) *net.IPNet {
|
|
|
|
_, parsedCIDR, err := net.ParseCIDR(cidr)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
return parsedCIDR
|
|
|
|
}
|
|
|
|
|
2015-08-21 18:57:53 +00:00
|
|
|
func assertRoutePresent(t *testing.T, gotRoutes RoutesInfo, wantRoute RouteInfo) {
|
|
|
|
for _, gotRoute := range gotRoutes {
|
|
|
|
if gotRoute.Path == wantRoute.Path && gotRoute.Method == wantRoute.Method {
|
2016-03-09 07:29:01 +00:00
|
|
|
assert.Regexp(t, wantRoute.Handler, gotRoute.Handler)
|
2015-08-21 18:57:53 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
t.Errorf("route not found: %v", wantRoute)
|
|
|
|
}
|
|
|
|
|
2016-04-14 23:16:46 +00:00
|
|
|
func handlerTest1(c *Context) {}
|
|
|
|
func handlerTest2(c *Context) {}
|
2024-03-11 14:35:30 +00:00
|
|
|
|
|
|
|
func TestNewOptionFunc(t *testing.T) {
|
|
|
|
var fc = func(e *Engine) {
|
|
|
|
e.GET("/test1", handlerTest1)
|
|
|
|
e.GET("/test2", handlerTest2)
|
|
|
|
|
|
|
|
e.Use(func(c *Context) {
|
|
|
|
c.Next()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
r := New(fc)
|
|
|
|
|
|
|
|
routes := r.Routes()
|
|
|
|
assertRoutePresent(t, routes, RouteInfo{Path: "/test1", Method: "GET", Handler: "github.com/gin-gonic/gin.handlerTest1"})
|
|
|
|
assertRoutePresent(t, routes, RouteInfo{Path: "/test2", Method: "GET", Handler: "github.com/gin-gonic/gin.handlerTest2"})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWithOptionFunc(t *testing.T) {
|
|
|
|
r := New()
|
|
|
|
|
|
|
|
r.With(func(e *Engine) {
|
|
|
|
e.GET("/test1", handlerTest1)
|
|
|
|
e.GET("/test2", handlerTest2)
|
|
|
|
|
|
|
|
e.Use(func(c *Context) {
|
|
|
|
c.Next()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
routes := r.Routes()
|
|
|
|
assertRoutePresent(t, routes, RouteInfo{Path: "/test1", Method: "GET", Handler: "github.com/gin-gonic/gin.handlerTest1"})
|
|
|
|
assertRoutePresent(t, routes, RouteInfo{Path: "/test2", Method: "GET", Handler: "github.com/gin-gonic/gin.handlerTest2"})
|
|
|
|
}
|