Skip to content

Commit

Permalink
Merge 6e18e54 into 5220853
Browse files Browse the repository at this point in the history
  • Loading branch information
furkansenharputlu committed Feb 4, 2019
2 parents 5220853 + 6e18e54 commit b1fcd46
Show file tree
Hide file tree
Showing 2 changed files with 222 additions and 6 deletions.
54 changes: 48 additions & 6 deletions apidef/api_definitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,10 @@ type CircuitBreakerMeta struct {
}

type StringRegexMap struct {
MatchPattern string `bson:"match_rx" json:"match_rx"`
matchRegex *regexp.Regexp
MatchPattern string `bson:"match_rx" json:"match_rx"`
matchRegex *regexp.Regexp
NotMatchPattern string `bson:"not_match_rx" json:"not_match_rx"`
notMatchRegex *regexp.Regexp
}

type RoutingTriggerOptions struct {
Expand Down Expand Up @@ -543,23 +545,63 @@ func (a *APIDefinition) DecodeFromDB() {
}
}

func (s *StringRegexMap) Check(value string) string {
func (s *StringRegexMap) Check(value string) (match string) {
if s.matchRegex == nil {
return
}

return s.matchRegex.FindString(value)
}

func (s *StringRegexMap) FindStringSubmatch(value string) []string {
return s.matchRegex.FindStringSubmatch(value)
var matches []string
if s.matchRegex == nil || s.notMatchRegex == nil {
return matches
}

matches = s.matchRegex.FindStringSubmatch(value)
filtered := matches[:0]
for _, m := range matches {
if s.notMatchRegex.FindString(m) == "" {
filtered = append(filtered, m)
}
}

return filtered
}

func (s *StringRegexMap) FindAllStringSubmatch(value string, n int) [][]string {
return s.matchRegex.FindAllStringSubmatch(value, n)
var matches [][]string
if s.matchRegex == nil || s.notMatchRegex == nil {
return matches
}

matches = s.matchRegex.FindAllStringSubmatch(value, n)
var filtered [][]string
for i := range matches {
var filteredRow []string
for _, m := range matches[i] {
if s.notMatchRegex.FindString(m) == "" {
filteredRow = append(filteredRow, m)
}
}
filtered = append(filtered, filteredRow)
}

return filtered
}

func (s *StringRegexMap) Init() error {
var err error
if s.matchRegex, err = regexp.Compile(s.MatchPattern); err != nil {
log.WithError(err).WithField("MatchPattern", s.MatchPattern).
Error("Could not compile regexp for StringRegexMap")
Error("Could not compile matchRegex for StringRegexMap")
return err
}

if s.notMatchRegex, err = regexp.Compile(s.NotMatchPattern); err != nil {
log.WithError(err).WithField("NotMatchPattern", s.NotMatchPattern).
Error("Could not compile notMatchRegex for StringRegexMap")
return err
}
return nil
Expand Down
174 changes: 174 additions & 0 deletions mw_url_rewrite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,58 @@ func TestRewriterTriggers(t *testing.T) {
func() TestDef {
r, _ := http.NewRequest("GET", "/test/straight/rewrite", nil)

r.Header.Set("x-test", "hello-world")

hOpt := apidef.StringRegexMap{MatchPattern: "hello-(\\w+)", NotMatchPattern: "^world"}
hOpt.Init()

return TestDef{
"Header Single Group NotMatchPattern",
"/test/straight/rewrite", "/change/to/me/ignore",
"/test/straight/rewrite", "/change/to/me/hello-world",
[]apidef.RoutingTrigger{
{
On: apidef.Any,
Options: apidef.RoutingTriggerOptions{
HeaderMatches: map[string]apidef.StringRegexMap{
"x-test": hOpt,
},
},
RewriteTo: "/change/to/me/$tyk_context.trigger-0-X-Test-0",
},
},
r,
}
},
func() TestDef {
r, _ := http.NewRequest("GET", "/test/straight/rewrite", nil)

r.Header.Set("x-test", "hello-world")

hOpt := apidef.StringRegexMap{MatchPattern: "hello-(\\w+)", NotMatchPattern: "^world"}
hOpt.Init()

return TestDef{
"Header Single Group NotMathPattern",
"/test/straight/rewrite", "/change/to/me/ignore",
"/test/straight/rewrite", "/change/to/me/hello-world",
[]apidef.RoutingTrigger{
{
On: apidef.Any,
Options: apidef.RoutingTriggerOptions{
HeaderMatches: map[string]apidef.StringRegexMap{
"x-test": hOpt,
},
},
RewriteTo: "/change/to/me/$tyk_context.trigger-0-X-Test-0",
},
},
r,
}
},
func() TestDef {
r, _ := http.NewRequest("GET", "/test/straight/rewrite", nil)

patt := "bar"
r.Header.Set("x-test-Two", patt)

Expand Down Expand Up @@ -364,6 +416,30 @@ func TestRewriterTriggers(t *testing.T) {
r,
}
},
func() TestDef {
r, _ := http.NewRequest("GET", "/test/query/rewrite?x_test=foo-bar", nil)

hOpt := apidef.StringRegexMap{MatchPattern: "foo-(\\w+)", NotMatchPattern: "^bar"}
hOpt.Init()

return TestDef{
"Query Single Group NotMatchPattern",
"/test/query/rewrite", "/change/to/me/ignore",
"/test/query/rewrite", "/change/to/me/foo-bar",
[]apidef.RoutingTrigger{
{
On: apidef.Any,
Options: apidef.RoutingTriggerOptions{
QueryValMatches: map[string]apidef.StringRegexMap{
"x_test": hOpt,
},
},
RewriteTo: "/change/to/me/$tyk_context.trigger-0-x_test-0",
},
},
r,
}
},
func() TestDef {
r, _ := http.NewRequest("GET", "/test/query/rewrite?x_test=foo&y_test=bar", nil)

Expand Down Expand Up @@ -535,6 +611,29 @@ func TestRewriterTriggers(t *testing.T) {
var jsonStr = []byte(`{"foo":"barxxx", "fooble":"baryyy"}`)
r, _ := http.NewRequest("POST", "/test/pl/rewrite", bytes.NewBuffer(jsonStr))

hOpt := apidef.StringRegexMap{MatchPattern: "bar\\w*", NotMatchPattern: ".*y$"}
hOpt.Init()

return TestDef{
"Payload Multiple Match NotMatchPattern",
"/test/pl/rewrite", "/change/to/me/ignore",
"/test/pl/rewrite", "/change/to/me/barxxx/",
[]apidef.RoutingTrigger{
{
On: apidef.Any,
Options: apidef.RoutingTriggerOptions{
PayloadMatches: hOpt,
},
RewriteTo: "/change/to/me/$tyk_context.trigger-0-payload-0/$tyk_context.trigger-0-payload-1",
},
},
r,
}
},
func() TestDef {
var jsonStr = []byte(`{"foo":"barxxx", "fooble":"baryyy"}`)
r, _ := http.NewRequest("POST", "/test/pl/rewrite", bytes.NewBuffer(jsonStr))

hOpt := apidef.StringRegexMap{MatchPattern: "bar(\\w*)"}
hOpt.Init()

Expand All @@ -554,6 +653,29 @@ func TestRewriterTriggers(t *testing.T) {
r,
}
},
func() TestDef {
var jsonStr = []byte(`{"foo":"barxxx", "fooble":"baryyy"}`)
r, _ := http.NewRequest("POST", "/test/pl/rewrite", bytes.NewBuffer(jsonStr))

hOpt := apidef.StringRegexMap{MatchPattern: "bar(\\w*)", NotMatchPattern: "^x"}
hOpt.Init()

return TestDef{
"Payload Multiple Match Groups",
"/test/pl/rewrite", "/change/to/me/ignore",
"/test/pl/rewrite", "/change/to/me//yyy",
[]apidef.RoutingTrigger{
{
On: apidef.Any,
Options: apidef.RoutingTriggerOptions{
PayloadMatches: hOpt,
},
RewriteTo: "/change/to/me/$tyk_context.trigger-0-payload-0-0/$tyk_context.trigger-0-payload-1-0",
},
},
r,
}
},
func() TestDef {
r, _ := http.NewRequest("GET", "/test/foo/rewrite", nil)
hOpt := apidef.StringRegexMap{MatchPattern: "foo"}
Expand Down Expand Up @@ -600,6 +722,29 @@ func TestRewriterTriggers(t *testing.T) {
r,
}
},
func() TestDef {
r, _ := http.NewRequest("GET", "/test/foobar/rewrite", nil)
hOpt := apidef.StringRegexMap{MatchPattern: "foo(\\w+)", NotMatchPattern: "^bar"}
hOpt.Init()

return TestDef{
"PathPart Single Group NotMatchPattern",
"/test/foobar/rewrite", "/change/to/me/ignore",
"/test/foobar/rewrite", "/change/to/me/foobar",
[]apidef.RoutingTrigger{
{
On: apidef.Any,
Options: apidef.RoutingTriggerOptions{
PathPartMatches: map[string]apidef.StringRegexMap{
"pathpart": hOpt,
},
},
RewriteTo: "/change/to/me/$tyk_context.trigger-0-pathpart-0",
},
},
r,
}
},
func() TestDef {
r, _ := http.NewRequest("GET", "/test/foo/rewrite/foo", nil)
hOpt := apidef.StringRegexMap{MatchPattern: "foo"}
Expand Down Expand Up @@ -681,6 +826,35 @@ func TestRewriterTriggers(t *testing.T) {
r,
}
},
func() TestDef {
r, _ := http.NewRequest("GET", "/test/foo/rewrite", nil)
hOpt := apidef.StringRegexMap{MatchPattern: "bar-(\\w+)", NotMatchPattern: "b"}
hOpt.Init()

ctxSetSession(r, &user.SessionState{
MetaData: map[string]interface{}{
"rewrite": "bar-baz",
},
}, "", false)

return TestDef{
"Meta Simple Group NotMatchPattern",
"/test/foo/rewrite", "/change/to/me/ignore",
"/test/foo/rewrite", "/change/to/me/ignore",
[]apidef.RoutingTrigger{
{
On: apidef.Any,
Options: apidef.RoutingTriggerOptions{
SessionMetaMatches: map[string]apidef.StringRegexMap{
"rewrite": hOpt,
},
},
RewriteTo: "/change/to/me/$tyk_context.trigger-0-rewrite-0",
},
},
r,
}
},
func() TestDef {
r, _ := http.NewRequest("GET", "/test/foo/rewrite", nil)
hOpt := apidef.StringRegexMap{MatchPattern: "bar"}
Expand Down

0 comments on commit b1fcd46

Please sign in to comment.