github.com/wangyougui/gf/v2@v2.6.5/net/ghttp/ghttp_z_unit_feature_router_object_rest1_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 ObjectRest struct{} 21 22 func (o *ObjectRest) Init(r *ghttp.Request) { 23 r.Response.Write("1") 24 } 25 26 func (o *ObjectRest) Shut(r *ghttp.Request) { 27 r.Response.Write("2") 28 } 29 30 func (o *ObjectRest) Get(r *ghttp.Request) { 31 r.Response.Write("Object Get") 32 } 33 34 func (o *ObjectRest) Put(r *ghttp.Request) { 35 r.Response.Write("Object Put") 36 } 37 38 func (o *ObjectRest) Post(r *ghttp.Request) { 39 r.Response.Write("Object Post") 40 } 41 42 func (o *ObjectRest) Delete(r *ghttp.Request) { 43 r.Response.Write("Object Delete") 44 } 45 46 func (o *ObjectRest) Patch(r *ghttp.Request) { 47 r.Response.Write("Object Patch") 48 } 49 50 func (o *ObjectRest) Options(r *ghttp.Request) { 51 r.Response.Write("Object Options") 52 } 53 54 func (o *ObjectRest) Head(r *ghttp.Request) { 55 r.Response.Header().Set("head-ok", "1") 56 } 57 58 func Test_Router_ObjectRest(t *testing.T) { 59 s := g.Server(guid.S()) 60 s.BindObjectRest("/", new(ObjectRest)) 61 s.BindObjectRest("/{.struct}/{.method}", new(ObjectRest)) 62 s.SetDumpRouterMap(false) 63 s.Start() 64 defer s.Shutdown() 65 66 time.Sleep(100 * time.Millisecond) 67 gtest.C(t, func(t *gtest.T) { 68 client := g.Client() 69 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 70 71 t.Assert(client.GetContent(ctx, "/"), "1Object Get2") 72 t.Assert(client.PutContent(ctx, "/"), "1Object Put2") 73 t.Assert(client.PostContent(ctx, "/"), "1Object Post2") 74 t.Assert(client.DeleteContent(ctx, "/"), "1Object Delete2") 75 t.Assert(client.PatchContent(ctx, "/"), "1Object Patch2") 76 t.Assert(client.OptionsContent(ctx, "/"), "1Object Options2") 77 resp1, err := client.Head(ctx, "/") 78 if err == nil { 79 defer resp1.Close() 80 } 81 t.AssertNil(err) 82 t.Assert(resp1.Header.Get("head-ok"), "1") 83 84 t.Assert(client.GetContent(ctx, "/object-rest/get"), "1Object Get2") 85 t.Assert(client.PutContent(ctx, "/object-rest/put"), "1Object Put2") 86 t.Assert(client.PostContent(ctx, "/object-rest/post"), "1Object Post2") 87 t.Assert(client.DeleteContent(ctx, "/object-rest/delete"), "1Object Delete2") 88 t.Assert(client.PatchContent(ctx, "/object-rest/patch"), "1Object Patch2") 89 t.Assert(client.OptionsContent(ctx, "/object-rest/options"), "1Object Options2") 90 resp2, err := client.Head(ctx, "/object-rest/head") 91 if err == nil { 92 defer resp2.Close() 93 } 94 t.AssertNil(err) 95 t.Assert(resp2.Header.Get("head-ok"), "1") 96 97 t.Assert(client.GetContent(ctx, "/none-exist"), "Not Found") 98 }) 99 }