github.com/gogf/gf@v1.16.9/net/ghttp/ghttp_unit_router_object_rest2_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 package ghttp_test 8 9 import ( 10 "fmt" 11 "testing" 12 "time" 13 14 "github.com/gogf/gf/frame/g" 15 "github.com/gogf/gf/net/ghttp" 16 "github.com/gogf/gf/test/gtest" 17 ) 18 19 type ObjectRest2 struct{} 20 21 func (o *ObjectRest2) Init(r *ghttp.Request) { 22 r.Response.Write("1") 23 } 24 25 func (o *ObjectRest2) Shut(r *ghttp.Request) { 26 r.Response.Write("2") 27 } 28 29 func (o *ObjectRest2) Get(r *ghttp.Request) { 30 r.Response.Write("Object Get", r.Get("id")) 31 } 32 33 func (o *ObjectRest2) Put(r *ghttp.Request) { 34 r.Response.Write("Object Put", r.Get("id")) 35 } 36 37 func (o *ObjectRest2) Post(r *ghttp.Request) { 38 r.Response.Write("Object Post", r.Get("id")) 39 } 40 41 func (o *ObjectRest2) Delete(r *ghttp.Request) { 42 r.Response.Write("Object Delete", r.Get("id")) 43 } 44 45 func Test_Router_ObjectRest_Id(t *testing.T) { 46 p, _ := ports.PopRand() 47 s := g.Server(p) 48 s.BindObjectRest("/object/:id", new(ObjectRest2)) 49 s.SetPort(p) 50 s.SetDumpRouterMap(false) 51 s.Start() 52 defer s.Shutdown() 53 54 time.Sleep(100 * time.Millisecond) 55 gtest.C(t, func(t *gtest.T) { 56 client := g.Client() 57 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p)) 58 59 t.Assert(client.GetContent("/object/99"), "1Object Get992") 60 t.Assert(client.PutContent("/object/99"), "1Object Put992") 61 t.Assert(client.PostContent("/object/99"), "1Object Post992") 62 t.Assert(client.DeleteContent("/object/99"), "1Object Delete992") 63 }) 64 }