Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

Returning an error if the route helper params are incomplete #1003

Merged
merged 2 commits into from
Apr 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions route.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}
}

Expand Down Expand Up @@ -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,
Expand Down
65 changes: 43 additions & 22 deletions router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}) %>
`))
}

Expand All @@ -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) {
Expand Down