github.com/gogf/gf@v1.16.9/net/ghttp/ghttp_unit_router_domain_object_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/gogf/gf. 6 7 package ghttp_test 8 9 import ( 10 "fmt" 11 "testing" 12 "time" 13 14 "github.com/gogf/gf/frame/g" 15 "github.com/gogf/gf/net/ghttp" 16 "github.com/gogf/gf/test/gtest" 17 ) 18 19 type DomainObjectRest struct{} 20 21 func (o *DomainObjectRest) Init(r *ghttp.Request) { 22 r.Response.Write("1") 23 } 24 25 func (o *DomainObjectRest) Shut(r *ghttp.Request) { 26 r.Response.Write("2") 27 } 28 29 func (o *DomainObjectRest) Get(r *ghttp.Request) { 30 r.Response.Write("Object Get") 31 } 32 33 func (o *DomainObjectRest) Put(r *ghttp.Request) { 34 r.Response.Write("Object Put") 35 } 36 37 func (o *DomainObjectRest) Post(r *ghttp.Request) { 38 r.Response.Write("Object Post") 39 } 40 41 func (o *DomainObjectRest) Delete(r *ghttp.Request) { 42 r.Response.Write("Object Delete") 43 } 44 45 func (o *DomainObjectRest) Patch(r *ghttp.Request) { 46 r.Response.Write("Object Patch") 47 } 48 49 func (o *DomainObjectRest) Options(r *ghttp.Request) { 50 r.Response.Write("Object Options") 51 } 52 53 func (o *DomainObjectRest) Head(r *ghttp.Request) { 54 r.Response.Header().Set("head-ok", "1") 55 } 56 57 func Test_Router_DomainObjectRest(t *testing.T) { 58 p, _ := ports.PopRand() 59 s := g.Server(p) 60 d := s.Domain("localhost, local") 61 d.BindObjectRest("/", new(DomainObjectRest)) 62 s.SetPort(p) 63 s.SetDumpRouterMap(false) 64 s.Start() 65 defer s.Shutdown() 66 67 time.Sleep(100 * time.Millisecond) 68 gtest.C(t, func(t *gtest.T) { 69 client := g.Client() 70 client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p)) 71 72 t.Assert(client.GetContent("/"), "Not Found") 73 t.Assert(client.PutContent("/"), "Not Found") 74 t.Assert(client.PostContent("/"), "Not Found") 75 t.Assert(client.DeleteContent("/"), "Not Found") 76 t.Assert(client.PatchContent("/"), "Not Found") 77 t.Assert(client.OptionsContent("/"), "Not Found") 78 resp1, err := client.Head("/") 79 if err == nil { 80 defer resp1.Close() 81 } 82 t.Assert(err, nil) 83 t.Assert(resp1.Header.Get("head-ok"), "") 84 t.Assert(client.GetContent("/none-exist"), "Not Found") 85 }) 86 gtest.C(t, func(t *gtest.T) { 87 client := g.Client() 88 client.SetPrefix(fmt.Sprintf("http://localhost:%d", p)) 89 90 t.Assert(client.GetContent("/"), "1Object Get2") 91 t.Assert(client.PutContent("/"), "1Object Put2") 92 t.Assert(client.PostContent("/"), "1Object Post2") 93 t.Assert(client.DeleteContent("/"), "1Object Delete2") 94 t.Assert(client.PatchContent("/"), "1Object Patch2") 95 t.Assert(client.OptionsContent("/"), "1Object Options2") 96 resp1, err := client.Head("/") 97 if err == nil { 98 defer resp1.Close() 99 } 100 t.Assert(err, nil) 101 t.Assert(resp1.Header.Get("head-ok"), "1") 102 t.Assert(client.GetContent("/none-exist"), "Not Found") 103 }) 104 gtest.C(t, func(t *gtest.T) { 105 client := g.Client() 106 client.SetPrefix(fmt.Sprintf("http://local:%d", p)) 107 108 t.Assert(client.GetContent("/"), "1Object Get2") 109 t.Assert(client.PutContent("/"), "1Object Put2") 110 t.Assert(client.PostContent("/"), "1Object Post2") 111 t.Assert(client.DeleteContent("/"), "1Object Delete2") 112 t.Assert(client.PatchContent("/"), "1Object Patch2") 113 t.Assert(client.OptionsContent("/"), "1Object Options2") 114 resp1, err := client.Head("/") 115 if err == nil { 116 defer resp1.Close() 117 } 118 t.Assert(err, nil) 119 t.Assert(resp1.Header.Get("head-ok"), "1") 120 t.Assert(client.GetContent("/none-exist"), "Not Found") 121 }) 122 }