forked from hverr/go-redirect
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain_test.go
79 lines (68 loc) · 2.14 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
package main
import (
"fmt"
"net/http"
"net/http/httptest"
"os"
"testing"
)
type redirTest struct {
method string
url string
dest string
rstatus int
rstring string
}
func TestHealthCheckHandler(t *testing.T) {
var redirTests = []redirTest{
{"GET", "http://foo/", "443", 301, "https://foo/"},
{"GET", "http://foo/", "8080", 301, "https://foo:8080/"},
{"GET", "http://foo/bar", "8080", 301, "https://foo:8080/bar"},
{"GET", "http://foo/bar?baz=true", "8080", 301, "https://foo:8080/bar?baz=true"},
{"POST", "http://foo/bar", "443", 308, ""},
}
for _, tt := range redirTests {
// Create a request to pass to our handler. We don't have any query parameters for now, so we'll
// pass 'nil' as the third parameter.
req, err := http.NewRequest(tt.method, tt.url, nil)
if err != nil {
t.Fatal(err)
}
// We create a ResponseRecorder (which satisfies http.ResponseWriter) to record the response.
rr := httptest.NewRecorder()
handler := RedirectHandler{Destination: tt.dest}
// Our handlers satisfy http.Handler, so we can call their ServeHTTP method
// directly and pass in our Request and ResponseRecorder.
handler.ServeHTTP(rr, req)
// Check the status code is what we expect.
if status := rr.Code; status != tt.rstatus {
t.Errorf("handler returned wrong status code: got %v want %v",
status, tt.rstatus)
}
// Check the response body is what we expect.
expected := ""
if tt.rstring != "" {
expected = fmt.Sprintf("<a href=\"%s\">Moved Permanently</a>.\n\n", tt.rstring)
}
if rr.Body.String() != expected {
t.Errorf("handler returned unexpected body: got %v want %v",
rr.Body.String(), expected)
}
}
}
func TestParseOptions(t *testing.T) {
oldArgs := os.Args
defer func() { os.Args = oldArgs }()
os.Args = []string{"redirect", "-port=81", "-destination=8443"}
expected := Options{
ShowHelp: false,
Source: "81",
Destination: "8443",
}
actual := parseOptions()
if actual != expected {
t.Errorf("Test failed, expected: '{%t,%s,%s}', got: '{%t,%s,%s}'",
expected.ShowHelp, expected.Source, expected.Destination,
actual.ShowHelp, actual.Source, actual.Destination)
}
}