forked from cortesi/devd
/
cli_test.go
81 lines (67 loc) · 1.75 KB
/
cli_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
package devd
import (
"testing"
"github.com/GeertJohan/go.rice"
"github.com/cortesi/devd/ricetemp"
"github.com/cortesi/devd/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)
return e
}
func TestDevdRouteHandler(t *testing.T) {
logger := termlog.NewLog()
logger.Quiet()
r := Route{"", "/", fsEndpoint("./testdata")}
templates := ricetemp.MustMakeTemplates(rice.MustFindBox("templates"))
devd := Devd{
Routes: make([]string, 0, 0),
OpenBrowser: false,
CertFile: "",
Address: "",
Port: 0,
// Shaping
Latency: 0,
DownKbps: 0,
UpKbps: 0,
// Livereload
LivereloadRoutes: false,
Watch: make([]string, 0, 0),
Excludes: make([]string, 0, 0),
// Logging
IgnoreLogs: make([]string, 0, 0),
}
h := devd.RouteHandler(logger, r, templates, nil)
ht := handlerTester{t, h}
AssertCode(t, ht.Request("GET", "/", nil), 200)
}