github.com/gogf/gf/v2@v2.7.4/net/ghttp/ghttp_z_unit_feature_router_basic_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/v2/frame/g"
    15  	"github.com/gogf/gf/v2/net/ghttp"
    16  	"github.com/gogf/gf/v2/test/gtest"
    17  	"github.com/gogf/gf/v2/util/guid"
    18  )
    19  
    20  func Test_Router_Basic1(t *testing.T) {
    21  	s := g.Server(guid.S())
    22  	s.BindHandler("/:name", func(r *ghttp.Request) {
    23  		r.Response.Write("/:name")
    24  	})
    25  	s.BindHandler("/:name/update", func(r *ghttp.Request) {
    26  		r.Response.Write(r.Get("name"))
    27  	})
    28  	s.BindHandler("/:name/:action", func(r *ghttp.Request) {
    29  		r.Response.Write(r.Get("action"))
    30  	})
    31  	s.BindHandler("/:name/*any", func(r *ghttp.Request) {
    32  		r.Response.Write(r.Get("any"))
    33  	})
    34  	s.BindHandler("/user/list/{field}.html", func(r *ghttp.Request) {
    35  		r.Response.Write(r.Get("field"))
    36  	})
    37  	s.SetDumpRouterMap(false)
    38  	s.Start()
    39  	defer s.Shutdown()
    40  
    41  	time.Sleep(100 * time.Millisecond)
    42  	gtest.C(t, func(t *gtest.T) {
    43  		client := g.Client()
    44  		client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
    45  		t.Assert(client.GetContent(ctx, "/john"), "")
    46  		t.Assert(client.GetContent(ctx, "/john/update"), "john")
    47  		t.Assert(client.GetContent(ctx, "/john/edit"), "edit")
    48  		t.Assert(client.GetContent(ctx, "/user/list/100.html"), "100")
    49  	})
    50  }
    51  
    52  func Test_Router_Basic2(t *testing.T) {
    53  	s := g.Server(guid.S())
    54  	s.BindHandler("/{hash}", func(r *ghttp.Request) {
    55  		r.Response.Write(r.Get("hash"))
    56  	})
    57  	s.BindHandler("/{hash}.{type}", func(r *ghttp.Request) {
    58  		r.Response.Write(r.Get("type"))
    59  	})
    60  	s.SetDumpRouterMap(false)
    61  	s.Start()
    62  	defer s.Shutdown()
    63  
    64  	time.Sleep(100 * time.Millisecond)
    65  	gtest.C(t, func(t *gtest.T) {
    66  		client := g.Client()
    67  		client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
    68  		t.Assert(client.GetContent(ctx, "/data"), "data")
    69  		t.Assert(client.GetContent(ctx, "/data.json"), "json")
    70  	})
    71  }
    72  
    73  func Test_Router_Value(t *testing.T) {
    74  	s := g.Server(guid.S())
    75  	s.BindHandler("/", func(r *ghttp.Request) {
    76  		r.Response.Write(r.GetRouterMap()["hash"])
    77  	})
    78  	s.BindHandler("/GetRouter", func(r *ghttp.Request) {
    79  		r.Response.Write(r.GetRouter("name", "john").String())
    80  	})
    81  	s.BindHandler("/{hash}", func(r *ghttp.Request) {
    82  		r.Response.Write(r.GetRouter("hash").String())
    83  	})
    84  	s.BindHandler("/{hash}.{type}", func(r *ghttp.Request) {
    85  		r.Response.Write(r.GetRouter("type").String())
    86  	})
    87  	s.BindHandler("/{hash}.{type}.map", func(r *ghttp.Request) {
    88  		r.Response.Write(r.GetRouterMap()["type"])
    89  	})
    90  	s.SetDumpRouterMap(false)
    91  	s.Start()
    92  	defer s.Shutdown()
    93  
    94  	time.Sleep(100 * time.Millisecond)
    95  	gtest.C(t, func(t *gtest.T) {
    96  		client := g.Client()
    97  		client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
    98  		t.Assert(client.GetContent(ctx, "/"), "")
    99  		t.Assert(client.GetContent(ctx, "/GetRouter"), "john")
   100  		t.Assert(client.GetContent(ctx, "/data"), "data")
   101  		t.Assert(client.GetContent(ctx, "/data.json"), "json")
   102  		t.Assert(client.GetContent(ctx, "/data.json.map"), "json")
   103  	})
   104  }
   105  
   106  // HTTP method register.
   107  func Test_Router_Method(t *testing.T) {
   108  	s := g.Server(guid.S())
   109  	s.BindHandler("GET:/get", func(r *ghttp.Request) {
   110  
   111  	})
   112  	s.BindHandler("POST:/post", func(r *ghttp.Request) {
   113  
   114  	})
   115  	s.SetDumpRouterMap(false)
   116  	s.Start()
   117  	defer s.Shutdown()
   118  
   119  	time.Sleep(100 * time.Millisecond)
   120  	gtest.C(t, func(t *gtest.T) {
   121  		client := g.Client()
   122  		client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
   123  
   124  		resp1, err := client.Get(ctx, "/get")
   125  		t.AssertNil(err)
   126  		defer resp1.Close()
   127  		t.Assert(resp1.StatusCode, 200)
   128  
   129  		resp2, err := client.Post(ctx, "/get")
   130  		t.AssertNil(err)
   131  		defer resp2.Close()
   132  		t.Assert(resp2.StatusCode, 404)
   133  
   134  		resp3, err := client.Get(ctx, "/post")
   135  		t.AssertNil(err)
   136  		defer resp3.Close()
   137  		t.Assert(resp3.StatusCode, 404)
   138  
   139  		resp4, err := client.Post(ctx, "/post")
   140  		t.AssertNil(err)
   141  		defer resp4.Close()
   142  		t.Assert(resp4.StatusCode, 200)
   143  	})
   144  }
   145  
   146  // Extra char '/' of the router.
   147  func Test_Router_ExtraChar(t *testing.T) {
   148  	s := g.Server(guid.S())
   149  	s.Group("/api", func(group *ghttp.RouterGroup) {
   150  		group.GET("/test", func(r *ghttp.Request) {
   151  			r.Response.Write("test")
   152  		})
   153  	})
   154  	s.SetDumpRouterMap(false)
   155  	s.Start()
   156  	defer s.Shutdown()
   157  
   158  	time.Sleep(100 * time.Millisecond)
   159  	gtest.C(t, func(t *gtest.T) {
   160  		client := g.Client()
   161  		client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
   162  
   163  		t.Assert(client.GetContent(ctx, "/api/test"), "test")
   164  		t.Assert(client.GetContent(ctx, "/api/test/"), "test")
   165  		t.Assert(client.GetContent(ctx, "/api/test//"), "test")
   166  		t.Assert(client.GetContent(ctx, "//api/test//"), "test")
   167  		t.Assert(client.GetContent(ctx, "//api//test//"), "test")
   168  		t.Assert(client.GetContent(ctx, "///api///test///"), "test")
   169  	})
   170  }
   171  
   172  // Custom status handler.
   173  func Test_Router_Status(t *testing.T) {
   174  	s := g.Server(guid.S())
   175  	s.BindHandler("/200", func(r *ghttp.Request) {
   176  		r.Response.WriteStatus(200)
   177  	})
   178  	s.BindHandler("/300", func(r *ghttp.Request) {
   179  		r.Response.WriteStatus(300)
   180  	})
   181  	s.BindHandler("/400", func(r *ghttp.Request) {
   182  		r.Response.WriteStatus(400)
   183  	})
   184  	s.BindHandler("/500", func(r *ghttp.Request) {
   185  		r.Response.WriteStatus(500)
   186  	})
   187  	s.SetDumpRouterMap(false)
   188  	s.Start()
   189  	defer s.Shutdown()
   190  
   191  	time.Sleep(100 * time.Millisecond)
   192  	gtest.C(t, func(t *gtest.T) {
   193  		client := g.Client()
   194  		client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
   195  
   196  		resp1, err := client.Get(ctx, "/200")
   197  		t.AssertNil(err)
   198  		defer resp1.Close()
   199  		t.Assert(resp1.StatusCode, 200)
   200  
   201  		resp2, err := client.Get(ctx, "/300")
   202  		t.AssertNil(err)
   203  		defer resp2.Close()
   204  		t.Assert(resp2.StatusCode, 300)
   205  
   206  		resp3, err := client.Get(ctx, "/400")
   207  		t.AssertNil(err)
   208  		defer resp3.Close()
   209  		t.Assert(resp3.StatusCode, 400)
   210  
   211  		resp4, err := client.Get(ctx, "/500")
   212  		t.AssertNil(err)
   213  		defer resp4.Close()
   214  		t.Assert(resp4.StatusCode, 500)
   215  
   216  		resp5, err := client.Get(ctx, "/404")
   217  		t.AssertNil(err)
   218  		defer resp5.Close()
   219  		t.Assert(resp5.StatusCode, 404)
   220  	})
   221  }
   222  
   223  func Test_Router_CustomStatusHandler(t *testing.T) {
   224  	s := g.Server(guid.S())
   225  	s.BindHandler("/", func(r *ghttp.Request) {
   226  		r.Response.Write("hello")
   227  	})
   228  	s.BindStatusHandler(404, func(r *ghttp.Request) {
   229  		r.Response.Write("404 page")
   230  	})
   231  	s.SetDumpRouterMap(false)
   232  	s.Start()
   233  	defer s.Shutdown()
   234  
   235  	time.Sleep(100 * time.Millisecond)
   236  	gtest.C(t, func(t *gtest.T) {
   237  		client := g.Client()
   238  		client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
   239  
   240  		t.Assert(client.GetContent(ctx, "/"), "hello")
   241  		resp, err := client.Get(ctx, "/ThisDoesNotExist")
   242  		t.AssertNil(err)
   243  		defer resp.Close()
   244  		t.Assert(resp.StatusCode, 404)
   245  		t.Assert(resp.ReadAllString(), "404 page")
   246  	})
   247  }
   248  
   249  // 404 not found router.
   250  func Test_Router_404(t *testing.T) {
   251  	s := g.Server(guid.S())
   252  	s.BindHandler("/", func(r *ghttp.Request) {
   253  		r.Response.Write("hello")
   254  	})
   255  	s.SetDumpRouterMap(false)
   256  	s.Start()
   257  	defer s.Shutdown()
   258  
   259  	time.Sleep(100 * time.Millisecond)
   260  	gtest.C(t, func(t *gtest.T) {
   261  		client := g.Client()
   262  		client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
   263  
   264  		t.Assert(client.GetContent(ctx, "/"), "hello")
   265  		resp, err := client.Get(ctx, "/ThisDoesNotExist")
   266  		t.AssertNil(err)
   267  		defer resp.Close()
   268  		t.Assert(resp.StatusCode, 404)
   269  	})
   270  }
   271  
   272  func Test_Router_Priority(t *testing.T) {
   273  	s := g.Server(guid.S())
   274  	s.BindHandler("/admin", func(r *ghttp.Request) {
   275  		r.Response.Write("admin")
   276  	})
   277  	s.BindHandler("/admin-{page}", func(r *ghttp.Request) {
   278  		r.Response.Write("admin-{page}")
   279  	})
   280  	s.BindHandler("/admin-goods", func(r *ghttp.Request) {
   281  		r.Response.Write("admin-goods")
   282  	})
   283  	s.BindHandler("/admin-goods-{page}", func(r *ghttp.Request) {
   284  		r.Response.Write("admin-goods-{page}")
   285  	})
   286  	s.SetDumpRouterMap(false)
   287  	s.Start()
   288  	defer s.Shutdown()
   289  
   290  	time.Sleep(100 * time.Millisecond)
   291  	gtest.C(t, func(t *gtest.T) {
   292  		client := g.Client()
   293  		client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
   294  
   295  		t.Assert(client.GetContent(ctx, "/admin"), "admin")
   296  		t.Assert(client.GetContent(ctx, "/admin-1"), "admin-{page}")
   297  		t.Assert(client.GetContent(ctx, "/admin-goods"), "admin-goods")
   298  		t.Assert(client.GetContent(ctx, "/admin-goods-2"), "admin-goods-{page}")
   299  	})
   300  }