Skip to content

Commit

Permalink
Add support for explicit 404s
Browse files Browse the repository at this point in the history
  • Loading branch information
robfig committed Aug 29, 2012
1 parent 6e56e8f commit e0072fa
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
12 changes: 12 additions & 0 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,13 @@ func (r *Route) Match(method string, reqPath string) *RouteMatch {
}
}

// Special handling for explicit 404's.
if action == "404" {
return &RouteMatch{
Action: "404",
}
}

// Split the action into controller and method
actionSplit := strings.Split(action, ".")
if len(actionSplit) != 2 {
Expand Down Expand Up @@ -236,6 +243,11 @@ func (router *Router) validate(route *Route) *Error {
return nil
}

// Skip 404s
if route.Action == "404" {
return nil
}

// We should be able to load the action.
parts := strings.Split(route.Action, ".")
if len(parts) != 2 {
Expand Down
15 changes: 14 additions & 1 deletion router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,9 @@ GET /app/{id} Application.Show
POST /app/{id} Application.Save
GET /public/ staticDir:www
* /{controller}/{action} {controller}.{action}
* /{controller}/{action} {controller}.{action}
GET /favicon.ico 404
`

var routeMatchTestCases = map[*http.Request]*RouteMatch{
Expand Down Expand Up @@ -201,6 +203,17 @@ var routeMatchTestCases = map[*http.Request]*RouteMatch{
Params: map[string]string{"controller": "Implicit", "action": "Route"},
StaticFilename: "",
},

&http.Request{
Method: "GET",
URL: &url.URL{Path: "/favicon.ico"},
}: &RouteMatch{
ControllerName: "",
MethodName: "",
Action: "404",
Params: map[string]string{},
StaticFilename: "",
},
}

func TestRouteMatches(t *testing.T) {
Expand Down
6 changes: 6 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ func handleInternal(w http.ResponseWriter, r *http.Request, ws *websocket.Conn)
return
}

// The route may want to explicitly return a 404.
if route.Action == "404" {
NotFound(req, resp, "(intentionally)")
return
}

// Dispatch the static files first.
if route.StaticFilename != "" {
http.ServeFile(w, r, route.StaticFilename)
Expand Down

0 comments on commit e0072fa

Please sign in to comment.