github.com/gogf/gf/v2@v2.7.4/net/ghttp/ghttp_z_unit_feature_ip_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  // static service testing.
     8  
     9  package ghttp_test
    10  
    11  import (
    12  	"fmt"
    13  	"testing"
    14  	"time"
    15  
    16  	"github.com/gogf/gf/v2/frame/g"
    17  	"github.com/gogf/gf/v2/net/ghttp"
    18  	"github.com/gogf/gf/v2/test/gtest"
    19  	"github.com/gogf/gf/v2/util/guid"
    20  )
    21  
    22  func TestRequest_GetRemoteIp(t *testing.T) {
    23  	gtest.C(t, func(t *gtest.T) {
    24  		s := g.Server(guid.S())
    25  		s.BindHandler("/", func(r *ghttp.Request) {
    26  			r.Response.Write(r.GetRemoteIp())
    27  		})
    28  		s.SetDumpRouterMap(false)
    29  		s.Start()
    30  		defer s.Shutdown()
    31  
    32  		time.Sleep(100 * time.Millisecond)
    33  
    34  		clientV4 := g.Client()
    35  		clientV4.SetPrefix(fmt.Sprintf("http://127.0.0.1:%d", s.GetListenedPort()))
    36  
    37  		clientV6 := g.Client()
    38  		clientV6.SetPrefix(fmt.Sprintf("http://[::1]:%d", s.GetListenedPort()))
    39  
    40  		t.Assert(clientV4.GetContent(ctx, "/"), "127.0.0.1")
    41  		t.Assert(clientV6.GetContent(ctx, "/"), "::1")
    42  	})
    43  }