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

Commit

Permalink
Merge 5589250 into 52edc60
Browse files Browse the repository at this point in the history
  • Loading branch information
m-sandusky committed Nov 10, 2017
2 parents 52edc60 + 5589250 commit 4da6d29
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
4 changes: 2 additions & 2 deletions r2/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,8 @@ func (s *service) sendResponse(w http.ResponseWriter, statusCode int, data inter
return writeAPIResponse(w, http.StatusInternalServerError, "could not create response object")
}

func (s *service) fetchNamespaces(w http.ResponseWriter, _ *http.Request) error {
data, err := handleRoute(fetchNamespaces, s, nil, "")
func (s *service) fetchNamespaces(w http.ResponseWriter, r *http.Request) error {
data, err := handleRoute(fetchNamespaces, s, r, "")
if err != nil {
return err
}
Expand Down
4 changes: 4 additions & 0 deletions r2/service_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package r2

import (
"errors"
"fmt"
"net/http"
"time"
Expand Down Expand Up @@ -250,6 +251,9 @@ func fetchRollupRuleHistory(s *service, r *http.Request) (data interface{}, err
type routeFunc func(s *service, r *http.Request) (data interface{}, err error)

func handleRoute(rf routeFunc, s *service, r *http.Request, namespace string) (interface{}, error) {
if r == nil || s == nil {
return nil, errors.New("Must pass non nil service and request to route handler")
}
start := s.nowFn()
data, err := rf(s, r)
s.metrics.recordMetric(r.RequestURI, r.Method, namespace, time.Since(start), err)
Expand Down
9 changes: 9 additions & 0 deletions r2/service_routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ func TestHandleRoute(t *testing.T) {
require.NoError(t, err)
require.Equal(t, expected, actual)
}

func TestHandleRouteNilRequest(t *testing.T) {
s := newTestService()
r := newTestGetRequest()
_, err1 := handleRoute(fetchNamespaces, s, nil, "ns")
_, err2 := handleRoute(fetchNamespaces, nil, r, "ns")
require.EqualError(t, err1, "Must pass non nil service and request to route handler")
require.EqualError(t, err2, "Must pass non nil service and request to route handler")
}
func TestFetchNamespacesSuccess(t *testing.T) {
expected := newNamespacesJSON(&rules.NamespacesView{})
actual, err := fetchNamespaces(newTestService(), nil)
Expand Down

0 comments on commit 4da6d29

Please sign in to comment.