From 6676d1b80749d94aef3e750dd38109255161be67 Mon Sep 17 00:00:00 2001 From: Lucas Roesler Date: Thu, 7 Jun 2018 00:05:00 +0200 Subject: [PATCH] Trim tailing slash from service name before proxy **What** - When determining the service name of the function, remove any trailing slashes, the slashes are not allowed in service names for either Swarm or K8S, so this can only be a left over from the url path **Why** - This was preventing service resolution, and hence failed functions, when the function was called with a trailing slash Fixes #714 Signed-off-by: Lucas Roesler --- gateway/handlers/forwarding_proxy.go | 3 +- gateway/handlers/forwarding_proxy_test.go | 50 +++++++++++++++++++++++ 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/gateway/handlers/forwarding_proxy.go b/gateway/handlers/forwarding_proxy.go index 59367f37..757b2ce4 100644 --- a/gateway/handlers/forwarding_proxy.go +++ b/gateway/handlers/forwarding_proxy.go @@ -133,7 +133,7 @@ func getServiceName(urlValue string) string { if strings.HasPrefix(urlValue, forward) { serviceName = urlValue[len(forward):] } - return serviceName + return strings.Trim(serviceName, "/") } // LoggingNotifier notifies a log about a request @@ -168,7 +168,6 @@ type FunctionAsHostBaseURLResolver struct { // Resolve the base URL for a request func (f FunctionAsHostBaseURLResolver) Resolve(r *http.Request) string { - svcName := getServiceName(r.URL.Path) const watchdogPort = 8080 diff --git a/gateway/handlers/forwarding_proxy_test.go b/gateway/handlers/forwarding_proxy_test.go index d6d72f9c..aaef9b96 100644 --- a/gateway/handlers/forwarding_proxy_test.go +++ b/gateway/handlers/forwarding_proxy_test.go @@ -4,6 +4,7 @@ import ( "bytes" "io/ioutil" "net/http" + "net/url" "testing" ) @@ -66,3 +67,52 @@ func Test_buildUpstreamRequest_NoBody_GetMethod_NoQuery(t *testing.T) { } } + +func Test_getServiceName(t *testing.T) { + scenarios := []struct { + name string + url string + serviceName string + }{ + { + name: "can handle request without trailing slash", + url: "/function/testFunc", + serviceName: "testFunc", + }, + { + name: "can handle request with trailing slash", + url: "/function/testFunc/", + serviceName: "testFunc", + }, + { + name: "can handle request with query parameters", + url: "/function/testFunc?name=foo", + serviceName: "testFunc", + }, + { + name: "can handle request with trailing slash and query parameters", + url: "/function/testFunc/?name=foo", + serviceName: "testFunc", + }, + { + name: "can handle request with a fragment", + url: "/function/testFunc#fragment", + serviceName: "testFunc", + }, + } + + for _, s := range scenarios { + t.Run(s.name, func(t *testing.T) { + + u, err := url.Parse("http://openfaas.local" + s.url) + if err != nil { + t.Fatal(err) + } + + service := getServiceName(u.Path) + if service != s.serviceName { + t.Fatalf("Incorrect service name - want: %s, got: %s", s.serviceName, service) + } + }) + } +}