This repository has been archived by the owner on May 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrequest_functions_test.go
80 lines (64 loc) · 2.6 KB
/
request_functions_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
package httpx
import (
"bytes"
"io"
"net/http"
"net/http/httptest"
"net/http/httputil"
"testing"
"github.com/smartystreets/assertions/should"
"github.com/smartystreets/gunit"
)
func TestReadClientAddressFixture(t *testing.T) {
gunit.Run(new(ReadClientAddressFixture), t)
}
type ReadClientAddressFixture struct {
*gunit.Fixture
request *http.Request
}
func (this *ReadClientAddressFixture) TestCorrectRemoteIPAddressSetOnRequest() {
this.assertOriginIP("[::1]:81", "", "::1")
this.assertOriginIP("[::1]:80", "1.2.3.4", "1.2.3.4")
this.assertOriginIP("1.2.3.4:1234", "", "1.2.3.4")
this.assertOriginIP("1.2.3.4:1234", "::1, 5.6.7.8", "5.6.7.8")
this.assertOriginIP("1.2.3.4:1234", "a.b.c.d, e.f.g.h, i.j.k.l", "i.j.k.l")
}
func (this *ReadClientAddressFixture) assertOriginIP(remoteAddress, forwardedAddress, expectedAddress string) {
this.setupRemoteRequest(remoteAddress, forwardedAddress)
this.So(ReadClientIPAddress(this.request, ""), should.Equal, expectedAddress)
}
func (this *ReadClientAddressFixture) setupRemoteRequest(remoteAddress, forwardedAddress string) {
this.request = httptest.NewRequest("GET", "/", nil)
this.request.RemoteAddr = remoteAddress
WriteHeader(this.request, HeaderXForwardedFor, forwardedAddress)
}
func (this *ReadClientAddressFixture) TestPreferTrustedHeaderForIPAddressWhenAvailable() {
this.setupRemoteRequest("1.2.3.4", "5.6.7.8")
WriteHeader(this.request, "X-Remote-Address", "a.b.c.d")
this.So(ReadClientIPAddress(this.request, "X-Remote-Address"), should.Equal, "a.b.c.d")
}
func (this *ReadClientAddressFixture) TestReadCorrectSourceAddress() {
this.request = httptest.NewRequest("GET", "/", nil)
this.request.RemoteAddr = "1.1.1.1:1234"
WriteHeader(this.request, "Via", "some value")
this.So(ReadClientIPAddress(this.request, "X-Remote-Address"), should.Equal, "1.1.1.1")
}
/////////////////////////////////////////////////////////////
func TestRequestFixture(t *testing.T) {
gunit.Run(new(RequestFixture), t)
}
type RequestFixture struct {
*gunit.Fixture
}
func (this *RequestFixture) TestCalculateRequestBodySize() {
buffer := bytes.NewBuffer([]byte{})
io.WriteString(buffer, "Line 1\n")
io.WriteString(buffer, "Line 2\n")
io.WriteString(buffer, "Line 3")
request, _ := http.NewRequest("POST", "https://user:pass@domain.com:1234/path/doc?query=value", buffer)
request.Header["Host"] = []string{"domain.com:1234"}
request.Header["User-Agent"] = []string{"my user agent"}
request.Header["Content-Type"] = []string{"application/json", "application/xml"}
raw, _ := httputil.DumpRequest(request, true)
this.So(CalculateRequestSize(request), should.Equal, len(raw))
}