github.com/zhongdalu/gf@v1.0.0/g/net/ghttp/ghttp_unit_router_controller_rest_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/frame/gmvc"
    13  	"github.com/zhongdalu/gf/g/net/ghttp"
    14  	"github.com/zhongdalu/gf/g/test/gtest"
    15  	"testing"
    16  	"time"
    17  )
    18  
    19  type ControllerRest struct {
    20  	gmvc.Controller
    21  }
    22  
    23  func (c *ControllerRest) Init(r *ghttp.Request) {
    24  	c.Controller.Init(r)
    25  	c.Response.Write("1")
    26  }
    27  
    28  func (c *ControllerRest) Shut() {
    29  	c.Response.Write("2")
    30  }
    31  
    32  func (c *ControllerRest) Get() {
    33  	c.Response.Write("Controller Get")
    34  }
    35  
    36  func (c *ControllerRest) Put() {
    37  	c.Response.Write("Controller Put")
    38  }
    39  
    40  func (c *ControllerRest) Post() {
    41  	c.Response.Write("Controller Post")
    42  }
    43  
    44  func (c *ControllerRest) Delete() {
    45  	c.Response.Write("Controller Delete")
    46  }
    47  
    48  func (c *ControllerRest) Patch() {
    49  	c.Response.Write("Controller Patch")
    50  }
    51  
    52  func (c *ControllerRest) Options() {
    53  	c.Response.Write("Controller Options")
    54  }
    55  
    56  func (c *ControllerRest) Head() {
    57  	c.Response.Header().Set("head-ok", "1")
    58  }
    59  
    60  // 控制器注册测试
    61  func Test_Router_ControllerRest(t *testing.T) {
    62  	p := ports.PopRand()
    63  	s := g.Server(p)
    64  	s.BindControllerRest("/", new(ControllerRest))
    65  	s.BindControllerRest("/{.struct}/{.method}", new(ControllerRest))
    66  	s.SetPort(p)
    67  	s.SetDumpRouteMap(false)
    68  	s.Start()
    69  	defer s.Shutdown()
    70  
    71  	// 等待启动完成
    72  	time.Sleep(time.Second)
    73  	gtest.Case(t, func() {
    74  		client := ghttp.NewClient()
    75  		client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
    76  
    77  		gtest.Assert(client.GetContent("/"), "1Controller Get2")
    78  		gtest.Assert(client.PutContent("/"), "1Controller Put2")
    79  		gtest.Assert(client.PostContent("/"), "1Controller Post2")
    80  		gtest.Assert(client.DeleteContent("/"), "1Controller Delete2")
    81  		gtest.Assert(client.PatchContent("/"), "1Controller Patch2")
    82  		gtest.Assert(client.OptionsContent("/"), "1Controller Options2")
    83  		resp1, err := client.Head("/")
    84  		if err == nil {
    85  			defer resp1.Close()
    86  		}
    87  		gtest.Assert(err, nil)
    88  		gtest.Assert(resp1.Header.Get("head-ok"), "1")
    89  
    90  		gtest.Assert(client.GetContent("/controller-rest/get"), "1Controller Get2")
    91  		gtest.Assert(client.PutContent("/controller-rest/put"), "1Controller Put2")
    92  		gtest.Assert(client.PostContent("/controller-rest/post"), "1Controller Post2")
    93  		gtest.Assert(client.DeleteContent("/controller-rest/delete"), "1Controller Delete2")
    94  		gtest.Assert(client.PatchContent("/controller-rest/patch"), "1Controller Patch2")
    95  		gtest.Assert(client.OptionsContent("/controller-rest/options"), "1Controller Options2")
    96  		resp2, err := client.Head("/controller-rest/head")
    97  		if err == nil {
    98  			defer resp2.Close()
    99  		}
   100  		gtest.Assert(err, nil)
   101  		gtest.Assert(resp2.Header.Get("head-ok"), "1")
   102  
   103  		gtest.Assert(client.GetContent("/none-exist"), "Not Found")
   104  	})
   105  }