forked from jmhodges/howsmyssl
/
index_test.go
86 lines (79 loc) · 2.56 KB
/
index_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
package main
import (
"bytes"
"expvar"
"net/http"
"net/http/httptest"
"testing"
)
func TestDumbNilishIndex(t *testing.T) {
tmpl := loadIndex()
buf := new(bytes.Buffer)
err := tmpl.Execute(buf, &clientInfo{})
if err != nil {
t.Errorf("index execution blew up with nilish clientInfo: %#v", err)
}
if len(buf.Bytes()) == 0 {
t.Errorf("index execution did not write anything")
}
}
type vhostTest struct {
rawVHost string
httpsAddr string
expectedRouteHost string
expectedRedirectHost string
}
func TestVHostCalculation(t *testing.T) {
tests := []vhostTest{
vhostTest{
rawVHost: "www.howsmyssl.com",
httpsAddr: "0:10443",
expectedRouteHost: "www.howsmyssl.com",
expectedRedirectHost: "www.howsmyssl.com",
},
vhostTest{
rawVHost: "localhost:10443",
httpsAddr: "localhost:10443",
expectedRouteHost: "localhost",
expectedRedirectHost: "localhost:10443",
},
vhostTest{
rawVHost: "example.com:10443",
httpsAddr: "localhost:10443",
expectedRouteHost: "example.com",
expectedRedirectHost: "example.com:10443",
},
vhostTest{
rawVHost: "example.com:443",
httpsAddr: "0:443",
expectedRouteHost: "example.com",
expectedRedirectHost: "example.com",
},
}
staticVars := expvar.NewMap("testStatic")
staticHandler := makeStaticHandler("/static", staticVars)
for i, vt := range tests {
routeHost, redirectHost := calculateDomains(vt.rawVHost, vt.httpsAddr)
if routeHost != vt.expectedRouteHost {
t.Errorf("#%d vhost %#v, httpsAddr %#v: want routeHost %#v, got %s", i, vt.rawVHost, vt.httpsAddr, vt.expectedRouteHost, routeHost)
}
if redirectHost != vt.expectedRedirectHost {
t.Errorf("#%d vhost %#v, httpsAddr %#v: want redirectHost %#v, got %#v", i, vt.rawVHost, vt.httpsAddr, vt.expectedRedirectHost, redirectHost)
}
tm := tlsMux(vt.expectedRouteHost, vt.expectedRedirectHost, staticHandler)
r, err := http.NewRequest("GET", "https://howsmyssl.com/", nil)
if err != nil {
t.Fatalf("borked request")
}
w := httptest.NewRecorder()
tm.ServeHTTP(w, r)
expectedLocation := "https://" + vt.expectedRedirectHost + "/"
location := w.Header()["Location"][0]
if w.Code != http.StatusMovedPermanently {
t.Errorf("#%d vhost %#v, httpsAddr %#v: want Code %d, got %d", i, vt.rawVHost, vt.httpsAddr, http.StatusMovedPermanently, w.Code)
}
if location != expectedLocation {
t.Errorf("#%d vhost %#v, httpsAddr %#v: want Location %s, got %s", i, vt.rawVHost, vt.httpsAddr, expectedLocation, location)
}
}
}