github.com/gogf/gf@v1.16.9/net/ghttp/ghttp_unit_router_group_hook_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  func Test_Router_Group_Hook1(t *testing.T) {
    20  	p, _ := ports.PopRand()
    21  	s := g.Server(p)
    22  	group := s.Group("/api")
    23  	group.GET("/handler", func(r *ghttp.Request) {
    24  		r.Response.Write("1")
    25  	})
    26  	group.ALL("/handler", func(r *ghttp.Request) {
    27  		r.Response.Write("0")
    28  	}, ghttp.HookBeforeServe)
    29  	group.ALL("/handler", func(r *ghttp.Request) {
    30  		r.Response.Write("2")
    31  	}, ghttp.HookAfterServe)
    32  
    33  	s.SetPort(p)
    34  	s.SetDumpRouterMap(false)
    35  	s.Start()
    36  	defer s.Shutdown()
    37  
    38  	time.Sleep(100 * time.Millisecond)
    39  	gtest.C(t, func(t *gtest.T) {
    40  		client := g.Client()
    41  		client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
    42  		t.Assert(client.GetContent("/api/handler"), "012")
    43  		t.Assert(client.PostContent("/api/handler"), "02")
    44  		t.Assert(client.GetContent("/api/ThisDoesNotExist"), "Not Found")
    45  	})
    46  }
    47  
    48  func Test_Router_Group_Hook2(t *testing.T) {
    49  	p, _ := ports.PopRand()
    50  	s := g.Server(p)
    51  	group := s.Group("/api")
    52  	group.GET("/handler", func(r *ghttp.Request) {
    53  		r.Response.Write("1")
    54  	})
    55  	group.GET("/*", func(r *ghttp.Request) {
    56  		r.Response.Write("0")
    57  	}, ghttp.HookBeforeServe)
    58  	group.GET("/*", func(r *ghttp.Request) {
    59  		r.Response.Write("2")
    60  	}, ghttp.HookAfterServe)
    61  
    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  		t.Assert(client.GetContent("/api/handler"), "012")
    72  		t.Assert(client.PostContent("/api/handler"), "Not Found")
    73  		t.Assert(client.GetContent("/api/ThisDoesNotExist"), "02")
    74  		t.Assert(client.PostContent("/api/ThisDoesNotExist"), "Not Found")
    75  	})
    76  }
    77  
    78  func Test_Router_Group_Hook3(t *testing.T) {
    79  	p, _ := ports.PopRand()
    80  	s := g.Server(p)
    81  	s.Group("/api").Bind([]g.Slice{
    82  		{"ALL", "handler", func(r *ghttp.Request) {
    83  			r.Response.Write("1")
    84  		}},
    85  		{"ALL", "/*", func(r *ghttp.Request) {
    86  			r.Response.Write("0")
    87  		}, ghttp.HookBeforeServe},
    88  		{"ALL", "/*", func(r *ghttp.Request) {
    89  			r.Response.Write("2")
    90  		}, ghttp.HookAfterServe},
    91  	})
    92  
    93  	s.SetPort(p)
    94  	s.SetDumpRouterMap(false)
    95  	s.Start()
    96  	defer s.Shutdown()
    97  
    98  	time.Sleep(100 * time.Millisecond)
    99  	gtest.C(t, func(t *gtest.T) {
   100  		client := g.Client()
   101  		client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
   102  		t.Assert(client.GetContent("/api/handler"), "012")
   103  		t.Assert(client.PostContent("/api/handler"), "012")
   104  		t.Assert(client.DeleteContent("/api/ThisDoesNotExist"), "02")
   105  	})
   106  }