Skip to content

Commit

Permalink
Query params in health check
Browse files Browse the repository at this point in the history
  • Loading branch information
mmatur authored and traefiker committed Nov 15, 2018
1 parent 4c25cda commit f25ebc7
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 14 deletions.
8 changes: 4 additions & 4 deletions healthcheck/healthcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@ type BackendConfig struct {
}

func (b *BackendConfig) newRequest(serverURL *url.URL) (*http.Request, error) {
u := &url.URL{}
*u = *serverURL
u, err := serverURL.Parse(b.Path)
if err != nil {
return nil, err
}

if len(b.Scheme) > 0 {
u.Scheme = b.Scheme
Expand All @@ -69,8 +71,6 @@ func (b *BackendConfig) newRequest(serverURL *url.URL) (*http.Request, error) {
u.Host = net.JoinHostPort(u.Hostname(), strconv.Itoa(b.Port))
}

u.Path += b.Path

return http.NewRequest(http.MethodGet, u.String(), http.NoBody)
}

Expand Down
81 changes: 71 additions & 10 deletions healthcheck/healthcheck_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,16 @@ func TestSetBackendsConfiguration(t *testing.T) {
}

func TestNewRequest(t *testing.T) {
type expected struct {
err bool
value string
}

testCases := []struct {
desc string
serverURL string
options Options
expected string
expected expected
}{
{
desc: "no port override",
Expand All @@ -163,7 +168,10 @@ func TestNewRequest(t *testing.T) {
Path: "/test",
Port: 0,
},
expected: "http://backend1:80/test",
expected: expected{
err: false,
value: "http://backend1:80/test",
},
},
{
desc: "port override",
Expand All @@ -172,7 +180,10 @@ func TestNewRequest(t *testing.T) {
Path: "/test",
Port: 8080,
},
expected: "http://backend2:8080/test",
expected: expected{
err: false,
value: "http://backend2:8080/test",
},
},
{
desc: "no port override with no port in server URL",
Expand All @@ -181,7 +192,10 @@ func TestNewRequest(t *testing.T) {
Path: "/health",
Port: 0,
},
expected: "http://backend1/health",
expected: expected{
err: false,
value: "http://backend1/health",
},
},
{
desc: "port override with no port in server URL",
Expand All @@ -190,7 +204,10 @@ func TestNewRequest(t *testing.T) {
Path: "/health",
Port: 8080,
},
expected: "http://backend2:8080/health",
expected: expected{
err: false,
value: "http://backend2:8080/health",
},
},
{
desc: "scheme override",
Expand All @@ -200,7 +217,46 @@ func TestNewRequest(t *testing.T) {
Path: "/test",
Port: 0,
},
expected: "http://backend1:80/test",
expected: expected{
err: false,
value: "http://backend1:80/test",
},
},
{
desc: "path with param",
serverURL: "http://backend1:80",
options: Options{
Path: "/health?powpow=do",
Port: 0,
},
expected: expected{
err: false,
value: "http://backend1:80/health?powpow=do",
},
},
{
desc: "path with params",
serverURL: "http://backend1:80",
options: Options{
Path: "/health?powpow=do&do=powpow",
Port: 0,
},
expected: expected{
err: false,
value: "http://backend1:80/health?powpow=do&do=powpow",
},
},
{
desc: "path with invalid path",
serverURL: "http://backend1:80",
options: Options{
Path: ":",
Port: 0,
},
expected: expected{
err: true,
value: "",
},
},
}

Expand All @@ -211,13 +267,18 @@ func TestNewRequest(t *testing.T) {

backend := NewBackendConfig(test.options, "backendName")

u, err := url.Parse(test.serverURL)
require.NoError(t, err)
u := testhelpers.MustParseURL(test.serverURL)

req, err := backend.newRequest(u)
require.NoError(t, err, "failed to create new backend request")

assert.Equal(t, test.expected, req.URL.String())
if test.expected.err {
require.Error(t, err)
assert.Nil(t, nil)
} else {
require.NoError(t, err, "failed to create new backend request")
require.NotNil(t, req)
assert.Equal(t, test.expected.value, req.URL.String())
}
})
}
}
Expand Down

0 comments on commit f25ebc7

Please sign in to comment.