github.com/zhongdalu/gf@v1.0.0/g/net/ghttp/ghttp_unit_cookie_test.go (about) 1 // Copyright 2018 gf Author(https://github.com/zhongdalu/gf). 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/zhongdalu/gf. 6 7 // COOKIE测试 8 package ghttp_test 9 10 import ( 11 "fmt" 12 "github.com/zhongdalu/gf/g" 13 "github.com/zhongdalu/gf/g/net/ghttp" 14 "github.com/zhongdalu/gf/g/test/gtest" 15 "testing" 16 "time" 17 ) 18 19 func Test_Cookie(t *testing.T) { 20 p := ports.PopRand() 21 s := g.Server(p) 22 s.BindHandler("/set", func(r *ghttp.Request) { 23 r.Cookie.Set(r.Get("k"), r.Get("v")) 24 }) 25 s.BindHandler("/get", func(r *ghttp.Request) { 26 //fmt.Println(r.Cookie.Map()) 27 r.Response.Write(r.Cookie.Get(r.Get("k"))) 28 }) 29 s.BindHandler("/remove", func(r *ghttp.Request) { 30 r.Cookie.Remove(r.Get("k")) 31 }) 32 s.SetPort(p) 33 s.SetDumpRouteMap(false) 34 s.Start() 35 defer s.Shutdown() 36 37 // 等待启动完成 38 time.Sleep(time.Second) 39 gtest.Case(t, func() { 40 client := ghttp.NewClient() 41 client.SetBrowserMode(true) 42 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p)) 43 r1, e1 := client.Get("/set?k=key1&v=100") 44 if r1 != nil { 45 defer r1.Close() 46 } 47 gtest.Assert(e1, nil) 48 gtest.Assert(r1.ReadAllString(), "") 49 50 gtest.Assert(client.GetContent("/set?k=key2&v=200"), "") 51 52 gtest.Assert(client.GetContent("/get?k=key1"), "100") 53 gtest.Assert(client.GetContent("/get?k=key2"), "200") 54 gtest.Assert(client.GetContent("/get?k=key3"), "") 55 gtest.Assert(client.GetContent("/remove?k=key1"), "") 56 gtest.Assert(client.GetContent("/remove?k=key3"), "") 57 gtest.Assert(client.GetContent("/remove?k=key4"), "") 58 gtest.Assert(client.GetContent("/get?k=key1"), "") 59 gtest.Assert(client.GetContent("/get?k=key2"), "200") 60 }) 61 }