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