github.com/gogf/gf@v1.16.9/net/ghttp/ghttp_unit_router_object_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 Object struct{}
    20  
    21  func (o *Object) Init(r *ghttp.Request) {
    22  	r.Response.Write("1")
    23  }
    24  
    25  func (o *Object) Shut(r *ghttp.Request) {
    26  	r.Response.Write("2")
    27  }
    28  
    29  func (o *Object) Index(r *ghttp.Request) {
    30  	r.Response.Write("Object Index")
    31  }
    32  
    33  func (o *Object) Show(r *ghttp.Request) {
    34  	r.Response.Write("Object Show")
    35  }
    36  
    37  func (o *Object) Info(r *ghttp.Request) {
    38  	r.Response.Write("Object Info")
    39  }
    40  
    41  func Test_Router_Object1(t *testing.T) {
    42  	p, _ := ports.PopRand()
    43  	s := g.Server(p)
    44  	s.BindObject("/", new(Object))
    45  	s.BindObject("/{.struct}/{.method}", new(Object))
    46  	s.SetPort(p)
    47  	s.SetDumpRouterMap(false)
    48  	s.Start()
    49  	defer s.Shutdown()
    50  
    51  	time.Sleep(100 * time.Millisecond)
    52  	gtest.C(t, func(t *gtest.T) {
    53  		client := g.Client()
    54  		client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
    55  
    56  		t.Assert(client.GetContent("/"), "1Object Index2")
    57  		t.Assert(client.GetContent("/init"), "Not Found")
    58  		t.Assert(client.GetContent("/shut"), "Not Found")
    59  		t.Assert(client.GetContent("/index"), "1Object Index2")
    60  		t.Assert(client.GetContent("/show"), "1Object Show2")
    61  
    62  		t.Assert(client.GetContent("/object"), "Not Found")
    63  		t.Assert(client.GetContent("/object/init"), "Not Found")
    64  		t.Assert(client.GetContent("/object/shut"), "Not Found")
    65  		t.Assert(client.GetContent("/object/index"), "1Object Index2")
    66  		t.Assert(client.GetContent("/object/show"), "1Object Show2")
    67  
    68  		t.Assert(client.GetContent("/none-exist"), "Not Found")
    69  	})
    70  }
    71  
    72  func Test_Router_Object2(t *testing.T) {
    73  	p, _ := ports.PopRand()
    74  	s := g.Server(p)
    75  	s.BindObject("/object", new(Object), "Show, Info")
    76  	s.SetPort(p)
    77  	s.SetDumpRouterMap(false)
    78  	s.Start()
    79  	defer s.Shutdown()
    80  
    81  	time.Sleep(100 * time.Millisecond)
    82  	gtest.C(t, func(t *gtest.T) {
    83  		client := g.Client()
    84  		client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
    85  
    86  		t.Assert(client.GetContent("/"), "Not Found")
    87  		t.Assert(client.GetContent("/object"), "Not Found")
    88  		t.Assert(client.GetContent("/object/init"), "Not Found")
    89  		t.Assert(client.GetContent("/object/shut"), "Not Found")
    90  		t.Assert(client.GetContent("/object/index"), "Not Found")
    91  		t.Assert(client.GetContent("/object/show"), "1Object Show2")
    92  		t.Assert(client.GetContent("/object/info"), "1Object Info2")
    93  
    94  		t.Assert(client.GetContent("/none-exist"), "Not Found")
    95  	})
    96  }
    97  
    98  func Test_Router_ObjectMethod(t *testing.T) {
    99  	p, _ := ports.PopRand()
   100  	s := g.Server(p)
   101  	s.BindObjectMethod("/object-info", new(Object), "Info")
   102  	s.SetPort(p)
   103  	s.SetDumpRouterMap(false)
   104  	s.Start()
   105  	defer s.Shutdown()
   106  
   107  	time.Sleep(100 * time.Millisecond)
   108  	gtest.C(t, func(t *gtest.T) {
   109  		client := g.Client()
   110  		client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
   111  
   112  		t.Assert(client.GetContent("/"), "Not Found")
   113  		t.Assert(client.GetContent("/object"), "Not Found")
   114  		t.Assert(client.GetContent("/object/init"), "Not Found")
   115  		t.Assert(client.GetContent("/object/shut"), "Not Found")
   116  		t.Assert(client.GetContent("/object/index"), "Not Found")
   117  		t.Assert(client.GetContent("/object/show"), "Not Found")
   118  		t.Assert(client.GetContent("/object/info"), "Not Found")
   119  		t.Assert(client.GetContent("/object-info"), "1Object Info2")
   120  
   121  		t.Assert(client.GetContent("/none-exist"), "Not Found")
   122  	})
   123  }