github.com/zhongdalu/gf@v1.0.0/g/net/ghttp/ghttp_unit_router_names_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  type NamesObject struct{}
    19  
    20  func (o *NamesObject) ShowName(r *ghttp.Request) {
    21  	r.Response.Write("Object Show Name")
    22  }
    23  
    24  func Test_NameToUri_FullName(t *testing.T) {
    25  	p := ports.PopRand()
    26  	s := g.Server(p)
    27  	s.SetNameToUriType(ghttp.NAME_TO_URI_TYPE_FULLNAME)
    28  	s.BindObject("/{.struct}/{.method}", new(NamesObject))
    29  	s.SetPort(p)
    30  	s.SetDumpRouteMap(false)
    31  	s.Start()
    32  	defer s.Shutdown()
    33  
    34  	// 等待启动完成
    35  	time.Sleep(time.Second)
    36  	gtest.Case(t, func() {
    37  		client := ghttp.NewClient()
    38  		client.SetBrowserMode(true)
    39  		client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
    40  		gtest.Assert(client.GetContent("/"), "Not Found")
    41  		gtest.Assert(client.GetContent("/NamesObject"), "Not Found")
    42  		gtest.Assert(client.GetContent("/NamesObject/ShowName"), "Object Show Name")
    43  	})
    44  }
    45  
    46  func Test_NameToUri_AllLower(t *testing.T) {
    47  	p := ports.PopRand()
    48  	s := g.Server(p)
    49  	s.SetNameToUriType(ghttp.NAME_TO_URI_TYPE_ALLLOWER)
    50  	s.BindObject("/{.struct}/{.method}", new(NamesObject))
    51  	s.SetPort(p)
    52  	s.SetDumpRouteMap(false)
    53  	s.Start()
    54  	defer s.Shutdown()
    55  
    56  	// 等待启动完成
    57  	time.Sleep(time.Second)
    58  	gtest.Case(t, func() {
    59  		client := ghttp.NewClient()
    60  		client.SetBrowserMode(true)
    61  		client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
    62  		gtest.Assert(client.GetContent("/"), "Not Found")
    63  		gtest.Assert(client.GetContent("/NamesObject"), "Not Found")
    64  		gtest.Assert(client.GetContent("/namesobject/showname"), "Object Show Name")
    65  	})
    66  }
    67  
    68  func Test_NameToUri_Camel(t *testing.T) {
    69  	p := ports.PopRand()
    70  	s := g.Server(p)
    71  	s.SetNameToUriType(ghttp.NAME_TO_URI_TYPE_CAMEL)
    72  	s.BindObject("/{.struct}/{.method}", new(NamesObject))
    73  	s.SetPort(p)
    74  	s.SetDumpRouteMap(false)
    75  	s.Start()
    76  	defer s.Shutdown()
    77  
    78  	// 等待启动完成
    79  	time.Sleep(time.Second)
    80  	gtest.Case(t, func() {
    81  		client := ghttp.NewClient()
    82  		client.SetBrowserMode(true)
    83  		client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
    84  		gtest.Assert(client.GetContent("/"), "Not Found")
    85  		gtest.Assert(client.GetContent("/NamesObject"), "Not Found")
    86  		gtest.Assert(client.GetContent("/namesObject/showName"), "Object Show Name")
    87  	})
    88  }
    89  
    90  func Test_NameToUri_Default(t *testing.T) {
    91  	p := ports.PopRand()
    92  	s := g.Server(p)
    93  	s.SetNameToUriType(ghttp.NAME_TO_URI_TYPE_DEFAULT)
    94  	s.BindObject("/{.struct}/{.method}", new(NamesObject))
    95  	s.SetPort(p)
    96  	s.SetDumpRouteMap(false)
    97  	s.Start()
    98  	defer s.Shutdown()
    99  
   100  	// 等待启动完成
   101  	time.Sleep(time.Second)
   102  	gtest.Case(t, func() {
   103  		client := ghttp.NewClient()
   104  		client.SetBrowserMode(true)
   105  		client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
   106  		gtest.Assert(client.GetContent("/"), "Not Found")
   107  		gtest.Assert(client.GetContent("/NamesObject"), "Not Found")
   108  		gtest.Assert(client.GetContent("/names-object/show-name"), "Object Show Name")
   109  	})
   110  }