Skip to content

Commit

Permalink
fixed Issue #140, changed example to use wildcard + curly router, fix…
Browse files Browse the repository at this point in the history
…ed test
  • Loading branch information
emicklei committed Aug 30, 2014
1 parent 35a224b commit 1560c5d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
2 changes: 1 addition & 1 deletion curly.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (c CurlyRouter) matchesRouteByPathTokens(routeTokens, requestTokens []strin

// regularMatchesPathToken tests whether the regular expression part of routeToken matches the requestToken or all remaining tokens
// format routeToken is {someVar:someExpression}, e.g. {zipcode:[\d][\d][\d][\d][A-Z][A-Z]}
func (c CurlyRouter) regularMatchesPathToken(routeToken string, colon int, requestToken string) (bool, bool) {
func (c CurlyRouter) regularMatchesPathToken(routeToken string, colon int, requestToken string) (matchesToken bool, matchesRemainder bool) {
regPart := routeToken[colon+1 : len(routeToken)-1]
if regPart == "*" {
return true, true
Expand Down
16 changes: 12 additions & 4 deletions curly_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,16 +137,24 @@ func Test_matchesRouteByPathTokens(t *testing.T) {
// clear && go test -v -test.run TestExtractParameters_Wildcard1 ...restful
func TestExtractParameters_Wildcard1(t *testing.T) {
params := doExtractParams("/fixed/{var:*}", 2, "/fixed/remainder", t)
if params["var"] == "remainder" {
t.Errorf("parameter mismatch var")
if params["var"] != "remainder" {
t.Errorf("parameter mismatch var: %s", params["var"])
}
}

// clear && go test -v -test.run TestExtractParameters_Wildcard2 ...restful
func TestExtractParameters_Wildcard2(t *testing.T) {
params := doExtractParams("/fixed/{var:*}", 2, "/fixed/remain/der", t)
if params["var"] == "remain/der" {
t.Errorf("parameter mismatch var")
if params["var"] != "remain/der" {
t.Errorf("parameter mismatch var: %s", params["var"])
}
}

// clear && go test -v -test.run TestExtractParameters_Wildcard3 ...restful
func TestExtractParameters_Wildcard3(t *testing.T) {
params := doExtractParams("/static/{var:*}", 2, "/static/test/sub/hi.html", t)
if params["var"] != "test/sub/hi.html" {
t.Errorf("parameter mismatch var: %s", params["var"])
}
}

Expand Down
11 changes: 8 additions & 3 deletions examples/restful-serve-static.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package main

import (
"github.com/emicklei/go-restful"
"fmt"
"net/http"
"path"

"github.com/emicklei/go-restful"
)

// This example shows how to define methods that serve static files
Expand All @@ -17,9 +19,10 @@ import (
var rootdir = "/tmp"

func main() {
restful.DefaultContainer.Router(restful.CurlyRouter{})

ws := new(restful.WebService)
ws.Route(ws.GET("/static/{resource}").To(staticFromPathParam))
ws.Route(ws.GET("/static/{subpath:*}").To(staticFromPathParam))
ws.Route(ws.GET("/static").To(staticFromQueryParam))
restful.Add(ws)

Expand All @@ -28,10 +31,12 @@ func main() {
}

func staticFromPathParam(req *restful.Request, resp *restful.Response) {
actual := path.Join(rootdir, req.PathParameter("subpath"))
fmt.Printf("serving %s ... (from %s)\n", actual, req.PathParameter("subpath"))
http.ServeFile(
resp.ResponseWriter,
req.Request,
path.Join(rootdir, req.PathParameter("resource")))
actual)
}

func staticFromQueryParam(req *restful.Request, resp *restful.Response) {
Expand Down
2 changes: 1 addition & 1 deletion route.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func (r Route) extractParameters(urlPath string) map[string]string {
regPart := key[colon+1 : len(key)-1]
keyPart := key[1:colon]
if regPart == "*" {
pathParameters[keyPart] = untokenizePath(i+1, urlParts)
pathParameters[keyPart] = untokenizePath(i, urlParts)
break
} else {
pathParameters[keyPart] = value
Expand Down

0 comments on commit 1560c5d

Please sign in to comment.