2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2019-03-08 17:31:30 +08:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2019-10-11 22:54:25 +08:00
|
|
|
// static service testing.
|
|
|
|
|
2019-03-08 17:31:30 +08:00
|
|
|
package ghttp_test
|
|
|
|
|
|
|
|
import (
|
2019-06-19 09:06:52 +08:00
|
|
|
"fmt"
|
2019-10-12 23:56:03 +08:00
|
|
|
"github.com/gogf/gf/debug/gdebug"
|
2019-06-19 09:06:52 +08:00
|
|
|
"testing"
|
|
|
|
"time"
|
2019-07-29 21:01:19 +08:00
|
|
|
|
2019-09-25 10:56:44 +08:00
|
|
|
"github.com/gogf/gf/text/gstr"
|
|
|
|
|
2019-07-29 21:01:19 +08:00
|
|
|
"github.com/gogf/gf/frame/g"
|
|
|
|
"github.com/gogf/gf/os/gfile"
|
|
|
|
"github.com/gogf/gf/test/gtest"
|
2019-03-08 17:31:30 +08:00
|
|
|
)
|
|
|
|
|
2019-03-10 00:35:03 +08:00
|
|
|
func Test_Static_ServerRoot(t *testing.T) {
|
2019-06-19 09:06:52 +08:00
|
|
|
// SetServerRoot with absolute path
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-03-30 20:47:50 +08:00
|
|
|
p, _ := ports.PopRand()
|
2019-06-19 09:06:52 +08:00
|
|
|
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()
|
2019-10-09 00:33:58 +08:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
2020-11-25 16:40:45 +08:00
|
|
|
client := g.Client()
|
2019-06-19 09:06:52 +08:00
|
|
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
2019-03-10 00:35:03 +08:00
|
|
|
|
2021-09-30 14:06:46 +08:00
|
|
|
t.Assert(client.GetContent(ctx, "/"), "index")
|
|
|
|
t.Assert(client.GetContent(ctx, "/index.htm"), "index")
|
2019-06-19 09:06:52 +08:00
|
|
|
})
|
2019-03-10 00:35:03 +08:00
|
|
|
|
2019-06-19 09:06:52 +08:00
|
|
|
// SetServerRoot with relative path
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-03-30 20:47:50 +08:00
|
|
|
p, _ := ports.PopRand()
|
2019-06-19 09:06:52 +08:00
|
|
|
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()
|
2019-10-09 00:33:58 +08:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
2020-11-25 16:40:45 +08:00
|
|
|
client := g.Client()
|
2019-06-19 09:06:52 +08:00
|
|
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
2019-03-10 00:35:03 +08:00
|
|
|
|
2021-09-30 14:06:46 +08:00
|
|
|
t.Assert(client.GetContent(ctx, "/"), "index")
|
|
|
|
t.Assert(client.GetContent(ctx, "/index.htm"), "index")
|
2019-06-19 09:06:52 +08:00
|
|
|
})
|
2019-03-10 00:35:03 +08:00
|
|
|
}
|
|
|
|
|
2019-10-12 23:56:03 +08:00
|
|
|
func Test_Static_ServerRoot_Security(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-03-30 20:47:50 +08:00
|
|
|
p, _ := ports.PopRand()
|
2019-10-12 23:56:03 +08:00
|
|
|
s := g.Server(p)
|
2020-03-21 19:41:05 +08:00
|
|
|
s.SetServerRoot(gdebug.TestDataPath("static1"))
|
2019-10-12 23:56:03 +08:00
|
|
|
s.SetPort(p)
|
|
|
|
s.Start()
|
|
|
|
defer s.Shutdown()
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
2020-11-25 16:40:45 +08:00
|
|
|
client := g.Client()
|
2019-10-12 23:56:03 +08:00
|
|
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
|
|
|
|
2021-09-30 14:06:46 +08:00
|
|
|
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")
|
2019-10-12 23:56:03 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-03-08 17:31:30 +08:00
|
|
|
func Test_Static_Folder_Forbidden(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-03-30 20:47:50 +08:00
|
|
|
p, _ := ports.PopRand()
|
2019-06-19 09:06:52 +08:00
|
|
|
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()
|
2019-10-09 00:33:58 +08:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
2020-11-25 16:40:45 +08:00
|
|
|
client := g.Client()
|
2019-06-19 09:06:52 +08:00
|
|
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
2019-03-08 17:31:30 +08:00
|
|
|
|
2021-09-30 14:06:46 +08:00
|
|
|
t.Assert(client.GetContent(ctx, "/"), "Forbidden")
|
|
|
|
t.Assert(client.GetContent(ctx, "/index.html"), "Not Found")
|
|
|
|
t.Assert(client.GetContent(ctx, "/test.html"), "test")
|
2019-06-19 09:06:52 +08:00
|
|
|
})
|
2019-03-08 17:31:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Static_IndexFolder(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-03-30 20:47:50 +08:00
|
|
|
p, _ := ports.PopRand()
|
2019-06-19 09:06:52 +08:00
|
|
|
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()
|
2019-10-09 00:33:58 +08:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
2020-11-25 16:40:45 +08:00
|
|
|
client := g.Client()
|
2019-06-19 09:06:52 +08:00
|
|
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
2019-03-08 17:31:30 +08:00
|
|
|
|
2021-09-30 14:06:46 +08:00
|
|
|
t.AssertNE(client.GetContent(ctx, "/"), "Forbidden")
|
|
|
|
t.AssertNE(gstr.Pos(client.GetContent(ctx, "/"), `<a href="/test.html"`), -1)
|
|
|
|
t.Assert(client.GetContent(ctx, "/index.html"), "Not Found")
|
|
|
|
t.Assert(client.GetContent(ctx, "/test.html"), "test")
|
2019-06-19 09:06:52 +08:00
|
|
|
})
|
2019-03-08 17:31:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Static_IndexFiles1(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-03-30 20:47:50 +08:00
|
|
|
p, _ := ports.PopRand()
|
2019-06-19 09:06:52 +08:00
|
|
|
s := g.Server(p)
|
|
|
|
path := fmt.Sprintf(`%s/ghttp/static/test/%d`, gfile.TempDir(), p)
|
|
|
|
defer gfile.Remove(path)
|
|
|
|
gfile.PutContents(path+"/index.html", "index")
|
|
|
|
gfile.PutContents(path+"/test.html", "test")
|
|
|
|
s.SetServerRoot(path)
|
|
|
|
s.SetPort(p)
|
|
|
|
s.Start()
|
|
|
|
defer s.Shutdown()
|
2019-10-09 00:33:58 +08:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
2020-11-25 16:40:45 +08:00
|
|
|
client := g.Client()
|
2019-06-19 09:06:52 +08:00
|
|
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
2019-03-08 17:31:30 +08:00
|
|
|
|
2021-09-30 14:06:46 +08:00
|
|
|
t.Assert(client.GetContent(ctx, "/"), "index")
|
|
|
|
t.Assert(client.GetContent(ctx, "/index.html"), "index")
|
|
|
|
t.Assert(client.GetContent(ctx, "/test.html"), "test")
|
2019-06-19 09:06:52 +08:00
|
|
|
})
|
2019-03-08 17:31:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Static_IndexFiles2(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-03-30 20:47:50 +08:00
|
|
|
p, _ := ports.PopRand()
|
2019-06-19 09:06:52 +08:00
|
|
|
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.SetIndexFiles([]string{"index.html", "test.html"})
|
|
|
|
s.SetServerRoot(path)
|
|
|
|
s.SetPort(p)
|
|
|
|
s.Start()
|
|
|
|
defer s.Shutdown()
|
2019-10-09 00:33:58 +08:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
2020-11-25 16:40:45 +08:00
|
|
|
client := g.Client()
|
2019-06-19 09:06:52 +08:00
|
|
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
2019-03-08 17:31:30 +08:00
|
|
|
|
2021-09-30 14:06:46 +08:00
|
|
|
t.Assert(client.GetContent(ctx, "/"), "test")
|
|
|
|
t.Assert(client.GetContent(ctx, "/index.html"), "Not Found")
|
|
|
|
t.Assert(client.GetContent(ctx, "/test.html"), "test")
|
2019-06-19 09:06:52 +08:00
|
|
|
})
|
2019-03-08 17:31:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Static_AddSearchPath1(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-03-30 20:47:50 +08:00
|
|
|
p, _ := ports.PopRand()
|
2019-06-19 09:06:52 +08:00
|
|
|
s := g.Server(p)
|
|
|
|
path1 := fmt.Sprintf(`%s/ghttp/static/test/%d`, gfile.TempDir(), p)
|
|
|
|
path2 := fmt.Sprintf(`%s/ghttp/static/test/%d/%d`, gfile.TempDir(), p, p)
|
|
|
|
defer gfile.Remove(path1)
|
|
|
|
defer gfile.Remove(path2)
|
|
|
|
gfile.PutContents(path2+"/test.html", "test")
|
|
|
|
s.SetServerRoot(path1)
|
|
|
|
s.AddSearchPath(path2)
|
|
|
|
s.SetPort(p)
|
|
|
|
s.Start()
|
|
|
|
defer s.Shutdown()
|
2019-10-09 00:33:58 +08:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
2020-11-25 16:40:45 +08:00
|
|
|
client := g.Client()
|
2019-06-19 09:06:52 +08:00
|
|
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
2019-03-08 17:31:30 +08:00
|
|
|
|
2021-09-30 14:06:46 +08:00
|
|
|
t.Assert(client.GetContent(ctx, "/"), "Forbidden")
|
|
|
|
t.Assert(client.GetContent(ctx, "/test.html"), "test")
|
2019-06-19 09:06:52 +08:00
|
|
|
})
|
2019-03-08 17:31:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Static_AddSearchPath2(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-03-30 20:47:50 +08:00
|
|
|
p, _ := ports.PopRand()
|
2019-06-19 09:06:52 +08:00
|
|
|
s := g.Server(p)
|
|
|
|
path1 := fmt.Sprintf(`%s/ghttp/static/test/%d`, gfile.TempDir(), p)
|
|
|
|
path2 := fmt.Sprintf(`%s/ghttp/static/test/%d/%d`, gfile.TempDir(), p, p)
|
|
|
|
defer gfile.Remove(path1)
|
|
|
|
defer gfile.Remove(path2)
|
|
|
|
gfile.PutContents(path1+"/test.html", "test1")
|
|
|
|
gfile.PutContents(path2+"/test.html", "test2")
|
|
|
|
s.SetServerRoot(path1)
|
|
|
|
s.AddSearchPath(path2)
|
|
|
|
s.SetPort(p)
|
|
|
|
s.Start()
|
|
|
|
defer s.Shutdown()
|
2019-10-09 00:33:58 +08:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
2020-11-25 16:40:45 +08:00
|
|
|
client := g.Client()
|
2019-06-19 09:06:52 +08:00
|
|
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
2019-03-08 17:31:30 +08:00
|
|
|
|
2021-09-30 14:06:46 +08:00
|
|
|
t.Assert(client.GetContent(ctx, "/"), "Forbidden")
|
|
|
|
t.Assert(client.GetContent(ctx, "/test.html"), "test1")
|
2019-06-19 09:06:52 +08:00
|
|
|
})
|
2019-03-08 17:31:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Static_AddStaticPath(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-03-30 20:47:50 +08:00
|
|
|
p, _ := ports.PopRand()
|
2019-06-19 09:06:52 +08:00
|
|
|
s := g.Server(p)
|
|
|
|
path1 := fmt.Sprintf(`%s/ghttp/static/test/%d`, gfile.TempDir(), p)
|
|
|
|
path2 := fmt.Sprintf(`%s/ghttp/static/test/%d/%d`, gfile.TempDir(), p, p)
|
|
|
|
defer gfile.Remove(path1)
|
|
|
|
defer gfile.Remove(path2)
|
|
|
|
gfile.PutContents(path1+"/test.html", "test1")
|
|
|
|
gfile.PutContents(path2+"/test.html", "test2")
|
|
|
|
s.SetServerRoot(path1)
|
|
|
|
s.AddStaticPath("/my-test", path2)
|
|
|
|
s.SetPort(p)
|
|
|
|
s.Start()
|
|
|
|
defer s.Shutdown()
|
2019-10-09 00:33:58 +08:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
2020-11-25 16:40:45 +08:00
|
|
|
client := g.Client()
|
2019-06-19 09:06:52 +08:00
|
|
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
2019-03-08 17:31:30 +08:00
|
|
|
|
2021-09-30 14:06:46 +08:00
|
|
|
t.Assert(client.GetContent(ctx, "/"), "Forbidden")
|
|
|
|
t.Assert(client.GetContent(ctx, "/test.html"), "test1")
|
|
|
|
t.Assert(client.GetContent(ctx, "/my-test/test.html"), "test2")
|
2019-06-19 09:06:52 +08:00
|
|
|
})
|
2019-03-08 17:31:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Static_AddStaticPath_Priority(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-03-30 20:47:50 +08:00
|
|
|
p, _ := ports.PopRand()
|
2019-06-19 09:06:52 +08:00
|
|
|
s := g.Server(p)
|
|
|
|
path1 := fmt.Sprintf(`%s/ghttp/static/test/%d/test`, gfile.TempDir(), p)
|
|
|
|
path2 := fmt.Sprintf(`%s/ghttp/static/test/%d/%d/test`, gfile.TempDir(), p, p)
|
|
|
|
defer gfile.Remove(path1)
|
|
|
|
defer gfile.Remove(path2)
|
|
|
|
gfile.PutContents(path1+"/test.html", "test1")
|
|
|
|
gfile.PutContents(path2+"/test.html", "test2")
|
|
|
|
s.SetServerRoot(path1)
|
|
|
|
s.AddStaticPath("/test", path2)
|
|
|
|
s.SetPort(p)
|
|
|
|
s.Start()
|
|
|
|
defer s.Shutdown()
|
2019-10-09 00:33:58 +08:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
2020-11-25 16:40:45 +08:00
|
|
|
client := g.Client()
|
2019-06-19 09:06:52 +08:00
|
|
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
2019-03-08 17:31:30 +08:00
|
|
|
|
2021-09-30 14:06:46 +08:00
|
|
|
t.Assert(client.GetContent(ctx, "/"), "Forbidden")
|
|
|
|
t.Assert(client.GetContent(ctx, "/test.html"), "test1")
|
|
|
|
t.Assert(client.GetContent(ctx, "/test/test.html"), "test2")
|
2019-06-19 09:06:52 +08:00
|
|
|
})
|
2019-03-08 17:31:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Static_Rewrite(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-03-30 20:47:50 +08:00
|
|
|
p, _ := ports.PopRand()
|
2019-06-19 09:06:52 +08:00
|
|
|
s := g.Server(p)
|
|
|
|
path := fmt.Sprintf(`%s/ghttp/static/test/%d`, gfile.TempDir(), p)
|
|
|
|
defer gfile.Remove(path)
|
|
|
|
gfile.PutContents(path+"/test1.html", "test1")
|
|
|
|
gfile.PutContents(path+"/test2.html", "test2")
|
|
|
|
s.SetServerRoot(path)
|
|
|
|
s.SetRewrite("/test.html", "/test1.html")
|
|
|
|
s.SetRewriteMap(g.MapStrStr{
|
|
|
|
"/my-test1": "/test1.html",
|
|
|
|
"/my-test2": "/test2.html",
|
|
|
|
})
|
|
|
|
s.SetPort(p)
|
|
|
|
s.Start()
|
|
|
|
defer s.Shutdown()
|
2019-10-09 00:33:58 +08:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
2020-11-25 16:40:45 +08:00
|
|
|
client := g.Client()
|
2019-06-19 09:06:52 +08:00
|
|
|
client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
|
2019-03-08 17:31:30 +08:00
|
|
|
|
2021-09-30 14:06:46 +08:00
|
|
|
t.Assert(client.GetContent(ctx, "/"), "Forbidden")
|
|
|
|
t.Assert(client.GetContent(ctx, "/test.html"), "test1")
|
|
|
|
t.Assert(client.GetContent(ctx, "/test1.html"), "test1")
|
|
|
|
t.Assert(client.GetContent(ctx, "/test2.html"), "test2")
|
|
|
|
t.Assert(client.GetContent(ctx, "/my-test1"), "test1")
|
|
|
|
t.Assert(client.GetContent(ctx, "/my-test2"), "test2")
|
2019-06-19 09:06:52 +08:00
|
|
|
})
|
|
|
|
}
|