github.com/wangyougui/gf/v2@v2.6.5/net/ghttp/ghttp_z_unit_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  	"context"
    11  	"fmt"
    12  	"net/http"
    13  	"net/url"
    14  	"runtime"
    15  	"testing"
    16  	"time"
    17  
    18  	"github.com/wangyougui/gf/v2/encoding/gurl"
    19  	"github.com/wangyougui/gf/v2/frame/g"
    20  	"github.com/wangyougui/gf/v2/internal/httputil"
    21  	"github.com/wangyougui/gf/v2/net/ghttp"
    22  	"github.com/wangyougui/gf/v2/os/genv"
    23  	"github.com/wangyougui/gf/v2/test/gtest"
    24  	"github.com/wangyougui/gf/v2/util/guid"
    25  )
    26  
    27  var (
    28  	ctx = context.TODO()
    29  )
    30  
    31  func init() {
    32  	genv.Set("UNDER_TEST", "1")
    33  }
    34  
    35  func Test_GetUrl(t *testing.T) {
    36  	s := g.Server(guid.S())
    37  	s.BindHandler("/url", func(r *ghttp.Request) {
    38  		r.Response.Write(r.GetUrl())
    39  	})
    40  	s.SetDumpRouterMap(false)
    41  	s.Start()
    42  	defer s.Shutdown()
    43  
    44  	time.Sleep(100 * time.Millisecond)
    45  	gtest.C(t, func(t *gtest.T) {
    46  		prefix := fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort())
    47  		client := g.Client()
    48  		client.SetBrowserMode(true)
    49  		client.SetPrefix(prefix)
    50  
    51  		t.Assert(client.GetContent(ctx, "/url"), prefix+"/url")
    52  	})
    53  }
    54  
    55  func Test_XUrlPath(t *testing.T) {
    56  	s := g.Server(guid.S())
    57  	s.BindHandler("/test1", func(r *ghttp.Request) {
    58  		r.Response.Write(`test1`)
    59  	})
    60  	s.BindHandler("/test2", func(r *ghttp.Request) {
    61  		r.Response.Write(`test2`)
    62  	})
    63  	s.SetHandler(func(w http.ResponseWriter, r *http.Request) {
    64  		r.Header.Set(ghttp.HeaderXUrlPath, "/test2")
    65  		s.ServeHTTP(w, r)
    66  	})
    67  	s.SetDumpRouterMap(false)
    68  	s.Start()
    69  	defer s.Shutdown()
    70  
    71  	time.Sleep(100 * time.Millisecond)
    72  	gtest.C(t, func(t *gtest.T) {
    73  		c := g.Client()
    74  		c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
    75  
    76  		t.Assert(c.GetContent(ctx, "/"), "test2")
    77  		t.Assert(c.GetContent(ctx, "/test/test"), "test2")
    78  		t.Assert(c.GetContent(ctx, "/test1"), "test2")
    79  	})
    80  }
    81  
    82  func Test_GetListenedAddress(t *testing.T) {
    83  	s := g.Server(guid.S())
    84  	s.BindHandler("/", func(r *ghttp.Request) {
    85  		r.Response.Write(`test`)
    86  	})
    87  	s.SetDumpRouterMap(false)
    88  	s.Start()
    89  	defer s.Shutdown()
    90  
    91  	time.Sleep(100 * time.Millisecond)
    92  	gtest.C(t, func(t *gtest.T) {
    93  		c := g.Client()
    94  		c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
    95  		t.Assert(c.GetContent(ctx, "/"), "test")
    96  	})
    97  	gtest.C(t, func(t *gtest.T) {
    98  		t.Assert(fmt.Sprintf(`:%d`, s.GetListenedPort()), s.GetListenedAddress())
    99  	})
   100  }
   101  
   102  func Test_GetListenedAddressWithHost(t *testing.T) {
   103  	s := g.Server(guid.S())
   104  	s.BindHandler("/", func(r *ghttp.Request) {
   105  		r.Response.Write(`test`)
   106  	})
   107  	s.SetAddr("127.0.0.1:0")
   108  	s.SetDumpRouterMap(false)
   109  	s.Start()
   110  	defer s.Shutdown()
   111  
   112  	time.Sleep(100 * time.Millisecond)
   113  	gtest.C(t, func(t *gtest.T) {
   114  		c := g.Client()
   115  		c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
   116  		t.Assert(c.GetContent(ctx, "/"), "test")
   117  	})
   118  	gtest.C(t, func(t *gtest.T) {
   119  		t.Assert(fmt.Sprintf(`127.0.0.1:%d`, s.GetListenedPort()), s.GetListenedAddress())
   120  	})
   121  }
   122  
   123  func Test_RoutePathParams(t *testing.T) {
   124  	s := g.Server(guid.S())
   125  	s.BindHandler("/:param", func(r *ghttp.Request) {
   126  		r.Response.Write(r.Get("param"), ",", r.Get("c"))
   127  	})
   128  	s.SetAddr("127.0.0.1:0")
   129  	s.SetDumpRouterMap(false)
   130  	s.Start()
   131  	defer s.Shutdown()
   132  
   133  	time.Sleep(100 * time.Millisecond)
   134  	gtest.C(t, func(t *gtest.T) {
   135  		c := g.Client()
   136  		param := "net/http/get"
   137  		c.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
   138  		t.Assert(c.GetContent(
   139  			ctx,
   140  			"/"+gurl.Encode(param)+"?a=1&b=2&c="+gurl.Encode(param)),
   141  			"net/http/get,net/http/get",
   142  		)
   143  	})
   144  }
   145  
   146  func Test_BuildParams(t *testing.T) {
   147  	// normal && special cases
   148  	params := map[string]string{
   149  		"val":   "12345678",
   150  		"code1": "x&a=1", // for fix
   151  		"code2": "x&a=111",
   152  		"id":    "1+- ", // for fix
   153  		"f":     "1#a=+- ",
   154  		"v":     "",
   155  		"n":     "null",
   156  	}
   157  
   158  	gtest.C(t, func(t *gtest.T) {
   159  		res1 := httputil.BuildParams(params)
   160  		vs, _ := url.ParseQuery(res1)
   161  		t.Assert(len(params), len(vs))
   162  		for k := range vs {
   163  			vv := vs.Get(k)
   164  			_, ok := params[k]
   165  			// check no additional param
   166  			t.Assert(ok, true)
   167  			// check equal
   168  			t.AssertEQ(params[k], vv)
   169  		}
   170  	})
   171  }
   172  
   173  func Test_ServerSignal(t *testing.T) {
   174  	if runtime.GOOS == "windows" {
   175  		t.Log("skip windows")
   176  		return
   177  	}
   178  	s := g.Server(guid.S())
   179  	s.BindHandler("/", func(r *ghttp.Request) {
   180  		r.Response.Write("hello world")
   181  	})
   182  	gtest.Assert(s.Start(), nil)
   183  	g.Wait()
   184  	time.Sleep(100 * time.Millisecond)
   185  	gtest.C(t, func(t *gtest.T) {
   186  		t.AssertEQ(s.Shutdown(), nil)
   187  	})
   188  }