diff options
author | Matthew Holt <mholt@users.noreply.github.com> | 2019-05-07 10:15:46 -0600 |
---|---|---|
committer | Matthew Holt <mholt@users.noreply.github.com> | 2019-05-07 10:15:46 -0600 |
commit | ad3d408067ee155a75e33a59ef02f547a943b94e (patch) | |
tree | 040f2dd97dbcc9281b7ba14fa6c1d8fd9318c0f4 | |
parent | e40bbecb16d196d2d700a9484e53c11b64dfe8d9 (diff) |
Add some tests and fix vet warning
-rw-r--r-- | internal/caddyscript/lib/regex.go | 2 | ||||
-rw-r--r-- | modules/caddyhttp/caddyhttp_test.go | 182 |
2 files changed, 146 insertions, 38 deletions
diff --git a/internal/caddyscript/lib/regex.go b/internal/caddyscript/lib/regex.go index a06a410..b151e64 100644 --- a/internal/caddyscript/lib/regex.go +++ b/internal/caddyscript/lib/regex.go @@ -45,6 +45,6 @@ func (r Regexp) MatchString(thread *starlark.Thread, fn *starlark.Builtin, args func (r Regexp) Freeze() {} func (r Regexp) Hash() (uint32, error) { return 0, fmt.Errorf("unhashable: Regexp") } -func (r Regexp) String() string { return fmt.Sprint(r) } +func (r Regexp) String() string { return "Regexp" } func (r Regexp) Type() string { return "Regexp" } func (r Regexp) Truth() starlark.Bool { return true } diff --git a/modules/caddyhttp/caddyhttp_test.go b/modules/caddyhttp/caddyhttp_test.go index 610a5f0..8d25332 100644 --- a/modules/caddyhttp/caddyhttp_test.go +++ b/modules/caddyhttp/caddyhttp_test.go @@ -5,73 +5,181 @@ import ( "testing" ) +func TestSplitListenerAddr(t *testing.T) { + for i, tc := range []struct { + input string + expectNetwork string + expectHost string + expectPort string + expectErr bool + }{ + { + input: "", + expectErr: true, + }, + { + input: "foo", + expectErr: true, + }, + { + input: "foo:1234", + expectHost: "foo", + expectPort: "1234", + }, + { + input: "foo:1234-5678", + expectHost: "foo", + expectPort: "1234-5678", + }, + { + input: "udp/foo:1234", + expectNetwork: "udp", + expectHost: "foo", + expectPort: "1234", + }, + { + input: "tcp6/foo:1234-5678", + expectNetwork: "tcp6", + expectHost: "foo", + expectPort: "1234-5678", + }, + { + input: "udp/", + expectNetwork: "udp", + expectErr: true, + }, + } { + actualNetwork, actualHost, actualPort, err := splitListenAddr(tc.input) + if tc.expectErr && err == nil { + t.Errorf("Test %d: Expected error but got: %v", i, err) + } + if !tc.expectErr && err != nil { + t.Errorf("Test %d: Expected no error but got: %v", i, err) + } + if actualNetwork != tc.expectNetwork { + t.Errorf("Test %d: Expected network '%s' but got '%s'", i, tc.expectNetwork, actualNetwork) + } + if actualHost != tc.expectHost { + t.Errorf("Test %d: Expected host '%s' but got '%s'", i, tc.expectHost, actualHost) + } + if actualPort != tc.expectPort { + t.Errorf("Test %d: Expected port '%s' but got '%s'", i, tc.expectPort, actualPort) + } + } +} + +func TestJoinListenerAddr(t *testing.T) { + for i, tc := range []struct { + network, host, port string + expect string + }{ + { + network: "", host: "", port: "", + expect: "", + }, + { + network: "tcp", host: "", port: "", + expect: "tcp/", + }, + { + network: "", host: "foo", port: "", + expect: "foo", + }, + { + network: "", host: "", port: "1234", + expect: ":1234", + }, + { + network: "", host: "", port: "1234-5678", + expect: ":1234-5678", + }, + { + network: "", host: "foo", port: "1234", + expect: "foo:1234", + }, + { + network: "udp", host: "foo", port: "1234", + expect: "udp/foo:1234", + }, + { + network: "udp", host: "", port: "1234", + expect: "udp/:1234", + }, + } { + actual := joinListenAddr(tc.network, tc.host, tc.port) + if actual != tc.expect { + t.Errorf("Test %d: Expected '%s' but got '%s'", i, tc.expect, actual) + } + } +} + func TestParseListenerAddr(t *testing.T) { for i, tc := range []struct { - input string - expectProto string - expectAddrs []string - expectErr bool + input string + expectNetwork string + expectAddrs []string + expectErr bool }{ { - input: "", - expectProto: "tcp", - expectErr: true, + input: "", + expectNetwork: "tcp", + expectErr: true, }, { - input: ":", - expectProto: "tcp", - expectErr: true, + input: ":", + expectNetwork: "tcp", + expectErr: true, }, { - input: ":1234", - expectProto: "tcp", - expectAddrs: []string{":1234"}, + input: ":1234", + expectNetwork: "tcp", + expectAddrs: []string{":1234"}, }, { - input: "tcp/:1234", - expectProto: "tcp", - expectAddrs: []string{":1234"}, + input: "tcp/:1234", + expectNetwork: "tcp", + expectAddrs: []string{":1234"}, }, { - input: "tcp6/:1234", - expectProto: "tcp6", - expectAddrs: []string{":1234"}, + input: "tcp6/:1234", + expectNetwork: "tcp6", + expectAddrs: []string{":1234"}, }, { - input: "tcp4/localhost:1234", - expectProto: "tcp4", - expectAddrs: []string{"localhost:1234"}, + input: "tcp4/localhost:1234", + expectNetwork: "tcp4", + expectAddrs: []string{"localhost:1234"}, }, { - input: "unix/localhost:1234-1236", - expectProto: "unix", - expectAddrs: []string{"localhost:1234", "localhost:1235", "localhost:1236"}, + input: "unix/localhost:1234-1236", + expectNetwork: "unix", + expectAddrs: []string{"localhost:1234", "localhost:1235", "localhost:1236"}, }, { - input: "localhost:1234-1234", - expectProto: "tcp", - expectAddrs: []string{"localhost:1234"}, + input: "localhost:1234-1234", + expectNetwork: "tcp", + expectAddrs: []string{"localhost:1234"}, }, { - input: "localhost:2-1", - expectProto: "tcp", - expectErr: true, + input: "localhost:2-1", + expectNetwork: "tcp", + expectErr: true, }, { - input: "localhost:0", - expectProto: "tcp", - expectAddrs: []string{"localhost:0"}, + input: "localhost:0", + expectNetwork: "tcp", + expectAddrs: []string{"localhost:0"}, }, } { - actualProto, actualAddrs, err := parseListenAddr(tc.input) + actualNetwork, actualAddrs, err := parseListenAddr(tc.input) if tc.expectErr && err == nil { t.Errorf("Test %d: Expected error but got: %v", i, err) } if !tc.expectErr && err != nil { t.Errorf("Test %d: Expected no error but got: %v", i, err) } - if actualProto != tc.expectProto { - t.Errorf("Test %d: Expeceted protocol '%s' but got '%s'", i, tc.expectProto, actualProto) + if actualNetwork != tc.expectNetwork { + t.Errorf("Test %d: Expected network '%s' but got '%s'", i, tc.expectNetwork, actualNetwork) } if !reflect.DeepEqual(tc.expectAddrs, actualAddrs) { t.Errorf("Test %d: Expected addresses %v but got %v", i, tc.expectAddrs, actualAddrs) |