-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproxy_test.go
147 lines (131 loc) · 4.26 KB
/
proxy_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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
var (
clientAnon = &http.Client{Transport: &http.Transport{
Proxy: func(req *http.Request) (*url.URL, error) {
return url.Parse(testServer.URL)
},
}}
clientInvalid = &http.Client{Transport: &http.Transport{
Proxy: func(req *http.Request) (*url.URL, error) {
u, err := url.Parse(testServer.URL)
u.User = url.UserPassword(githubTestLogin, "foobar")
return u, err
},
}}
clientSuccess = &http.Client{Transport: &http.Transport{
Proxy: func(req *http.Request) (*url.URL, error) {
u, err := url.Parse(testServer.URL)
u.User = url.UserPassword(githubTestLogin, githubTestToken)
return u, err
},
}}
testServer = httptest.NewServer(proxy)
)
func TestProxyAnonHTTP(t *testing.T) {
request, err := http.NewRequest("GET", "http://httpbin.org/ip", nil)
assert.Nil(t, err)
response, err := clientAnon.Do(request)
assert.NoError(t, err)
assert.Equal(t, 407, response.StatusCode)
body, err := ioutil.ReadAll(response.Body)
assert.NoError(t, err)
assert.Equal(t, statusProxyAuthReq, string(body))
}
func TestProxyAnonHTTPS(t *testing.T) {
request, err := http.NewRequest("GET", "https://httpbin.org/ip", nil)
assert.Nil(t, err)
response, err := clientAnon.Do(request)
assert.Error(t, err)
assert.Nil(t, response)
}
func TestProxyFailDirect(t *testing.T) {
resp, err := http.Get(testServer.URL)
assert.NoError(t, err)
assert.Equal(t, 400, resp.StatusCode)
assert.Equal(t, "400 Bad Request", resp.Status)
}
func TestProxyInvalidAuth(t *testing.T) {
request, err := http.NewRequest("GET", "http://httpbin.org/ip", nil)
request.Header.Set("Proxy-Authorization", "token invalid")
assert.Nil(t, err)
response, err := clientInvalid.Do(request)
assert.NoError(t, err)
assert.Equal(t, 407, response.StatusCode)
body, err := ioutil.ReadAll(response.Body)
assert.NoError(t, err)
assert.Equal(t, statusProxyAuthReq, string(body))
}
func TestProxyInvalidHTTP(t *testing.T) {
request, err := http.NewRequest("GET", "http://httpbin.org/ip", nil)
assert.Nil(t, err)
response, err := clientInvalid.Do(request)
assert.NoError(t, err)
assert.Equal(t, 407, response.StatusCode)
body, err := ioutil.ReadAll(response.Body)
assert.NoError(t, err)
assert.Equal(t, statusProxyAuthReq, string(body))
}
func TestProxyInvalidHTTPS(t *testing.T) {
request, err := http.NewRequest("GET", "https://httpbin.org/ip", nil)
assert.Nil(t, err)
response, err := clientInvalid.Do(request)
assert.Error(t, err)
assert.Nil(t, response)
}
func TestProxySuccess0(t *testing.T) {
duoAuthAuth = duoAuthOK
}
func TestProxySuccess1(t *testing.T) {
request, err := http.NewRequest("GET", "http://httpbin.org/ip", nil)
request.Header.Set("X-Forwarded-For", "127.127.127.127")
assert.Nil(t, err)
response, err := clientSuccess.Do(request)
assert.NoError(t, err)
assert.Equal(t, 200, response.StatusCode)
body, err := ioutil.ReadAll(response.Body)
assert.NoError(t, err)
assert.Equal(t, "{\n \"origin\"", strings.Split(string(body), ":")[0])
}
func TestProxySuccess2(t *testing.T) {
request, err := http.NewRequest("GET", "http://httpbin.org/ip", nil)
assert.Nil(t, err)
response, err := clientSuccess.Do(request)
assert.NoError(t, err)
assert.Equal(t, 200, response.StatusCode)
body, err := ioutil.ReadAll(response.Body)
assert.NoError(t, err)
assert.Equal(t, "{\n \"origin\"", strings.Split(string(body), ":")[0])
}
func TestProxySuccess3(t *testing.T) {
request, err := http.NewRequest("GET", "http://httpbin.org/ip", nil)
assert.Nil(t, err)
response, err := clientSuccess.Do(request)
assert.NoError(t, err)
assert.Equal(t, 200, response.StatusCode)
body, err := ioutil.ReadAll(response.Body)
assert.NoError(t, err)
assert.Equal(t, "{\n \"origin\"", strings.Split(string(body), ":")[0])
}
func TestProxySuccessFinal(t *testing.T) {
duoAuthAuth = duoAuthDeny
*duoSkip = true
}
func TestProxySuccessHTTPS(t *testing.T) {
request, err := http.NewRequest("GET", "https://httpbin.org/ip", nil)
assert.Nil(t, err)
response, err := clientSuccess.Do(request)
assert.NoError(t, err)
assert.Equal(t, 200, response.StatusCode)
body, err := ioutil.ReadAll(response.Body)
assert.NoError(t, err)
assert.Equal(t, "{\n \"origin\"", strings.Split(string(body), ":")[0])
}