github.com/xzl8028/xenia-server@v0.0.0-20190809101854-18450a97da63/utils/utils_test.go (about) 1 // Copyright (c) 2016-present Xenia, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package utils 5 6 import ( 7 "net/http" 8 "testing" 9 10 "github.com/stretchr/testify/assert" 11 ) 12 13 func TestStringArrayIntersection(t *testing.T) { 14 a := []string{ 15 "abc", 16 "def", 17 "ghi", 18 } 19 b := []string{ 20 "jkl", 21 } 22 c := []string{ 23 "def", 24 } 25 26 if len(StringArrayIntersection(a, b)) != 0 { 27 t.Fatal("should be 0") 28 } 29 30 if len(StringArrayIntersection(a, c)) != 1 { 31 t.Fatal("should be 1") 32 } 33 } 34 35 func TestRemoveDuplicatesFromStringArray(t *testing.T) { 36 a := []string{ 37 "a", 38 "b", 39 "a", 40 "a", 41 "b", 42 "c", 43 "a", 44 } 45 46 if len(RemoveDuplicatesFromStringArray(a)) != 3 { 47 t.Fatal("should be 3") 48 } 49 } 50 51 func TestStringSliceDiff(t *testing.T) { 52 a := []string{"one", "two", "three", "four", "five", "six"} 53 b := []string{"two", "seven", "four", "six"} 54 expected := []string{"one", "three", "five"} 55 56 assert.Equal(t, StringSliceDiff(a, b), expected) 57 } 58 59 func TestGetIpAddress(t *testing.T) { 60 // Test with a single IP in the X-Forwarded-For 61 httpRequest1 := http.Request{ 62 Header: http.Header{ 63 "X-Forwarded-For": []string{"10.0.0.1"}, 64 "X-Real-Ip": []string{"10.1.0.1"}, 65 }, 66 RemoteAddr: "10.2.0.1:12345", 67 } 68 69 assert.Equal(t, "10.0.0.1", GetIpAddress(&httpRequest1, []string{"X-Forwarded-For"})) 70 71 // Test with multiple IPs in the X-Forwarded-For 72 httpRequest2 := http.Request{ 73 Header: http.Header{ 74 "X-Forwarded-For": []string{"10.0.0.1, 10.0.0.2, 10.0.0.3"}, 75 "X-Real-Ip": []string{"10.1.0.1"}, 76 }, 77 RemoteAddr: "10.2.0.1:12345", 78 } 79 80 assert.Equal(t, "10.0.0.1", GetIpAddress(&httpRequest2, []string{"X-Forwarded-For"})) 81 82 // Test with an empty X-Forwarded-For 83 httpRequest3 := http.Request{ 84 Header: http.Header{ 85 "X-Forwarded-For": []string{""}, 86 "X-Real-Ip": []string{"10.1.0.1"}, 87 }, 88 RemoteAddr: "10.2.0.1:12345", 89 } 90 91 assert.Equal(t, "10.1.0.1", GetIpAddress(&httpRequest3, []string{"X-Forwarded-For", "X-Real-Ip"})) 92 93 // Test without an X-Fowarded-For 94 httpRequest4 := http.Request{ 95 Header: http.Header{ 96 "X-Real-Ip": []string{"10.1.0.1"}, 97 }, 98 RemoteAddr: "10.2.0.1:12345", 99 } 100 101 assert.Equal(t, "10.1.0.1", GetIpAddress(&httpRequest4, []string{"X-Forwarded-For", "X-Real-Ip"})) 102 103 // Test without any headers 104 httpRequest5 := http.Request{ 105 RemoteAddr: "10.2.0.1:12345", 106 } 107 108 assert.Equal(t, "10.2.0.1", GetIpAddress(&httpRequest5, []string{"X-Forwarded-For", "X-Real-Ip"})) 109 110 // Test with both headers, but both untrusted 111 httpRequest6 := http.Request{ 112 Header: http.Header{ 113 "X-Forwarded-For": []string{"10.3.0.1"}, 114 "X-Real-Ip": []string{"10.1.0.1"}, 115 }, 116 RemoteAddr: "10.2.0.1:12345", 117 } 118 119 assert.Equal(t, "10.2.0.1", GetIpAddress(&httpRequest6, nil)) 120 121 // Test with both headers, but only X-Real-Ip trusted 122 httpRequest7 := http.Request{ 123 Header: http.Header{ 124 "X-Forwarded-For": []string{"10.3.0.1"}, 125 "X-Real-Ip": []string{"10.1.0.1"}, 126 }, 127 RemoteAddr: "10.2.0.1:12345", 128 } 129 130 assert.Equal(t, "10.1.0.1", GetIpAddress(&httpRequest7, []string{"X-Real-Ip"})) 131 132 // Test with X-Forwarded-For, comma separated, untrusted 133 httpRequest8 := http.Request{ 134 Header: http.Header{ 135 "X-Forwarded-For": []string{"10.3.0.1, 10.1.0.1"}, 136 }, 137 RemoteAddr: "10.2.0.1:12345", 138 } 139 140 assert.Equal(t, "10.2.0.1", GetIpAddress(&httpRequest8, nil)) 141 142 // Test with X-Forwarded-For, comma separated, untrusted 143 httpRequest9 := http.Request{ 144 Header: http.Header{ 145 "X-Forwarded-For": []string{"10.3.0.1, 10.1.0.1"}, 146 }, 147 RemoteAddr: "10.2.0.1:12345", 148 } 149 150 assert.Equal(t, "10.3.0.1", GetIpAddress(&httpRequest9, []string{"X-Forwarded-For"})) 151 152 // Test with both headers, both allowed, first one in trusted used 153 httpRequest10 := http.Request{ 154 Header: http.Header{ 155 "X-Forwarded-For": []string{"10.3.0.1"}, 156 "X-Real-Ip": []string{"10.1.0.1"}, 157 }, 158 RemoteAddr: "10.2.0.1:12345", 159 } 160 161 assert.Equal(t, "10.1.0.1", GetIpAddress(&httpRequest10, []string{"X-Real-Ip", "X-Forwarded-For"})) 162 }