github.com/zhongdalu/gf@v1.0.0/g/net/ghttp/ghttp_unit_router_domain_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 DomainControllerRest struct {
    20  	gmvc.Controller
    21  }
    22  
    23  func (c *DomainControllerRest) Init(r *ghttp.Request) {
    24  	c.Controller.Init(r)
    25  	c.Response.Write("1")
    26  }
    27  
    28  func (c *DomainControllerRest) Shut() {
    29  	c.Response.Write("2")
    30  }
    31  
    32  func (c *DomainControllerRest) Get() {
    33  	c.Response.Write("Controller Get")
    34  }
    35  
    36  func (c *DomainControllerRest) Put() {
    37  	c.Response.Write("Controller Put")
    38  }
    39  
    40  func (c *DomainControllerRest) Post() {
    41  	c.Response.Write("Controller Post")
    42  }
    43  
    44  func (c *DomainControllerRest) Delete() {
    45  	c.Response.Write("Controller Delete")
    46  }
    47  
    48  func (c *DomainControllerRest) Patch() {
    49  	c.Response.Write("Controller Patch")
    50  }
    51  
    52  func (c *DomainControllerRest) Options() {
    53  	c.Response.Write("Controller Options")
    54  }
    55  
    56  func (c *DomainControllerRest) Head() {
    57  	c.Response.Header().Set("head-ok", "1")
    58  }
    59  
    60  // 控制器注册测试
    61  func Test_Router_DomainControllerRest(t *testing.T) {
    62  	p := ports.PopRand()
    63  	s := g.Server(p)
    64  	d := s.Domain("localhost, local")
    65  	d.BindControllerRest("/", new(DomainControllerRest))
    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("/"), "Not Found")
    78  		gtest.Assert(client.PutContent("/"), "Not Found")
    79  		gtest.Assert(client.PostContent("/"), "Not Found")
    80  		gtest.Assert(client.DeleteContent("/"), "Not Found")
    81  		gtest.Assert(client.PatchContent("/"), "Not Found")
    82  		gtest.Assert(client.OptionsContent("/"), "Not Found")
    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"), "")
    89  		gtest.Assert(client.GetContent("/none-exist"), "Not Found")
    90  	})
    91  	gtest.Case(t, func() {
    92  		client := ghttp.NewClient()
    93  		client.SetPrefix(fmt.Sprintf("http://localhost:%d", p))
    94  
    95  		gtest.Assert(client.GetContent("/"), "1Controller Get2")
    96  		gtest.Assert(client.PutContent("/"), "1Controller Put2")
    97  		gtest.Assert(client.PostContent("/"), "1Controller Post2")
    98  		gtest.Assert(client.DeleteContent("/"), "1Controller Delete2")
    99  		gtest.Assert(client.PatchContent("/"), "1Controller Patch2")
   100  		gtest.Assert(client.OptionsContent("/"), "1Controller Options2")
   101  		resp1, err := client.Head("/")
   102  		if err == nil {
   103  			defer resp1.Close()
   104  		}
   105  		gtest.Assert(err, nil)
   106  		gtest.Assert(resp1.Header.Get("head-ok"), "1")
   107  		gtest.Assert(client.GetContent("/none-exist"), "Not Found")
   108  	})
   109  	gtest.Case(t, func() {
   110  		client := ghttp.NewClient()
   111  		client.SetPrefix(fmt.Sprintf("http://local:%d", p))
   112  
   113  		gtest.Assert(client.GetContent("/"), "1Controller Get2")
   114  		gtest.Assert(client.PutContent("/"), "1Controller Put2")
   115  		gtest.Assert(client.PostContent("/"), "1Controller Post2")
   116  		gtest.Assert(client.DeleteContent("/"), "1Controller Delete2")
   117  		gtest.Assert(client.PatchContent("/"), "1Controller Patch2")
   118  		gtest.Assert(client.OptionsContent("/"), "1Controller Options2")
   119  		resp1, err := client.Head("/")
   120  		if err == nil {
   121  			defer resp1.Close()
   122  		}
   123  		gtest.Assert(err, nil)
   124  		gtest.Assert(resp1.Header.Get("head-ok"), "1")
   125  		gtest.Assert(client.GetContent("/none-exist"), "Not Found")
   126  	})
   127  }