Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query params in health check #4188

Merged
merged 1 commit into from
Nov 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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