diff --git a/internal/nginx/configurator.go b/internal/nginx/configurator.go index 813a5b1816..d425cb8a15 100644 --- a/internal/nginx/configurator.go +++ b/internal/nginx/configurator.go @@ -637,7 +637,7 @@ func getRewrites(ingEx *IngressEx) map[string]string { } func parseRewrites(service string) (serviceName string, rewrite string, err error) { - parts := strings.SplitN(service, " ", 2) + parts := strings.SplitN(strings.TrimSpace(service), " ", 2) if len(parts) != 2 { return "", "", fmt.Errorf("Invalid rewrite format: %s", service) diff --git a/internal/nginx/configurator_test.go b/internal/nginx/configurator_test.go index 2b1bffcb21..1b7b1e1e91 100644 --- a/internal/nginx/configurator_test.go +++ b/internal/nginx/configurator_test.go @@ -42,6 +42,19 @@ func TestParseRewrites(t *testing.T) { } } +func TestParseRewritesWithLeadingAndTrailingWhitespace(t *testing.T) { + serviceName := "coffee-svc" + serviceNamePart := "serviceName=" + serviceName + rewritePath := "/beans/" + rewritePathPart := "rewrite=" + rewritePath + rewriteService := "\t\n " + serviceNamePart + " " + rewritePathPart + " \t\n" + + serviceNameActual, rewritePathActual, err := parseRewrites(rewriteService) + if serviceName != serviceNameActual || rewritePath != rewritePathActual || err != nil { + t.Errorf("parseRewrites(%s) should return %q, %q, nil; got %q, %q, %v", rewriteService, serviceName, rewritePath, serviceNameActual, rewritePathActual, err) + } +} + func TestParseRewritesInvalidFormat(t *testing.T) { rewriteService := "serviceNamecoffee-svc rewrite=/"