github.com/wangyougui/gf/v2@v2.6.5/net/ghttp/ghttp_z_unit_feature_router_domain_object_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/wangyougui/gf.
     6  
     7  package ghttp_test
     8  
     9  import (
    10  	"fmt"
    11  	"testing"
    12  	"time"
    13  
    14  	"github.com/wangyougui/gf/v2/frame/g"
    15  	"github.com/wangyougui/gf/v2/net/ghttp"
    16  	"github.com/wangyougui/gf/v2/test/gtest"
    17  	"github.com/wangyougui/gf/v2/util/guid"
    18  )
    19  
    20  type DomainObject struct{}
    21  
    22  func (o *DomainObject) Init(r *ghttp.Request) {
    23  	r.Response.Write("1")
    24  }
    25  
    26  func (o *DomainObject) Shut(r *ghttp.Request) {
    27  	r.Response.Write("2")
    28  }
    29  
    30  func (o *DomainObject) Index(r *ghttp.Request) {
    31  	r.Response.Write("Object Index")
    32  }
    33  
    34  func (o *DomainObject) Show(r *ghttp.Request) {
    35  	r.Response.Write("Object Show")
    36  }
    37  
    38  func (o *DomainObject) Info(r *ghttp.Request) {
    39  	r.Response.Write("Object Info")
    40  }
    41  
    42  func Test_Router_DomainObject1(t *testing.T) {
    43  	s := g.Server(guid.S())
    44  	s.Domain("localhost, local").BindObject("/", new(DomainObject))
    45  	s.SetDumpRouterMap(false)
    46  	s.Start()
    47  	defer s.Shutdown()
    48  
    49  	time.Sleep(100 * time.Millisecond)
    50  	gtest.C(t, func(t *gtest.T) {
    51  		client := g.Client()
    52  		client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
    53  
    54  		t.Assert(client.GetContent(ctx, "/"), "Not Found")
    55  		t.Assert(client.GetContent(ctx, "/init"), "Not Found")
    56  		t.Assert(client.GetContent(ctx, "/shut"), "Not Found")
    57  		t.Assert(client.GetContent(ctx, "/index"), "Not Found")
    58  		t.Assert(client.GetContent(ctx, "/show"), "Not Found")
    59  		t.Assert(client.GetContent(ctx, "/none-exist"), "Not Found")
    60  	})
    61  
    62  	gtest.C(t, func(t *gtest.T) {
    63  		client := g.Client()
    64  		client.SetPrefix(fmt.Sprintf("http://localhost:%d", s.GetListenedPort()))
    65  
    66  		t.Assert(client.GetContent(ctx, "/"), "1Object Index2")
    67  		t.Assert(client.GetContent(ctx, "/init"), "Not Found")
    68  		t.Assert(client.GetContent(ctx, "/shut"), "Not Found")
    69  		t.Assert(client.GetContent(ctx, "/index"), "1Object Index2")
    70  		t.Assert(client.GetContent(ctx, "/show"), "1Object Show2")
    71  		t.Assert(client.GetContent(ctx, "/info"), "1Object Info2")
    72  		t.Assert(client.GetContent(ctx, "/none-exist"), "Not Found")
    73  	})
    74  
    75  	gtest.C(t, func(t *gtest.T) {
    76  		client := g.Client()
    77  		client.SetPrefix(fmt.Sprintf("http://local:%d", s.GetListenedPort()))
    78  
    79  		t.Assert(client.GetContent(ctx, "/"), "1Object Index2")
    80  		t.Assert(client.GetContent(ctx, "/init"), "Not Found")
    81  		t.Assert(client.GetContent(ctx, "/shut"), "Not Found")
    82  		t.Assert(client.GetContent(ctx, "/index"), "1Object Index2")
    83  		t.Assert(client.GetContent(ctx, "/show"), "1Object Show2")
    84  		t.Assert(client.GetContent(ctx, "/info"), "1Object Info2")
    85  		t.Assert(client.GetContent(ctx, "/none-exist"), "Not Found")
    86  	})
    87  }
    88  
    89  func Test_Router_DomainObject2(t *testing.T) {
    90  	s := g.Server(guid.S())
    91  	s.Domain("localhost, local").BindObject("/object", new(DomainObject), "Show, Info")
    92  	s.SetDumpRouterMap(false)
    93  	s.Start()
    94  	defer s.Shutdown()
    95  
    96  	time.Sleep(100 * time.Millisecond)
    97  	gtest.C(t, func(t *gtest.T) {
    98  		client := g.Client()
    99  		client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
   100  
   101  		t.Assert(client.GetContent(ctx, "/"), "Not Found")
   102  		t.Assert(client.GetContent(ctx, "/object"), "Not Found")
   103  		t.Assert(client.GetContent(ctx, "/object/init"), "Not Found")
   104  		t.Assert(client.GetContent(ctx, "/object/shut"), "Not Found")
   105  		t.Assert(client.GetContent(ctx, "/object/index"), "Not Found")
   106  		t.Assert(client.GetContent(ctx, "/object/show"), "Not Found")
   107  		t.Assert(client.GetContent(ctx, "/object/info"), "Not Found")
   108  		t.Assert(client.GetContent(ctx, "/none-exist"), "Not Found")
   109  	})
   110  	gtest.C(t, func(t *gtest.T) {
   111  		client := g.Client()
   112  		client.SetPrefix(fmt.Sprintf("http://localhost:%d", s.GetListenedPort()))
   113  
   114  		t.Assert(client.GetContent(ctx, "/"), "Not Found")
   115  		t.Assert(client.GetContent(ctx, "/object"), "Not Found")
   116  		t.Assert(client.GetContent(ctx, "/object/init"), "Not Found")
   117  		t.Assert(client.GetContent(ctx, "/object/shut"), "Not Found")
   118  		t.Assert(client.GetContent(ctx, "/object/index"), "Not Found")
   119  		t.Assert(client.GetContent(ctx, "/object/show"), "1Object Show2")
   120  		t.Assert(client.GetContent(ctx, "/object/info"), "1Object Info2")
   121  		t.Assert(client.GetContent(ctx, "/none-exist"), "Not Found")
   122  	})
   123  	gtest.C(t, func(t *gtest.T) {
   124  		client := g.Client()
   125  		client.SetPrefix(fmt.Sprintf("http://local:%d", s.GetListenedPort()))
   126  
   127  		t.Assert(client.GetContent(ctx, "/"), "Not Found")
   128  		t.Assert(client.GetContent(ctx, "/object"), "Not Found")
   129  		t.Assert(client.GetContent(ctx, "/object/init"), "Not Found")
   130  		t.Assert(client.GetContent(ctx, "/object/shut"), "Not Found")
   131  		t.Assert(client.GetContent(ctx, "/object/index"), "Not Found")
   132  		t.Assert(client.GetContent(ctx, "/object/show"), "1Object Show2")
   133  		t.Assert(client.GetContent(ctx, "/object/info"), "1Object Info2")
   134  		t.Assert(client.GetContent(ctx, "/none-exist"), "Not Found")
   135  	})
   136  }
   137  
   138  func Test_Router_DomainObjectMethod(t *testing.T) {
   139  	s := g.Server(guid.S())
   140  	s.Domain("localhost, local").BindObjectMethod("/object-info", new(DomainObject), "Info")
   141  	s.SetDumpRouterMap(false)
   142  	s.Start()
   143  	defer s.Shutdown()
   144  
   145  	time.Sleep(100 * time.Millisecond)
   146  	gtest.C(t, func(t *gtest.T) {
   147  		client := g.Client()
   148  		client.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
   149  
   150  		t.Assert(client.GetContent(ctx, "/"), "Not Found")
   151  		t.Assert(client.GetContent(ctx, "/object"), "Not Found")
   152  		t.Assert(client.GetContent(ctx, "/object/init"), "Not Found")
   153  		t.Assert(client.GetContent(ctx, "/object/shut"), "Not Found")
   154  		t.Assert(client.GetContent(ctx, "/object/index"), "Not Found")
   155  		t.Assert(client.GetContent(ctx, "/object/show"), "Not Found")
   156  		t.Assert(client.GetContent(ctx, "/object/info"), "Not Found")
   157  		t.Assert(client.GetContent(ctx, "/object-info"), "Not Found")
   158  		t.Assert(client.GetContent(ctx, "/none-exist"), "Not Found")
   159  	})
   160  	gtest.C(t, func(t *gtest.T) {
   161  		client := g.Client()
   162  		client.SetPrefix(fmt.Sprintf("http://localhost:%d", s.GetListenedPort()))
   163  
   164  		t.Assert(client.GetContent(ctx, "/"), "Not Found")
   165  		t.Assert(client.GetContent(ctx, "/object"), "Not Found")
   166  		t.Assert(client.GetContent(ctx, "/object/init"), "Not Found")
   167  		t.Assert(client.GetContent(ctx, "/object/shut"), "Not Found")
   168  		t.Assert(client.GetContent(ctx, "/object/index"), "Not Found")
   169  		t.Assert(client.GetContent(ctx, "/object/show"), "Not Found")
   170  		t.Assert(client.GetContent(ctx, "/object/info"), "Not Found")
   171  		t.Assert(client.GetContent(ctx, "/object-info"), "1Object Info2")
   172  		t.Assert(client.GetContent(ctx, "/none-exist"), "Not Found")
   173  	})
   174  	gtest.C(t, func(t *gtest.T) {
   175  		client := g.Client()
   176  		client.SetPrefix(fmt.Sprintf("http://local:%d", s.GetListenedPort()))
   177  
   178  		t.Assert(client.GetContent(ctx, "/"), "Not Found")
   179  		t.Assert(client.GetContent(ctx, "/object"), "Not Found")
   180  		t.Assert(client.GetContent(ctx, "/object/init"), "Not Found")
   181  		t.Assert(client.GetContent(ctx, "/object/shut"), "Not Found")
   182  		t.Assert(client.GetContent(ctx, "/object/index"), "Not Found")
   183  		t.Assert(client.GetContent(ctx, "/object/show"), "Not Found")
   184  		t.Assert(client.GetContent(ctx, "/object/info"), "Not Found")
   185  		t.Assert(client.GetContent(ctx, "/object-info"), "1Object Info2")
   186  		t.Assert(client.GetContent(ctx, "/none-exist"), "Not Found")
   187  	})
   188  }