github.com/gogf/gf@v1.16.9/net/ghttp/ghttp_unit_router_exit_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_Exit(t *testing.T) {
    20  	p, _ := ports.PopRand()
    21  	s := g.Server(p)
    22  	s.BindHookHandlerByMap("/*", map[string]ghttp.HandlerFunc{
    23  		ghttp.HookBeforeServe:  func(r *ghttp.Request) { r.Response.Write("1") },
    24  		ghttp.HookAfterServe:   func(r *ghttp.Request) { r.Response.Write("2") },
    25  		ghttp.HookBeforeOutput: func(r *ghttp.Request) { r.Response.Write("3") },
    26  		ghttp.HookAfterOutput:  func(r *ghttp.Request) { r.Response.Write("4") },
    27  	})
    28  	s.BindHandler("/test/test", func(r *ghttp.Request) {
    29  		r.Response.Write("test-start")
    30  		r.Exit()
    31  		r.Response.Write("test-end")
    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  
    43  		t.Assert(client.GetContent("/"), "123")
    44  		t.Assert(client.GetContent("/test/test"), "1test-start23")
    45  	})
    46  }
    47  
    48  func Test_Router_ExitHook(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  		ghttp.HookBeforeServe: func(r *ghttp.Request) {
    57  			r.Response.Write("1")
    58  		},
    59  	})
    60  	s.BindHookHandlerByMap("/priority/*any", map[string]ghttp.HandlerFunc{
    61  		ghttp.HookBeforeServe: func(r *ghttp.Request) {
    62  			r.Response.Write("2")
    63  		},
    64  	})
    65  	s.BindHookHandlerByMap("/priority/show", map[string]ghttp.HandlerFunc{
    66  		ghttp.HookBeforeServe: func(r *ghttp.Request) {
    67  			r.Response.Write("3")
    68  			r.ExitHook()
    69  		},
    70  	})
    71  	s.SetPort(p)
    72  	s.SetDumpRouterMap(false)
    73  	s.Start()
    74  	defer s.Shutdown()
    75  
    76  	time.Sleep(100 * time.Millisecond)
    77  	gtest.C(t, func(t *gtest.T) {
    78  		client := g.Client()
    79  		client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
    80  
    81  		t.Assert(client.GetContent("/"), "Not Found")
    82  		t.Assert(client.GetContent("/priority/show"), "3show")
    83  	})
    84  }
    85  
    86  func Test_Router_ExitAll(t *testing.T) {
    87  	p, _ := ports.PopRand()
    88  	s := g.Server(p)
    89  	s.BindHandler("/priority/show", func(r *ghttp.Request) {
    90  		r.Response.Write("show")
    91  	})
    92  
    93  	s.BindHookHandlerByMap("/priority/:name", map[string]ghttp.HandlerFunc{
    94  		ghttp.HookBeforeServe: func(r *ghttp.Request) {
    95  			r.Response.Write("1")
    96  		},
    97  	})
    98  	s.BindHookHandlerByMap("/priority/*any", map[string]ghttp.HandlerFunc{
    99  		ghttp.HookBeforeServe: func(r *ghttp.Request) {
   100  			r.Response.Write("2")
   101  		},
   102  	})
   103  	s.BindHookHandlerByMap("/priority/show", map[string]ghttp.HandlerFunc{
   104  		ghttp.HookBeforeServe: func(r *ghttp.Request) {
   105  			r.Response.Write("3")
   106  			r.ExitAll()
   107  		},
   108  	})
   109  	s.SetPort(p)
   110  	s.SetDumpRouterMap(false)
   111  	s.Start()
   112  	defer s.Shutdown()
   113  
   114  	time.Sleep(100 * time.Millisecond)
   115  	gtest.C(t, func(t *gtest.T) {
   116  		client := g.Client()
   117  		client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
   118  
   119  		t.Assert(client.GetContent("/"), "Not Found")
   120  		t.Assert(client.GetContent("/priority/show"), "3")
   121  	})
   122  }