github.com/wangyougui/gf/v2@v2.6.5/net/ghttp/ghttp_z_unit_feature_cookie_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/wangyougui/gf.
     6  
     7  package ghttp_test
     8  
     9  import (
    10  	"fmt"
    11  	"net/http"
    12  	"strings"
    13  	"testing"
    14  	"time"
    15  
    16  	"github.com/wangyougui/gf/v2/frame/g"
    17  	"github.com/wangyougui/gf/v2/net/ghttp"
    18  	"github.com/wangyougui/gf/v2/test/gtest"
    19  	"github.com/wangyougui/gf/v2/util/guid"
    20  )
    21  
    22  func Test_Cookie(t *testing.T) {
    23  	s := g.Server(guid.S())
    24  	s.BindHandler("/set", func(r *ghttp.Request) {
    25  		r.Cookie.Set(r.Get("k").String(), r.Get("v").String())
    26  	})
    27  	s.BindHandler("/get", func(r *ghttp.Request) {
    28  		r.Response.Write(r.Cookie.Get(r.Get("k").String()))
    29  	})
    30  	s.BindHandler("/remove", func(r *ghttp.Request) {
    31  		r.Cookie.Remove(r.Get("k").String())
    32  	})
    33  	s.SetDumpRouterMap(false)
    34  	s.Start()
    35  	defer s.Shutdown()
    36  
    37  	time.Sleep(100 * time.Millisecond)
    38  	gtest.C(t, func(t *gtest.T) {
    39  		client := g.Client()
    40  		client.SetBrowserMode(true)
    41  		client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
    42  		r1, e1 := client.Get(ctx, "/set?k=key1&v=100")
    43  		if r1 != nil {
    44  			defer r1.Close()
    45  		}
    46  
    47  		t.Assert(e1, nil)
    48  		t.Assert(r1.ReadAllString(), "")
    49  
    50  		t.Assert(client.GetContent(ctx, "/set?k=key2&v=200"), "")
    51  
    52  		t.Assert(client.GetContent(ctx, "/get?k=key1"), "100")
    53  		t.Assert(client.GetContent(ctx, "/get?k=key2"), "200")
    54  		t.Assert(client.GetContent(ctx, "/get?k=key3"), "")
    55  		t.Assert(client.GetContent(ctx, "/remove?k=key1"), "")
    56  		t.Assert(client.GetContent(ctx, "/remove?k=key3"), "")
    57  		t.Assert(client.GetContent(ctx, "/remove?k=key4"), "")
    58  		t.Assert(client.GetContent(ctx, "/get?k=key1"), "")
    59  		t.Assert(client.GetContent(ctx, "/get?k=key2"), "200")
    60  	})
    61  }
    62  
    63  func Test_SetHttpCookie(t *testing.T) {
    64  	s := g.Server(guid.S())
    65  	s.BindHandler("/set", func(r *ghttp.Request) {
    66  		r.Cookie.SetHttpCookie(&http.Cookie{
    67  			Name:  r.Get("k").String(),
    68  			Value: r.Get("v").String(),
    69  		})
    70  	})
    71  	s.BindHandler("/get", func(r *ghttp.Request) {
    72  		r.Response.Write(r.Cookie.Get(r.Get("k").String()))
    73  	})
    74  	s.BindHandler("/remove", func(r *ghttp.Request) {
    75  		r.Cookie.Remove(r.Get("k").String())
    76  	})
    77  	s.SetDumpRouterMap(false)
    78  	s.Start()
    79  	defer s.Shutdown()
    80  
    81  	time.Sleep(100 * time.Millisecond)
    82  	gtest.C(t, func(t *gtest.T) {
    83  		client := g.Client()
    84  		client.SetBrowserMode(true)
    85  		client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
    86  		r1, e1 := client.Get(ctx, "/set?k=key1&v=100")
    87  		if r1 != nil {
    88  			defer r1.Close()
    89  		}
    90  		t.Assert(e1, nil)
    91  		t.Assert(r1.ReadAllString(), "")
    92  
    93  		t.Assert(client.GetContent(ctx, "/set?k=key2&v=200"), "")
    94  
    95  		t.Assert(client.GetContent(ctx, "/get?k=key1"), "100")
    96  		//t.Assert(client.GetContent(ctx, "/get?k=key2"), "200")
    97  		//t.Assert(client.GetContent(ctx, "/get?k=key3"), "")
    98  		//t.Assert(client.GetContent(ctx, "/remove?k=key1"), "")
    99  		//t.Assert(client.GetContent(ctx, "/remove?k=key3"), "")
   100  		//t.Assert(client.GetContent(ctx, "/remove?k=key4"), "")
   101  		//t.Assert(client.GetContent(ctx, "/get?k=key1"), "")
   102  		//t.Assert(client.GetContent(ctx, "/get?k=key2"), "200")
   103  	})
   104  }
   105  
   106  func Test_CookieOptionsDefault(t *testing.T) {
   107  	s := g.Server(guid.S())
   108  	s.BindHandler("/test", func(r *ghttp.Request) {
   109  		r.Cookie.Set(r.Get("k").String(), r.Get("v").String())
   110  	})
   111  	s.SetDumpRouterMap(false)
   112  	s.Start()
   113  	defer s.Shutdown()
   114  
   115  	time.Sleep(100 * time.Millisecond)
   116  	gtest.C(t, func(t *gtest.T) {
   117  		client := g.Client()
   118  		client.SetBrowserMode(true)
   119  		client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
   120  		r1, e1 := client.Get(ctx, "/test?k=key1&v=100")
   121  		if r1 != nil {
   122  			defer r1.Close()
   123  		}
   124  
   125  		t.Assert(e1, nil)
   126  		t.Assert(r1.ReadAllString(), "")
   127  
   128  		parts := strings.Split(r1.Header.Get("Set-Cookie"), "; ")
   129  
   130  		t.AssertIN(len(parts), []int{3, 4}) // For go < 1.16 cookie always output "SameSite", see: https://github.com/golang/go/commit/542693e00529fbb4248fac614ece68b127a5ec4d
   131  	})
   132  }
   133  
   134  func Test_CookieOptions(t *testing.T) {
   135  	s := g.Server(guid.S())
   136  	s.SetConfigWithMap(g.Map{
   137  		"cookieSameSite": "lax",
   138  		"cookieSecure":   true,
   139  		"cookieHttpOnly": true,
   140  	})
   141  	s.BindHandler("/test", func(r *ghttp.Request) {
   142  		r.Cookie.Set(r.Get("k").String(), r.Get("v").String())
   143  	})
   144  	s.SetDumpRouterMap(false)
   145  	s.Start()
   146  	defer s.Shutdown()
   147  
   148  	time.Sleep(100 * time.Millisecond)
   149  	gtest.C(t, func(t *gtest.T) {
   150  		client := g.Client()
   151  		client.SetBrowserMode(true)
   152  		client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
   153  		r1, e1 := client.Get(ctx, "/test?k=key1&v=100")
   154  		if r1 != nil {
   155  			defer r1.Close()
   156  		}
   157  
   158  		t.Assert(e1, nil)
   159  		t.Assert(r1.ReadAllString(), "")
   160  
   161  		parts := strings.Split(r1.Header.Get("Set-Cookie"), "; ")
   162  
   163  		t.AssertEQ(len(parts), 6)
   164  		t.Assert(parts[3], "HttpOnly")
   165  		t.Assert(parts[4], "Secure")
   166  		t.Assert(parts[5], "SameSite=Lax")
   167  	})
   168  }