Skip to content

Commit

Permalink
Merge pull request #6658 from hashicorp/b-404-redirect
Browse files Browse the repository at this point in the history
return a 404 if not found instead of redirect to ui
  • Loading branch information
drewbailey committed Nov 11, 2019
2 parents 6e59e9b + 29585dd commit 2f0c588
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
12 changes: 8 additions & 4 deletions api/allocations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package api

import (
"context"
"fmt"
"os"
"reflect"
"sort"
Expand Down Expand Up @@ -265,8 +266,10 @@ func TestAllocations_ExecErrors(t *testing.T) {
}
job.Canonicalize()

allocID := uuid.Generate()

alloc := &Allocation{
ID: "",
ID: allocID,
Namespace: DefaultNamespace,
EvalID: uuid.Generate(),
Name: "foo-bar[1]",
Expand All @@ -280,9 +283,10 @@ func TestAllocations_ExecErrors(t *testing.T) {

// make a request that will result in an error
// ensure the error is what we expect
_, err := a.Exec(context.Background(), alloc, "bar", false, []string{"command"}, os.Stdin, os.Stdout, os.Stderr, sizeCh, nil)
require.Contains(t, err.Error(), "Unexpected response code: 301")
require.Contains(t, err.Error(), "Moved Permanently")
exitCode, err := a.Exec(context.Background(), alloc, "bar", false, []string{"command"}, os.Stdin, os.Stdout, os.Stderr, sizeCh, nil)

require.Equal(t, exitCode, -2)
require.Equal(t, err.Error(), fmt.Sprintf("Unknown allocation \"%s\"", allocID))
}

func TestAllocations_ShouldMigrate(t *testing.T) {
Expand Down
11 changes: 7 additions & 4 deletions command/agent/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func (s *HTTPServer) registerHandlers(enableDebug bool) {
w.Write([]byte(stubHTML))
})
}
s.mux.Handle("/", handleRootRedirect())
s.mux.Handle("/", handleRootFallthrough())

if enableDebug {
s.mux.HandleFunc("/debug/pprof/", pprof.Index)
Expand Down Expand Up @@ -275,10 +275,13 @@ func handleUI(h http.Handler) http.Handler {
})
}

func handleRootRedirect() http.Handler {
func handleRootFallthrough() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
http.Redirect(w, req, "/ui/", 307)
return
if req.URL.Path == "/" {
http.Redirect(w, req, "/ui/", 307)
} else {
w.WriteHeader(http.StatusNotFound)
}
})
}

Expand Down

0 comments on commit 2f0c588

Please sign in to comment.