-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
74 lines (64 loc) · 1.56 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
package main
import (
"bytes"
"flag"
"net"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
func TestUsage(t *testing.T) {
w := bytes.NewBuffer(nil)
fs := flag.NewFlagSet("servedir", flag.ExitOnError)
fs.SetOutput(w)
usage(fs)
opts := `[-a] [-p <port>] [<dir>]`
desc := `Simple HTTP server, serving files from given directory.`
require.Contains(t, w.String(), opts)
require.Contains(t, w.String(), desc)
}
func TestDir(t *testing.T) {
require.Equal(t, ".", dir(nil))
require.Equal(t, "abc", dir([]string{"abc"}))
require.Equal(t, "abc", dir([]string{"abc", "efg"}))
}
func TestListenAddr(t *testing.T) {
require.Equal(t, ":0", listenAddr(0, true))
require.Equal(t, "127.0.0.1:0", listenAddr(0, false))
}
func TestListenAddrURL(t *testing.T) {
addr := &net.TCPAddr{
Port: 80,
IP: net.IPv4(127, 0, 0, 1),
}
require.Equal(t, "http://localhost:80", listenAddrURL(addr))
addr = &net.TCPAddr{
Port: 80,
IP: net.IPv4(0, 0, 0, 0),
}
got := listenAddrURL(addr)
require.True(t, strings.HasPrefix(got, "http://"))
require.True(t, strings.HasSuffix(got, ":80"))
addr = &net.TCPAddr{
Port: 80,
}
require.Equal(t, "http://:80", listenAddrURL(addr))
}
func TestParseFlags(t *testing.T) {
want := config{
listenAddr: "127.0.0.1:0",
dir: ".",
}
got := parseFlags()
require.Equal(t, want, got)
}
func TestParseFlagsFromEnvironment(t *testing.T) {
want := config{
listenAddr: ":1",
dir: ".",
}
t.Setenv("SERVEDIR_ALL_INTERFACES", "true")
t.Setenv("SERVEDIR_PORT", "1")
got := parseFlags()
require.Equal(t, want, got)
}