// Copyright 2018 gf Author(https://github.com/gogf/gf). 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. // 静态文件服务测试 package ghttp_test import ( "fmt" "testing" "time" "github.com/gogf/gf/text/gstr" "github.com/gogf/gf/frame/g" "github.com/gogf/gf/net/ghttp" "github.com/gogf/gf/os/gfile" "github.com/gogf/gf/test/gtest" ) func Test_Static_ServerRoot(t *testing.T) { // SetServerRoot with absolute path gtest.Case(t, func() { 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(200 * time.Millisecond) client := ghttp.NewClient() client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p)) gtest.Assert(client.GetContent("/"), "index") gtest.Assert(client.GetContent("/index.htm"), "index") }) // SetServerRoot with relative path gtest.Case(t, func() { 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(200 * time.Millisecond) client := ghttp.NewClient() client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p)) gtest.Assert(client.GetContent("/"), "index") gtest.Assert(client.GetContent("/index.htm"), "index") }) } func Test_Static_Folder_Forbidden(t *testing.T) { gtest.Case(t, func() { 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(200 * time.Millisecond) client := ghttp.NewClient() client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p)) gtest.Assert(client.GetContent("/"), "Forbidden") gtest.Assert(client.GetContent("/index.html"), "Not Found") gtest.Assert(client.GetContent("/test.html"), "test") }) } func Test_Static_IndexFolder(t *testing.T) { gtest.Case(t, func() { 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(200 * time.Millisecond) client := ghttp.NewClient() client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p)) gtest.AssertNE(client.GetContent("/"), "Forbidden") gtest.AssertNE(gstr.Pos(client.GetContent("/"), `