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) + } + }) + } +}