Skip to content

Commit 1560c5d

Browse files
committed
fixed Issue #140, changed example to use wildcard + curly router, fixed test
1 parent 35a224b commit 1560c5d

File tree

4 files changed

+22
-9
lines changed

4 files changed

+22
-9
lines changed

curly.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func (c CurlyRouter) matchesRouteByPathTokens(routeTokens, requestTokens []strin
8282

8383
// regularMatchesPathToken tests whether the regular expression part of routeToken matches the requestToken or all remaining tokens
8484
// format routeToken is {someVar:someExpression}, e.g. {zipcode:[\d][\d][\d][\d][A-Z][A-Z]}
85-
func (c CurlyRouter) regularMatchesPathToken(routeToken string, colon int, requestToken string) (bool, bool) {
85+
func (c CurlyRouter) regularMatchesPathToken(routeToken string, colon int, requestToken string) (matchesToken bool, matchesRemainder bool) {
8686
regPart := routeToken[colon+1 : len(routeToken)-1]
8787
if regPart == "*" {
8888
return true, true

curly_test.go

+12-4
Original file line numberDiff line numberDiff line change
@@ -137,16 +137,24 @@ func Test_matchesRouteByPathTokens(t *testing.T) {
137137
// clear && go test -v -test.run TestExtractParameters_Wildcard1 ...restful
138138
func TestExtractParameters_Wildcard1(t *testing.T) {
139139
params := doExtractParams("/fixed/{var:*}", 2, "/fixed/remainder", t)
140-
if params["var"] == "remainder" {
141-
t.Errorf("parameter mismatch var")
140+
if params["var"] != "remainder" {
141+
t.Errorf("parameter mismatch var: %s", params["var"])
142142
}
143143
}
144144

145145
// clear && go test -v -test.run TestExtractParameters_Wildcard2 ...restful
146146
func TestExtractParameters_Wildcard2(t *testing.T) {
147147
params := doExtractParams("/fixed/{var:*}", 2, "/fixed/remain/der", t)
148-
if params["var"] == "remain/der" {
149-
t.Errorf("parameter mismatch var")
148+
if params["var"] != "remain/der" {
149+
t.Errorf("parameter mismatch var: %s", params["var"])
150+
}
151+
}
152+
153+
// clear && go test -v -test.run TestExtractParameters_Wildcard3 ...restful
154+
func TestExtractParameters_Wildcard3(t *testing.T) {
155+
params := doExtractParams("/static/{var:*}", 2, "/static/test/sub/hi.html", t)
156+
if params["var"] != "test/sub/hi.html" {
157+
t.Errorf("parameter mismatch var: %s", params["var"])
150158
}
151159
}
152160

examples/restful-serve-static.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package main
22

33
import (
4-
"github.com/emicklei/go-restful"
4+
"fmt"
55
"net/http"
66
"path"
7+
8+
"github.com/emicklei/go-restful"
79
)
810

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

1921
func main() {
22+
restful.DefaultContainer.Router(restful.CurlyRouter{})
2023

2124
ws := new(restful.WebService)
22-
ws.Route(ws.GET("/static/{resource}").To(staticFromPathParam))
25+
ws.Route(ws.GET("/static/{subpath:*}").To(staticFromPathParam))
2326
ws.Route(ws.GET("/static").To(staticFromQueryParam))
2427
restful.Add(ws)
2528

@@ -28,10 +31,12 @@ func main() {
2831
}
2932

3033
func staticFromPathParam(req *restful.Request, resp *restful.Response) {
34+
actual := path.Join(rootdir, req.PathParameter("subpath"))
35+
fmt.Printf("serving %s ... (from %s)\n", actual, req.PathParameter("subpath"))
3136
http.ServeFile(
3237
resp.ResponseWriter,
3338
req.Request,
34-
path.Join(rootdir, req.PathParameter("resource")))
39+
actual)
3540
}
3641

3742
func staticFromQueryParam(req *restful.Request, resp *restful.Response) {

route.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ func (r Route) extractParameters(urlPath string) map[string]string {
124124
regPart := key[colon+1 : len(key)-1]
125125
keyPart := key[1:colon]
126126
if regPart == "*" {
127-
pathParameters[keyPart] = untokenizePath(i+1, urlParts)
127+
pathParameters[keyPart] = untokenizePath(i, urlParts)
128128
break
129129
} else {
130130
pathParameters[keyPart] = value

0 commit comments

Comments
 (0)