github.com/zhongdalu/gf@v1.0.0/g/net/ghttp/ghttp_unit_router_group_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  // 分组路由测试
     8  package ghttp_test
     9  
    10  import (
    11  	"fmt"
    12  	"github.com/zhongdalu/gf/g"
    13  	"github.com/zhongdalu/gf/g/frame/gmvc"
    14  	"github.com/zhongdalu/gf/g/net/ghttp"
    15  	"github.com/zhongdalu/gf/g/test/gtest"
    16  	"testing"
    17  	"time"
    18  )
    19  
    20  type GroupCtlRest struct {
    21  	gmvc.Controller
    22  }
    23  
    24  func (c *GroupCtlRest) Init(r *ghttp.Request) {
    25  	c.Controller.Init(r)
    26  	c.Response.Write("1")
    27  }
    28  
    29  func (c *GroupCtlRest) Shut() {
    30  	c.Response.Write("2")
    31  }
    32  
    33  func (c *GroupCtlRest) Get() {
    34  	c.Response.Write("Controller Get")
    35  }
    36  
    37  func (c *GroupCtlRest) Put() {
    38  	c.Response.Write("Controller Put")
    39  }
    40  
    41  func (c *GroupCtlRest) Post() {
    42  	c.Response.Write("Controller Post")
    43  }
    44  
    45  func (c *GroupCtlRest) Delete() {
    46  	c.Response.Write("Controller Delete")
    47  }
    48  
    49  func (c *GroupCtlRest) Patch() {
    50  	c.Response.Write("Controller Patch")
    51  }
    52  
    53  func (c *GroupCtlRest) Options() {
    54  	c.Response.Write("Controller Options")
    55  }
    56  
    57  func (c *GroupCtlRest) Head() {
    58  	c.Response.Header().Set("head-ok", "1")
    59  }
    60  
    61  type GroupObjRest struct{}
    62  
    63  func (o *GroupObjRest) Init(r *ghttp.Request) {
    64  	r.Response.Write("1")
    65  }
    66  
    67  func (o *GroupObjRest) Shut(r *ghttp.Request) {
    68  	r.Response.Write("2")
    69  }
    70  
    71  func (o *GroupObjRest) Get(r *ghttp.Request) {
    72  	r.Response.Write("Object Get")
    73  }
    74  
    75  func (o *GroupObjRest) Put(r *ghttp.Request) {
    76  	r.Response.Write("Object Put")
    77  }
    78  
    79  func (o *GroupObjRest) Post(r *ghttp.Request) {
    80  	r.Response.Write("Object Post")
    81  }
    82  
    83  func (o *GroupObjRest) Delete(r *ghttp.Request) {
    84  	r.Response.Write("Object Delete")
    85  }
    86  
    87  func (o *GroupObjRest) Patch(r *ghttp.Request) {
    88  	r.Response.Write("Object Patch")
    89  }
    90  
    91  func (o *GroupObjRest) Options(r *ghttp.Request) {
    92  	r.Response.Write("Object Options")
    93  }
    94  
    95  func (o *GroupObjRest) Head(r *ghttp.Request) {
    96  	r.Response.Header().Set("head-ok", "1")
    97  }
    98  
    99  func Test_Router_GroupRest(t *testing.T) {
   100  	p := ports.PopRand()
   101  	s := g.Server(p)
   102  	g := s.Group("/api")
   103  	ctl := new(GroupCtlRest)
   104  	obj := new(GroupObjRest)
   105  	g.REST("/ctl", ctl)
   106  	g.REST("/obj", obj)
   107  	g.REST("/{.struct}/{.method}", ctl)
   108  	g.REST("/{.struct}/{.method}", obj)
   109  	s.SetPort(p)
   110  	s.SetDumpRouteMap(false)
   111  	s.Start()
   112  	defer s.Shutdown()
   113  
   114  	time.Sleep(time.Second)
   115  	gtest.Case(t, func() {
   116  		client := ghttp.NewClient()
   117  		client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", p))
   118  
   119  		gtest.Assert(client.GetContent("/api/ctl"), "1Controller Get2")
   120  		gtest.Assert(client.PutContent("/api/ctl"), "1Controller Put2")
   121  		gtest.Assert(client.PostContent("/api/ctl"), "1Controller Post2")
   122  		gtest.Assert(client.DeleteContent("/api/ctl"), "1Controller Delete2")
   123  		gtest.Assert(client.PatchContent("/api/ctl"), "1Controller Patch2")
   124  		gtest.Assert(client.OptionsContent("/api/ctl"), "1Controller Options2")
   125  		resp1, err := client.Head("/api/ctl")
   126  		if err == nil {
   127  			defer resp1.Close()
   128  		}
   129  		gtest.Assert(err, nil)
   130  		gtest.Assert(resp1.Header.Get("head-ok"), "1")
   131  
   132  		gtest.Assert(client.GetContent("/api/obj"), "1Object Get2")
   133  		gtest.Assert(client.PutContent("/api/obj"), "1Object Put2")
   134  		gtest.Assert(client.PostContent("/api/obj"), "1Object Post2")
   135  		gtest.Assert(client.DeleteContent("/api/obj"), "1Object Delete2")
   136  		gtest.Assert(client.PatchContent("/api/obj"), "1Object Patch2")
   137  		gtest.Assert(client.OptionsContent("/api/obj"), "1Object Options2")
   138  		resp2, err := client.Head("/api/obj")
   139  		if err == nil {
   140  			defer resp2.Close()
   141  		}
   142  		gtest.Assert(err, nil)
   143  		gtest.Assert(resp2.Header.Get("head-ok"), "1")
   144  
   145  		gtest.Assert(client.GetContent("/api/group-ctl-rest"), "Not Found")
   146  		gtest.Assert(client.GetContent("/api/group-ctl-rest/get"), "1Controller Get2")
   147  		gtest.Assert(client.PutContent("/api/group-ctl-rest/put"), "1Controller Put2")
   148  		gtest.Assert(client.PostContent("/api/group-ctl-rest/post"), "1Controller Post2")
   149  		gtest.Assert(client.DeleteContent("/api/group-ctl-rest/delete"), "1Controller Delete2")
   150  		gtest.Assert(client.PatchContent("/api/group-ctl-rest/patch"), "1Controller Patch2")
   151  		gtest.Assert(client.OptionsContent("/api/group-ctl-rest/options"), "1Controller Options2")
   152  		resp3, err := client.Head("/api/group-ctl-rest/head")
   153  		if err == nil {
   154  			defer resp3.Close()
   155  		}
   156  		gtest.Assert(err, nil)
   157  		gtest.Assert(resp3.Header.Get("head-ok"), "1")
   158  
   159  		gtest.Assert(client.GetContent("/api/group-obj-rest"), "Not Found")
   160  		gtest.Assert(client.GetContent("/api/group-obj-rest/get"), "1Object Get2")
   161  		gtest.Assert(client.PutContent("/api/group-obj-rest/put"), "1Object Put2")
   162  		gtest.Assert(client.PostContent("/api/group-obj-rest/post"), "1Object Post2")
   163  		gtest.Assert(client.DeleteContent("/api/group-obj-rest/delete"), "1Object Delete2")
   164  		gtest.Assert(client.PatchContent("/api/group-obj-rest/patch"), "1Object Patch2")
   165  		gtest.Assert(client.OptionsContent("/api/group-obj-rest/options"), "1Object Options2")
   166  		resp4, err := client.Head("/api/group-obj-rest/head")
   167  		if err == nil {
   168  			defer resp4.Close()
   169  		}
   170  		gtest.Assert(err, nil)
   171  		gtest.Assert(resp4.Header.Get("head-ok"), "1")
   172  	})
   173  }