forked from cortesi/devd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_test.go
115 lines (99 loc) · 2.69 KB
/
server_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
package devd
import (
"reflect"
"testing"
"github.com/cortesi/devd/fstmpl"
"github.com/cortesi/devd/inject"
"github.com/cortesi/devd/templates"
"github.com/cortesi/termlog"
)
var formatURLTests = []struct {
tls bool
addr string
port int
output string
}{
{true, "127.0.0.1", 8000, "https://devd.io:8000"},
{false, "127.0.0.1", 8000, "http://devd.io:8000"},
{false, "127.0.0.1", 80, "http://devd.io"},
{true, "127.0.0.1", 443, "https://devd.io"},
{false, "127.0.0.1", 443, "http://devd.io:443"},
}
func TestFormatURL(t *testing.T) {
for i, tt := range formatURLTests {
url := formatURL(tt.tls, tt.addr, tt.port)
if url != tt.output {
t.Errorf("Test %d, expected \"%s\" got \"%s\"", i, tt.output, url)
}
}
}
func TestPickPort(t *testing.T) {
_, err := pickPort("127.0.0.1", 8000, 10000, true)
if err != nil {
t.Errorf("Could not bind to any port: %s", err)
}
_, err = pickPort("127.0.0.1", 8000, 8000, true)
if err == nil {
t.Errorf("Expected not to be able to bind to any port")
}
}
func fsEndpoint(s string) *filesystemEndpoint {
e, _ := newFilesystemEndpoint(s, []string{})
return e
}
func TestDevdRouteHandler(t *testing.T) {
logger := termlog.NewLog()
logger.Quiet()
r := Route{"", "/", fsEndpoint("./testdata")}
templates := fstmpl.MustMakeTemplates(templates.FS)
ci := inject.CopyInject{}
devd := Devd{LivereloadRoutes: true}
h := devd.WrapHandler(logger, r.Endpoint.Handler("", templates, ci))
ht := handlerTester{t, h}
AssertCode(t, ht.Request("GET", "/", nil), 200)
}
func TestDevdHandler(t *testing.T) {
logger := termlog.NewLog()
logger.Quiet()
templates := fstmpl.MustMakeTemplates(templates.FS)
devd := Devd{LivereloadRoutes: true, WatchPaths: []string{"./"}}
err := devd.AddRoutes([]string{"./"}, []string{})
if err != nil {
t.Error(err)
}
h, err := devd.Router(logger, templates)
if err != nil {
t.Error(err)
}
ht := handlerTester{t, h}
AssertCode(t, ht.Request("GET", "/", nil), 200)
AssertCode(t, ht.Request("GET", "/nonexistent", nil), 404)
}
func TestGetTLSConfig(t *testing.T) {
_, err := getTLSConfig("nonexistent")
if err == nil {
t.Error("Expected failure, found success.")
}
_, err = getTLSConfig("./testdata/certbundle.pem")
if err != nil {
t.Errorf("Could not get TLS config: %s", err)
}
}
var credentialsTests = []struct {
spec string
creds *Credentials
}{
{"foo:bar", &Credentials{"foo", "bar"}},
{"foo:", nil},
{":bar", nil},
{"foo:bar:voing", &Credentials{"foo", "bar:voing"}},
{"foo", nil},
}
func TestCredentials(t *testing.T) {
for i, data := range credentialsTests {
got, _ := CredentialsFromSpec(data.spec)
if !reflect.DeepEqual(data.creds, got) {
t.Errorf("%d: got %v, expected %v", i, got, data.creds)
}
}
}