github.com/gogf/gf/v2@v2.7.4/net/ghttp/ghttp_z_unit_feature_template_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  	"testing"
    14  	"time"
    15  
    16  	"github.com/gogf/gf/v2/encoding/ghtml"
    17  	"github.com/gogf/gf/v2/frame/g"
    18  	"github.com/gogf/gf/v2/net/ghttp"
    19  	"github.com/gogf/gf/v2/os/gview"
    20  	"github.com/gogf/gf/v2/test/gtest"
    21  	"github.com/gogf/gf/v2/util/guid"
    22  )
    23  
    24  func Test_Template_Basic(t *testing.T) {
    25  	gtest.C(t, func(t *gtest.T) {
    26  		v := gview.New(gtest.DataPath("template", "basic"))
    27  		s := g.Server(guid.S())
    28  		s.SetView(v)
    29  		s.BindHandler("/", func(r *ghttp.Request) {
    30  			err := r.Response.WriteTpl("index.html", g.Map{
    31  				"name": "john",
    32  			})
    33  			t.AssertNil(err)
    34  		})
    35  		s.SetDumpRouterMap(false)
    36  		s.Start()
    37  		defer s.Shutdown()
    38  		time.Sleep(100 * time.Millisecond)
    39  		client := g.Client()
    40  		client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
    41  
    42  		t.Assert(client.GetContent(ctx, "/"), "Name:john")
    43  		t.Assert(client.GetContent(ctx, "/"), "Name:john")
    44  	})
    45  }
    46  
    47  func Test_Template_Encode(t *testing.T) {
    48  	gtest.C(t, func(t *gtest.T) {
    49  		v := gview.New(gtest.DataPath("template", "basic"))
    50  		v.SetAutoEncode(true)
    51  		s := g.Server(guid.S())
    52  		s.SetView(v)
    53  		s.BindHandler("/", func(r *ghttp.Request) {
    54  			err := r.Response.WriteTpl("index.html", g.Map{
    55  				"name": "john",
    56  			})
    57  			t.AssertNil(err)
    58  		})
    59  		s.SetDumpRouterMap(false)
    60  		s.Start()
    61  		defer s.Shutdown()
    62  		time.Sleep(100 * time.Millisecond)
    63  		client := g.Client()
    64  		client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
    65  
    66  		t.Assert(client.GetContent(ctx, "/"), "Name:john")
    67  		t.Assert(client.GetContent(ctx, "/"), "Name:john")
    68  	})
    69  }
    70  
    71  func Test_Template_Layout1(t *testing.T) {
    72  	gtest.C(t, func(t *gtest.T) {
    73  		v := gview.New(gtest.DataPath("template", "layout1"))
    74  		s := g.Server(guid.S())
    75  		s.SetView(v)
    76  		s.BindHandler("/layout", func(r *ghttp.Request) {
    77  			err := r.Response.WriteTpl("layout.html", g.Map{
    78  				"mainTpl": "main/main1.html",
    79  			})
    80  			t.AssertNil(err)
    81  		})
    82  		s.BindHandler("/nil", func(r *ghttp.Request) {
    83  			err := r.Response.WriteTpl("layout.html", nil)
    84  			t.AssertNil(err)
    85  		})
    86  		s.SetDumpRouterMap(false)
    87  		s.Start()
    88  		defer s.Shutdown()
    89  		time.Sleep(100 * time.Millisecond)
    90  		client := g.Client()
    91  		client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
    92  
    93  		t.Assert(client.GetContent(ctx, "/"), "Not Found")
    94  		t.Assert(client.GetContent(ctx, "/layout"), "123")
    95  		t.Assert(client.GetContent(ctx, "/nil"), "123")
    96  	})
    97  }
    98  
    99  func Test_Template_Layout2(t *testing.T) {
   100  	gtest.C(t, func(t *gtest.T) {
   101  		v := gview.New(gtest.DataPath("template", "layout2"))
   102  		s := g.Server(guid.S())
   103  		s.SetView(v)
   104  		s.BindHandler("/main1", func(r *ghttp.Request) {
   105  			err := r.Response.WriteTpl("layout.html", g.Map{
   106  				"mainTpl": "main/main1.html",
   107  			})
   108  			t.AssertNil(err)
   109  		})
   110  		s.BindHandler("/main2", func(r *ghttp.Request) {
   111  			err := r.Response.WriteTpl("layout.html", g.Map{
   112  				"mainTpl": "main/main2.html",
   113  			})
   114  			t.AssertNil(err)
   115  		})
   116  		s.BindHandler("/nil", func(r *ghttp.Request) {
   117  			err := r.Response.WriteTpl("layout.html", nil)
   118  			t.AssertNil(err)
   119  		})
   120  		s.SetDumpRouterMap(false)
   121  		s.Start()
   122  		defer s.Shutdown()
   123  		time.Sleep(100 * time.Millisecond)
   124  		client := g.Client()
   125  		client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
   126  
   127  		t.Assert(client.GetContent(ctx, "/"), "Not Found")
   128  		t.Assert(client.GetContent(ctx, "/main1"), "a1b")
   129  		t.Assert(client.GetContent(ctx, "/main2"), "a2b")
   130  		t.Assert(client.GetContent(ctx, "/nil"), "ab")
   131  	})
   132  }
   133  
   134  func Test_Template_BuildInVarRequest(t *testing.T) {
   135  	gtest.C(t, func(t *gtest.T) {
   136  		s := g.Server(guid.S())
   137  		s.BindHandler("/:table/test", func(r *ghttp.Request) {
   138  			err := r.Response.WriteTplContent("{{.Request.table}}")
   139  			t.AssertNil(err)
   140  		})
   141  		s.SetDumpRouterMap(false)
   142  		s.Start()
   143  		defer s.Shutdown()
   144  		time.Sleep(100 * time.Millisecond)
   145  		client := g.Client()
   146  		client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
   147  
   148  		t.Assert(client.GetContent(ctx, "/user/test"), "user")
   149  		t.Assert(client.GetContent(ctx, "/order/test"), "order")
   150  	})
   151  }
   152  
   153  func Test_Template_XSS(t *testing.T) {
   154  	gtest.C(t, func(t *gtest.T) {
   155  		v := gview.New()
   156  		v.SetAutoEncode(true)
   157  		c := "<br>"
   158  		s := g.Server(guid.S())
   159  		s.SetView(v)
   160  		s.BindHandler("/", func(r *ghttp.Request) {
   161  			err := r.Response.WriteTplContent("{{if eq 1 1}}{{.v}}{{end}}", g.Map{
   162  				"v": c,
   163  			})
   164  			t.AssertNil(err)
   165  		})
   166  		s.SetDumpRouterMap(false)
   167  		s.Start()
   168  		defer s.Shutdown()
   169  		time.Sleep(100 * time.Millisecond)
   170  		client := g.Client()
   171  		client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
   172  
   173  		t.Assert(client.GetContent(ctx, "/"), ghtml.Entities(c))
   174  	})
   175  }