github.com/zhongdalu/gf@v1.0.0/g/net/ghttp/ghttp_unit_router_domain_basic_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  // 基本路由功能以及优先级测试
     8  package ghttp_test
     9  
    10  import (
    11  	"fmt"
    12  	"github.com/zhongdalu/gf/g"
    13  	"github.com/zhongdalu/gf/g/net/ghttp"
    14  	"github.com/zhongdalu/gf/g/test/gtest"
    15  	"testing"
    16  	"time"
    17  )
    18  
    19  // 基本路由功能测试
    20  func Test_Router_DomainBasic(t *testing.T) {
    21  	p := ports.PopRand()
    22  	s := g.Server(p)
    23  	d := s.Domain("localhost, local")
    24  	d.BindHandler("/:name", func(r *ghttp.Request) {
    25  		r.Response.Write("/:name")
    26  	})
    27  	d.BindHandler("/:name/update", func(r *ghttp.Request) {
    28  		r.Response.Write(r.Get("name"))
    29  	})
    30  	d.BindHandler("/:name/:action", func(r *ghttp.Request) {
    31  		r.Response.Write(r.Get("action"))
    32  	})
    33  	d.BindHandler("/:name/*any", func(r *ghttp.Request) {
    34  		r.Response.Write(r.Get("any"))
    35  	})
    36  	d.BindHandler("/user/list/{field}.html", func(r *ghttp.Request) {
    37  		r.Response.Write(r.Get("field"))
    38  	})
    39  	s.SetPort(p)
    40  	s.SetDumpRouteMap(false)
    41  	s.Start()
    42  	defer s.Shutdown()
    43  
    44  	// 等待启动完成
    45  	time.Sleep(time.Second)
    46  	gtest.Case(t, func() {
    47  		client := ghttp.NewClient()
    48  		client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
    49  		gtest.Assert(client.GetContent("/john"), "Not Found")
    50  		gtest.Assert(client.GetContent("/john/update"), "Not Found")
    51  		gtest.Assert(client.GetContent("/john/edit"), "Not Found")
    52  		gtest.Assert(client.GetContent("/user/list/100.html"), "Not Found")
    53  	})
    54  	gtest.Case(t, func() {
    55  		client := ghttp.NewClient()
    56  		client.SetPrefix(fmt.Sprintf("http://localhost:%d", p))
    57  		gtest.Assert(client.GetContent("/john"), "")
    58  		gtest.Assert(client.GetContent("/john/update"), "john")
    59  		gtest.Assert(client.GetContent("/john/edit"), "edit")
    60  		gtest.Assert(client.GetContent("/user/list/100.html"), "100")
    61  	})
    62  	gtest.Case(t, func() {
    63  		client := ghttp.NewClient()
    64  		client.SetPrefix(fmt.Sprintf("http://local:%d", p))
    65  		gtest.Assert(client.GetContent("/john"), "")
    66  		gtest.Assert(client.GetContent("/john/update"), "john")
    67  		gtest.Assert(client.GetContent("/john/edit"), "edit")
    68  		gtest.Assert(client.GetContent("/user/list/100.html"), "100")
    69  	})
    70  }
    71  
    72  // 测试HTTP Method注册.
    73  func Test_Router_DomainMethod(t *testing.T) {
    74  	p := ports.PopRand()
    75  	s := g.Server(p)
    76  	d := s.Domain("localhost, local")
    77  	d.BindHandler("GET:/get", func(r *ghttp.Request) {
    78  
    79  	})
    80  	d.BindHandler("POST:/post", func(r *ghttp.Request) {
    81  
    82  	})
    83  	s.SetPort(p)
    84  	s.SetDumpRouteMap(false)
    85  	s.Start()
    86  	defer s.Shutdown()
    87  
    88  	// 等待启动完成
    89  	time.Sleep(time.Second)
    90  	gtest.Case(t, func() {
    91  		client := ghttp.NewClient()
    92  		client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
    93  
    94  		resp1, err := client.Get("/get")
    95  		defer resp1.Close()
    96  		gtest.Assert(err, nil)
    97  		gtest.Assert(resp1.StatusCode, 404)
    98  
    99  		resp2, err := client.Post("/get")
   100  		defer resp2.Close()
   101  		gtest.Assert(err, nil)
   102  		gtest.Assert(resp2.StatusCode, 404)
   103  
   104  		resp3, err := client.Get("/post")
   105  		defer resp3.Close()
   106  		gtest.Assert(err, nil)
   107  		gtest.Assert(resp3.StatusCode, 404)
   108  
   109  		resp4, err := client.Post("/post")
   110  		defer resp4.Close()
   111  		gtest.Assert(err, nil)
   112  		gtest.Assert(resp4.StatusCode, 404)
   113  	})
   114  
   115  	gtest.Case(t, func() {
   116  		client := ghttp.NewClient()
   117  		client.SetPrefix(fmt.Sprintf("http://localhost:%d", p))
   118  
   119  		resp1, err := client.Get("/get")
   120  		defer resp1.Close()
   121  		gtest.Assert(err, nil)
   122  		gtest.Assert(resp1.StatusCode, 200)
   123  
   124  		resp2, err := client.Post("/get")
   125  		defer resp2.Close()
   126  		gtest.Assert(err, nil)
   127  		gtest.Assert(resp2.StatusCode, 404)
   128  
   129  		resp3, err := client.Get("/post")
   130  		defer resp3.Close()
   131  		gtest.Assert(err, nil)
   132  		gtest.Assert(resp3.StatusCode, 404)
   133  
   134  		resp4, err := client.Post("/post")
   135  		defer resp4.Close()
   136  		gtest.Assert(err, nil)
   137  		gtest.Assert(resp4.StatusCode, 200)
   138  	})
   139  
   140  	gtest.Case(t, func() {
   141  		client := ghttp.NewClient()
   142  		client.SetPrefix(fmt.Sprintf("http://local:%d", p))
   143  
   144  		resp1, err := client.Get("/get")
   145  		defer resp1.Close()
   146  		gtest.Assert(err, nil)
   147  		gtest.Assert(resp1.StatusCode, 200)
   148  
   149  		resp2, err := client.Post("/get")
   150  		defer resp2.Close()
   151  		gtest.Assert(err, nil)
   152  		gtest.Assert(resp2.StatusCode, 404)
   153  
   154  		resp3, err := client.Get("/post")
   155  		defer resp3.Close()
   156  		gtest.Assert(err, nil)
   157  		gtest.Assert(resp3.StatusCode, 404)
   158  
   159  		resp4, err := client.Post("/post")
   160  		defer resp4.Close()
   161  		gtest.Assert(err, nil)
   162  		gtest.Assert(resp4.StatusCode, 200)
   163  	})
   164  }
   165  
   166  // 测试状态返回.
   167  func Test_Router_DomainStatus(t *testing.T) {
   168  	p := ports.PopRand()
   169  	s := g.Server(p)
   170  	d := s.Domain("localhost, local")
   171  	d.BindHandler("/200", func(r *ghttp.Request) {
   172  		r.Response.WriteStatus(200)
   173  	})
   174  	d.BindHandler("/300", func(r *ghttp.Request) {
   175  		r.Response.WriteStatus(300)
   176  	})
   177  	d.BindHandler("/400", func(r *ghttp.Request) {
   178  		r.Response.WriteStatus(400)
   179  	})
   180  	d.BindHandler("/500", func(r *ghttp.Request) {
   181  		r.Response.WriteStatus(500)
   182  	})
   183  	s.SetPort(p)
   184  	s.SetDumpRouteMap(false)
   185  	s.Start()
   186  	defer s.Shutdown()
   187  
   188  	// 等待启动完成
   189  	time.Sleep(time.Second)
   190  	gtest.Case(t, func() {
   191  		client := ghttp.NewClient()
   192  		client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
   193  
   194  		resp1, err := client.Get("/200")
   195  		defer resp1.Close()
   196  		gtest.Assert(err, nil)
   197  		gtest.Assert(resp1.StatusCode, 404)
   198  
   199  		resp2, err := client.Get("/300")
   200  		defer resp2.Close()
   201  		gtest.Assert(err, nil)
   202  		gtest.Assert(resp2.StatusCode, 404)
   203  
   204  		resp3, err := client.Get("/400")
   205  		defer resp3.Close()
   206  		gtest.Assert(err, nil)
   207  		gtest.Assert(resp3.StatusCode, 404)
   208  
   209  		resp4, err := client.Get("/500")
   210  		defer resp4.Close()
   211  		gtest.Assert(err, nil)
   212  		gtest.Assert(resp4.StatusCode, 404)
   213  	})
   214  	gtest.Case(t, func() {
   215  		client := ghttp.NewClient()
   216  		client.SetPrefix(fmt.Sprintf("http://localhost:%d", p))
   217  
   218  		resp1, err := client.Get("/200")
   219  		defer resp1.Close()
   220  		gtest.Assert(err, nil)
   221  		gtest.Assert(resp1.StatusCode, 200)
   222  
   223  		resp2, err := client.Get("/300")
   224  		defer resp2.Close()
   225  		gtest.Assert(err, nil)
   226  		gtest.Assert(resp2.StatusCode, 300)
   227  
   228  		resp3, err := client.Get("/400")
   229  		defer resp3.Close()
   230  		gtest.Assert(err, nil)
   231  		gtest.Assert(resp3.StatusCode, 400)
   232  
   233  		resp4, err := client.Get("/500")
   234  		defer resp4.Close()
   235  		gtest.Assert(err, nil)
   236  		gtest.Assert(resp4.StatusCode, 500)
   237  	})
   238  	gtest.Case(t, func() {
   239  		client := ghttp.NewClient()
   240  		client.SetPrefix(fmt.Sprintf("http://local:%d", p))
   241  
   242  		resp1, err := client.Get("/200")
   243  		defer resp1.Close()
   244  		gtest.Assert(err, nil)
   245  		gtest.Assert(resp1.StatusCode, 200)
   246  
   247  		resp2, err := client.Get("/300")
   248  		defer resp2.Close()
   249  		gtest.Assert(err, nil)
   250  		gtest.Assert(resp2.StatusCode, 300)
   251  
   252  		resp3, err := client.Get("/400")
   253  		defer resp3.Close()
   254  		gtest.Assert(err, nil)
   255  		gtest.Assert(resp3.StatusCode, 400)
   256  
   257  		resp4, err := client.Get("/500")
   258  		defer resp4.Close()
   259  		gtest.Assert(err, nil)
   260  		gtest.Assert(resp4.StatusCode, 500)
   261  	})
   262  }
   263  
   264  // 自定义状态码处理.
   265  func Test_Router_DomainCustomStatusHandler(t *testing.T) {
   266  	p := ports.PopRand()
   267  	s := g.Server(p)
   268  	d := s.Domain("localhost, local")
   269  	d.BindHandler("/", func(r *ghttp.Request) {
   270  		r.Response.Write("hello")
   271  	})
   272  	d.BindStatusHandler(404, func(r *ghttp.Request) {
   273  		r.Response.Write("404 page")
   274  	})
   275  	s.SetPort(p)
   276  	s.SetDumpRouteMap(false)
   277  	s.Start()
   278  	defer s.Shutdown()
   279  
   280  	// 等待启动完成
   281  	time.Sleep(time.Second)
   282  	gtest.Case(t, func() {
   283  		client := ghttp.NewClient()
   284  		client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
   285  
   286  		gtest.Assert(client.GetContent("/"), "Not Found")
   287  		gtest.Assert(client.GetContent("/ThisDoesNotExist"), "Not Found")
   288  	})
   289  	gtest.Case(t, func() {
   290  		client := ghttp.NewClient()
   291  		client.SetPrefix(fmt.Sprintf("http://localhost:%d", p))
   292  
   293  		gtest.Assert(client.GetContent("/"), "hello")
   294  		gtest.Assert(client.GetContent("/ThisDoesNotExist"), "404 page")
   295  	})
   296  	gtest.Case(t, func() {
   297  		client := ghttp.NewClient()
   298  		client.SetPrefix(fmt.Sprintf("http://local:%d", p))
   299  
   300  		gtest.Assert(client.GetContent("/"), "hello")
   301  		gtest.Assert(client.GetContent("/ThisDoesNotExist"), "404 page")
   302  	})
   303  }
   304  
   305  // 测试不存在的路由.
   306  func Test_Router_Domain404(t *testing.T) {
   307  	p := ports.PopRand()
   308  	s := g.Server(p)
   309  	d := s.Domain("localhost, local")
   310  	d.BindHandler("/", func(r *ghttp.Request) {
   311  		r.Response.Write("hello")
   312  	})
   313  	s.SetPort(p)
   314  	s.SetDumpRouteMap(false)
   315  	s.Start()
   316  	defer s.Shutdown()
   317  
   318  	// 等待启动完成
   319  	time.Sleep(time.Second)
   320  	gtest.Case(t, func() {
   321  		client := ghttp.NewClient()
   322  		client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
   323  
   324  		gtest.Assert(client.GetContent("/"), "Not Found")
   325  	})
   326  	gtest.Case(t, func() {
   327  		client := ghttp.NewClient()
   328  		client.SetPrefix(fmt.Sprintf("http://localhost:%d", p))
   329  
   330  		gtest.Assert(client.GetContent("/"), "hello")
   331  	})
   332  	gtest.Case(t, func() {
   333  		client := ghttp.NewClient()
   334  		client.SetPrefix(fmt.Sprintf("http://local:%d", p))
   335  
   336  		gtest.Assert(client.GetContent("/"), "hello")
   337  	})
   338  }