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