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 <roesler.lucas@gmail.com>
This commit is contained in:
Lucas Roesler
2018-06-07 00:05:00 +02:00
committed by Alex Ellis
parent 1f6e174454
commit 6676d1b807
2 changed files with 51 additions and 2 deletions

View File

@ -133,7 +133,7 @@ func getServiceName(urlValue string) string {
if strings.HasPrefix(urlValue, forward) { if strings.HasPrefix(urlValue, forward) {
serviceName = urlValue[len(forward):] serviceName = urlValue[len(forward):]
} }
return serviceName return strings.Trim(serviceName, "/")
} }
// LoggingNotifier notifies a log about a request // LoggingNotifier notifies a log about a request
@ -168,7 +168,6 @@ type FunctionAsHostBaseURLResolver struct {
// Resolve the base URL for a request // Resolve the base URL for a request
func (f FunctionAsHostBaseURLResolver) Resolve(r *http.Request) string { func (f FunctionAsHostBaseURLResolver) Resolve(r *http.Request) string {
svcName := getServiceName(r.URL.Path) svcName := getServiceName(r.URL.Path)
const watchdogPort = 8080 const watchdogPort = 8080

View File

@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"net/url"
"testing" "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)
}
})
}
}