Commit fc9a8a29 authored by Jacob Vosmaer's avatar Jacob Vosmaer

Add tests for mustParseAddress

parent 10191b81
...@@ -53,16 +53,20 @@ func NewRoundTripper(backend *url.URL, socket string, proxyHeadersTimeout time.D ...@@ -53,16 +53,20 @@ func NewRoundTripper(backend *url.URL, socket string, proxyHeadersTimeout time.D
} }
func mustParseAddress(address, scheme string) string { func mustParseAddress(address, scheme string) string {
if host, port, err := net.SplitHostPort(address); err == nil { if scheme == "https" {
panic("TLS is not supported for backend connections")
}
if host, port, err := net.SplitHostPort(address); err == nil && host != "" && port != "" {
return host + ":" + port return host + ":" + port
} }
address = address + ":" + scheme address = address + ":" + scheme
if host, port, err := net.SplitHostPort(address); err == nil { if host, port, err := net.SplitHostPort(address); err == nil && host != "" && port != "" {
return host + ":" + port return host + ":" + port
} }
panic("could not parse host/port from addres / scheme") panic("could not parse host:port from address and scheme")
} }
func (t *RoundTripper) RoundTrip(r *http.Request) (res *http.Response, err error) { func (t *RoundTripper) RoundTrip(r *http.Request) (res *http.Response, err error) {
......
package badgateway
import (
"log"
"testing"
)
func TestMustParseAddress(t *testing.T) {
successExamples := []struct{ address, scheme, expected string }{
{"1.2.3.4:56", "http", "1.2.3.4:56"},
{"[::1]:23", "http", "::1:23"},
{"4.5.6.7", "http", "4.5.6.7:http"},
}
for _, example := range successExamples {
result := mustParseAddress(example.address, example.scheme)
if example.expected != result {
t.Errorf("expected %q, got %q", example.expected, result)
}
}
panicExamples := []struct{ address, scheme string }{
{"1.2.3.4", ""},
{"1.2.3.4", "https"},
}
for _, panicExample := range panicExamples {
func() {
defer func() {
if r := recover(); r == nil {
t.Errorf("expected panic for %v but none occurred", panicExample)
}
}()
log.Print(mustParseAddress(panicExample.address, panicExample.scheme))
}()
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment