Skip to content

Commit

Permalink
Using regex delimiter fails in character list in eskip #134 (#578)
Browse files Browse the repository at this point in the history
  • Loading branch information
snurmine authored and aryszka committed Mar 15, 2018
1 parent a4f57ec commit 060e92a
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 4 deletions.
40 changes: 39 additions & 1 deletion eskip/lexer.go
Expand Up @@ -151,8 +151,46 @@ func scanEscaped(delimiter byte, code string) ([]byte, string) {
return b, code
}

func scanRegexp(code string) ([]byte, string) {
var b []byte
escaped := false
var insideGroup = false
for len(code) > 0 {
c := code[0]
isDelimiter := c == '/'
isEscapeChar := c == escapeChar

//Check if starting [... or ending ...]. Ignore if group character is escaped i.e. \[ or \]
if !escaped && !insideGroup && c == '[' {
insideGroup = true
} else if !escaped && insideGroup && c == ']' {
insideGroup = false
}

if escaped {
//delimeter / is escaped in PathRegexp so that it means no end PathRegexp(/\//)
if !isDelimiter && !isEscapeChar {
b = append(b, escapeChar)
}
b = append(b, c)
escaped = false
} else {
if isDelimiter && !insideGroup {
return b, code
}
if isEscapeChar {
escaped = true
} else {
b = append(b, c)
}
}
code = code[1:]
}
return b, code
}

func scanRegexpLiteral(code string) (t token, rest string, err error) {
b, rest := scanEscaped('/', code[1:])
b, rest := scanRegexp(code[1:])
if len(rest) == 0 {
err = incompleteToken
return
Expand Down
30 changes: 27 additions & 3 deletions eskip/parser_test.go
Expand Up @@ -29,15 +29,15 @@ const (

routingDocumentExample = `
route0: ` + singleRouteExample + `;
route1: Path("/some/path") -> "https://backend-0.example.com";
route2: Path("/some/other/path") -> fixPath() -> "https://backend-1.example.com";
route3:
Method("POST") && Path("/api") ->
requestHeader("X-Type", "ajax-post") ->
"https://api.example.com";
catchAll: * -> "https://www.example.org";
catchAllWithCustom: * && Custom() -> "https://www.example.org"`
)
Expand Down Expand Up @@ -186,3 +186,27 @@ func TestNumber(t *testing.T) {
t.Error("failed to parse number", err)
}
}

func TestRegExp(t *testing.T) {
testRegExpOnce(t, `PathRegexp(/[/]/)-> <shunt>`, `[/]`)
testRegExpOnce(t, `PathRegexp(/[\[]/)-> <shunt>`, `[\[]`)
testRegExpOnce(t, `PathRegexp(/[\]]/)-> <shunt>`, `[\]]`)
testRegExpOnce(t, `PathRegexp(/[\\]/)-> <shunt>`, `[\]`)
testRegExpOnce(t, `PathRegexp(/[\/]/)-> <shunt>`, `[/]`)
testRegExpOnce(t, `PathRegexp(/["]/)-> <shunt>`, `["]`)
testRegExpOnce(t, `PathRegexp(/[\"]/)-> <shunt>`, `[\"]`)
testRegExpOnce(t, `PathRegexp(/\//)-> <shunt>`, `/`)
testRegExpOnce(t, `PathRegexp(/[[:upper:]]/)-> <shunt>`, `[[:upper:]]`)
}

func testRegExpOnce(t *testing.T, regexpStr string, expectedRegExp string) {
routes, err := parse(regexpStr)
if err != nil {
t.Error("failed to parse PathRegexp:"+regexpStr, err)
return
}

if expectedRegExp != routes[0].matchers[0].args[0] {
t.Error("failed to parse PathRegexp:"+regexpStr+", expected regexp to be "+expectedRegExp, err)
}
}

0 comments on commit 060e92a

Please sign in to comment.