-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler_test.go
More file actions
136 lines (124 loc) · 3.55 KB
/
handler_test.go
File metadata and controls
136 lines (124 loc) · 3.55 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
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
/*
Copyright 2021 Mark Wilkerson
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>
*/
package main
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
)
func TestHandler(t *testing.T) {
tt := []struct {
label string
input string
method string
want string
statuscode int
debug bool
}{
{
label: "Valid request",
input: "https://whatver.biz:8080/foo?go-get=1",
method: http.MethodGet,
want: `<meta name="go-import" content="whatver.biz/foo git test.com/foo.git">`,
statuscode: http.StatusOK,
debug: true,
},
{
label: "Valid request, nested path",
input: "https://whatver.biz:8080/foo/bar/baz?go-get=1",
method: http.MethodGet,
want: `<meta name="go-import" content="whatver.biz/foo/bar/baz git test.com/foo/bar/baz.git">`,
statuscode: http.StatusOK,
debug: true,
},
{
label: "Valid URI, bad method: POST",
input: "https://whatver.biz:8080/foo?go-get=1",
method: http.MethodPost,
want: "",
statuscode: http.StatusMethodNotAllowed,
},
{
label: "Valid URI, bad method: PUT",
input: "https://whatver.biz:8080/foo?go-get=1",
method: http.MethodPut,
want: "",
statuscode: http.StatusMethodNotAllowed,
},
{
label: "Valid URI, bad method: HEAD",
input: "https://whatver.biz:8080/foo?go-get=1",
method: http.MethodHead,
want: "",
statuscode: http.StatusMethodNotAllowed,
},
{
label: "Valid URI, bad method: DELETE",
input: "https://whatver.biz:8080/foo?go-get=1",
method: http.MethodDelete,
want: "",
statuscode: http.StatusMethodNotAllowed,
},
{
label: "Wrong go-get value",
input: "https://whatver.biz:8080/foo?go-get=",
method: http.MethodGet,
want: "",
statuscode: http.StatusNotFound,
},
{
label: "Missing go-get",
input: "https://whatver.biz:8080/foo?go-away=1",
method: http.MethodGet,
want: "",
statuscode: http.StatusNotFound,
},
{
label: "No query params",
input: "https://whatver.biz:8080/foo",
method: http.MethodGet,
want: "",
statuscode: http.StatusNotFound,
},
}
for _, tc := range tt {
t.Run(tc.label, func(t *testing.T) {
opts := options{
BindAddr: "0.0.0.0",
Port: 8080,
Dest: "test.com",
Debug: tc.debug,
}
h := handler(opts)
req, err := http.NewRequest(tc.method, tc.input, nil)
if err != nil {
t.Fatalf("failed to create request: %v", err)
}
mock := httptest.NewRecorder()
h(mock, req)
result := mock.Result()
if result.StatusCode != tc.statuscode {
t.Fatalf("expected status %d, got %d", tc.statuscode, result.StatusCode)
}
body, err := ioutil.ReadAll(result.Body)
result.Body.Close()
if err != nil {
t.Fatalf("failed to read response body: %v", err)
}
if string(body) != tc.want {
t.Fatalf("expected %s, got %s", tc.want, body)
}
})
}
}