// Copyright GoFrame Author(https://goframe.org). 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. // static service testing. package ghttp_test import ( "fmt" "testing" "time" "github.com/gogf/gf/v2/debug/gdebug" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/os/gfile" "github.com/gogf/gf/v2/test/gtest" "github.com/gogf/gf/v2/text/gstr" ) func Test_Static_ServerRoot(t *testing.T) { // SetServerRoot with absolute path gtest.C(t, func(t *gtest.T) { p, _ := ports.PopRand() s := g.Server(p) path := fmt.Sprintf(`%s/ghttp/static/test/%d`, gfile.TempDir(), p) defer gfile.Remove(path) gfile.PutContents(path+"/index.htm", "index") s.SetServerRoot(path) s.SetPort(p) s.Start() defer s.Shutdown() time.Sleep(100 * time.Millisecond) client := g.Client() client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p)) t.Assert(client.GetContent(ctx, "/"), "index") t.Assert(client.GetContent(ctx, "/index.htm"), "index") }) // SetServerRoot with relative path gtest.C(t, func(t *gtest.T) { p, _ := ports.PopRand() s := g.Server(p) path := fmt.Sprintf(`static/test/%d`, p) defer gfile.Remove(path) gfile.PutContents(path+"/index.htm", "index") s.SetServerRoot(path) s.SetPort(p) s.Start() defer s.Shutdown() time.Sleep(100 * time.Millisecond) client := g.Client() client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p)) t.Assert(client.GetContent(ctx, "/"), "index") t.Assert(client.GetContent(ctx, "/index.htm"), "index") }) } func Test_Static_ServerRoot_Security(t *testing.T) { gtest.C(t, func(t *gtest.T) { p, _ := ports.PopRand() s := g.Server(p) s.SetServerRoot(gdebug.TestDataPath("static1")) s.SetPort(p) s.Start() defer s.Shutdown() time.Sleep(100 * time.Millisecond) client := g.Client() client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p)) t.Assert(client.GetContent(ctx, "/"), "index") t.Assert(client.GetContent(ctx, "/index.htm"), "Not Found") t.Assert(client.GetContent(ctx, "/index.html"), "index") t.Assert(client.GetContent(ctx, "/test.html"), "test") t.Assert(client.GetContent(ctx, "/../main.html"), "Not Found") t.Assert(client.GetContent(ctx, "/..%2Fmain.html"), "Not Found") }) } func Test_Static_Folder_Forbidden(t *testing.T) { gtest.C(t, func(t *gtest.T) { p, _ := ports.PopRand() s := g.Server(p) path := fmt.Sprintf(`%s/ghttp/static/test/%d`, gfile.TempDir(), p) defer gfile.Remove(path) gfile.PutContents(path+"/test.html", "test") s.SetServerRoot(path) s.SetPort(p) s.Start() defer s.Shutdown() time.Sleep(100 * time.Millisecond) client := g.Client() client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p)) t.Assert(client.GetContent(ctx, "/"), "Forbidden") t.Assert(client.GetContent(ctx, "/index.html"), "Not Found") t.Assert(client.GetContent(ctx, "/test.html"), "test") }) } func Test_Static_IndexFolder(t *testing.T) { gtest.C(t, func(t *gtest.T) { p, _ := ports.PopRand() s := g.Server(p) path := fmt.Sprintf(`%s/ghttp/static/test/%d`, gfile.TempDir(), p) defer gfile.Remove(path) gfile.PutContents(path+"/test.html", "test") s.SetIndexFolder(true) s.SetServerRoot(path) s.SetPort(p) s.Start() defer s.Shutdown() time.Sleep(100 * time.Millisecond) client := g.Client() client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p)) t.AssertNE(client.GetContent(ctx, "/"), "Forbidden") t.AssertNE(gstr.Pos(client.GetContent(ctx, "/"), `