From b37fed4cc81ac5732968001852f30e166d2e4582 Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Thu, 21 May 2015 16:31:01 -0600 Subject: [PATCH] Beginning some tests for the parser --- config/parse/parsing.go | 19 ++++++++----- config/parse/parsing_test.go | 54 ++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 7 deletions(-) create mode 100644 config/parse/parsing_test.go diff --git a/config/parse/parsing.go b/config/parse/parsing.go index bcf0a8e7..ee4cc202 100644 --- a/config/parse/parsing.go +++ b/config/parse/parsing.go @@ -257,7 +257,7 @@ func (p *parser) closeCurlyBrace() error { // standardAddress turns the accepted host and port patterns // into a format accepted by net.Dial. func standardAddress(str string) (host, port string, err error) { - var schemePort string + var schemePort, splitPort string if strings.HasPrefix(str, "https://") { schemePort = "https" @@ -267,14 +267,19 @@ func standardAddress(str string) (host, port string, err error) { str = str[7:] } - host, port, err = net.SplitHostPort(str) - if err != nil && schemePort == "" { - host, port, err = net.SplitHostPort(str + ":") // tack on empty port + host, splitPort, err = net.SplitHostPort(str) + if err != nil { + host, splitPort, err = net.SplitHostPort(str + ":") // tack on empty port } - if err != nil && schemePort != "" { + if err != nil { + // ¯\_(ツ)_/¯ host = str - port = schemePort // assume port from scheme - err = nil + } + + if splitPort != "" { + port = splitPort + } else { + port = schemePort } return diff --git a/config/parse/parsing_test.go b/config/parse/parsing_test.go new file mode 100644 index 00000000..2cb21f83 --- /dev/null +++ b/config/parse/parsing_test.go @@ -0,0 +1,54 @@ +package parse + +import "testing" + +func TestStandardAddress(t *testing.T) { + for i, test := range []struct { + input string + host, port string + shouldErr bool + }{ + {`localhost`, "localhost", "", false}, + {`localhost:1234`, "localhost", "1234", false}, + {`localhost:`, "localhost", "", false}, + {`0.0.0.0`, "0.0.0.0", "", false}, + {`127.0.0.1:1234`, "127.0.0.1", "1234", false}, + {`:1234`, "", "1234", false}, + {`[::1]`, "::1", "", false}, + {`[::1]:1234`, "::1", "1234", false}, + {`:`, "", "", false}, + {`localhost:http`, "localhost", "http", false}, + {`localhost:https`, "localhost", "https", false}, + {`:http`, "", "http", false}, + {`:https`, "", "https", false}, + {`http://localhost`, "localhost", "http", false}, + {`https://localhost`, "localhost", "https", false}, + {`http://127.0.0.1`, "127.0.0.1", "http", false}, + {`https://127.0.0.1`, "127.0.0.1", "https", false}, + {`http://[::1]`, "::1", "http", false}, + {`http://localhost:1234`, "localhost", "1234", false}, + {`https://127.0.0.1:1234`, "127.0.0.1", "1234", false}, + {`http://[::1]:1234`, "::1", "1234", false}, + {``, "", "", false}, + {`::1`, "::1", "", true}, + {`localhost::`, "localhost::", "", true}, + {`#$%@`, "#$%@", "", true}, + } { + host, port, err := standardAddress(test.input) + + if err != nil && !test.shouldErr { + t.Errorf("Test %d: Expected no error, but had error: %v", i, err) + } + if err == nil && test.shouldErr { + t.Errorf("Test %d: Expected error, but had none", i) + } + + if host != test.host { + t.Errorf("Test %d: Expected host '%s', got '%s'", i, test.host, host) + } + + if port != test.port { + t.Errorf("Test %d: Expected port '%s', got '%s'", i, test.port, port) + } + } +}