-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils_test.go
More file actions
93 lines (80 loc) · 1.73 KB
/
Copy pathutils_test.go
File metadata and controls
93 lines (80 loc) · 1.73 KB
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
83
84
85
86
87
88
89
90
91
92
93
package dhttp
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestRealIP(t *testing.T) {
tests := []struct {
name string
header http.Header
modifier func(r *http.Request)
expected string
}{
{
"no x-forward-for header",
http.Header{},
nil,
"192.0.2.1",
},
{
"x-forward-for header, 1 IP", http.Header{
"X-Forwarded-For": []string{"1.1.1.1"},
},
nil,
"192.0.2.1",
},
{
"x-forward-for header, 2 IP", http.Header{
"X-Forwarded-For": []string{"1.1.1.1,2.2.2.2"},
},
nil,
"1.1.1.1",
},
{
"x-forward-for header, 3 IP", http.Header{
"X-Forwarded-For": []string{"1.1.1.1,2.2.2.2,3.3.3.3"},
},
nil,
"2.2.2.2",
},
{
"x-forward-for header, 5 IP", http.Header{
"X-Forwarded-For": []string{"1.1.1.1,2.2.2.2,3.3.3.3,4.4.4.4"},
},
nil,
"3.3.3.3",
},
{
"no x-forward-for, no remote address somehow",
nil,
func(r *http.Request) {
r.RemoteAddr = ""
},
"",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
request := httptest.NewRequest("GET", "/", strings.NewReader("test"))
request.Header = test.header
if test.modifier != nil {
test.modifier(request)
}
assert.Equal(t, test.expected, RealIP(request))
})
}
}
func Test_FowardErrorResponse(t *testing.T) {
recorder := httptest.NewRecorder()
request := httptest.NewRequest("GET", "/", strings.NewReader("test"))
response := http.Response{}
response.Body = ioutil.NopCloser(strings.NewReader("body"))
response.StatusCode = 512
FowardResponse(request.Context(), recorder, &response)
assert.Equal(t, 512, recorder.Code)
assert.Equal(t, "body", recorder.Body.String())
}