github.com/zhongdalu/gf@v1.0.0/g/net/ghttp/ghttp_unit_router_basic_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 // 基本路由功能以及优先级测试 8 package ghttp_test 9 10 import ( 11 "fmt" 12 "github.com/zhongdalu/gf/g" 13 "github.com/zhongdalu/gf/g/net/ghttp" 14 "github.com/zhongdalu/gf/g/test/gtest" 15 "testing" 16 "time" 17 ) 18 19 // 基本路由功能测试 20 func Test_Router_Basic(t *testing.T) { 21 p := ports.PopRand() 22 s := g.Server(p) 23 s.BindHandler("/:name", func(r *ghttp.Request) { 24 r.Response.Write("/:name") 25 }) 26 s.BindHandler("/:name/update", func(r *ghttp.Request) { 27 r.Response.Write(r.Get("name")) 28 }) 29 s.BindHandler("/:name/:action", func(r *ghttp.Request) { 30 r.Response.Write(r.Get("action")) 31 }) 32 s.BindHandler("/:name/*any", func(r *ghttp.Request) { 33 r.Response.Write(r.Get("any")) 34 }) 35 s.BindHandler("/user/list/{field}.html", func(r *ghttp.Request) { 36 r.Response.Write(r.Get("field")) 37 }) 38 s.SetPort(p) 39 s.SetDumpRouteMap(false) 40 s.Start() 41 defer s.Shutdown() 42 43 // 等待启动完成 44 time.Sleep(time.Second) 45 gtest.Case(t, func() { 46 client := ghttp.NewClient() 47 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p)) 48 gtest.Assert(client.GetContent("/john"), "") 49 gtest.Assert(client.GetContent("/john/update"), "john") 50 gtest.Assert(client.GetContent("/john/edit"), "edit") 51 gtest.Assert(client.GetContent("/user/list/100.html"), "100") 52 }) 53 } 54 55 // 测试HTTP Method注册. 56 func Test_Router_Method(t *testing.T) { 57 p := ports.PopRand() 58 s := g.Server(p) 59 s.BindHandler("GET:/get", func(r *ghttp.Request) { 60 61 }) 62 s.BindHandler("POST:/post", func(r *ghttp.Request) { 63 64 }) 65 s.SetPort(p) 66 s.SetDumpRouteMap(false) 67 s.Start() 68 defer s.Shutdown() 69 70 // 等待启动完成 71 time.Sleep(time.Second) 72 gtest.Case(t, func() { 73 client := ghttp.NewClient() 74 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p)) 75 76 resp1, err := client.Get("/get") 77 defer resp1.Close() 78 gtest.Assert(err, nil) 79 gtest.Assert(resp1.StatusCode, 200) 80 81 resp2, err := client.Post("/get") 82 defer resp2.Close() 83 gtest.Assert(err, nil) 84 gtest.Assert(resp2.StatusCode, 404) 85 86 resp3, err := client.Get("/post") 87 defer resp3.Close() 88 gtest.Assert(err, nil) 89 gtest.Assert(resp3.StatusCode, 404) 90 91 resp4, err := client.Post("/post") 92 defer resp4.Close() 93 gtest.Assert(err, nil) 94 gtest.Assert(resp4.StatusCode, 200) 95 }) 96 } 97 98 // 测试状态返回. 99 func Test_Router_Status(t *testing.T) { 100 p := ports.PopRand() 101 s := g.Server(p) 102 s.BindHandler("/200", func(r *ghttp.Request) { 103 r.Response.WriteStatus(200) 104 }) 105 s.BindHandler("/300", func(r *ghttp.Request) { 106 r.Response.WriteStatus(300) 107 }) 108 s.BindHandler("/400", func(r *ghttp.Request) { 109 r.Response.WriteStatus(400) 110 }) 111 s.BindHandler("/500", func(r *ghttp.Request) { 112 r.Response.WriteStatus(500) 113 }) 114 s.SetPort(p) 115 s.SetDumpRouteMap(false) 116 s.Start() 117 defer s.Shutdown() 118 119 // 等待启动完成 120 time.Sleep(time.Second) 121 gtest.Case(t, func() { 122 client := ghttp.NewClient() 123 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p)) 124 125 resp1, err := client.Get("/200") 126 defer resp1.Close() 127 gtest.Assert(err, nil) 128 gtest.Assert(resp1.StatusCode, 200) 129 130 resp2, err := client.Get("/300") 131 defer resp2.Close() 132 gtest.Assert(err, nil) 133 gtest.Assert(resp2.StatusCode, 300) 134 135 resp3, err := client.Get("/400") 136 defer resp3.Close() 137 gtest.Assert(err, nil) 138 gtest.Assert(resp3.StatusCode, 400) 139 140 resp4, err := client.Get("/500") 141 defer resp4.Close() 142 gtest.Assert(err, nil) 143 gtest.Assert(resp4.StatusCode, 500) 144 }) 145 } 146 147 // 自定义状态码处理. 148 func Test_Router_CustomStatusHandler(t *testing.T) { 149 p := ports.PopRand() 150 s := g.Server(p) 151 s.BindHandler("/", func(r *ghttp.Request) { 152 r.Response.Write("hello") 153 }) 154 s.BindStatusHandler(404, func(r *ghttp.Request) { 155 r.Response.Write("404 page") 156 }) 157 s.SetPort(p) 158 s.SetDumpRouteMap(false) 159 s.Start() 160 defer s.Shutdown() 161 162 // 等待启动完成 163 time.Sleep(time.Second) 164 gtest.Case(t, func() { 165 client := ghttp.NewClient() 166 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p)) 167 168 gtest.Assert(client.GetContent("/"), "hello") 169 resp, err := client.Get("/ThisDoesNotExist") 170 defer resp.Close() 171 gtest.Assert(err, nil) 172 gtest.Assert(resp.StatusCode, 404) 173 gtest.Assert(resp.ReadAllString(), "404 page") 174 }) 175 } 176 177 // 测试不存在的路由. 178 func Test_Router_404(t *testing.T) { 179 p := ports.PopRand() 180 s := g.Server(p) 181 s.BindHandler("/", func(r *ghttp.Request) { 182 r.Response.Write("hello") 183 }) 184 s.SetPort(p) 185 s.SetDumpRouteMap(false) 186 s.Start() 187 defer s.Shutdown() 188 189 // 等待启动完成 190 time.Sleep(time.Second) 191 gtest.Case(t, func() { 192 client := ghttp.NewClient() 193 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p)) 194 195 gtest.Assert(client.GetContent("/"), "hello") 196 resp, err := client.Get("/ThisDoesNotExist") 197 defer resp.Close() 198 gtest.Assert(err, nil) 199 gtest.Assert(resp.StatusCode, 404) 200 }) 201 }