github.com/gogf/gf@v1.16.9/net/ghttp/ghttp_unit_router_group_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  // 执行对象
    20  type GroupObject struct{}
    21  
    22  func (o *GroupObject) Init(r *ghttp.Request) {
    23  	r.Response.Write("1")
    24  }
    25  
    26  func (o *GroupObject) Shut(r *ghttp.Request) {
    27  	r.Response.Write("2")
    28  }
    29  
    30  func (o *GroupObject) Index(r *ghttp.Request) {
    31  	r.Response.Write("Object Index")
    32  }
    33  
    34  func (o *GroupObject) Show(r *ghttp.Request) {
    35  	r.Response.Write("Object Show")
    36  }
    37  
    38  func (o *GroupObject) Delete(r *ghttp.Request) {
    39  	r.Response.Write("Object Delete")
    40  }
    41  
    42  func Handler(r *ghttp.Request) {
    43  	r.Response.Write("Handler")
    44  }
    45  
    46  func Test_Router_GroupBasic1(t *testing.T) {
    47  	p, _ := ports.PopRand()
    48  	s := g.Server(p)
    49  	obj := new(GroupObject)
    50  	// 分组路由方法注册
    51  	group := s.Group("/api")
    52  	group.ALL("/handler", Handler)
    53  	group.ALL("/obj", obj)
    54  	group.GET("/obj/my-show", obj, "Show")
    55  	group.REST("/obj/rest", obj)
    56  	s.SetPort(p)
    57  	s.SetDumpRouterMap(false)
    58  	s.Start()
    59  	defer s.Shutdown()
    60  
    61  	time.Sleep(100 * time.Millisecond)
    62  	gtest.C(t, func(t *gtest.T) {
    63  		client := g.Client()
    64  		client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
    65  
    66  		t.Assert(client.GetContent("/api/handler"), "Handler")
    67  
    68  		t.Assert(client.GetContent("/api/obj"), "1Object Index2")
    69  		t.Assert(client.GetContent("/api/obj/"), "1Object Index2")
    70  		t.Assert(client.GetContent("/api/obj/index"), "1Object Index2")
    71  		t.Assert(client.GetContent("/api/obj/delete"), "1Object Delete2")
    72  		t.Assert(client.GetContent("/api/obj/my-show"), "1Object Show2")
    73  		t.Assert(client.GetContent("/api/obj/show"), "1Object Show2")
    74  		t.Assert(client.DeleteContent("/api/obj/rest"), "1Object Delete2")
    75  
    76  		t.Assert(client.DeleteContent("/ThisDoesNotExist"), "Not Found")
    77  		t.Assert(client.DeleteContent("/api/ThisDoesNotExist"), "Not Found")
    78  	})
    79  }
    80  
    81  func Test_Router_GroupBasic2(t *testing.T) {
    82  	p, _ := ports.PopRand()
    83  	s := g.Server(p)
    84  	obj := new(GroupObject)
    85  	// 分组路由批量注册
    86  	s.Group("/api").Bind([]g.Slice{
    87  		{"ALL", "/handler", Handler},
    88  		{"ALL", "/obj", obj},
    89  		{"GET", "/obj/my-show", obj, "Show"},
    90  		{"REST", "/obj/rest", obj},
    91  	})
    92  	s.SetPort(p)
    93  	s.SetDumpRouterMap(false)
    94  	s.Start()
    95  	defer s.Shutdown()
    96  
    97  	time.Sleep(100 * time.Millisecond)
    98  	gtest.C(t, func(t *gtest.T) {
    99  		client := g.Client()
   100  		client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
   101  
   102  		t.Assert(client.GetContent("/api/handler"), "Handler")
   103  
   104  		t.Assert(client.GetContent("/api/obj/delete"), "1Object Delete2")
   105  		t.Assert(client.GetContent("/api/obj/my-show"), "1Object Show2")
   106  		t.Assert(client.GetContent("/api/obj/show"), "1Object Show2")
   107  		t.Assert(client.DeleteContent("/api/obj/rest"), "1Object Delete2")
   108  
   109  		t.Assert(client.DeleteContent("/ThisDoesNotExist"), "Not Found")
   110  		t.Assert(client.DeleteContent("/api/ThisDoesNotExist"), "Not Found")
   111  	})
   112  }
   113  
   114  func Test_Router_GroupBuildInVar(t *testing.T) {
   115  	p, _ := ports.PopRand()
   116  	s := g.Server(p)
   117  	obj := new(GroupObject)
   118  	// 分组路由方法注册
   119  	group := s.Group("/api")
   120  	group.ALL("/{.struct}/{.method}", obj)
   121  	s.SetPort(p)
   122  	s.SetDumpRouterMap(false)
   123  	s.Start()
   124  	defer s.Shutdown()
   125  
   126  	time.Sleep(100 * time.Millisecond)
   127  	gtest.C(t, func(t *gtest.T) {
   128  		client := g.Client()
   129  		client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
   130  
   131  		t.Assert(client.GetContent("/api/group-object/index"), "1Object Index2")
   132  		t.Assert(client.GetContent("/api/group-object/delete"), "1Object Delete2")
   133  		t.Assert(client.GetContent("/api/group-object/show"), "1Object Show2")
   134  
   135  		t.Assert(client.DeleteContent("/ThisDoesNotExist"), "Not Found")
   136  		t.Assert(client.DeleteContent("/api/ThisDoesNotExist"), "Not Found")
   137  	})
   138  }
   139  
   140  func Test_Router_Group_Methods(t *testing.T) {
   141  	p, _ := ports.PopRand()
   142  	s := g.Server(p)
   143  	obj := new(GroupObject)
   144  	group := s.Group("/")
   145  	group.ALL("/obj", obj, "Show, Delete")
   146  	s.SetPort(p)
   147  	s.SetDumpRouterMap(false)
   148  	s.Start()
   149  	defer s.Shutdown()
   150  
   151  	time.Sleep(100 * time.Millisecond)
   152  	gtest.C(t, func(t *gtest.T) {
   153  		client := g.Client()
   154  		client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
   155  		t.Assert(client.GetContent("/obj/show"), "1Object Show2")
   156  		t.Assert(client.GetContent("/obj/delete"), "1Object Delete2")
   157  	})
   158  }
   159  
   160  func Test_Router_Group_MultiServer(t *testing.T) {
   161  	p1, _ := ports.PopRand()
   162  	p2, _ := ports.PopRand()
   163  	s1 := g.Server(p1)
   164  	s2 := g.Server(p2)
   165  	s1.Group("/", func(group *ghttp.RouterGroup) {
   166  		group.POST("/post", func(r *ghttp.Request) {
   167  			r.Response.Write("post1")
   168  		})
   169  	})
   170  	s2.Group("/", func(group *ghttp.RouterGroup) {
   171  		group.POST("/post", func(r *ghttp.Request) {
   172  			r.Response.Write("post2")
   173  		})
   174  	})
   175  	s1.SetPort(p1)
   176  	s2.SetPort(p2)
   177  	s1.SetDumpRouterMap(false)
   178  	s2.SetDumpRouterMap(false)
   179  	gtest.Assert(s1.Start(), nil)
   180  	gtest.Assert(s2.Start(), nil)
   181  	defer s1.Shutdown()
   182  	defer s2.Shutdown()
   183  
   184  	time.Sleep(100 * time.Millisecond)
   185  	gtest.C(t, func(t *gtest.T) {
   186  		c1 := g.Client()
   187  		c1.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p1))
   188  		c2 := g.Client()
   189  		c2.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p2))
   190  		t.Assert(c1.PostContent("/post"), "post1")
   191  		t.Assert(c2.PostContent("/post"), "post2")
   192  	})
   193  }
   194  
   195  func Test_Router_Group_Map(t *testing.T) {
   196  	testFuncGet := func(r *ghttp.Request) {
   197  		r.Response.Write("get")
   198  	}
   199  	testFuncPost := func(r *ghttp.Request) {
   200  		r.Response.Write("post")
   201  	}
   202  	p, _ := ports.PopRand()
   203  	s := g.Server(p)
   204  	s.Group("/", func(group *ghttp.RouterGroup) {
   205  		group.Map(map[string]interface{}{
   206  			"Get: /test": testFuncGet,
   207  			"Post:/test": testFuncPost,
   208  		})
   209  	})
   210  	s.SetPort(p)
   211  	//s.SetDumpRouterMap(false)
   212  	gtest.Assert(s.Start(), nil)
   213  	defer s.Shutdown()
   214  
   215  	time.Sleep(100 * time.Millisecond)
   216  	gtest.C(t, func(t *gtest.T) {
   217  		c := g.Client()
   218  		c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
   219  
   220  		t.Assert(c.GetContent("/test"), "get")
   221  		t.Assert(c.PostContent("/test"), "post")
   222  	})
   223  }