-
Notifications
You must be signed in to change notification settings - Fork 126
/
xff_header_test.go
82 lines (76 loc) · 3.53 KB
/
xff_header_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package main
import (
"net/http"
"testing"
)
func TestNoHeaderProvided(t *testing.T) {
adserverRequest, _ := http.NewRequest("GET", "ad1/test", nil)
adserverRequest.RemoteAddr = "192.168.0.1:80"
updateForwardedHeaders(adserverRequest)
var xffHeader = adserverRequest.Header.Get("X-FORWARDED-FOR")
var forwardedHeader = adserverRequest.Header.Get("FORWARDED")
if expectation := "192.168.0.1"; xffHeader != expectation {
t.Errorf("Expected ''%s'', but received ''%s''", expectation, xffHeader)
}
if expectation := "for=192.168.0.1"; forwardedHeader != expectation {
t.Errorf("Expected '%s', but received '%s'", expectation, forwardedHeader)
}
}
func TestOnlyXFFProvided(t *testing.T) {
adserverRequest, _ := http.NewRequest("GET", "ad1/test", nil)
adserverRequest.RemoteAddr = "192.168.0.1:80"
adserverRequest.Header.Add("X-FORWARDED-FOR", "172.20.2.5")
updateForwardedHeaders(adserverRequest)
var xffHeader = adserverRequest.Header.Get("X-FORWARDED-FOR")
var forwardedHeader = adserverRequest.Header.Get("FORWARDED")
if expectation := "172.20.2.5, 192.168.0.1"; xffHeader != expectation {
t.Errorf("Expected '%s', but received '%s'", expectation, xffHeader)
}
if expectation := "for=192.168.0.1"; forwardedHeader != expectation {
t.Errorf("Expected '%s', but received '%s'", expectation, forwardedHeader)
}
}
func TestOnlyForwardedProvided(t *testing.T) {
adserverRequest, _ := http.NewRequest("GET", "ad1/test", nil)
adserverRequest.RemoteAddr = "192.168.0.1:80"
adserverRequest.Header.Add("FORWARDED", "for=172.20.2.5")
updateForwardedHeaders(adserverRequest)
var xffHeader = adserverRequest.Header.Get("X-FORWARDED-FOR")
var forwardedHeader = adserverRequest.Header.Get("FORWARDED")
if expectation := "192.168.0.1"; xffHeader != expectation {
t.Errorf("Expected '%s', but received '%s'", expectation, xffHeader)
}
if expectation := "for=172.20.2.5, for=192.168.0.1"; forwardedHeader != expectation {
t.Errorf("Expected '%s', but received '%s'", expectation, forwardedHeader)
}
}
func TestBothProvided(t *testing.T) {
adserverRequest, _ := http.NewRequest("GET", "ad1/test", nil)
adserverRequest.RemoteAddr = "192.168.0.1:80"
adserverRequest.Header.Add("FORWARDED", "for=172.20.2.5")
adserverRequest.Header.Add("X-FORWARDED-FOR", "172.20.2.5")
updateForwardedHeaders(adserverRequest)
var xffHeader = adserverRequest.Header.Get("X-FORWARDED-FOR")
var forwardedHeader = adserverRequest.Header.Get("FORWARDED")
if expectation := "172.20.2.5, 192.168.0.1"; xffHeader != expectation {
t.Errorf("Expected '%s', but received '%s'", expectation, xffHeader)
}
if expectation := "for=172.20.2.5, for=192.168.0.1"; forwardedHeader != expectation {
t.Errorf("Expected '%s', but received '%s'", expectation, forwardedHeader)
}
}
func TestBothProvidedWithMoreProxies(t *testing.T) {
adserverRequest, _ := http.NewRequest("GET", "ad1/test", nil)
adserverRequest.RemoteAddr = "192.168.0.15:80"
adserverRequest.Header.Add("FORWARDED", "for=172.20.2.5, for=172.20.2.36")
adserverRequest.Header.Add("X-FORWARDED-FOR", "172.20.2.5, 172.20.2.36")
updateForwardedHeaders(adserverRequest)
var xffHeader = adserverRequest.Header.Get("X-FORWARDED-FOR")
var forwardedHeader = adserverRequest.Header.Get("FORWARDED")
if expectation := "172.20.2.5, 172.20.2.36, 192.168.0.15"; xffHeader != expectation {
t.Errorf("Expected '%s', but received '%s'", expectation, xffHeader)
}
if expectation := "for=172.20.2.5, for=172.20.2.36, for=192.168.0.15"; forwardedHeader != expectation {
t.Errorf("Expected '%s', but received '%s'", expectation, forwardedHeader)
}
}