Skip to content

Commit

Permalink
fix handling no match access selected path #467 (#468)
Browse files Browse the repository at this point in the history
* fix handling no match access selected path

* remove obsolete field
  • Loading branch information
emicklei authored Apr 12, 2021
1 parent 4d03210 commit 2326c8e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
18 changes: 13 additions & 5 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ var defaultRequestContentType string

// Request is a wrapper for a http Request that provides convenience methods
type Request struct {
Request *http.Request
pathParameters map[string]string
attributes map[string]interface{} // for storing request-scoped values
selectedRoutePath string // root path + route path that matched the request, e.g. /meetings/{id}/attendees
selectedRoute *Route
Request *http.Request
pathParameters map[string]string
attributes map[string]interface{} // for storing request-scoped values
selectedRoute *Route // is nil when no route was matched
}

func NewRequest(httpRequest *http.Request) *Request {
Expand Down Expand Up @@ -114,11 +113,20 @@ func (r Request) Attribute(name string) interface{} {
}

// SelectedRoutePath root path + route path that matched the request, e.g. /meetings/{id}/attendees
// If no route was matched then return an empty string.
func (r Request) SelectedRoutePath() string {
if r.selectedRoute == nil {
return ""
}
// skip creating an accessor
return r.selectedRoute.Path
}

// SelectedRoute returns a reader to access the selected Route by the container
// Returns nil if no route was matched.
func (r Request) SelectedRoute() RouteReader {
if r.selectedRoute == nil {
return nil
}
return routeAccessor{route: r.selectedRoute}
}
4 changes: 4 additions & 0 deletions web_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,10 @@ func newSelectedRouteTestingService() *WebService {
}

func selectedRouteChecker(req *Request, resp *Response) {
if req.SelectedRoute() == nil {
resp.InternalServerError()
return
}
if req.SelectedRoutePath() != pathGetFriends {
resp.InternalServerError()
}
Expand Down

0 comments on commit 2326c8e

Please sign in to comment.