-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed the router allow url that contains uri (#138)
- Loading branch information
Showing
7 changed files
with
127 additions
and
11 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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
--- | ||
request: | ||
method: GET | ||
path: /api/v1/notes/urn:organization:notebook:eu:note:123 | ||
response: | ||
statusCode: 200 | ||
headers: | ||
Content-Type: | ||
- "application/json" | ||
body: "It works" |
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,65 @@ | ||
package route | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
type Route struct { | ||
Keys []string | ||
Regex *regexp.Regexp | ||
Pattern string | ||
} | ||
|
||
type Match struct { | ||
Params Params | ||
Pattern string | ||
} | ||
|
||
type Params map[string]string | ||
|
||
func (route *Route) Params(url string) Params { | ||
match := route.Regex.FindAllStringSubmatch(url, -1)[0][1:] | ||
result := make(Params) | ||
|
||
for i := range match { | ||
if len(route.Keys) <= i { | ||
break | ||
} | ||
|
||
result[route.Keys[i]] = match[i] | ||
} | ||
|
||
return result | ||
} | ||
func (route *Route) Match(url string) *Match { | ||
if route.Regex == nil || !route.Regex.MatchString(url) { | ||
return nil | ||
} | ||
return &Match{route.Params(url), route.Pattern} | ||
} | ||
|
||
func NewRoute(pattern string) *Route { | ||
regex, keys := pathToRegex(pattern) | ||
return &Route{keys, regex, pattern} | ||
} | ||
|
||
func pathToRegex(path string) (*regexp.Regexp, []string) { | ||
pattern, _ := regexp.Compile("\\/:([A-Za-z0-9]+)") | ||
matches := pattern.FindAllStringSubmatch(path, -1) | ||
keys := []string{} | ||
|
||
for i := range matches { | ||
keys = append(keys, matches[i][1]) | ||
} | ||
|
||
str := fmt.Sprintf("^%s\\/?$", strings.Replace(path, "/", "\\/", -1)) | ||
|
||
str = pattern.ReplaceAllString(str, "/([^\\/]+)") | ||
str = strings.Replace(str, ".", "\\.", -1) | ||
|
||
regex, _ := regexp.Compile(str) | ||
|
||
return regex, keys | ||
} |
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,45 @@ | ||
package route | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func TestMatching(t *testing.T) { | ||
|
||
route := NewRoute("/user/:name") | ||
equal(t, route.Match("/user/azer").Pattern, "/user/:name") | ||
|
||
route = NewRoute("/fruits/:fruit") | ||
equal(t, route.Match("/fruits/watermelon").Pattern, "/fruits/:fruit") | ||
|
||
route = NewRoute("/fruits/:fruit/:page") | ||
equal(t, route.Match("/fruits/cherry/452").Pattern, "/fruits/:fruit/:page") | ||
|
||
route = NewRoute("/api/urn:note:123") | ||
equal(t, route.Match("/api/urn:note:123").Pattern, "/api/urn:note:123") | ||
|
||
route = NewRoute("/api/:userid.json") | ||
equal(t, route.Match("/api/99.json").Pattern, "/api/:userid.json") | ||
|
||
route = NewRoute("/") | ||
equal(t, route.Match("/").Pattern, "/") | ||
} | ||
|
||
func TestParams(t *testing.T) { | ||
route := NewRoute("/user/:name") | ||
equal(t, route.Match("/user/azer").Params["name"], "azer") | ||
|
||
route = NewRoute("/fruits/:fruit") | ||
equal(t, route.Match("/fruits/watermelon").Params["fruit"], "watermelon") | ||
|
||
route = NewRoute("/fruits/:fruit/:page") | ||
equal(t, route.Match("/fruits/cherry/452").Params["fruit"], "cherry") | ||
equal(t, route.Match("/fruits/cherry/452").Params["page"], "452") | ||
} | ||
|
||
func equal(t *testing.T, a string, b string) { | ||
if a != b { | ||
t.Error(fmt.Sprintf("%s and %s aren't equal", a, b)) | ||
} | ||
} |
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