github.com/zhongdalu/gf@v1.0.0/g/net/ghttp/ghttp_unit_router_hook_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  func Test_Router_Hook_Basic(t *testing.T) {
    19  	p := ports.PopRand()
    20  	s := g.Server(p)
    21  	s.BindHookHandlerByMap("/*", map[string]ghttp.HandlerFunc{
    22  		"BeforeServe":  func(r *ghttp.Request) { r.Response.Write("1") },
    23  		"AfterServe":   func(r *ghttp.Request) { r.Response.Write("2") },
    24  		"BeforeOutput": func(r *ghttp.Request) { r.Response.Write("3") },
    25  		"AfterOutput":  func(r *ghttp.Request) { r.Response.Write("4") },
    26  		"BeforeClose":  func(r *ghttp.Request) { r.Response.Write("5") },
    27  		"AfterClose":   func(r *ghttp.Request) { r.Response.Write("6") },
    28  	})
    29  	s.BindHandler("/test/test", func(r *ghttp.Request) {
    30  		r.Response.Write("test")
    31  	})
    32  	s.SetPort(p)
    33  	s.SetDumpRouteMap(false)
    34  	s.Start()
    35  	defer s.Shutdown()
    36  
    37  	// 等待启动完成
    38  	time.Sleep(time.Second)
    39  	gtest.Case(t, func() {
    40  		client := ghttp.NewClient()
    41  		client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
    42  
    43  		gtest.Assert(client.GetContent("/"), "123")
    44  		gtest.Assert(client.GetContent("/test/test"), "1test23")
    45  	})
    46  }
    47  
    48  func Test_Router_Hook_Priority(t *testing.T) {
    49  	p := ports.PopRand()
    50  	s := g.Server(p)
    51  	s.BindHandler("/priority/show", func(r *ghttp.Request) {
    52  		r.Response.Write("show")
    53  	})
    54  
    55  	s.BindHookHandlerByMap("/priority/:name", map[string]ghttp.HandlerFunc{
    56  		"BeforeServe": func(r *ghttp.Request) {
    57  			r.Response.Write("1")
    58  		},
    59  	})
    60  	s.BindHookHandlerByMap("/priority/*any", map[string]ghttp.HandlerFunc{
    61  		"BeforeServe": func(r *ghttp.Request) {
    62  			r.Response.Write("2")
    63  		},
    64  	})
    65  	s.BindHookHandlerByMap("/priority/show", map[string]ghttp.HandlerFunc{
    66  		"BeforeServe": func(r *ghttp.Request) {
    67  			r.Response.Write("3")
    68  		},
    69  	})
    70  	s.SetPort(p)
    71  	s.SetDumpRouteMap(false)
    72  	s.Start()
    73  	defer s.Shutdown()
    74  
    75  	// 等待启动完成
    76  	time.Sleep(time.Second)
    77  	gtest.Case(t, func() {
    78  		client := ghttp.NewClient()
    79  		client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
    80  
    81  		gtest.Assert(client.GetContent("/"), "Not Found")
    82  		gtest.Assert(client.GetContent("/priority/show"), "312show")
    83  		gtest.Assert(client.GetContent("/priority/any/any"), "2")
    84  		gtest.Assert(client.GetContent("/priority/name"), "12")
    85  	})
    86  }
    87  
    88  func Test_Router_Hook_Multi(t *testing.T) {
    89  	p := ports.PopRand()
    90  	s := g.Server(p)
    91  	s.BindHandler("/multi-hook", func(r *ghttp.Request) {
    92  		r.Response.Write("show")
    93  	})
    94  
    95  	s.BindHookHandlerByMap("/multi-hook", map[string]ghttp.HandlerFunc{
    96  		"BeforeServe": func(r *ghttp.Request) {
    97  			r.Response.Write("1")
    98  		},
    99  	})
   100  	s.BindHookHandlerByMap("/multi-hook", map[string]ghttp.HandlerFunc{
   101  		"BeforeServe": func(r *ghttp.Request) {
   102  			r.Response.Write("2")
   103  		},
   104  	})
   105  	s.SetPort(p)
   106  	s.SetDumpRouteMap(false)
   107  	s.Start()
   108  	defer s.Shutdown()
   109  
   110  	// 等待启动完成
   111  	time.Sleep(time.Second)
   112  	gtest.Case(t, func() {
   113  		client := ghttp.NewClient()
   114  		client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
   115  
   116  		gtest.Assert(client.GetContent("/"), "Not Found")
   117  		gtest.Assert(client.GetContent("/multi-hook"), "12show")
   118  	})
   119  }