mirror of
https://gitee.com/johng/gf
synced 2026-06-28 02:06:20 +08:00
add more unit test cases for ghttp.Server
This commit is contained in:
@ -64,6 +64,7 @@ func (s *Server)SetServerRoot(root string) {
|
||||
if path == "" {
|
||||
glog.Fatal(fmt.Sprintf(`[ghttp] SetServerRoot failed: path "%s" does not exist`, root))
|
||||
}
|
||||
glog.Debug("[ghttp] SetServerRoot path:", path)
|
||||
s.config.SearchPaths = []string{strings.TrimRight(path, gfile.Separator)}
|
||||
s.config.FileServerEnabled = true
|
||||
}
|
||||
|
||||
@ -17,6 +17,46 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
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(time.Second)
|
||||
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(time.Second)
|
||||
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()
|
||||
|
||||
@ -26,7 +26,8 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
DEFAULT_CONFIG_FILE = "config.toml" // 默认的配置管理文件名称
|
||||
// 默认的配置管理文件名称
|
||||
DEFAULT_CONFIG_FILE = "config.toml"
|
||||
)
|
||||
|
||||
// 配置管理对象
|
||||
|
||||
Reference in New Issue
Block a user