-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
main_test.go
232 lines (220 loc) · 6.67 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package main
import (
"bytes"
"context"
"fmt"
"github.com/google/go-cmp/cmp"
"net/http"
"net/http/httptest"
"os"
"testing"
)
// args - required arguments for any request to serve
type args struct {
w *httptest.ResponseRecorder
r *http.Request
// a dummy server
srv *http.Server
}
// resChecker - response checker for checking test response
type resChecker struct {
code int
body string
// set noBodyCheck to true if do not want to check body
noBodyCheck bool
// list header keys which should be present in response
headers []string
}
// defineTest - defines a single unit test
type defineTest struct {
name string
args *args
resChr *resChecker
setup func(*args, *resChecker)
}
func changeStdOut(s string) *os.File {
tmp := os.Stdout
l, _ := os.Create("./bin/logs_" + s)
os.Stdout = l
return tmp
}
func resetStdOut(tmp *os.File) {
os.Stdout = tmp
}
// NOTE: Test_RootRequest should be changed after the error for Root
// request is successfully replaced with the documentation or landing
// page. Follow Issue: Shivam010/bypass-cors#3
func Test_RootRequest(t *testing.T) {
tmp := changeStdOut(t.Name())
defer resetStdOut(tmp)
test := defineTest{
name: "Root Request",
args: &args{
w: httptest.NewRecorder(),
r: nil,
},
resChr: &resChecker{},
setup: func(ar *args, rc *resChecker) {
ar.r, _ = http.NewRequest("GET", "/", &bytes.Buffer{})
rc.code = http.StatusPreconditionFailed
rc.body = `{"error":{"Code":412,"Message":"URL not provided","Detail":{"method":"GET","requestedURL":"/"}}}` + "\n"
},
}
t.Run(test.name, func(t *testing.T) {
ha := &handler{}
test.setup(test.args, test.resChr)
ha.ServeHTTP(test.args.w, test.args.r)
if test.args.w.Code != test.resChr.code {
t.Fatalf("Status code mismatched got: %v, want: %v", test.args.w.Code, test.resChr.code)
}
if !test.resChr.noBodyCheck && !cmp.Equal(test.args.w.Body.String(), test.resChr.body) {
t.Fatalf("Body mismatched got: %s, want: %s", test.args.w.Body.String(), test.resChr.body)
}
})
}
func Test_Success(t *testing.T) {
tmp := changeStdOut(t.Name())
defer resetStdOut(tmp)
tests := []defineTest{
{
name: "GET-Request",
args: &args{
w: httptest.NewRecorder(),
r: nil,
srv: &http.Server{Addr: ":8181", Handler: http.NotFoundHandler()},
},
resChr: &resChecker{
headers: []string{
VaryHeader, QuoteHeader,
AllowOrigin, AllowCredentials,
},
},
setup: func(ar *args, rc *resChecker) {
ar.srv.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = fmt.Fprintf(w, "Success")
})
go ar.srv.ListenAndServe()
ar.r, _ = http.NewRequest("GET", "/localhost"+ar.srv.Addr, &bytes.Buffer{})
rc.code = http.StatusOK
rc.body = fmt.Sprintln("Success")
},
},
{
name: "OPTIONS-Request",
args: &args{
w: httptest.NewRecorder(),
r: nil,
srv: &http.Server{Addr: ":8181", Handler: http.NotFoundHandler()},
},
resChr: &resChecker{
headers: []string{
VaryHeader, QuoteHeader,
AllowOrigin, AllowCredentials,
},
},
setup: func(ar *args, rc *resChecker) {
ar.srv.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = fmt.Fprintf(w, "Success")
})
go ar.srv.ListenAndServe()
ar.r, _ = http.NewRequest("OPTIONS", "/localhost"+ar.srv.Addr, &bytes.Buffer{})
rc.code = http.StatusOK
rc.body = fmt.Sprintln("Success")
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.setup(tt.args, tt.resChr)
ctx, cancel := context.WithTimeout(context.Background(), 1000)
defer cancel()
defer tt.args.srv.Shutdown(ctx)
ha := &handler{}
ha.ServeHTTP(tt.args.w, tt.args.r)
if tt.args.w.Code != tt.resChr.code {
t.Fatalf("Status code mismatched got: %v, want: %v", tt.args.w.Code, tt.resChr.code)
}
if !tt.resChr.noBodyCheck && !cmp.Equal(tt.args.w.Body.String(), tt.resChr.body) {
t.Fatalf("Body mismatched got: %s, want: %s", tt.args.w.Body.String(), tt.resChr.body)
}
})
}
}
func Test_OtherRequests(t *testing.T) {
tmp := changeStdOut(t.Name())
defer resetStdOut(tmp)
tests := []defineTest{
{
name: "Can not Process",
args: &args{
w: httptest.NewRecorder(),
r: nil,
},
resChr: &resChecker{},
setup: func(ar *args, rc *resChecker) {
ar.r, _ = http.NewRequest("GET", "/invalid-request", &bytes.Buffer{})
rc.code = http.StatusUnprocessableEntity
// Note: the error message `Get http://invalid-request: dial tcp: lookup invalid-request: no such host`
// varies from environment to environment and hence, omitting the check
rc.noBodyCheck = true
//rc.body = `{"error":{"Code":422,"Message":"Get http://invalid-request: dial tcp: lookup invalid-request: no such host","Detail":{"body":"","method":"GET","requestedURL":"http://invalid-request","response":null}}}` + "\n"
},
},
{
name: "Invalid Request",
args: &args{
w: httptest.NewRecorder(),
r: nil,
},
resChr: &resChecker{},
setup: func(ar *args, rc *resChecker) {
ar.r, _ = http.NewRequest("GET", "", &bytes.Buffer{})
ar.r.URL.Path = "%invalid%"
rc.code = http.StatusPreconditionFailed
rc.body = `{"error":{"Code":412,"Message":"parse http://invalid%: invalid URL escape \"%\"","Detail":{"method":"GET","requestedURL":"http://invalid%"}}}` + "\n"
},
},
{
name: "URL not Provided",
args: &args{
w: httptest.NewRecorder(),
r: nil,
},
resChr: &resChecker{},
setup: func(ar *args, rc *resChecker) {
ar.r, _ = http.NewRequest("GET", "", &bytes.Buffer{})
rc.code = http.StatusPreconditionFailed
rc.body = `{"error":{"Code":412,"Message":"URL not provided","Detail":{"method":"GET","requestedURL":""}}}` + "\n"
},
},
{
name: "Invalid Method",
args: &args{
w: httptest.NewRecorder(),
r: nil,
},
resChr: &resChecker{},
setup: func(ar *args, rc *resChecker) {
ar.r, _ = http.NewRequest("GET", "/localhost", &bytes.Buffer{})
ar.r.Method += "/"
rc.code = http.StatusPreconditionFailed
rc.body = `{"error":{"Code":412,"Message":"net/http: invalid method \"GET/\"","Detail":{"body":"","method":"GET/","requestedURL":"http://localhost"}}}` + "\n"
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ha := &handler{}
tt.setup(tt.args, tt.resChr)
ha.ServeHTTP(tt.args.w, tt.args.r)
if tt.args.w.Code != tt.resChr.code {
t.Fatalf("Status code mismatched got: %v, want: %v", tt.args.w.Code, tt.resChr.code)
}
if !tt.resChr.noBodyCheck && !cmp.Equal(tt.args.w.Body.String(), tt.resChr.body) {
t.Fatalf("Body mismatched got: %s, want: %s", tt.args.w.Body.String(), tt.resChr.body)
}
})
}
}