github.com/wangyougui/gf/v2@v2.6.5/net/ghttp/ghttp_z_unit_feature_router_group_rest_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 GroupObjRest struct{} 21 22 func (o *GroupObjRest) Init(r *ghttp.Request) { 23 r.Response.Write("1") 24 } 25 26 func (o *GroupObjRest) Shut(r *ghttp.Request) { 27 r.Response.Write("2") 28 } 29 30 func (o *GroupObjRest) Get(r *ghttp.Request) { 31 r.Response.Write("Object Get") 32 } 33 34 func (o *GroupObjRest) Put(r *ghttp.Request) { 35 r.Response.Write("Object Put") 36 } 37 38 func (o *GroupObjRest) Post(r *ghttp.Request) { 39 r.Response.Write("Object Post") 40 } 41 42 func (o *GroupObjRest) Delete(r *ghttp.Request) { 43 r.Response.Write("Object Delete") 44 } 45 46 func (o *GroupObjRest) Patch(r *ghttp.Request) { 47 r.Response.Write("Object Patch") 48 } 49 50 func (o *GroupObjRest) Options(r *ghttp.Request) { 51 r.Response.Write("Object Options") 52 } 53 54 func (o *GroupObjRest) Head(r *ghttp.Request) { 55 r.Response.Header().Set("head-ok", "1") 56 } 57 58 func Test_Router_GroupRest1(t *testing.T) { 59 s := g.Server(guid.S()) 60 group := s.Group("/api") 61 obj := new(GroupObjRest) 62 group.REST("/obj", obj) 63 group.REST("/{.struct}/{.method}", obj) 64 s.SetDumpRouterMap(false) 65 s.Start() 66 defer s.Shutdown() 67 68 time.Sleep(100 * time.Millisecond) 69 gtest.C(t, func(t *gtest.T) { 70 client := g.Client() 71 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 72 73 t.Assert(client.GetContent(ctx, "/api/obj"), "1Object Get2") 74 t.Assert(client.PutContent(ctx, "/api/obj"), "1Object Put2") 75 t.Assert(client.PostContent(ctx, "/api/obj"), "1Object Post2") 76 t.Assert(client.DeleteContent(ctx, "/api/obj"), "1Object Delete2") 77 t.Assert(client.PatchContent(ctx, "/api/obj"), "1Object Patch2") 78 t.Assert(client.OptionsContent(ctx, "/api/obj"), "1Object Options2") 79 resp2, err := client.Head(ctx, "/api/obj") 80 if err == nil { 81 defer resp2.Close() 82 } 83 t.AssertNil(err) 84 t.Assert(resp2.Header.Get("head-ok"), "1") 85 86 t.Assert(client.GetContent(ctx, "/api/group-obj-rest"), "Not Found") 87 t.Assert(client.GetContent(ctx, "/api/group-obj-rest/get"), "1Object Get2") 88 t.Assert(client.PutContent(ctx, "/api/group-obj-rest/put"), "1Object Put2") 89 t.Assert(client.PostContent(ctx, "/api/group-obj-rest/post"), "1Object Post2") 90 t.Assert(client.DeleteContent(ctx, "/api/group-obj-rest/delete"), "1Object Delete2") 91 t.Assert(client.PatchContent(ctx, "/api/group-obj-rest/patch"), "1Object Patch2") 92 t.Assert(client.OptionsContent(ctx, "/api/group-obj-rest/options"), "1Object Options2") 93 resp4, err := client.Head(ctx, "/api/group-obj-rest/head") 94 if err == nil { 95 defer resp4.Close() 96 } 97 t.AssertNil(err) 98 t.Assert(resp4.Header.Get("head-ok"), "1") 99 }) 100 } 101 102 func Test_Router_GroupRest2(t *testing.T) { 103 s := g.Server(guid.S()) 104 s.Group("/api", func(group *ghttp.RouterGroup) { 105 obj := new(GroupObjRest) 106 group.REST("/obj", obj) 107 group.REST("/{.struct}/{.method}", obj) 108 }) 109 s.SetDumpRouterMap(false) 110 s.Start() 111 defer s.Shutdown() 112 113 time.Sleep(100 * time.Millisecond) 114 gtest.C(t, func(t *gtest.T) { 115 client := g.Client() 116 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())) 117 118 t.Assert(client.GetContent(ctx, "/api/obj"), "1Object Get2") 119 t.Assert(client.PutContent(ctx, "/api/obj"), "1Object Put2") 120 t.Assert(client.PostContent(ctx, "/api/obj"), "1Object Post2") 121 t.Assert(client.DeleteContent(ctx, "/api/obj"), "1Object Delete2") 122 t.Assert(client.PatchContent(ctx, "/api/obj"), "1Object Patch2") 123 t.Assert(client.OptionsContent(ctx, "/api/obj"), "1Object Options2") 124 resp2, err := client.Head(ctx, "/api/obj") 125 if err == nil { 126 defer resp2.Close() 127 } 128 t.AssertNil(err) 129 t.Assert(resp2.Header.Get("head-ok"), "1") 130 131 t.Assert(client.GetContent(ctx, "/api/group-obj-rest"), "Not Found") 132 t.Assert(client.GetContent(ctx, "/api/group-obj-rest/get"), "1Object Get2") 133 t.Assert(client.PutContent(ctx, "/api/group-obj-rest/put"), "1Object Put2") 134 t.Assert(client.PostContent(ctx, "/api/group-obj-rest/post"), "1Object Post2") 135 t.Assert(client.DeleteContent(ctx, "/api/group-obj-rest/delete"), "1Object Delete2") 136 t.Assert(client.PatchContent(ctx, "/api/group-obj-rest/patch"), "1Object Patch2") 137 t.Assert(client.OptionsContent(ctx, "/api/group-obj-rest/options"), "1Object Options2") 138 resp4, err := client.Head(ctx, "/api/group-obj-rest/head") 139 if err == nil { 140 defer resp4.Close() 141 } 142 t.AssertNil(err) 143 t.Assert(resp4.Header.Get("head-ok"), "1") 144 }) 145 }