Skip to content

Commit

Permalink
fix bug when having at least 3 prepend filters (#1239)
Browse files Browse the repository at this point in the history
* fix bug when having at least 3 prepend filters

Signed-off-by: Robert Biter <robert.biter@zalando.de>

* use copy function instead of for

Signed-off-by: Robert Biter <robert.biter@zalando.de>
  • Loading branch information
NorthFury authored and szuecs committed Oct 31, 2019
1 parent 27a42f2 commit 9644c00
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
17 changes: 15 additions & 2 deletions eskip/eskip.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,25 @@ type DefaultFilters struct {
// prepends filters stored to incoming routes and returns the modified
// version of routes.
func (df *DefaultFilters) Do(routes []*Route) []*Route {
pn := len(df.Prepend)
an := len(df.Append)
if pn == 0 && an == 0 {
return routes
}

nextRoutes := make([]*Route, len(routes))
for i, r := range routes {
nextRoutes[i] = new(Route)
*nextRoutes[i] = *r
nextRoutes[i].Filters = append(df.Prepend, nextRoutes[i].Filters...)
nextRoutes[i].Filters = append(nextRoutes[i].Filters, df.Append...)

fn := len(r.Filters)

filters := make([]*Filter, fn+pn+an)
copy(filters[:pn], df.Prepend)
copy(filters[pn:pn+fn], r.Filters)
copy(filters[pn+fn:], df.Append)

nextRoutes[i].Filters = filters
}

return nextRoutes
Expand Down
24 changes: 24 additions & 0 deletions eskip/eskip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -562,3 +562,27 @@ func TestDefaultFiltersDo(t *testing.T) {
}

}

func TestDefaultFiltersDoCorrectPrependFilters(t *testing.T) {
filters, err := ParseFilters("status(1) -> status(2) -> status(3)")
if err != nil {
t.Errorf("Failed to parse filter: %v", err)
}

routes, err := Parse(`
r1: Method("GET") -> inlineContent("r1") -> <shunt>;
r2: Method("POST") -> inlineContent("r2") -> <shunt>;
`)
if err != nil {
t.Errorf("Failed to parse route: %v", err)
}

df := &DefaultFilters{Prepend: filters}

finalRoutes := df.Do(routes)
for _, route := range finalRoutes {
if route.Id != route.Filters[len(route.Filters)-1].Args[0].(string) {
t.Errorf("Route %v has incorrect filters: %v", route.Id, route.Filters[3])
}
}
}

0 comments on commit 9644c00

Please sign in to comment.