-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from JSainsburyPLC/feature/rewrite-rules
Support nginx style URL rewrites
Showing
10 changed files
with
194 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
ui-dev-proxy | ||
dist/ | ||
.sequence/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,9 @@ | ||
module github.com/JSainsburyPLC/ui-dev-proxy | ||
|
||
go 1.12 | ||
go 1.13 | ||
|
||
require ( | ||
github.com/steinfletcher/apitest v1.3.8 | ||
github.com/stretchr/objx v0.2.0 // indirect | ||
github.com/steinfletcher/apitest v1.3.13 | ||
github.com/stretchr/testify v1.4.0 | ||
github.com/urfave/cli v1.22.1 | ||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package rewrite | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"net/url" | ||
"path" | ||
"regexp" | ||
) | ||
|
||
type Rule struct { | ||
pattern string | ||
to string | ||
regexp *regexp.Regexp | ||
} | ||
|
||
func NewRule(pattern, to string) (Rule, error) { | ||
reg, err := regexp.Compile(pattern) | ||
if err != nil { | ||
return Rule{}, err | ||
} | ||
|
||
return Rule{ | ||
pattern: pattern, | ||
to: to, | ||
regexp: reg, | ||
}, nil | ||
} | ||
|
||
func (r *Rule) Rewrite(req *http.Request) (bool, error) { | ||
oriPath := req.URL.Path | ||
|
||
if !r.regexp.MatchString(oriPath) { | ||
return false, nil | ||
} | ||
|
||
to := path.Clean(r.Replace(req.URL)) | ||
u, e := url.Parse(to) | ||
if e != nil { | ||
return false, fmt.Errorf("rewritten URL is not valid. %w", e) | ||
} | ||
|
||
req.URL.Path = u.Path | ||
req.URL.RawPath = u.RawPath | ||
if u.RawQuery != "" { | ||
req.URL.RawQuery = u.RawQuery | ||
} | ||
|
||
return true, nil | ||
} | ||
|
||
func (r *Rule) Replace(u *url.URL) string { | ||
uri := u.RequestURI() | ||
patternRegexp := regexp.MustCompile(r.pattern) | ||
match := patternRegexp.FindStringSubmatchIndex(uri) | ||
result := patternRegexp.ExpandString([]byte(""), r.to, uri, match) | ||
return string(result[:]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package rewrite_test | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/JSainsburyPLC/ui-dev-proxy/http/rewrite" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestRewrite(t *testing.T) { | ||
tests := map[string]struct { | ||
pattern string | ||
to string | ||
before string | ||
after string | ||
matched bool | ||
}{ | ||
"constant": { | ||
pattern: "/a", | ||
to: "/b", | ||
before: "/a", | ||
after: "/b", | ||
matched: true, | ||
}, | ||
"preserves original URL if no match": { | ||
pattern: "/a", | ||
to: "/b", | ||
before: "/c", | ||
after: "/c", | ||
matched: false, | ||
}, | ||
"match group": { | ||
pattern: "/api/(.*)", | ||
to: "/$1", | ||
before: "/api/my-endpoint", | ||
after: "/my-endpoint", | ||
matched: true, | ||
}, | ||
"multiple match groups": { | ||
pattern: "/a/(.*)/b/(.*)", | ||
to: "/x/y/$1/z/$2", | ||
before: "/a/oo/b/qq", | ||
after: "/x/y/oo/z/qq", | ||
matched: true, | ||
}, | ||
"encoded characters": { | ||
pattern: "/a/(.*)", | ||
to: "/b/$1", | ||
before: "/a/x-1%2F", | ||
after: "/b/x-1%2F", | ||
matched: true, | ||
}, | ||
} | ||
for name, test := range tests { | ||
t.Run(name, func(t *testing.T) { | ||
req, err := http.NewRequest("GET", test.before, nil) | ||
if err != nil { | ||
t.Fatalf("failed to create request %v %v", test, err) | ||
} | ||
rule, err := rewrite.NewRule(test.pattern, test.to) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
matched, err := rule.Rewrite(req) | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, test.after, req.URL.EscapedPath()) | ||
assert.Equal(t, test.matched, matched) | ||
}) | ||
} | ||
} |