From efd19c7048d5722bffe8e89772a340632999392d Mon Sep 17 00:00:00 2001 From: Antonio Pagano Date: Tue, 3 Apr 2018 17:34:18 -0500 Subject: [PATCH 1/2] returning an eerror if the route functions doesn't have needed params --- Dockerfile | 1 + route.go | 8 +++---- router_test.go | 65 +++++++++++++++++++++++++++++++++----------------- 3 files changed, 48 insertions(+), 26 deletions(-) diff --git a/Dockerfile b/Dockerfile index ca2dfbfcb..4237fe091 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,6 +6,7 @@ RUN go get -v -u github.com/golang/lint/golint RUN go get -v -u github.com/markbates/filetest RUN go get -v -u github.com/gobuffalo/makr RUN go get -v -u github.com/markbates/grift +RUN go get -v -u github.com/markbates/willie RUN go get -v -u github.com/markbates/inflect RUN go get -v -u github.com/markbates/refresh RUN go get -v -u github.com/gobuffalo/tags diff --git a/route.go b/route.go index 0ad147349..853bcb101 100644 --- a/route.go +++ b/route.go @@ -79,7 +79,7 @@ func (ri *RouteInfo) Name(name string) *RouteInfo { //BuildPathHelper Builds a routeHelperfunc for a particular RouteInfo func (ri *RouteInfo) BuildPathHelper() RouteHelperFunc { cRoute := ri - return func(opts map[string]interface{}) template.HTML { + return func(opts map[string]interface{}) (template.HTML, error) { pairs := []string{} for k, v := range opts { pairs = append(pairs, k) @@ -88,13 +88,13 @@ func (ri *RouteInfo) BuildPathHelper() RouteHelperFunc { url, err := cRoute.MuxRoute.URL(pairs...) if err != nil { - return template.HTML(cRoute.Path) + return "", fmt.Errorf("missing parameters for %v", cRoute.Path) } result := url.Path result = addExtraParamsTo(result, opts) - return template.HTML(result) + return template.HTML(result), nil } } @@ -138,7 +138,7 @@ func addExtraParamsTo(path string, opts map[string]interface{}) string { } //RouteHelperFunc represents the function that takes the route and the opts and build the path -type RouteHelperFunc func(opts map[string]interface{}) template.HTML +type RouteHelperFunc func(opts map[string]interface{}) (template.HTML, error) // RouteList contains a mapping of the routes defined // in the application. This listing contains, Method, Path, diff --git a/router_test.go b/router_test.go index 27f5e8210..9efb8b0be 100644 --- a/router_test.go +++ b/router_test.go @@ -358,17 +358,15 @@ func Test_App_NamedRoutes(t *testing.T) { c.Set("opts", map[string]interface{}{}) return c.Render(200, rr.String(` 1. <%= rootPath() %> - 2. <%= usersPath() %> - 3. <%= userPath({user_id: 1}) %> - 4. <%= myPeepsPath() %> - 5. <%= userPath(opts) %> - 6. <%= carPath({car_id: 1}) %> - 7. <%= newCarPath() %> - 8. <%= editCarPath({car_id: 1}) %> - 9. <%= editCarPath({car_id: 1, other: 12}) %> - 10. <%= rootPath({"some":"variable","other": 12}) %> - 11. <%= rootPath() %> - 12. <%= rootPath({"special/":"12=ss"}) %> + 2. <%= userPath({user_id: 1}) %> + 3. <%= myPeepsPath() %> + 5. <%= carPath({car_id: 1}) %> + 6. <%= newCarPath() %> + 7. <%= editCarPath({car_id: 1}) %> + 8. <%= editCarPath({car_id: 1, other: 12}) %> + 9. <%= rootPath({"some":"variable","other": 12}) %> + 10. <%= rootPath() %> + 11. <%= rootPath({"special/":"12=ss"}) %> `)) } @@ -383,17 +381,40 @@ func Test_App_NamedRoutes(t *testing.T) { r.Equal(200, res.Code) r.Contains(res.Body.String(), "1. /") - r.Contains(res.Body.String(), "2. /users") - r.Contains(res.Body.String(), "3. /users/1") - r.Contains(res.Body.String(), "4. /peeps") - r.Contains(res.Body.String(), "5. /users/{user_id}") - r.Contains(res.Body.String(), "6. /car/1") - r.Contains(res.Body.String(), "7. /car/new") - r.Contains(res.Body.String(), "8. /car/1/edit") - r.Contains(res.Body.String(), "9. /car/1/edit?other=12") - r.Contains(res.Body.String(), "10. /?other=12&some=variable") - r.Contains(res.Body.String(), "11. /") - r.Contains(res.Body.String(), "12. /?special%2F=12%3Dss") + r.Contains(res.Body.String(), "2. /users/1") + r.Contains(res.Body.String(), "3. /peeps") + r.Contains(res.Body.String(), "5. /car/1") + r.Contains(res.Body.String(), "6. /car/new") + r.Contains(res.Body.String(), "7. /car/1/edit") + r.Contains(res.Body.String(), "8. /car/1/edit?other=12") + r.Contains(res.Body.String(), "9. /?other=12&some=variable") + r.Contains(res.Body.String(), "10. /") + r.Contains(res.Body.String(), "11. /?special%2F=12%3Dss") +} + +func Test_App_NamedRoutes_MissingParameter(t *testing.T) { + r := require.New(t) + a := New(Options{}) + + rr := render.New(render.Options{ + HTMLLayout: "application.html", + TemplatesBox: packr.NewBox("../templates"), + Helpers: map[string]interface{}{}, + }) + + sampleHandler := func(c Context) error { + c.Set("opts", map[string]interface{}{}) + return c.Render(200, rr.String(` + <%= userPath(opts) %> + `)) + } + + a.GET("/users/{user_id}", sampleHandler) + w := willie.New(a) + res := w.Request("/users/1").Get() + + r.Equal(500, res.Code) + r.Contains(res.Body.String(), "missing parameters for /users/{user_id}") } func Test_Resource(t *testing.T) { From fdd89dd5d9f85589849a15db5fc84de7916f0622 Mon Sep 17 00:00:00 2001 From: Antonio Pagano Date: Tue, 3 Apr 2018 17:39:34 -0500 Subject: [PATCH 2/2] removing willie from dockerfile --- Dockerfile | 1 - 1 file changed, 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 4237fe091..ca2dfbfcb 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,7 +6,6 @@ RUN go get -v -u github.com/golang/lint/golint RUN go get -v -u github.com/markbates/filetest RUN go get -v -u github.com/gobuffalo/makr RUN go get -v -u github.com/markbates/grift -RUN go get -v -u github.com/markbates/willie RUN go get -v -u github.com/markbates/inflect RUN go get -v -u github.com/markbates/refresh RUN go get -v -u github.com/gobuffalo/tags