Skip to content

Commit

Permalink
add subrouter function to Server (#160)
Browse files Browse the repository at this point in the history
  • Loading branch information
atonks2 authored Apr 9, 2021
1 parent 11ed2ef commit 794a68c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
17 changes: 17 additions & 0 deletions admin/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,23 @@ func (s *Server) AddVersionHandler(version string) {
})
}

// Subrouter creates and returns a subrouter with the specific prefix.
//
// The returned subrouter can use middleware without impacting
// the parent router. For example:
//
// svr := NewServer(":8080")
// subRouter := svr.Subrouter("/prefix")
// subRouter.Use(someMiddleware)
// subRouter.HandleFunc("/resource", ResourceHandler)
//
// Here, requests for "/prefix/resource" would go through someMiddleware while
// the liveliness and readiness routes added to the parent router by NewServer()
// would not.
func (s *Server) Subrouter(pathPrefix string) *mux.Router {
return s.router.PathPrefix(pathPrefix).Subrouter()
}

// profileEnabled returns if a given pprof handler should be
// enabled according to pprofHandlers and the PPROF_* environment
// variables.
Expand Down
39 changes: 39 additions & 0 deletions admin/admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import (
"io/ioutil"
"net/http"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestAdmin__pprof(t *testing.T) {
Expand Down Expand Up @@ -146,3 +149,39 @@ func TestAdmin__BindAddr(t *testing.T) {
t.Errorf("bogus HTTP status code: %d", resp.StatusCode)
}
}

func TestServer_Subrouter(t *testing.T) {
svc := NewServer(":0")
subrouter := svc.Subrouter("/sub")
subrouter.Use(func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("middleware\n"))
h.ServeHTTP(w, r)
})
})
subrouter.Path("/test").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("/sub/test"))
})
go svc.Listen()
defer svc.Shutdown()

// This request is expected to go through the subrouter with its middleware
resp, err := http.DefaultClient.Get("http://" + svc.BindAddr() + "/sub/test")
require.NoError(t, err)
defer resp.Body.Close()

assert.Equal(t, http.StatusOK, resp.StatusCode)
body, err := ioutil.ReadAll(resp.Body)
require.NoError(t, err)
assert.Equal(t, "middleware\n/sub/test", string(body))

// This request hits the main router, so should not have a path prefix or middleware
liveResponse, err := http.DefaultClient.Get("http://" + svc.BindAddr() + "/live")
require.NoError(t, err)
defer liveResponse.Body.Close()

assert.Equal(t, http.StatusOK, liveResponse.StatusCode)
liveBody, err := ioutil.ReadAll(liveResponse.Body)
require.NoError(t, err)
assert.NotContains(t, string(liveBody), "middleware")
}

0 comments on commit 794a68c

Please sign in to comment.