2019-03-06 15:21:00 +08:00
|
|
|
// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
|
|
|
//
|
|
|
|
// 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,
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
|
|
|
|
package ghttp_test
|
|
|
|
|
|
|
|
import (
|
2019-06-19 09:06:52 +08:00
|
|
|
"fmt"
|
|
|
|
"github.com/gogf/gf/g"
|
|
|
|
"github.com/gogf/gf/g/frame/gmvc"
|
|
|
|
"github.com/gogf/gf/g/net/ghttp"
|
|
|
|
"github.com/gogf/gf/g/test/gtest"
|
|
|
|
"testing"
|
|
|
|
"time"
|
2019-03-06 15:21:00 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type DomainController struct {
|
2019-06-19 09:06:52 +08:00
|
|
|
gmvc.Controller
|
2019-03-06 15:21:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *DomainController) Init(r *ghttp.Request) {
|
2019-06-19 09:06:52 +08:00
|
|
|
c.Controller.Init(r)
|
|
|
|
c.Response.Write("1")
|
2019-03-06 15:21:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *DomainController) Shut() {
|
2019-06-19 09:06:52 +08:00
|
|
|
c.Response.Write("2")
|
2019-03-06 15:21:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *DomainController) Index() {
|
2019-06-19 09:06:52 +08:00
|
|
|
c.Response.Write("Controller Index")
|
2019-03-06 15:21:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *DomainController) Show() {
|
2019-06-19 09:06:52 +08:00
|
|
|
c.Response.Write("Controller Show")
|
2019-03-06 15:21:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *DomainController) Info() {
|
2019-06-19 09:06:52 +08:00
|
|
|
c.Response.Write("Controller Info")
|
2019-03-06 15:21:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Router_DomainController1(t *testing.T) {
|
2019-06-19 09:06:52 +08:00
|
|
|
p := ports.PopRand()
|
|
|
|
s := g.Server(p)
|
|
|
|
s.Domain("localhost, local").BindController("/", new(DomainController))
|
|
|
|
s.SetPort(p)
|
|
|
|
s.SetDumpRouteMap(false)
|
|
|
|
s.Start()
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
|
|
|
// 等待启动完成
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
gtest.Case(t, func() {
|
|
|
|
client := ghttp.NewClient()
|
|
|
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
|
|
|
|
|
|
|
gtest.Assert(client.GetContent("/"), "Not Found")
|
|
|
|
gtest.Assert(client.GetContent("/init"), "Not Found")
|
|
|
|
gtest.Assert(client.GetContent("/shut"), "Not Found")
|
|
|
|
gtest.Assert(client.GetContent("/index"), "Not Found")
|
|
|
|
gtest.Assert(client.GetContent("/show"), "Not Found")
|
|
|
|
gtest.Assert(client.GetContent("/info"), "Not Found")
|
|
|
|
gtest.Assert(client.GetContent("/none-exist"), "Not Found")
|
|
|
|
})
|
|
|
|
|
|
|
|
gtest.Case(t, func() {
|
|
|
|
client := ghttp.NewClient()
|
|
|
|
client.SetPrefix(fmt.Sprintf("http://localhost:%d", p))
|
|
|
|
|
|
|
|
gtest.Assert(client.GetContent("/"), "1Controller Index2")
|
|
|
|
gtest.Assert(client.GetContent("/init"), "Not Found")
|
|
|
|
gtest.Assert(client.GetContent("/shut"), "Not Found")
|
|
|
|
gtest.Assert(client.GetContent("/index"), "1Controller Index2")
|
|
|
|
gtest.Assert(client.GetContent("/show"), "1Controller Show2")
|
|
|
|
gtest.Assert(client.GetContent("/info"), "1Controller Info2")
|
|
|
|
gtest.Assert(client.GetContent("/none-exist"), "Not Found")
|
|
|
|
})
|
|
|
|
|
|
|
|
gtest.Case(t, func() {
|
|
|
|
client := ghttp.NewClient()
|
|
|
|
client.SetPrefix(fmt.Sprintf("http://local:%d", p))
|
|
|
|
|
|
|
|
gtest.Assert(client.GetContent("/"), "1Controller Index2")
|
|
|
|
gtest.Assert(client.GetContent("/init"), "Not Found")
|
|
|
|
gtest.Assert(client.GetContent("/shut"), "Not Found")
|
|
|
|
gtest.Assert(client.GetContent("/index"), "1Controller Index2")
|
|
|
|
gtest.Assert(client.GetContent("/show"), "1Controller Show2")
|
|
|
|
gtest.Assert(client.GetContent("/info"), "1Controller Info2")
|
|
|
|
gtest.Assert(client.GetContent("/none-exist"), "Not Found")
|
|
|
|
})
|
2019-03-06 15:21:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Router_DomainController2(t *testing.T) {
|
2019-06-19 09:06:52 +08:00
|
|
|
p := ports.PopRand()
|
|
|
|
s := g.Server(p)
|
|
|
|
s.Domain("localhost, local").BindController("/controller", new(DomainController), "Show, Info")
|
|
|
|
s.SetPort(p)
|
|
|
|
s.SetDumpRouteMap(false)
|
|
|
|
s.Start()
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
|
|
|
// 等待启动完成
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
gtest.Case(t, func() {
|
|
|
|
client := ghttp.NewClient()
|
|
|
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
|
|
|
|
|
|
|
gtest.Assert(client.GetContent("/"), "Not Found")
|
|
|
|
gtest.Assert(client.GetContent("/controller"), "Not Found")
|
|
|
|
gtest.Assert(client.GetContent("/controller/init"), "Not Found")
|
|
|
|
gtest.Assert(client.GetContent("/controller/shut"), "Not Found")
|
|
|
|
gtest.Assert(client.GetContent("/controller/index"), "Not Found")
|
|
|
|
gtest.Assert(client.GetContent("/controller/show"), "Not Found")
|
|
|
|
gtest.Assert(client.GetContent("/controller/info"), "Not Found")
|
|
|
|
gtest.Assert(client.GetContent("/none-exist"), "Not Found")
|
|
|
|
})
|
|
|
|
|
|
|
|
gtest.Case(t, func() {
|
|
|
|
client := ghttp.NewClient()
|
|
|
|
client.SetPrefix(fmt.Sprintf("http://localhost:%d", p))
|
|
|
|
|
|
|
|
gtest.Assert(client.GetContent("/"), "Not Found")
|
|
|
|
gtest.Assert(client.GetContent("/controller"), "Not Found")
|
|
|
|
gtest.Assert(client.GetContent("/controller/init"), "Not Found")
|
|
|
|
gtest.Assert(client.GetContent("/controller/shut"), "Not Found")
|
|
|
|
gtest.Assert(client.GetContent("/controller/index"), "Not Found")
|
|
|
|
gtest.Assert(client.GetContent("/controller/show"), "1Controller Show2")
|
|
|
|
gtest.Assert(client.GetContent("/controller/info"), "1Controller Info2")
|
|
|
|
gtest.Assert(client.GetContent("/none-exist"), "Not Found")
|
|
|
|
})
|
|
|
|
|
|
|
|
gtest.Case(t, func() {
|
|
|
|
client := ghttp.NewClient()
|
|
|
|
client.SetPrefix(fmt.Sprintf("http://local:%d", p))
|
|
|
|
|
|
|
|
gtest.Assert(client.GetContent("/"), "Not Found")
|
|
|
|
gtest.Assert(client.GetContent("/controller"), "Not Found")
|
|
|
|
gtest.Assert(client.GetContent("/controller/init"), "Not Found")
|
|
|
|
gtest.Assert(client.GetContent("/controller/shut"), "Not Found")
|
|
|
|
gtest.Assert(client.GetContent("/controller/index"), "Not Found")
|
|
|
|
gtest.Assert(client.GetContent("/controller/show"), "1Controller Show2")
|
|
|
|
gtest.Assert(client.GetContent("/controller/info"), "1Controller Info2")
|
|
|
|
gtest.Assert(client.GetContent("/none-exist"), "Not Found")
|
|
|
|
})
|
2019-03-06 15:21:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Router_DomainControllerMethod(t *testing.T) {
|
2019-06-19 09:06:52 +08:00
|
|
|
p := ports.PopRand()
|
|
|
|
s := g.Server(p)
|
|
|
|
s.Domain("localhost, local").BindControllerMethod("/controller-info", new(DomainController), "Info")
|
|
|
|
s.SetPort(p)
|
|
|
|
s.SetDumpRouteMap(false)
|
|
|
|
s.Start()
|
|
|
|
defer s.Shutdown()
|
|
|
|
|
|
|
|
// 等待启动完成
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
gtest.Case(t, func() {
|
|
|
|
client := ghttp.NewClient()
|
|
|
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
|
|
|
|
|
|
|
gtest.Assert(client.GetContent("/"), "Not Found")
|
|
|
|
gtest.Assert(client.GetContent("/controller"), "Not Found")
|
|
|
|
gtest.Assert(client.GetContent("/controller/init"), "Not Found")
|
|
|
|
gtest.Assert(client.GetContent("/controller/shut"), "Not Found")
|
|
|
|
gtest.Assert(client.GetContent("/controller/index"), "Not Found")
|
|
|
|
gtest.Assert(client.GetContent("/controller/show"), "Not Found")
|
|
|
|
gtest.Assert(client.GetContent("/controller/info"), "Not Found")
|
|
|
|
gtest.Assert(client.GetContent("/controller-info"), "Not Found")
|
|
|
|
gtest.Assert(client.GetContent("/none-exist"), "Not Found")
|
|
|
|
})
|
|
|
|
gtest.Case(t, func() {
|
|
|
|
client := ghttp.NewClient()
|
|
|
|
client.SetPrefix(fmt.Sprintf("http://localhost:%d", p))
|
|
|
|
|
|
|
|
gtest.Assert(client.GetContent("/"), "Not Found")
|
|
|
|
gtest.Assert(client.GetContent("/controller"), "Not Found")
|
|
|
|
gtest.Assert(client.GetContent("/controller/init"), "Not Found")
|
|
|
|
gtest.Assert(client.GetContent("/controller/shut"), "Not Found")
|
|
|
|
gtest.Assert(client.GetContent("/controller/index"), "Not Found")
|
|
|
|
gtest.Assert(client.GetContent("/controller/show"), "Not Found")
|
|
|
|
gtest.Assert(client.GetContent("/controller/info"), "Not Found")
|
|
|
|
gtest.Assert(client.GetContent("/controller-info"), "1Controller Info2")
|
|
|
|
gtest.Assert(client.GetContent("/none-exist"), "Not Found")
|
|
|
|
})
|
|
|
|
gtest.Case(t, func() {
|
|
|
|
client := ghttp.NewClient()
|
|
|
|
client.SetPrefix(fmt.Sprintf("http://local:%d", p))
|
|
|
|
|
|
|
|
gtest.Assert(client.GetContent("/"), "Not Found")
|
|
|
|
gtest.Assert(client.GetContent("/controller"), "Not Found")
|
|
|
|
gtest.Assert(client.GetContent("/controller/init"), "Not Found")
|
|
|
|
gtest.Assert(client.GetContent("/controller/shut"), "Not Found")
|
|
|
|
gtest.Assert(client.GetContent("/controller/index"), "Not Found")
|
|
|
|
gtest.Assert(client.GetContent("/controller/show"), "Not Found")
|
|
|
|
gtest.Assert(client.GetContent("/controller/info"), "Not Found")
|
|
|
|
gtest.Assert(client.GetContent("/controller-info"), "1Controller Info2")
|
|
|
|
gtest.Assert(client.GetContent("/none-exist"), "Not Found")
|
|
|
|
})
|
|
|
|
}
|