2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2018-12-30 22:02:46 +08:00
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
2019-02-02 16:18:25 +08:00
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
2018-12-30 22:02:46 +08:00
|
|
|
|
|
|
|
package ghttp_test
|
|
|
|
|
|
|
|
import (
|
2019-06-19 09:06:52 +08:00
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
"time"
|
2019-07-29 21:01:19 +08:00
|
|
|
|
2021-10-11 21:41:56 +08:00
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
|
|
"github.com/gogf/gf/v2/net/ghttp"
|
|
|
|
"github.com/gogf/gf/v2/test/gtest"
|
2018-12-30 22:02:46 +08:00
|
|
|
)
|
|
|
|
|
2020-06-03 00:09:51 +08:00
|
|
|
func Test_Router_Basic1(t *testing.T) {
|
2020-03-30 20:47:50 +08:00
|
|
|
p, _ := ports.PopRand()
|
2019-06-19 09:06:52 +08:00
|
|
|
s := g.Server(p)
|
|
|
|
s.BindHandler("/:name", func(r *ghttp.Request) {
|
|
|
|
r.Response.Write("/:name")
|
|
|
|
})
|
2019-08-03 17:14:54 +08:00
|
|
|
s.BindHandler("/:name/update", func(r *ghttp.Request) {
|
|
|
|
r.Response.Write(r.Get("name"))
|
|
|
|
})
|
|
|
|
s.BindHandler("/:name/:action", func(r *ghttp.Request) {
|
|
|
|
r.Response.Write(r.Get("action"))
|
|
|
|
})
|
2019-06-19 09:06:52 +08:00
|
|
|
s.BindHandler("/:name/*any", func(r *ghttp.Request) {
|
|
|
|
r.Response.Write(r.Get("any"))
|
|
|
|
})
|
2019-08-03 17:14:54 +08:00
|
|
|
s.BindHandler("/user/list/{field}.html", func(r *ghttp.Request) {
|
|
|
|
r.Response.Write(r.Get("field"))
|
|
|
|
})
|
2019-06-19 09:06:52 +08:00
|
|
|
s.SetPort(p)
|
2019-12-04 13:55:08 +08:00
|
|
|
s.SetDumpRouterMap(false)
|
2019-06-19 09:06:52 +08:00
|
|
|
s.Start()
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
2019-10-09 00:33:58 +08:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-11-25 16:40:45 +08:00
|
|
|
client := g.Client()
|
2019-06-19 09:06:52 +08:00
|
|
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
2021-09-30 14:06:46 +08:00
|
|
|
t.Assert(client.GetContent(ctx, "/john"), "")
|
|
|
|
t.Assert(client.GetContent(ctx, "/john/update"), "john")
|
|
|
|
t.Assert(client.GetContent(ctx, "/john/edit"), "edit")
|
|
|
|
t.Assert(client.GetContent(ctx, "/user/list/100.html"), "100")
|
2019-06-19 09:06:52 +08:00
|
|
|
})
|
2018-12-30 22:02:46 +08:00
|
|
|
}
|
2019-02-17 20:45:35 +08:00
|
|
|
|
2020-06-03 00:09:51 +08:00
|
|
|
func Test_Router_Basic2(t *testing.T) {
|
|
|
|
p, _ := ports.PopRand()
|
|
|
|
s := g.Server(p)
|
|
|
|
s.BindHandler("/{hash}", func(r *ghttp.Request) {
|
|
|
|
r.Response.Write(r.Get("hash"))
|
|
|
|
})
|
|
|
|
s.BindHandler("/{hash}.{type}", func(r *ghttp.Request) {
|
|
|
|
r.Response.Write(r.Get("type"))
|
|
|
|
})
|
|
|
|
s.SetPort(p)
|
|
|
|
s.SetDumpRouterMap(false)
|
|
|
|
s.Start()
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-11-25 16:40:45 +08:00
|
|
|
client := g.Client()
|
2020-06-03 00:09:51 +08:00
|
|
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
2021-09-30 14:06:46 +08:00
|
|
|
t.Assert(client.GetContent(ctx, "/data"), "data")
|
|
|
|
t.Assert(client.GetContent(ctx, "/data.json"), "json")
|
2020-06-03 00:09:51 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-08-12 21:01:17 +08:00
|
|
|
func Test_Router_Value(t *testing.T) {
|
|
|
|
p, _ := ports.PopRand()
|
|
|
|
s := g.Server(p)
|
|
|
|
s.BindHandler("/{hash}", func(r *ghttp.Request) {
|
2021-09-27 21:27:24 +08:00
|
|
|
r.Response.Write(r.GetRouter("hash").String())
|
2020-08-12 21:01:17 +08:00
|
|
|
})
|
|
|
|
s.BindHandler("/{hash}.{type}", func(r *ghttp.Request) {
|
2021-09-27 21:27:24 +08:00
|
|
|
r.Response.Write(r.GetRouter("type").String())
|
2020-08-12 21:01:17 +08:00
|
|
|
})
|
|
|
|
s.BindHandler("/{hash}.{type}.map", func(r *ghttp.Request) {
|
|
|
|
r.Response.Write(r.GetRouterMap()["type"])
|
|
|
|
})
|
|
|
|
s.SetPort(p)
|
|
|
|
s.SetDumpRouterMap(false)
|
|
|
|
s.Start()
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-11-25 16:40:45 +08:00
|
|
|
client := g.Client()
|
2020-08-12 21:01:17 +08:00
|
|
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
2021-09-30 14:06:46 +08:00
|
|
|
t.Assert(client.GetContent(ctx, "/data"), "data")
|
|
|
|
t.Assert(client.GetContent(ctx, "/data.json"), "json")
|
|
|
|
t.Assert(client.GetContent(ctx, "/data.json.map"), "json")
|
2020-08-12 21:01:17 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-02-14 22:13:41 +08:00
|
|
|
// HTTP method register.
|
2019-02-17 20:45:35 +08:00
|
|
|
func Test_Router_Method(t *testing.T) {
|
2020-03-30 20:47:50 +08:00
|
|
|
p, _ := ports.PopRand()
|
2019-06-19 09:06:52 +08:00
|
|
|
s := g.Server(p)
|
|
|
|
s.BindHandler("GET:/get", func(r *ghttp.Request) {
|
|
|
|
|
|
|
|
})
|
|
|
|
s.BindHandler("POST:/post", func(r *ghttp.Request) {
|
|
|
|
|
|
|
|
})
|
|
|
|
s.SetPort(p)
|
2019-12-04 13:55:08 +08:00
|
|
|
s.SetDumpRouterMap(false)
|
2019-06-19 09:06:52 +08:00
|
|
|
s.Start()
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
2019-10-09 00:33:58 +08:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-11-25 16:40:45 +08:00
|
|
|
client := g.Client()
|
2019-06-19 09:06:52 +08:00
|
|
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
|
|
|
|
2021-09-30 14:06:46 +08:00
|
|
|
resp1, err := client.Get(ctx, "/get")
|
2019-06-19 09:06:52 +08:00
|
|
|
defer resp1.Close()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
|
|
|
t.Assert(resp1.StatusCode, 200)
|
2019-06-19 09:06:52 +08:00
|
|
|
|
2021-09-30 14:06:46 +08:00
|
|
|
resp2, err := client.Post(ctx, "/get")
|
2019-06-19 09:06:52 +08:00
|
|
|
defer resp2.Close()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
|
|
|
t.Assert(resp2.StatusCode, 404)
|
2019-06-19 09:06:52 +08:00
|
|
|
|
2021-09-30 14:06:46 +08:00
|
|
|
resp3, err := client.Get(ctx, "/post")
|
2019-06-19 09:06:52 +08:00
|
|
|
defer resp3.Close()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
|
|
|
t.Assert(resp3.StatusCode, 404)
|
2019-06-19 09:06:52 +08:00
|
|
|
|
2021-09-30 14:06:46 +08:00
|
|
|
resp4, err := client.Post(ctx, "/post")
|
2019-06-19 09:06:52 +08:00
|
|
|
defer resp4.Close()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
|
|
|
t.Assert(resp4.StatusCode, 200)
|
2019-06-19 09:06:52 +08:00
|
|
|
})
|
2019-02-17 20:45:35 +08:00
|
|
|
}
|
|
|
|
|
2020-02-14 22:13:41 +08:00
|
|
|
// Extra char '/' of the router.
|
|
|
|
func Test_Router_ExtraChar(t *testing.T) {
|
2020-03-30 20:47:50 +08:00
|
|
|
p, _ := ports.PopRand()
|
2020-02-14 22:13:41 +08:00
|
|
|
s := g.Server(p)
|
|
|
|
s.Group("/api", func(group *ghttp.RouterGroup) {
|
|
|
|
group.GET("/test", func(r *ghttp.Request) {
|
|
|
|
r.Response.Write("test")
|
|
|
|
})
|
|
|
|
})
|
|
|
|
s.SetPort(p)
|
|
|
|
s.SetDumpRouterMap(false)
|
|
|
|
s.Start()
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-11-25 16:40:45 +08:00
|
|
|
client := g.Client()
|
2020-02-14 22:13:41 +08:00
|
|
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
|
|
|
|
2021-09-30 14:06:46 +08:00
|
|
|
t.Assert(client.GetContent(ctx, "/api/test"), "test")
|
|
|
|
t.Assert(client.GetContent(ctx, "/api/test/"), "test")
|
|
|
|
t.Assert(client.GetContent(ctx, "/api/test//"), "test")
|
|
|
|
t.Assert(client.GetContent(ctx, "//api/test//"), "test")
|
|
|
|
t.Assert(client.GetContent(ctx, "//api//test//"), "test")
|
|
|
|
t.Assert(client.GetContent(ctx, "///api///test///"), "test")
|
2020-02-14 22:13:41 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Custom status handler.
|
2019-02-17 20:45:35 +08:00
|
|
|
func Test_Router_Status(t *testing.T) {
|
2020-03-30 20:47:50 +08:00
|
|
|
p, _ := ports.PopRand()
|
2019-06-19 09:06:52 +08:00
|
|
|
s := g.Server(p)
|
|
|
|
s.BindHandler("/200", func(r *ghttp.Request) {
|
|
|
|
r.Response.WriteStatus(200)
|
|
|
|
})
|
|
|
|
s.BindHandler("/300", func(r *ghttp.Request) {
|
|
|
|
r.Response.WriteStatus(300)
|
|
|
|
})
|
|
|
|
s.BindHandler("/400", func(r *ghttp.Request) {
|
|
|
|
r.Response.WriteStatus(400)
|
|
|
|
})
|
|
|
|
s.BindHandler("/500", func(r *ghttp.Request) {
|
|
|
|
r.Response.WriteStatus(500)
|
|
|
|
})
|
|
|
|
s.SetPort(p)
|
2019-12-04 13:55:08 +08:00
|
|
|
s.SetDumpRouterMap(false)
|
2019-06-19 09:06:52 +08:00
|
|
|
s.Start()
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
2019-10-09 00:33:58 +08:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-11-25 16:40:45 +08:00
|
|
|
client := g.Client()
|
2019-06-19 09:06:52 +08:00
|
|
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
|
|
|
|
2021-09-30 14:06:46 +08:00
|
|
|
resp1, err := client.Get(ctx, "/200")
|
2019-06-19 09:06:52 +08:00
|
|
|
defer resp1.Close()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
|
|
|
t.Assert(resp1.StatusCode, 200)
|
2019-06-19 09:06:52 +08:00
|
|
|
|
2021-09-30 14:06:46 +08:00
|
|
|
resp2, err := client.Get(ctx, "/300")
|
2019-06-19 09:06:52 +08:00
|
|
|
defer resp2.Close()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
|
|
|
t.Assert(resp2.StatusCode, 300)
|
2019-06-19 09:06:52 +08:00
|
|
|
|
2021-09-30 14:06:46 +08:00
|
|
|
resp3, err := client.Get(ctx, "/400")
|
2019-06-19 09:06:52 +08:00
|
|
|
defer resp3.Close()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
|
|
|
t.Assert(resp3.StatusCode, 400)
|
2019-06-19 09:06:52 +08:00
|
|
|
|
2021-09-30 14:06:46 +08:00
|
|
|
resp4, err := client.Get(ctx, "/500")
|
2019-06-19 09:06:52 +08:00
|
|
|
defer resp4.Close()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
|
|
|
t.Assert(resp4.StatusCode, 500)
|
2019-08-10 17:46:11 +08:00
|
|
|
|
2021-09-30 14:06:46 +08:00
|
|
|
resp5, err := client.Get(ctx, "/404")
|
2019-08-10 17:46:11 +08:00
|
|
|
defer resp5.Close()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
|
|
|
t.Assert(resp5.StatusCode, 404)
|
2019-06-19 09:06:52 +08:00
|
|
|
})
|
2019-02-17 20:45:35 +08:00
|
|
|
}
|
|
|
|
|
2019-02-18 16:12:59 +08:00
|
|
|
func Test_Router_CustomStatusHandler(t *testing.T) {
|
2020-03-30 20:47:50 +08:00
|
|
|
p, _ := ports.PopRand()
|
2019-06-19 09:06:52 +08:00
|
|
|
s := g.Server(p)
|
|
|
|
s.BindHandler("/", func(r *ghttp.Request) {
|
|
|
|
r.Response.Write("hello")
|
|
|
|
})
|
|
|
|
s.BindStatusHandler(404, func(r *ghttp.Request) {
|
|
|
|
r.Response.Write("404 page")
|
|
|
|
})
|
|
|
|
s.SetPort(p)
|
2019-12-04 13:55:08 +08:00
|
|
|
s.SetDumpRouterMap(false)
|
2019-06-19 09:06:52 +08:00
|
|
|
s.Start()
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
2019-10-09 00:33:58 +08:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-11-25 16:40:45 +08:00
|
|
|
client := g.Client()
|
2019-06-19 09:06:52 +08:00
|
|
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
|
|
|
|
2021-09-30 14:06:46 +08:00
|
|
|
t.Assert(client.GetContent(ctx, "/"), "hello")
|
|
|
|
resp, err := client.Get(ctx, "/ThisDoesNotExist")
|
2019-06-19 09:06:52 +08:00
|
|
|
defer resp.Close()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
|
|
|
t.Assert(resp.StatusCode, 404)
|
|
|
|
t.Assert(resp.ReadAllString(), "404 page")
|
2019-06-19 09:06:52 +08:00
|
|
|
})
|
2019-02-18 16:12:59 +08:00
|
|
|
}
|
|
|
|
|
2020-02-14 22:13:41 +08:00
|
|
|
// 404 not found router.
|
2019-02-18 16:12:59 +08:00
|
|
|
func Test_Router_404(t *testing.T) {
|
2020-03-30 20:47:50 +08:00
|
|
|
p, _ := ports.PopRand()
|
2019-06-19 09:06:52 +08:00
|
|
|
s := g.Server(p)
|
|
|
|
s.BindHandler("/", func(r *ghttp.Request) {
|
|
|
|
r.Response.Write("hello")
|
|
|
|
})
|
|
|
|
s.SetPort(p)
|
2019-12-04 13:55:08 +08:00
|
|
|
s.SetDumpRouterMap(false)
|
2019-06-19 09:06:52 +08:00
|
|
|
s.Start()
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
2019-10-09 00:33:58 +08:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-11-25 16:40:45 +08:00
|
|
|
client := g.Client()
|
2019-06-19 09:06:52 +08:00
|
|
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
|
|
|
|
2021-09-30 14:06:46 +08:00
|
|
|
t.Assert(client.GetContent(ctx, "/"), "hello")
|
|
|
|
resp, err := client.Get(ctx, "/ThisDoesNotExist")
|
2019-06-19 09:06:52 +08:00
|
|
|
defer resp.Close()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
|
|
|
t.Assert(resp.StatusCode, 404)
|
2019-06-19 09:06:52 +08:00
|
|
|
})
|
|
|
|
}
|
2019-12-02 21:26:12 +08:00
|
|
|
|
|
|
|
func Test_Router_Priority(t *testing.T) {
|
2020-03-30 20:47:50 +08:00
|
|
|
p, _ := ports.PopRand()
|
2019-12-02 21:26:12 +08:00
|
|
|
s := g.Server(p)
|
|
|
|
s.BindHandler("/admin", func(r *ghttp.Request) {
|
|
|
|
r.Response.Write("admin")
|
|
|
|
})
|
|
|
|
s.BindHandler("/admin-{page}", func(r *ghttp.Request) {
|
|
|
|
r.Response.Write("admin-{page}")
|
|
|
|
})
|
|
|
|
s.BindHandler("/admin-goods", func(r *ghttp.Request) {
|
|
|
|
r.Response.Write("admin-goods")
|
|
|
|
})
|
|
|
|
s.BindHandler("/admin-goods-{page}", func(r *ghttp.Request) {
|
|
|
|
r.Response.Write("admin-goods-{page}")
|
|
|
|
})
|
|
|
|
s.SetPort(p)
|
2019-12-04 13:55:08 +08:00
|
|
|
s.SetDumpRouterMap(false)
|
2019-12-02 21:26:12 +08:00
|
|
|
s.Start()
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-11-25 16:40:45 +08:00
|
|
|
client := g.Client()
|
2019-12-02 21:26:12 +08:00
|
|
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
|
|
|
|
2021-09-30 14:06:46 +08:00
|
|
|
t.Assert(client.GetContent(ctx, "/admin"), "admin")
|
|
|
|
t.Assert(client.GetContent(ctx, "/admin-1"), "admin-{page}")
|
|
|
|
t.Assert(client.GetContent(ctx, "/admin-goods"), "admin-goods")
|
|
|
|
t.Assert(client.GetContent(ctx, "/admin-goods-2"), "admin-goods-{page}")
|
2019-12-02 21:26:12 +08:00
|
|
|
})
|
|
|
|
}
|