github.com/blend/go-sdk@v1.20220411.3/webutil/get_remoteaddr_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package webutil
     9  
    10  import (
    11  	"net/http"
    12  	"testing"
    13  
    14  	"github.com/blend/go-sdk/assert"
    15  )
    16  
    17  func TestGetRemoteAddr(t *testing.T) {
    18  	assert := assert.New(t)
    19  
    20  	hdr := http.Header{}
    21  	hdr.Set("X-Forwarded-For", "123")
    22  	r := http.Request{
    23  		Header: hdr,
    24  	}
    25  	assert.Equal("123", GetRemoteAddr(&r))
    26  
    27  	hdr = http.Header{}
    28  	hdr.Set("X-FORWARDED-FOR", "1")
    29  	r = http.Request{
    30  		Header: hdr,
    31  	}
    32  	assert.Equal("1", GetRemoteAddr(&r))
    33  
    34  	hdr = http.Header{}
    35  	hdr.Set("X-FORWARDED-FOR", "1,2,3")
    36  	r = http.Request{
    37  		Header: hdr,
    38  	}
    39  	assert.Equal("3", GetRemoteAddr(&r))
    40  
    41  	hdr = http.Header{}
    42  	hdr.Set("X-Real-Ip", "1")
    43  	r = http.Request{
    44  		Header: hdr,
    45  	}
    46  	assert.Equal("1", GetRemoteAddr(&r))
    47  
    48  	hdr = http.Header{}
    49  	hdr.Set("X-REAL-IP", "1")
    50  	r = http.Request{
    51  		Header: hdr,
    52  	}
    53  	assert.Equal("1", GetRemoteAddr(&r))
    54  
    55  	hdr = http.Header{}
    56  	hdr.Set("X-REAL-IP", "1,2,3")
    57  	r = http.Request{
    58  		Header: hdr,
    59  	}
    60  	assert.Equal("3", GetRemoteAddr(&r))
    61  
    62  	r = http.Request{
    63  		RemoteAddr: "1:1",
    64  	}
    65  	assert.Equal("1", GetRemoteAddr(&r))
    66  
    67  	r = http.Request{
    68  		RemoteAddr: "1",
    69  	}
    70  	assert.Equal("", GetRemoteAddr(&r))
    71  }