Skip to content

Commit

Permalink
Merge pull request #6670 from hashicorp/api/fallthrough-test
Browse files Browse the repository at this point in the history
test rootfallthrough handler
  • Loading branch information
drewbailey authored Nov 13, 2019
2 parents d696de6 + e187a7f commit c24a631
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions command/agent/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/hashicorp/nomad/nomad/structs/config"
"github.com/hashicorp/nomad/testutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/ugorji/go/codec"
)

Expand Down Expand Up @@ -60,6 +61,58 @@ func BenchmarkHTTPRequests(b *testing.B) {
})
}

// TestRootFallthrough tests rootFallthrough handler to
// verify redirect and 404 behavior
func TestRootFallthrough(t *testing.T) {
t.Parallel()

cases := []struct {
desc string
path string
expectedPath string
expectedCode int
}{
{
desc: "unknown endpoint 404s",
path: "/v1/unknown/endpoint",
expectedCode: 404,
},
{
desc: "root path redirects to ui",
path: "/",
expectedPath: "/ui/",
expectedCode: 307,
},
}

s := makeHTTPServer(t, nil)
defer s.Shutdown()

// setup a client that doesn't follow redirects
client := &http.Client{
CheckRedirect: func(_ *http.Request, _ []*http.Request) error {
return http.ErrUseLastResponse
},
}

for _, tc := range cases {
t.Run(tc.desc, func(t *testing.T) {

reqURL := fmt.Sprintf("http://%s%s", s.Agent.config.AdvertiseAddrs.HTTP, tc.path)

resp, err := client.Get(reqURL)
require.NoError(t, err)
require.Equal(t, tc.expectedCode, resp.StatusCode)

if tc.expectedPath != "" {
loc, err := resp.Location()
require.NoError(t, err)
require.Equal(t, tc.expectedPath, loc.Path)
}
})
}
}

func TestSetIndex(t *testing.T) {
t.Parallel()
resp := httptest.NewRecorder()
Expand Down

0 comments on commit c24a631

Please sign in to comment.