Skip to content

Commit

Permalink
Improve parsing nginx.org/rewrites annotation
Browse files Browse the repository at this point in the history
Handle leading and trailing whitespace when parsing rewrites
for a service.
  • Loading branch information
pleshakov committed Oct 4, 2018
1 parent b561c62 commit 78e3e44
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion internal/nginx/configurator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 13 additions & 0 deletions internal/nginx/configurator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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=/"

Expand Down

0 comments on commit 78e3e44

Please sign in to comment.