github.com/v2fly/v2ray-core/v4@v4.45.2/common/protocol/http/headers_test.go (about)

     1  package http_test
     2  
     3  import (
     4  	"bufio"
     5  	"net/http"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/google/go-cmp/cmp"
    10  
    11  	"github.com/v2fly/v2ray-core/v4/common"
    12  	"github.com/v2fly/v2ray-core/v4/common/net"
    13  	. "github.com/v2fly/v2ray-core/v4/common/protocol/http"
    14  )
    15  
    16  func TestParseXForwardedFor(t *testing.T) {
    17  	header := http.Header{}
    18  	header.Add("X-Forwarded-For", "129.78.138.66, 129.78.64.103")
    19  	addrs := ParseXForwardedFor(header)
    20  	if r := cmp.Diff(addrs, []net.Address{net.ParseAddress("129.78.138.66"), net.ParseAddress("129.78.64.103")}); r != "" {
    21  		t.Error(r)
    22  	}
    23  }
    24  
    25  func TestHopByHopHeadersRemoving(t *testing.T) {
    26  	rawRequest := `GET /pkg/net/http/ HTTP/1.1
    27  Host: golang.org
    28  Connection: keep-alive,Foo, Bar
    29  Foo: foo
    30  Bar: bar
    31  Proxy-Connection: keep-alive
    32  Proxy-Authenticate: abc
    33  Accept-Encoding: gzip
    34  Accept-Charset: ISO-8859-1,UTF-8;q=0.7,*;q=0.7
    35  Cache-Control: no-cache
    36  Accept-Language: de,en;q=0.7,en-us;q=0.3
    37  
    38  `
    39  	b := bufio.NewReader(strings.NewReader(rawRequest))
    40  	req, err := http.ReadRequest(b)
    41  	common.Must(err)
    42  	headers := []struct {
    43  		Key   string
    44  		Value string
    45  	}{
    46  		{
    47  			Key:   "Foo",
    48  			Value: "foo",
    49  		},
    50  		{
    51  			Key:   "Bar",
    52  			Value: "bar",
    53  		},
    54  		{
    55  			Key:   "Connection",
    56  			Value: "keep-alive,Foo, Bar",
    57  		},
    58  		{
    59  			Key:   "Proxy-Connection",
    60  			Value: "keep-alive",
    61  		},
    62  		{
    63  			Key:   "Proxy-Authenticate",
    64  			Value: "abc",
    65  		},
    66  	}
    67  	for _, header := range headers {
    68  		if v := req.Header.Get(header.Key); v != header.Value {
    69  			t.Error("header ", header.Key, " = ", v, " want ", header.Value)
    70  		}
    71  	}
    72  
    73  	RemoveHopByHopHeaders(req.Header)
    74  
    75  	for _, header := range []string{"Connection", "Foo", "Bar", "Proxy-Connection", "Proxy-Authenticate"} {
    76  		if v := req.Header.Get(header); v != "" {
    77  			t.Error("header ", header, " = ", v)
    78  		}
    79  	}
    80  }
    81  
    82  func TestParseHost(t *testing.T) {
    83  	testCases := []struct {
    84  		RawHost     string
    85  		DefaultPort net.Port
    86  		Destination net.Destination
    87  		Error       bool
    88  	}{
    89  		{
    90  			RawHost:     "v2fly.org:80",
    91  			DefaultPort: 443,
    92  			Destination: net.TCPDestination(net.DomainAddress("v2fly.org"), 80),
    93  		},
    94  		{
    95  			RawHost:     "tls.v2fly.org",
    96  			DefaultPort: 443,
    97  			Destination: net.TCPDestination(net.DomainAddress("tls.v2fly.org"), 443),
    98  		},
    99  		{
   100  			RawHost:     "[2401:1bc0:51f0:ec08::1]:80",
   101  			DefaultPort: 443,
   102  			Destination: net.TCPDestination(net.ParseAddress("[2401:1bc0:51f0:ec08::1]"), 80),
   103  		},
   104  	}
   105  
   106  	for _, testCase := range testCases {
   107  		dest, err := ParseHost(testCase.RawHost, testCase.DefaultPort)
   108  		if testCase.Error {
   109  			if err == nil {
   110  				t.Error("for test case: ", testCase.RawHost, " expected error, but actually nil")
   111  			}
   112  		} else {
   113  			if dest != testCase.Destination {
   114  				t.Error("for test case: ", testCase.RawHost, " expected host: ", testCase.Destination.String(), " but got ", dest.String())
   115  			}
   116  		}
   117  	}
   118  }