Skip to content

Commit

Permalink
minor test structure update. minor comments. go-spatial#199
Browse files Browse the repository at this point in the history
  • Loading branch information
ARolek committed Dec 6, 2017
1 parent 0fdc75e commit 953f2c7
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 25 deletions.
3 changes: 1 addition & 2 deletions server/handle_capabilities_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ func TestHandleCapabilities(t *testing.T) {
// request uri.
{
handler: server.HandleCapabilities{},
hostname: "",
uri: "http://localhost:8080/capabilities",
uriPattern: "/capabilities",
reqMethod: "GET",
Expand Down Expand Up @@ -68,6 +67,7 @@ func TestHandleCapabilities(t *testing.T) {
},
},
// With hostname set and port set to "none" in config, urls should have host "cdn.tegola.io"
// debug layers turned on
{
handler: server.HandleCapabilities{},
hostname: "cdn.tegola.io",
Expand Down Expand Up @@ -127,7 +127,6 @@ func TestHandleCapabilities(t *testing.T) {
},
{
handler: server.HandleCapabilities{},
hostname: "",
uri: "http://localhost:8080/capabilities",
uriPattern: "/capabilities",
reqMethod: "GET",
Expand Down
2 changes: 1 addition & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func hostName(r *http.Request) string {
requestHostname = substrs[0]
requestPort = substrs[1]
default:
log.Printf("Multiple colons (':') in host string: %v", r.Host)
log.Printf("multiple colons (':') in host string: %v", r.Host)
}

retHost := HostName
Expand Down
36 changes: 14 additions & 22 deletions server/server_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,54 +8,39 @@ import (
)

func TestHostName(t *testing.T) {
// Helper function to set up table tests.
urlFromString := func(urlString string) *url.URL {
url, err := url.Parse(urlString)
if err != nil {
t.Errorf("Could not create url.URL from %v: %v", urlString, err)
}
return url
}

// Minimal http.Request with only URL & Host properties set
mockRequest := func(u *url.URL) http.Request {
r := http.Request{URL: u, Host: u.Host}
return r
}

testcases := []struct {
request http.Request
url string
hostName string
port string
expected string
}{
{
// With hostname & port unset in config, expect host:port matching URL
request: mockRequest(urlFromString("http://localhost:8080/capabilities")),
url: "http://localhost:8080/capabilities",
expected: "localhost:8080",
},
{
// With hostname set and port set to "none" in config, expect "cdn.tegola.io"
request: mockRequest(urlFromString("http://localhost:8080/capabilities")),
url: "http://localhost:8080/capabilities",
hostName: "cdn.tegola.io",
port: "none",
expected: "cdn.tegola.io",
},
{
// Hostname set, no port in config, but port in url. Expect <config_host>:<url_port>.
request: mockRequest(urlFromString("http://localhost:8080/capabilities")),
url: "http://localhost:8080/capabilities",
hostName: "cdn.tegola.io",
expected: "cdn.tegola.io:8080",
},
{
// Hostname set, no port in config or url, expect hostname to match config.
request: mockRequest(urlFromString("http://localhost/capabilities")),
url: "http://localhost/capabilities",
hostName: "cdn.tegola.io",
expected: "cdn.tegola.io",
},
{
// Hostname unset, no port in config or url, expect hostname to match url host.
request: mockRequest(urlFromString("http://localhost/capabilities")),
url: "http://localhost/capabilities",
expected: "localhost",
},
}
Expand All @@ -65,7 +50,14 @@ func TestHostName(t *testing.T) {
HostName = tc.hostName
Port = tc.port

output := hostName(&tc.request)
url, err := url.Parse(tc.url)
if err != nil {
t.Errorf("testcase (%v) failed. could not create url.URL from (%v): %v", tc.url, err)
}

req := http.Request{URL: url, Host: url.Host}

output := hostName(&req)
if output != tc.expected {
t.Errorf("testcase (%v) failed. expected (%v) does not match result (%v)", i, tc.expected, output)
}
Expand Down

0 comments on commit 953f2c7

Please sign in to comment.