-
Notifications
You must be signed in to change notification settings - Fork 4
/
main_test.go
194 lines (147 loc) · 4.49 KB
/
main_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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package main
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"github.com/gin-gonic/gin"
)
func getRouter() *gin.Engine {
router := gin.Default()
return router
}
// Test the main file
func TestMain(t *testing.T) {
go main()
}
// Test that a GET request to the home page returns the status page
func TestStatusPage(t *testing.T) {
// Create a response recorder
w := httptest.NewRecorder()
// Get a new router
r := getRouter()
// Define the route similar to its definition in the routes file
r.GET("/", Index)
// Create a request to send to the above route
req, _ := http.NewRequest("GET", "/", nil)
// Create the service and process the above request.
r.ServeHTTP(w, req)
// Test that the http status code is 200
if w.Code != http.StatusOK {
t.Fail()
}
// Test that the page contains `time`
p, err := ioutil.ReadAll(w.Body)
if err != nil || strings.Index(string(p), "time") < 0 {
t.Fail()
}
}
// Test that a GET returns a 404 when an invalid URL is requested
func Test404Page(t *testing.T) {
// Create a response recorder
w := httptest.NewRecorder()
// Get a new router
r := getRouter()
// Define the route similar to its definition in the routes file
r.GET("/qwerty", Index)
// Create a request to send to the above route
req, _ := http.NewRequest("GET", "/qwerty", nil)
// Create the service and process the above request.
r.ServeHTTP(w, req)
// Test that the http status code is 404
if w.Code != http.StatusNotFound {
t.Fail()
}
// Test that the page contains `404`
p, err := ioutil.ReadAll(w.Body)
if err != nil || strings.Index(string(p), "404") < 0 {
t.Fail()
}
}
// Test that a favicon.ico request returns a 404
func TestFaviconNotFound(t *testing.T) {
// Create a response recorder
w := httptest.NewRecorder()
// Get a new router
r := getRouter()
// Define the route similar to its definition in the routes file
r.GET("/favicon.ico", Index)
// Create a request to send to the above route
req, _ := http.NewRequest("GET", "/favicon.ico", nil)
// Create the service and process the above request.
r.ServeHTTP(w, req)
// Test that the http status code is 404
if w.Code != http.StatusNotFound {
t.Fail()
}
// Test that the page contains `404`
p, err := ioutil.ReadAll(w.Body)
if err != nil || strings.Index(string(p), "404") < 0 {
t.Fail()
}
}
// Test that a GET returns a 200 when an URL with a scheme is specified in the Path
func TestProxyRequestExistingPage(t *testing.T) {
// Create a response recorder
w := httptest.NewRecorder()
// Get a new router
r := getRouter()
// Define the route similar to its definition in the routes file
r.GET("/*path", Index)
// Create a request to send to the above route
req, _ := http.NewRequest("GET", "/https://www.google.com", nil)
// Create the service and process the above request.
r.ServeHTTP(w, req)
// Test that the http status code is 200
if w.Code != http.StatusOK {
t.Fail()
}
// Test that the page contains `Google`
p, err := ioutil.ReadAll(w.Body)
if err != nil || strings.Index(string(p), "Google") < 0 {
t.Fail()
}
}
// Test that a GET returns a 200 when a valid URL without scheme is specified in the Path
func TestProxyRequestExistingPageWithoutScheme(t *testing.T) {
// Create a response recorder
w := httptest.NewRecorder()
// Get a new router
r := getRouter()
// Define the route similar to its definition in the routes file
r.GET("/*path", Index)
// Create a request to send to the above route
req, _ := http.NewRequest("GET", "/www.google.com", nil)
// Create the service and process the above request.
r.ServeHTTP(w, req)
// Test that the http status code is 200
if w.Code != http.StatusOK {
t.Fail()
}
// Test that the page contains `Google`
p, err := ioutil.ReadAll(w.Body)
if err != nil || strings.Index(string(p), "Google") < 0 {
t.Fail()
}
}
// Test that a GET returns a 200 when a valid PATH matches the configuration set
func TestProxyRequestWithAPathMappingMatch(t *testing.T) {
os.Setenv("PATH_MAPPING", "{\"google\": \"https://www.google.com\"}")
// Create a response recorder
w := httptest.NewRecorder()
// Get a new router
r := getRouter()
// Define the route similar to its definition in the routes file
r.GET("/*path", Index)
// Create a request to send to the above route
req, _ := http.NewRequest("GET", "/google/?q=test", nil)
// Create the service and process the above request.
r.ServeHTTP(w, req)
// Test that the http status code is 200
if w.Code != http.StatusOK {
t.Fail()
}
os.Unsetenv("PATH_MAPPING")
}