Skip to content

Commit

Permalink
added check around route matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
jacquesconey committed Sep 20, 2019
1 parent 5af6a1f commit 9cf1364
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func handler(
return func(w http.ResponseWriter, r *http.Request) {
logger.Printf("inbound request on '%s %s'\n", r.Method, r.URL.String())

matchedRoute, err := matchRoute(conf, matcher, r)
matchedRoute, err := matchRoute(conf, matcher, r, mocksEnabled)
if err != nil {
logger.Printf(err.Error())
w.WriteHeader(http.StatusBadGateway)
Expand Down Expand Up @@ -121,19 +121,21 @@ func handler(
}
}

func matchRoute(conf domain.Config, matcher domain.Matcher, r *http.Request) (*domain.Route, error) {
func matchRoute(conf domain.Config, matcher domain.Matcher, r *http.Request, mocksEnabled bool) (*domain.Route, error) {
for _, route := range conf.Routes {
switch route.Type {
case domain.RouteTypeProxy:
if route.PathPattern.MatchString(r.URL.Path) {
return &route, nil
}
case domain.RouteTypeMock:
if route.Mock == nil {
return nil, errors.New("missing mock in config")
}
if matcher.Match(r, *route.Mock) {
return &route, nil
if mocksEnabled {
if route.Mock == nil {
return nil, errors.New("missing mock in config")
}
if matcher.Match(r, *route.Mock) {
return &route, nil
}
}
default:
return nil, fmt.Errorf("unknown route type '%s'", route.Type)
Expand Down

0 comments on commit 9cf1364

Please sign in to comment.