Skip to content

Commit

Permalink
Fix / -> /graph redirect when using a path prefix.
Browse files Browse the repository at this point in the history
  • Loading branch information
juliusv committed Sep 23, 2015
1 parent cac6eea commit 0179bbf
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
7 changes: 7 additions & 0 deletions util/route/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ func (r *Router) Post(path string, h http.HandlerFunc) {
r.rtr.POST(r.prefix+path, handle(h))
}

// Redirect takes an absolute path and sends an internal HTTP redirect for it,
// prefixed by the router's path prefix. Note that this method does not include
// functionality for handling relative paths or full URL redirects.
func (r *Router) Redirect(w http.ResponseWriter, req *http.Request, path string, code int) {
http.Redirect(w, req, r.prefix+path, code)
}

// ServeHTTP implements http.Handler.
func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
r.rtr.ServeHTTP(w, req)
Expand Down
27 changes: 27 additions & 0 deletions util/route/route_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package route

import (
"net/http"
"net/http/httptest"
"testing"
)

func TestRedirect(t *testing.T) {
router := New().WithPrefix("/test/prefix")
w := httptest.NewRecorder()
r, err := http.NewRequest("GET", "http://localhost:9090/foo", nil)
if err != nil {
t.Fatalf("Error building test request: %s", err)
}

router.Redirect(w, r, "/some/endpoint", http.StatusFound)
if w.Code != http.StatusFound {
t.Fatalf("Unexpected redirect status code: got %d, want %d", w.Code, http.StatusFound)
}

want := "/test/prefix/some/endpoint"
got := w.Header()["Location"][0]
if want != got {
t.Fatalf("Unexpected redirect location: got %s, want %s", got, want)
}
}
2 changes: 1 addition & 1 deletion web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func New(st local.Storage, qe *promql.Engine, rm *rules.Manager, status *Prometh
instrf := prometheus.InstrumentHandlerFunc

router.Get("/", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/graph", http.StatusFound)
router.Redirect(w, r, "/graph", http.StatusFound)
})
router.Get("/graph", instrf("graph", h.graph))

Expand Down

0 comments on commit 0179bbf

Please sign in to comment.