github.com/zhongdalu/gf@v1.0.0/g/net/ghttp/ghttp_unit_router_controller_test.go (about) 1 // Copyright 2018 gf Author(https://github.com/zhongdalu/gf). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/zhongdalu/gf. 6 7 package ghttp_test 8 9 import ( 10 "fmt" 11 "github.com/zhongdalu/gf/g" 12 "github.com/zhongdalu/gf/g/frame/gmvc" 13 "github.com/zhongdalu/gf/g/net/ghttp" 14 "github.com/zhongdalu/gf/g/test/gtest" 15 "testing" 16 "time" 17 ) 18 19 // 控制器 20 type Controller struct { 21 gmvc.Controller 22 } 23 24 func (c *Controller) Init(r *ghttp.Request) { 25 c.Controller.Init(r) 26 c.Response.Write("1") 27 } 28 29 func (c *Controller) Shut() { 30 c.Response.Write("2") 31 } 32 33 func (c *Controller) Index() { 34 c.Response.Write("Controller Index") 35 } 36 37 func (c *Controller) Show() { 38 c.Response.Write("Controller Show") 39 } 40 41 func (c *Controller) Info() { 42 c.Response.Write("Controller Info") 43 } 44 45 func Test_Router_Controller1(t *testing.T) { 46 p := ports.PopRand() 47 s := g.Server(p) 48 s.BindController("/", new(Controller)) 49 s.BindController("/{.struct}/{.method}", new(Controller)) 50 s.SetPort(p) 51 s.SetDumpRouteMap(false) 52 s.Start() 53 defer s.Shutdown() 54 55 // 等待启动完成 56 time.Sleep(time.Second) 57 gtest.Case(t, func() { 58 client := ghttp.NewClient() 59 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p)) 60 61 gtest.Assert(client.GetContent("/"), "1Controller Index2") 62 gtest.Assert(client.GetContent("/init"), "Not Found") 63 gtest.Assert(client.GetContent("/shut"), "Not Found") 64 gtest.Assert(client.GetContent("/index"), "1Controller Index2") 65 gtest.Assert(client.GetContent("/show"), "1Controller Show2") 66 67 gtest.Assert(client.GetContent("/controller"), "Not Found") 68 gtest.Assert(client.GetContent("/controller/init"), "Not Found") 69 gtest.Assert(client.GetContent("/controller/shut"), "Not Found") 70 gtest.Assert(client.GetContent("/controller/index"), "1Controller Index2") 71 gtest.Assert(client.GetContent("/controller/show"), "1Controller Show2") 72 73 gtest.Assert(client.GetContent("/none-exist"), "Not Found") 74 }) 75 } 76 77 func Test_Router_Controller2(t *testing.T) { 78 p := ports.PopRand() 79 s := g.Server(p) 80 s.BindController("/controller", new(Controller), "Show, Info") 81 s.SetPort(p) 82 s.SetDumpRouteMap(false) 83 s.Start() 84 defer s.Shutdown() 85 86 // 等待启动完成 87 time.Sleep(time.Second) 88 gtest.Case(t, func() { 89 client := ghttp.NewClient() 90 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p)) 91 92 gtest.Assert(client.GetContent("/"), "Not Found") 93 gtest.Assert(client.GetContent("/controller"), "Not Found") 94 gtest.Assert(client.GetContent("/controller/init"), "Not Found") 95 gtest.Assert(client.GetContent("/controller/shut"), "Not Found") 96 gtest.Assert(client.GetContent("/controller/index"), "Not Found") 97 gtest.Assert(client.GetContent("/controller/show"), "1Controller Show2") 98 gtest.Assert(client.GetContent("/controller/info"), "1Controller Info2") 99 100 gtest.Assert(client.GetContent("/none-exist"), "Not Found") 101 }) 102 } 103 104 func Test_Router_ControllerMethod(t *testing.T) { 105 p := ports.PopRand() 106 s := g.Server(p) 107 s.BindControllerMethod("/controller-info", new(Controller), "Info") 108 s.SetPort(p) 109 s.SetDumpRouteMap(false) 110 s.Start() 111 defer s.Shutdown() 112 113 // 等待启动完成 114 time.Sleep(time.Second) 115 gtest.Case(t, func() { 116 client := ghttp.NewClient() 117 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p)) 118 119 gtest.Assert(client.GetContent("/"), "Not Found") 120 gtest.Assert(client.GetContent("/controller"), "Not Found") 121 gtest.Assert(client.GetContent("/controller/init"), "Not Found") 122 gtest.Assert(client.GetContent("/controller/shut"), "Not Found") 123 gtest.Assert(client.GetContent("/controller/index"), "Not Found") 124 gtest.Assert(client.GetContent("/controller/show"), "Not Found") 125 gtest.Assert(client.GetContent("/controller/info"), "Not Found") 126 gtest.Assert(client.GetContent("/controller-info"), "1Controller Info2") 127 128 gtest.Assert(client.GetContent("/none-exist"), "Not Found") 129 }) 130 }