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