-
Notifications
You must be signed in to change notification settings - Fork 1
/
redirect_test.go
153 lines (128 loc) · 4.25 KB
/
redirect_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
package main
import (
"net/http"
"testing"
"github.com/sirupsen/logrus"
)
func TestNewRedirect(t *testing.T) {
v := NewViper(config{
"resolver.type": "dns",
"redirect.host": ".dev.local.",
"redirect.options.keyword.slash": "--slash--",
"redirect.options.keyword.dot": "--dot--",
"redirect.options.keyword.colon": "--colon--",
"redirect.options.keyword.interrogation-mark": "--int--",
"redirect.options.keyword.ampersand": "--amp--",
"redirect.options.keyword.equal": "--equal--",
"redirect.options.keyword.percent": "--percent--",
"redirect.options.permanent_redirect": true,
"redirect.options.enforce_https": false,
"redirect.options.keep_uri": true,
})
r, err := NewRedirect(logrus.StandardLogger(), v)
if err != nil {
t.Fatalf("expected nil, got %s", err)
}
if r.host != ".dev.local." {
t.Errorf("expected %s, got %s", ".dev.local.", r.host)
}
if r.options.statusCodeRedirect != http.StatusPermanentRedirect {
t.Errorf("expected %d, got %d", http.StatusPermanentRedirect, r.options.statusCodeRedirect)
}
if r.options.schema != "" {
t.Errorf("expected \"\", got %s", r.options.schema)
}
if !r.options.keepURI {
t.Error("expected true, got false")
}
}
func TestNewRedirect_config(t *testing.T) {
v := NewViper(config{
"resolver.type": "static",
"redirect.host": ".dev.local.",
"redirect.options.keyword.slash": "--slash--",
"redirect.options.keyword.dot": "--dot--",
"redirect.options.keyword.colon": "--colon--",
"redirect.options.keyword.interrogation-mark": "--int--",
"redirect.options.keyword.ampersand": "--amp--",
"redirect.options.keyword.equal": "--equal--",
"redirect.options.keyword.percent": "--percent--",
"redirect.options.permanent_redirect": false,
"redirect.options.enforce_https": true,
"redirect.options.keep_uri": false,
})
r, err := NewRedirect(logrus.StandardLogger(), v)
if err != nil {
t.Fatalf("expected nil, got %s", err)
}
if r.host != ".dev.local." {
t.Errorf("expected %s, got %s", ".dev.local.", r.host)
}
if r.options.statusCodeRedirect != http.StatusTemporaryRedirect {
t.Errorf("expected %d, got %d", http.StatusTemporaryRedirect, r.options.statusCodeRedirect)
}
if r.options.schema != "https://" {
t.Errorf("expected \"https://\", got %s", r.options.schema)
}
if r.options.keepURI {
t.Error("expected false, got true")
}
}
func TestNewRedirect_err(t *testing.T) {
v := NewViper(config{
"resolver.type": "nope",
"redirect.host": ".dev.local.",
})
r, err := NewRedirect(logrus.StandardLogger(), v)
if err != UnknownResolverTypeErr {
t.Errorf("expected UnknowResolverTypeErr, got %s", err)
}
if r != nil {
t.Errorf("expected nil, got %T", r)
}
}
func TestNewRedirect_Redirect(t *testing.T) {
v := NewViper(config{
"resolver.type": "dns",
"redirect.host": ".local.labesse.me.",
"redirect.options.keyword.slash": "--slash--",
"redirect.options.keyword.dot": "--dot--",
"redirect.options.keyword.colon": "--colon--",
"redirect.options.keyword.interrogation-mark": "--int--",
"redirect.options.keyword.ampersand": "--amp--",
"redirect.options.keyword.equal": "--equal--",
"redirect.options.keyword.percent": "--percent--",
"redirect.options.permanent_redirect": true,
"redirect.options.enforce_https": false,
"redirect.options.keep_uri": true,
})
r, err := NewRedirect(logrus.StandardLogger(), v)
if err != nil {
t.Fatalf("expected nil, got %s", err)
}
inputs := []struct{
in *http.Request
outHost string
outRedirectCode int
errExpected bool
}{
{in: &http.Request{Host: "dns-redirect.labesse.me"}, outHost: "google.com", outRedirectCode: http.StatusPermanentRedirect, errExpected: false},
{in: &http.Request{Host: "dns-redirect.labesse.me", RequestURI: "/uri"}, outHost: "google.com/uri", outRedirectCode: http.StatusPermanentRedirect, errExpected: false},
{in: &http.Request{Host: "invalid-host"}, outHost: "", outRedirectCode: 0, errExpected: true},
}
for _, i := range inputs {
h, c, err := r.Redirect(i.in)
if h != i.outHost {
t.Errorf("expected %s, got %s", i.outHost, h)
}
if c != i.outRedirectCode {
t.Errorf("expected %d, got %d", i.outRedirectCode, c)
}
if err == nil && i.errExpected {
t.Errorf("expected err, got nil")
}
if err != nil && !i.errExpected {
t.Errorf("expected nil, get %s", err)
}
}
}