From 2ae5b1a4f89255cf8fb72ecddec1d2b02059d093 Mon Sep 17 00:00:00 2001 From: John Date: Sun, 10 Mar 2019 00:35:03 +0800 Subject: [PATCH] add more unit test cases for ghttp.Server --- g/net/ghttp/ghttp_server_config_static.go | 1 + g/net/ghttp/ghttp_unit_static_test.go | 40 +++++++++++++++++++++++ g/os/gcfg/gcfg.go | 3 +- 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/g/net/ghttp/ghttp_server_config_static.go b/g/net/ghttp/ghttp_server_config_static.go index 8412da927..87a02158e 100644 --- a/g/net/ghttp/ghttp_server_config_static.go +++ b/g/net/ghttp/ghttp_server_config_static.go @@ -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 } diff --git a/g/net/ghttp/ghttp_unit_static_test.go b/g/net/ghttp/ghttp_unit_static_test.go index ac0c6d89d..33a694db9 100644 --- a/g/net/ghttp/ghttp_unit_static_test.go +++ b/g/net/ghttp/ghttp_unit_static_test.go @@ -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() diff --git a/g/os/gcfg/gcfg.go b/g/os/gcfg/gcfg.go index 88a34567a..5c2486cc0 100644 --- a/g/os/gcfg/gcfg.go +++ b/g/os/gcfg/gcfg.go @@ -26,7 +26,8 @@ import ( ) const ( - DEFAULT_CONFIG_FILE = "config.toml" // 默认的配置管理文件名称 + // 默认的配置管理文件名称 + DEFAULT_CONFIG_FILE = "config.toml" ) // 配置管理对象