From fa562dc916b2529d547d66dc25871c120efac080 Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 15 Nov 2018 15:50:03 +0100 Subject: [PATCH] Query params in health check --- healthcheck/healthcheck.go | 8 ++-- healthcheck/healthcheck_test.go | 81 +++++++++++++++++++++++++++++---- 2 files changed, 75 insertions(+), 14 deletions(-) diff --git a/healthcheck/healthcheck.go b/healthcheck/healthcheck.go index 542f0c8e2..a37c4107a 100644 --- a/healthcheck/healthcheck.go +++ b/healthcheck/healthcheck.go @@ -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 @@ -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) } diff --git a/healthcheck/healthcheck_test.go b/healthcheck/healthcheck_test.go index c354a4238..463962694 100644 --- a/healthcheck/healthcheck_test.go +++ b/healthcheck/healthcheck_test.go @@ -153,11 +153,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", @@ -166,7 +171,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", @@ -175,7 +183,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", @@ -184,7 +195,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", @@ -193,7 +207,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", @@ -203,7 +220,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: "", + }, }, } @@ -214,13 +270,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()) + } }) } }