Skip to content

Commit

Permalink
fix rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
luominggang committed Jan 9, 2019
1 parent 9de8acd commit 9435b12
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
6 changes: 5 additions & 1 deletion http/httpProxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,11 @@ func (r *rewriteRule) rewrite(uri string) (string, bool) {
ruleMatched = !ruleMatched
}
if ruleMatched {
return string(r.pattern.ExpandString(nil, r.replace, uri, r.pattern.FindStringSubmatchIndex(uri))), true
matchedIndex := r.pattern.FindStringSubmatchIndex(uri)
if matchedIndex == nil {
return uri, false
}
return string(r.pattern.ExpandString(nil, r.replace, uri, matchedIndex)), true
}
return uri, false
}
Expand Down
9 changes: 8 additions & 1 deletion http/httpProxy_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package http

import (
"github.com/stretchr/testify/assert"
"fmt"
"testing"

"github.com/stretchr/testify/assert"
)

func TestNewLocationMatcher(t *testing.T) {
matcher := NewLocationMatcher([]*ProxyLocation{
{Upstream: "test1", Match: "/", Type: "start", RewriteRules: []string{"exact /Test2/1 /(.*) /test"}},
{Upstream: "test2", Match: "/test2/.*", Type: "regexp", RewriteRules: []string{"!iregexp ^/Test2/1/.* ^/test2/(.*) /test/$1"}},
{Upstream: "test3", Match: "/test3/.*", Type: "iregexp", RewriteRules: []string{"start / ^/(.*) /test/$1"}},
{Upstream: "test4", Match: "^(/|/2/)(p1|p2).*", Type: "regexp", RewriteRules: []string{"start / ^/(p1|p2)/(.*) /2/$1/$2"}},
})

service := ""
Expand All @@ -28,4 +31,8 @@ func TestNewLocationMatcher(t *testing.T) {
service, rewritePath, _ = matcher.Pick("/Test2/1", true)
assert.Equal(t, "test1", service)
assert.Equal(t, "/test", rewritePath)
fmt.Println(matcher.Pick("/p1/test", true))
fmt.Println(matcher.Pick("/p2/test", true))
fmt.Println(matcher.Pick("/2/p1/test", true))
fmt.Println(matcher.Pick("/2/p2/test", true))
}

0 comments on commit 9435b12

Please sign in to comment.