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