github.com/gogf/gf@v1.16.9/net/ghttp/ghttp_unit_static_test.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/gogf/gf.
     6  
     7  // static service testing.
     8  
     9  package ghttp_test
    10  
    11  import (
    12  	"fmt"
    13  	"github.com/gogf/gf/debug/gdebug"
    14  	"testing"
    15  	"time"
    16  
    17  	"github.com/gogf/gf/text/gstr"
    18  
    19  	"github.com/gogf/gf/frame/g"
    20  	"github.com/gogf/gf/os/gfile"
    21  	"github.com/gogf/gf/test/gtest"
    22  )
    23  
    24  func Test_Static_ServerRoot(t *testing.T) {
    25  	// SetServerRoot with absolute path
    26  	gtest.C(t, func(t *gtest.T) {
    27  		p, _ := ports.PopRand()
    28  		s := g.Server(p)
    29  		path := fmt.Sprintf(`%s/ghttp/static/test/%d`, gfile.TempDir(), p)
    30  		defer gfile.Remove(path)
    31  		gfile.PutContents(path+"/index.htm", "index")
    32  		s.SetServerRoot(path)
    33  		s.SetPort(p)
    34  		s.Start()
    35  		defer s.Shutdown()
    36  		time.Sleep(100 * time.Millisecond)
    37  		client := g.Client()
    38  		client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
    39  
    40  		t.Assert(client.GetContent("/"), "index")
    41  		t.Assert(client.GetContent("/index.htm"), "index")
    42  	})
    43  
    44  	// SetServerRoot with relative path
    45  	gtest.C(t, func(t *gtest.T) {
    46  		p, _ := ports.PopRand()
    47  		s := g.Server(p)
    48  		path := fmt.Sprintf(`static/test/%d`, p)
    49  		defer gfile.Remove(path)
    50  		gfile.PutContents(path+"/index.htm", "index")
    51  		s.SetServerRoot(path)
    52  		s.SetPort(p)
    53  		s.Start()
    54  		defer s.Shutdown()
    55  		time.Sleep(100 * time.Millisecond)
    56  		client := g.Client()
    57  		client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
    58  
    59  		t.Assert(client.GetContent("/"), "index")
    60  		t.Assert(client.GetContent("/index.htm"), "index")
    61  	})
    62  }
    63  
    64  func Test_Static_ServerRoot_Security(t *testing.T) {
    65  	gtest.C(t, func(t *gtest.T) {
    66  		p, _ := ports.PopRand()
    67  		s := g.Server(p)
    68  		s.SetServerRoot(gdebug.TestDataPath("static1"))
    69  		s.SetPort(p)
    70  		s.Start()
    71  		defer s.Shutdown()
    72  		time.Sleep(100 * time.Millisecond)
    73  		client := g.Client()
    74  		client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
    75  
    76  		t.Assert(client.GetContent("/"), "index")
    77  		t.Assert(client.GetContent("/index.htm"), "Not Found")
    78  		t.Assert(client.GetContent("/index.html"), "index")
    79  		t.Assert(client.GetContent("/test.html"), "test")
    80  		t.Assert(client.GetContent("/../main.html"), "Not Found")
    81  		t.Assert(client.GetContent("/..%2Fmain.html"), "Not Found")
    82  	})
    83  }
    84  
    85  func Test_Static_Folder_Forbidden(t *testing.T) {
    86  	gtest.C(t, func(t *gtest.T) {
    87  		p, _ := ports.PopRand()
    88  		s := g.Server(p)
    89  		path := fmt.Sprintf(`%s/ghttp/static/test/%d`, gfile.TempDir(), p)
    90  		defer gfile.Remove(path)
    91  		gfile.PutContents(path+"/test.html", "test")
    92  		s.SetServerRoot(path)
    93  		s.SetPort(p)
    94  		s.Start()
    95  		defer s.Shutdown()
    96  		time.Sleep(100 * time.Millisecond)
    97  		client := g.Client()
    98  		client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
    99  
   100  		t.Assert(client.GetContent("/"), "Forbidden")
   101  		t.Assert(client.GetContent("/index.html"), "Not Found")
   102  		t.Assert(client.GetContent("/test.html"), "test")
   103  	})
   104  }
   105  
   106  func Test_Static_IndexFolder(t *testing.T) {
   107  	gtest.C(t, func(t *gtest.T) {
   108  		p, _ := ports.PopRand()
   109  		s := g.Server(p)
   110  		path := fmt.Sprintf(`%s/ghttp/static/test/%d`, gfile.TempDir(), p)
   111  		defer gfile.Remove(path)
   112  		gfile.PutContents(path+"/test.html", "test")
   113  		s.SetIndexFolder(true)
   114  		s.SetServerRoot(path)
   115  		s.SetPort(p)
   116  		s.Start()
   117  		defer s.Shutdown()
   118  		time.Sleep(100 * time.Millisecond)
   119  		client := g.Client()
   120  		client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
   121  
   122  		t.AssertNE(client.GetContent("/"), "Forbidden")
   123  		t.AssertNE(gstr.Pos(client.GetContent("/"), `<a href="/test.html"`), -1)
   124  		t.Assert(client.GetContent("/index.html"), "Not Found")
   125  		t.Assert(client.GetContent("/test.html"), "test")
   126  	})
   127  }
   128  
   129  func Test_Static_IndexFiles1(t *testing.T) {
   130  	gtest.C(t, func(t *gtest.T) {
   131  		p, _ := ports.PopRand()
   132  		s := g.Server(p)
   133  		path := fmt.Sprintf(`%s/ghttp/static/test/%d`, gfile.TempDir(), p)
   134  		defer gfile.Remove(path)
   135  		gfile.PutContents(path+"/index.html", "index")
   136  		gfile.PutContents(path+"/test.html", "test")
   137  		s.SetServerRoot(path)
   138  		s.SetPort(p)
   139  		s.Start()
   140  		defer s.Shutdown()
   141  		time.Sleep(100 * time.Millisecond)
   142  		client := g.Client()
   143  		client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
   144  
   145  		t.Assert(client.GetContent("/"), "index")
   146  		t.Assert(client.GetContent("/index.html"), "index")
   147  		t.Assert(client.GetContent("/test.html"), "test")
   148  	})
   149  }
   150  
   151  func Test_Static_IndexFiles2(t *testing.T) {
   152  	gtest.C(t, func(t *gtest.T) {
   153  		p, _ := ports.PopRand()
   154  		s := g.Server(p)
   155  		path := fmt.Sprintf(`%s/ghttp/static/test/%d`, gfile.TempDir(), p)
   156  		defer gfile.Remove(path)
   157  		gfile.PutContents(path+"/test.html", "test")
   158  		s.SetIndexFiles([]string{"index.html", "test.html"})
   159  		s.SetServerRoot(path)
   160  		s.SetPort(p)
   161  		s.Start()
   162  		defer s.Shutdown()
   163  		time.Sleep(100 * time.Millisecond)
   164  		client := g.Client()
   165  		client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
   166  
   167  		t.Assert(client.GetContent("/"), "test")
   168  		t.Assert(client.GetContent("/index.html"), "Not Found")
   169  		t.Assert(client.GetContent("/test.html"), "test")
   170  	})
   171  }
   172  
   173  func Test_Static_AddSearchPath1(t *testing.T) {
   174  	gtest.C(t, func(t *gtest.T) {
   175  		p, _ := ports.PopRand()
   176  		s := g.Server(p)
   177  		path1 := fmt.Sprintf(`%s/ghttp/static/test/%d`, gfile.TempDir(), p)
   178  		path2 := fmt.Sprintf(`%s/ghttp/static/test/%d/%d`, gfile.TempDir(), p, p)
   179  		defer gfile.Remove(path1)
   180  		defer gfile.Remove(path2)
   181  		gfile.PutContents(path2+"/test.html", "test")
   182  		s.SetServerRoot(path1)
   183  		s.AddSearchPath(path2)
   184  		s.SetPort(p)
   185  		s.Start()
   186  		defer s.Shutdown()
   187  		time.Sleep(100 * time.Millisecond)
   188  		client := g.Client()
   189  		client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
   190  
   191  		t.Assert(client.GetContent("/"), "Forbidden")
   192  		t.Assert(client.GetContent("/test.html"), "test")
   193  	})
   194  }
   195  
   196  func Test_Static_AddSearchPath2(t *testing.T) {
   197  	gtest.C(t, func(t *gtest.T) {
   198  		p, _ := ports.PopRand()
   199  		s := g.Server(p)
   200  		path1 := fmt.Sprintf(`%s/ghttp/static/test/%d`, gfile.TempDir(), p)
   201  		path2 := fmt.Sprintf(`%s/ghttp/static/test/%d/%d`, gfile.TempDir(), p, p)
   202  		defer gfile.Remove(path1)
   203  		defer gfile.Remove(path2)
   204  		gfile.PutContents(path1+"/test.html", "test1")
   205  		gfile.PutContents(path2+"/test.html", "test2")
   206  		s.SetServerRoot(path1)
   207  		s.AddSearchPath(path2)
   208  		s.SetPort(p)
   209  		s.Start()
   210  		defer s.Shutdown()
   211  		time.Sleep(100 * time.Millisecond)
   212  		client := g.Client()
   213  		client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
   214  
   215  		t.Assert(client.GetContent("/"), "Forbidden")
   216  		t.Assert(client.GetContent("/test.html"), "test1")
   217  	})
   218  }
   219  
   220  func Test_Static_AddStaticPath(t *testing.T) {
   221  	gtest.C(t, func(t *gtest.T) {
   222  		p, _ := ports.PopRand()
   223  		s := g.Server(p)
   224  		path1 := fmt.Sprintf(`%s/ghttp/static/test/%d`, gfile.TempDir(), p)
   225  		path2 := fmt.Sprintf(`%s/ghttp/static/test/%d/%d`, gfile.TempDir(), p, p)
   226  		defer gfile.Remove(path1)
   227  		defer gfile.Remove(path2)
   228  		gfile.PutContents(path1+"/test.html", "test1")
   229  		gfile.PutContents(path2+"/test.html", "test2")
   230  		s.SetServerRoot(path1)
   231  		s.AddStaticPath("/my-test", path2)
   232  		s.SetPort(p)
   233  		s.Start()
   234  		defer s.Shutdown()
   235  		time.Sleep(100 * time.Millisecond)
   236  		client := g.Client()
   237  		client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
   238  
   239  		t.Assert(client.GetContent("/"), "Forbidden")
   240  		t.Assert(client.GetContent("/test.html"), "test1")
   241  		t.Assert(client.GetContent("/my-test/test.html"), "test2")
   242  	})
   243  }
   244  
   245  func Test_Static_AddStaticPath_Priority(t *testing.T) {
   246  	gtest.C(t, func(t *gtest.T) {
   247  		p, _ := ports.PopRand()
   248  		s := g.Server(p)
   249  		path1 := fmt.Sprintf(`%s/ghttp/static/test/%d/test`, gfile.TempDir(), p)
   250  		path2 := fmt.Sprintf(`%s/ghttp/static/test/%d/%d/test`, gfile.TempDir(), p, p)
   251  		defer gfile.Remove(path1)
   252  		defer gfile.Remove(path2)
   253  		gfile.PutContents(path1+"/test.html", "test1")
   254  		gfile.PutContents(path2+"/test.html", "test2")
   255  		s.SetServerRoot(path1)
   256  		s.AddStaticPath("/test", path2)
   257  		s.SetPort(p)
   258  		s.Start()
   259  		defer s.Shutdown()
   260  		time.Sleep(100 * time.Millisecond)
   261  		client := g.Client()
   262  		client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
   263  
   264  		t.Assert(client.GetContent("/"), "Forbidden")
   265  		t.Assert(client.GetContent("/test.html"), "test1")
   266  		t.Assert(client.GetContent("/test/test.html"), "test2")
   267  	})
   268  }
   269  
   270  func Test_Static_Rewrite(t *testing.T) {
   271  	gtest.C(t, func(t *gtest.T) {
   272  		p, _ := ports.PopRand()
   273  		s := g.Server(p)
   274  		path := fmt.Sprintf(`%s/ghttp/static/test/%d`, gfile.TempDir(), p)
   275  		defer gfile.Remove(path)
   276  		gfile.PutContents(path+"/test1.html", "test1")
   277  		gfile.PutContents(path+"/test2.html", "test2")
   278  		s.SetServerRoot(path)
   279  		s.SetRewrite("/test.html", "/test1.html")
   280  		s.SetRewriteMap(g.MapStrStr{
   281  			"/my-test1": "/test1.html",
   282  			"/my-test2": "/test2.html",
   283  		})
   284  		s.SetPort(p)
   285  		s.Start()
   286  		defer s.Shutdown()
   287  		time.Sleep(100 * time.Millisecond)
   288  		client := g.Client()
   289  		client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
   290  
   291  		t.Assert(client.GetContent("/"), "Forbidden")
   292  		t.Assert(client.GetContent("/test.html"), "test1")
   293  		t.Assert(client.GetContent("/test1.html"), "test1")
   294  		t.Assert(client.GetContent("/test2.html"), "test2")
   295  		t.Assert(client.GetContent("/my-test1"), "test1")
   296  		t.Assert(client.GetContent("/my-test2"), "test2")
   297  	})
   298  }