github.com/zhongdalu/gf@v1.0.0/g/net/ghttp/ghttp_unit_router_domain_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 DomainObjectRest struct{} 19 20 func (o *DomainObjectRest) Init(r *ghttp.Request) { 21 r.Response.Write("1") 22 } 23 24 func (o *DomainObjectRest) Shut(r *ghttp.Request) { 25 r.Response.Write("2") 26 } 27 28 func (o *DomainObjectRest) Get(r *ghttp.Request) { 29 r.Response.Write("Object Get") 30 } 31 32 func (o *DomainObjectRest) Put(r *ghttp.Request) { 33 r.Response.Write("Object Put") 34 } 35 36 func (o *DomainObjectRest) Post(r *ghttp.Request) { 37 r.Response.Write("Object Post") 38 } 39 40 func (o *DomainObjectRest) Delete(r *ghttp.Request) { 41 r.Response.Write("Object Delete") 42 } 43 44 func (o *DomainObjectRest) Patch(r *ghttp.Request) { 45 r.Response.Write("Object Patch") 46 } 47 48 func (o *DomainObjectRest) Options(r *ghttp.Request) { 49 r.Response.Write("Object Options") 50 } 51 52 func (o *DomainObjectRest) Head(r *ghttp.Request) { 53 r.Response.Header().Set("head-ok", "1") 54 } 55 56 func Test_Router_DomainObjectRest(t *testing.T) { 57 p := ports.PopRand() 58 s := g.Server(p) 59 d := s.Domain("localhost, local") 60 d.BindObjectRest("/", new(DomainObjectRest)) 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("/"), "Not Found") 73 gtest.Assert(client.PutContent("/"), "Not Found") 74 gtest.Assert(client.PostContent("/"), "Not Found") 75 gtest.Assert(client.DeleteContent("/"), "Not Found") 76 gtest.Assert(client.PatchContent("/"), "Not Found") 77 gtest.Assert(client.OptionsContent("/"), "Not Found") 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"), "") 84 gtest.Assert(client.GetContent("/none-exist"), "Not Found") 85 }) 86 gtest.Case(t, func() { 87 client := ghttp.NewClient() 88 client.SetPrefix(fmt.Sprintf("http://localhost:%d", p)) 89 90 gtest.Assert(client.GetContent("/"), "1Object Get2") 91 gtest.Assert(client.PutContent("/"), "1Object Put2") 92 gtest.Assert(client.PostContent("/"), "1Object Post2") 93 gtest.Assert(client.DeleteContent("/"), "1Object Delete2") 94 gtest.Assert(client.PatchContent("/"), "1Object Patch2") 95 gtest.Assert(client.OptionsContent("/"), "1Object Options2") 96 resp1, err := client.Head("/") 97 if err == nil { 98 defer resp1.Close() 99 } 100 gtest.Assert(err, nil) 101 gtest.Assert(resp1.Header.Get("head-ok"), "1") 102 gtest.Assert(client.GetContent("/none-exist"), "Not Found") 103 }) 104 gtest.Case(t, func() { 105 client := ghttp.NewClient() 106 client.SetPrefix(fmt.Sprintf("http://local:%d", p)) 107 108 gtest.Assert(client.GetContent("/"), "1Object Get2") 109 gtest.Assert(client.PutContent("/"), "1Object Put2") 110 gtest.Assert(client.PostContent("/"), "1Object Post2") 111 gtest.Assert(client.DeleteContent("/"), "1Object Delete2") 112 gtest.Assert(client.PatchContent("/"), "1Object Patch2") 113 gtest.Assert(client.OptionsContent("/"), "1Object Options2") 114 resp1, err := client.Head("/") 115 if err == nil { 116 defer resp1.Close() 117 } 118 gtest.Assert(err, nil) 119 gtest.Assert(resp1.Header.Get("head-ok"), "1") 120 gtest.Assert(client.GetContent("/none-exist"), "Not Found") 121 }) 122 }