Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
kataras committed Dec 21, 2017
1 parent 53e79a1 commit 0e07d15
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## I'm working hard on the [dev](https://github.com/kataras/iris/tree/dev) branch for the next release of Iris, v9.
## I'm working hard on the [dev](https://github.com/kataras/iris/tree/dev) branch for the next release of Iris.

Do you remember, last Christmas? I did publish the version 6 with net/http and HTTP/2 support, and you've embraced Iris with so much love, ultimately it was a successful move.

Expand Down
6 changes: 2 additions & 4 deletions core/router/api_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,14 +199,12 @@ func (api *APIBuilder) Handle(method string, relativePath string, handlers ...co
// This method is used behind the scenes at the `Controller` function
// in order to handle more than one paths for the same controller instance.
func (api *APIBuilder) HandleMany(methodOrMulti string, relativePathorMulti string, handlers ...context.Handler) (routes []*Route) {
trimmedPath := strings.Trim(relativePathorMulti, " ")
trimmedMethod := strings.Trim(methodOrMulti, " ")
// at least slash
// a space
// at least one other slash for the next path
// app.Controller("/user /user{id}", new(UserController))
paths := strings.Split(trimmedPath, " ")
methods := strings.Split(trimmedMethod, " ")
paths := splitPath(relativePathorMulti)
methods := splitMethod(methodOrMulti)
for _, p := range paths {
if p != "" {
for _, method := range methods {
Expand Down
20 changes: 20 additions & 0 deletions core/router/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,26 @@ func suffix(s string, suffix string) string {
return s
}

func splitMethod(methodMany string) []string {
methodMany = strings.Trim(methodMany, " ")
return strings.Split(methodMany, " ")
}

func splitPath(pathMany string) (paths []string) {
pathMany = strings.Trim(pathMany, " ")
pathsWithoutSlashFromFirstAndSoOn := strings.Split(pathMany, " /")
for _, path := range pathsWithoutSlashFromFirstAndSoOn {
if path == "" {
continue
}
if path[0] != '/' {
path = "/" + path
}
paths = append(paths, path)
}
return
}

func joinPath(path1 string, path2 string) string {
return path.Join(path1, path2)
}
Expand Down
38 changes: 38 additions & 0 deletions core/router/path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,44 @@ import (
"testing"
)

func TestSplitPath(t *testing.T) {
tests := []struct {
path string
expected []string
}{
{"/v2/stores/{id:string format(uuid)} /v3",
[]string{"/v2/stores/{id:string format(uuid)}", "/v3"}},
{"/user/{id:int} /admin/{id:int}",
[]string{"/user/{id:int}", "/admin/{id:int}"}},
{"/user /admin",
[]string{"/user", "/admin"}},
{"/single_no_params",
[]string{"/single_no_params"}},
{"/single/{id:int}",
[]string{"/single/{id:int}"}},
}

equalSlice := func(s1 []string, s2 []string) bool {
if len(s1) != len(s2) {
return false
}

for i := range s1 {
if s2[i] != s1[i] {
return false
}
}

return true
}

for i, tt := range tests {
paths := splitPath(tt.path)
if expected, got := tt.expected, paths; !equalSlice(expected, got) {
t.Fatalf("[%d] - expected paths '%#v' but got '%#v'", i, expected, got)
}
}
}
func TestSplitSubdomainAndPath(t *testing.T) {
tests := []struct {
original string
Expand Down

0 comments on commit 0e07d15

Please sign in to comment.