github.com/zhongdalu/gf@v1.0.0/g/net/ghttp/ghttp_unit_router_object_rest_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 package ghttp_test 8 9 import ( 10 "fmt" 11 "github.com/zhongdalu/gf/g" 12 "github.com/zhongdalu/gf/g/net/ghttp" 13 "github.com/zhongdalu/gf/g/test/gtest" 14 "testing" 15 "time" 16 ) 17 18 type ObjectRest struct{} 19 20 func (o *ObjectRest) Init(r *ghttp.Request) { 21 r.Response.Write("1") 22 } 23 24 func (o *ObjectRest) Shut(r *ghttp.Request) { 25 r.Response.Write("2") 26 } 27 28 func (o *ObjectRest) Get(r *ghttp.Request) { 29 r.Response.Write("Object Get") 30 } 31 32 func (o *ObjectRest) Put(r *ghttp.Request) { 33 r.Response.Write("Object Put") 34 } 35 36 func (o *ObjectRest) Post(r *ghttp.Request) { 37 r.Response.Write("Object Post") 38 } 39 40 func (o *ObjectRest) Delete(r *ghttp.Request) { 41 r.Response.Write("Object Delete") 42 } 43 44 func (o *ObjectRest) Patch(r *ghttp.Request) { 45 r.Response.Write("Object Patch") 46 } 47 48 func (o *ObjectRest) Options(r *ghttp.Request) { 49 r.Response.Write("Object Options") 50 } 51 52 func (o *ObjectRest) Head(r *ghttp.Request) { 53 r.Response.Header().Set("head-ok", "1") 54 } 55 56 func Test_Router_ObjectRest(t *testing.T) { 57 p := ports.PopRand() 58 s := g.Server(p) 59 s.BindObjectRest("/", new(ObjectRest)) 60 s.BindObjectRest("/{.struct}/{.method}", new(ObjectRest)) 61 s.SetPort(p) 62 s.SetDumpRouteMap(false) 63 s.Start() 64 defer s.Shutdown() 65 66 // 等待启动完成 67 time.Sleep(time.Second) 68 gtest.Case(t, func() { 69 client := ghttp.NewClient() 70 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p)) 71 72 gtest.Assert(client.GetContent("/"), "1Object Get2") 73 gtest.Assert(client.PutContent("/"), "1Object Put2") 74 gtest.Assert(client.PostContent("/"), "1Object Post2") 75 gtest.Assert(client.DeleteContent("/"), "1Object Delete2") 76 gtest.Assert(client.PatchContent("/"), "1Object Patch2") 77 gtest.Assert(client.OptionsContent("/"), "1Object Options2") 78 resp1, err := client.Head("/") 79 if err == nil { 80 defer resp1.Close() 81 } 82 gtest.Assert(err, nil) 83 gtest.Assert(resp1.Header.Get("head-ok"), "1") 84 85 gtest.Assert(client.GetContent("/object-rest/get"), "1Object Get2") 86 gtest.Assert(client.PutContent("/object-rest/put"), "1Object Put2") 87 gtest.Assert(client.PostContent("/object-rest/post"), "1Object Post2") 88 gtest.Assert(client.DeleteContent("/object-rest/delete"), "1Object Delete2") 89 gtest.Assert(client.PatchContent("/object-rest/patch"), "1Object Patch2") 90 gtest.Assert(client.OptionsContent("/object-rest/options"), "1Object Options2") 91 resp2, err := client.Head("/object-rest/head") 92 if err == nil { 93 defer resp2.Close() 94 } 95 gtest.Assert(err, nil) 96 gtest.Assert(resp2.Header.Get("head-ok"), "1") 97 98 gtest.Assert(client.GetContent("/none-exist"), "Not Found") 99 }) 100 }