Fix / -> /graph redirect when using a path prefix.
This commit is contained in:
parent
cac6eea434
commit
0179bbfe03
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
|
@ -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))
|
||||
|
||||
|
|
Loading…
Reference in New Issue