github.com/gogf/gf@v1.16.9/net/ghttp/ghttp_unit_router_group_rest_test.go (about)

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