github.com/zhongdalu/gf@v1.0.0/g/net/ghttp/ghttp_unit_static_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/os/gfile" 15 "github.com/zhongdalu/gf/g/test/gtest" 16 "testing" 17 "time" 18 ) 19 20 func Test_Static_ServerRoot(t *testing.T) { 21 // SetServerRoot with absolute path 22 gtest.Case(t, func() { 23 p := ports.PopRand() 24 s := g.Server(p) 25 path := fmt.Sprintf(`%s/ghttp/static/test/%d`, gfile.TempDir(), p) 26 defer gfile.Remove(path) 27 gfile.PutContents(path+"/index.htm", "index") 28 s.SetServerRoot(path) 29 s.SetPort(p) 30 s.Start() 31 defer s.Shutdown() 32 time.Sleep(time.Second) 33 client := ghttp.NewClient() 34 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p)) 35 36 gtest.Assert(client.GetContent("/"), "index") 37 gtest.Assert(client.GetContent("/index.htm"), "index") 38 }) 39 40 // SetServerRoot with relative path 41 gtest.Case(t, func() { 42 p := ports.PopRand() 43 s := g.Server(p) 44 path := fmt.Sprintf(`static/test/%d`, p) 45 defer gfile.Remove(path) 46 gfile.PutContents(path+"/index.htm", "index") 47 s.SetServerRoot(path) 48 s.SetPort(p) 49 s.Start() 50 defer s.Shutdown() 51 time.Sleep(time.Second) 52 client := ghttp.NewClient() 53 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p)) 54 55 gtest.Assert(client.GetContent("/"), "index") 56 gtest.Assert(client.GetContent("/index.htm"), "index") 57 }) 58 } 59 60 func Test_Static_Folder_Forbidden(t *testing.T) { 61 gtest.Case(t, func() { 62 p := ports.PopRand() 63 s := g.Server(p) 64 path := fmt.Sprintf(`%s/ghttp/static/test/%d`, gfile.TempDir(), p) 65 defer gfile.Remove(path) 66 gfile.PutContents(path+"/test.html", "test") 67 s.SetServerRoot(path) 68 s.SetPort(p) 69 s.Start() 70 defer s.Shutdown() 71 time.Sleep(time.Second) 72 client := ghttp.NewClient() 73 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p)) 74 75 gtest.Assert(client.GetContent("/"), "Forbidden") 76 gtest.Assert(client.GetContent("/index.html"), "Not Found") 77 gtest.Assert(client.GetContent("/test.html"), "test") 78 }) 79 } 80 81 func Test_Static_IndexFolder(t *testing.T) { 82 gtest.Case(t, func() { 83 p := ports.PopRand() 84 s := g.Server(p) 85 path := fmt.Sprintf(`%s/ghttp/static/test/%d`, gfile.TempDir(), p) 86 defer gfile.Remove(path) 87 gfile.PutContents(path+"/test.html", "test") 88 s.SetIndexFolder(true) 89 s.SetServerRoot(path) 90 s.SetPort(p) 91 s.Start() 92 defer s.Shutdown() 93 time.Sleep(time.Second) 94 client := ghttp.NewClient() 95 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p)) 96 97 gtest.AssertNE(client.GetContent("/"), "Forbidden") 98 gtest.Assert(client.GetContent("/index.html"), "Not Found") 99 gtest.Assert(client.GetContent("/test.html"), "test") 100 }) 101 } 102 103 func Test_Static_IndexFiles1(t *testing.T) { 104 gtest.Case(t, func() { 105 p := ports.PopRand() 106 s := g.Server(p) 107 path := fmt.Sprintf(`%s/ghttp/static/test/%d`, gfile.TempDir(), p) 108 defer gfile.Remove(path) 109 gfile.PutContents(path+"/index.html", "index") 110 gfile.PutContents(path+"/test.html", "test") 111 s.SetServerRoot(path) 112 s.SetPort(p) 113 s.Start() 114 defer s.Shutdown() 115 time.Sleep(time.Second) 116 client := ghttp.NewClient() 117 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p)) 118 119 gtest.Assert(client.GetContent("/"), "index") 120 gtest.Assert(client.GetContent("/index.html"), "index") 121 gtest.Assert(client.GetContent("/test.html"), "test") 122 }) 123 } 124 125 func Test_Static_IndexFiles2(t *testing.T) { 126 gtest.Case(t, func() { 127 p := ports.PopRand() 128 s := g.Server(p) 129 path := fmt.Sprintf(`%s/ghttp/static/test/%d`, gfile.TempDir(), p) 130 defer gfile.Remove(path) 131 gfile.PutContents(path+"/test.html", "test") 132 s.SetIndexFiles([]string{"index.html", "test.html"}) 133 s.SetServerRoot(path) 134 s.SetPort(p) 135 s.Start() 136 defer s.Shutdown() 137 time.Sleep(time.Second) 138 client := ghttp.NewClient() 139 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p)) 140 141 gtest.Assert(client.GetContent("/"), "test") 142 gtest.Assert(client.GetContent("/index.html"), "Not Found") 143 gtest.Assert(client.GetContent("/test.html"), "test") 144 }) 145 } 146 147 func Test_Static_AddSearchPath1(t *testing.T) { 148 gtest.Case(t, func() { 149 p := ports.PopRand() 150 s := g.Server(p) 151 path1 := fmt.Sprintf(`%s/ghttp/static/test/%d`, gfile.TempDir(), p) 152 path2 := fmt.Sprintf(`%s/ghttp/static/test/%d/%d`, gfile.TempDir(), p, p) 153 defer gfile.Remove(path1) 154 defer gfile.Remove(path2) 155 gfile.PutContents(path2+"/test.html", "test") 156 s.SetServerRoot(path1) 157 s.AddSearchPath(path2) 158 s.SetPort(p) 159 s.Start() 160 defer s.Shutdown() 161 time.Sleep(time.Second) 162 client := ghttp.NewClient() 163 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p)) 164 165 gtest.Assert(client.GetContent("/"), "Forbidden") 166 gtest.Assert(client.GetContent("/test.html"), "test") 167 }) 168 } 169 170 func Test_Static_AddSearchPath2(t *testing.T) { 171 gtest.Case(t, func() { 172 p := ports.PopRand() 173 s := g.Server(p) 174 path1 := fmt.Sprintf(`%s/ghttp/static/test/%d`, gfile.TempDir(), p) 175 path2 := fmt.Sprintf(`%s/ghttp/static/test/%d/%d`, gfile.TempDir(), p, p) 176 defer gfile.Remove(path1) 177 defer gfile.Remove(path2) 178 gfile.PutContents(path1+"/test.html", "test1") 179 gfile.PutContents(path2+"/test.html", "test2") 180 s.SetServerRoot(path1) 181 s.AddSearchPath(path2) 182 s.SetPort(p) 183 s.Start() 184 defer s.Shutdown() 185 time.Sleep(time.Second) 186 client := ghttp.NewClient() 187 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p)) 188 189 gtest.Assert(client.GetContent("/"), "Forbidden") 190 gtest.Assert(client.GetContent("/test.html"), "test1") 191 }) 192 } 193 194 func Test_Static_AddStaticPath(t *testing.T) { 195 gtest.Case(t, func() { 196 p := ports.PopRand() 197 s := g.Server(p) 198 path1 := fmt.Sprintf(`%s/ghttp/static/test/%d`, gfile.TempDir(), p) 199 path2 := fmt.Sprintf(`%s/ghttp/static/test/%d/%d`, gfile.TempDir(), p, p) 200 defer gfile.Remove(path1) 201 defer gfile.Remove(path2) 202 gfile.PutContents(path1+"/test.html", "test1") 203 gfile.PutContents(path2+"/test.html", "test2") 204 s.SetServerRoot(path1) 205 s.AddStaticPath("/my-test", path2) 206 s.SetPort(p) 207 s.Start() 208 defer s.Shutdown() 209 time.Sleep(time.Second) 210 client := ghttp.NewClient() 211 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p)) 212 213 gtest.Assert(client.GetContent("/"), "Forbidden") 214 gtest.Assert(client.GetContent("/test.html"), "test1") 215 gtest.Assert(client.GetContent("/my-test/test.html"), "test2") 216 }) 217 } 218 219 func Test_Static_AddStaticPath_Priority(t *testing.T) { 220 gtest.Case(t, func() { 221 p := ports.PopRand() 222 s := g.Server(p) 223 path1 := fmt.Sprintf(`%s/ghttp/static/test/%d/test`, gfile.TempDir(), p) 224 path2 := fmt.Sprintf(`%s/ghttp/static/test/%d/%d/test`, gfile.TempDir(), p, p) 225 defer gfile.Remove(path1) 226 defer gfile.Remove(path2) 227 gfile.PutContents(path1+"/test.html", "test1") 228 gfile.PutContents(path2+"/test.html", "test2") 229 s.SetServerRoot(path1) 230 s.AddStaticPath("/test", path2) 231 s.SetPort(p) 232 s.Start() 233 defer s.Shutdown() 234 time.Sleep(time.Second) 235 client := ghttp.NewClient() 236 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p)) 237 238 gtest.Assert(client.GetContent("/"), "Forbidden") 239 gtest.Assert(client.GetContent("/test.html"), "test1") 240 gtest.Assert(client.GetContent("/test/test.html"), "test2") 241 }) 242 } 243 244 func Test_Static_Rewrite(t *testing.T) { 245 gtest.Case(t, func() { 246 p := ports.PopRand() 247 s := g.Server(p) 248 path := fmt.Sprintf(`%s/ghttp/static/test/%d`, gfile.TempDir(), p) 249 defer gfile.Remove(path) 250 gfile.PutContents(path+"/test1.html", "test1") 251 gfile.PutContents(path+"/test2.html", "test2") 252 s.SetServerRoot(path) 253 s.SetRewrite("/test.html", "/test1.html") 254 s.SetRewriteMap(g.MapStrStr{ 255 "/my-test1": "/test1.html", 256 "/my-test2": "/test2.html", 257 }) 258 s.SetPort(p) 259 s.Start() 260 defer s.Shutdown() 261 time.Sleep(time.Second) 262 client := ghttp.NewClient() 263 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p)) 264 265 gtest.Assert(client.GetContent("/"), "Forbidden") 266 gtest.Assert(client.GetContent("/test.html"), "test1") 267 gtest.Assert(client.GetContent("/test1.html"), "test1") 268 gtest.Assert(client.GetContent("/test2.html"), "test2") 269 gtest.Assert(client.GetContent("/my-test1"), "test1") 270 gtest.Assert(client.GetContent("/my-test2"), "test2") 271 }) 272 }