Query params in health check

This commit is contained in:
Michael 2018-11-15 15:50:03 +01:00 committed by Traefiker Bot
parent 0be895febb
commit fa562dc916
2 changed files with 75 additions and 14 deletions

View file

@ -58,8 +58,10 @@ type BackendConfig struct {
} }
func (b *BackendConfig) newRequest(serverURL *url.URL) (*http.Request, error) { func (b *BackendConfig) newRequest(serverURL *url.URL) (*http.Request, error) {
u := &url.URL{} u, err := serverURL.Parse(b.Path)
*u = *serverURL if err != nil {
return nil, err
}
if len(b.Scheme) > 0 { if len(b.Scheme) > 0 {
u.Scheme = b.Scheme 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.Host = net.JoinHostPort(u.Hostname(), strconv.Itoa(b.Port))
} }
u.Path += b.Path
return http.NewRequest(http.MethodGet, u.String(), http.NoBody) return http.NewRequest(http.MethodGet, u.String(), http.NoBody)
} }

View file

@ -153,11 +153,16 @@ func TestSetBackendsConfiguration(t *testing.T) {
} }
func TestNewRequest(t *testing.T) { func TestNewRequest(t *testing.T) {
type expected struct {
err bool
value string
}
testCases := []struct { testCases := []struct {
desc string desc string
serverURL string serverURL string
options Options options Options
expected string expected expected
}{ }{
{ {
desc: "no port override", desc: "no port override",
@ -166,7 +171,10 @@ func TestNewRequest(t *testing.T) {
Path: "/test", Path: "/test",
Port: 0, Port: 0,
}, },
expected: "http://backend1:80/test", expected: expected{
err: false,
value: "http://backend1:80/test",
},
}, },
{ {
desc: "port override", desc: "port override",
@ -175,7 +183,10 @@ func TestNewRequest(t *testing.T) {
Path: "/test", Path: "/test",
Port: 8080, 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", desc: "no port override with no port in server URL",
@ -184,7 +195,10 @@ func TestNewRequest(t *testing.T) {
Path: "/health", Path: "/health",
Port: 0, Port: 0,
}, },
expected: "http://backend1/health", expected: expected{
err: false,
value: "http://backend1/health",
},
}, },
{ {
desc: "port override with no port in server URL", desc: "port override with no port in server URL",
@ -193,7 +207,10 @@ func TestNewRequest(t *testing.T) {
Path: "/health", Path: "/health",
Port: 8080, Port: 8080,
}, },
expected: "http://backend2:8080/health", expected: expected{
err: false,
value: "http://backend2:8080/health",
},
}, },
{ {
desc: "scheme override", desc: "scheme override",
@ -203,7 +220,46 @@ func TestNewRequest(t *testing.T) {
Path: "/test", Path: "/test",
Port: 0, 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") backend := NewBackendConfig(test.options, "backendName")
u, err := url.Parse(test.serverURL) u := testhelpers.MustParseURL(test.serverURL)
require.NoError(t, err)
req, err := backend.newRequest(u) 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())
}
}) })
} }
} }