2020-10-09 14:58:30 +00:00
|
|
|
package web
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
|
|
|
|
)
|
|
|
|
|
2020-10-21 22:32:00 +00:00
|
|
|
func TestHandler(t *testing.T) {
|
2020-10-09 14:58:30 +00:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
requestURI string
|
|
|
|
wantStatus int
|
|
|
|
wantContentType string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "base route",
|
|
|
|
requestURI: "/",
|
|
|
|
wantStatus: 200,
|
|
|
|
wantContentType: "text/html; charset=utf-8",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "index.html",
|
|
|
|
requestURI: "/index.html",
|
|
|
|
wantStatus: 200,
|
|
|
|
wantContentType: "text/html; charset=utf-8",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "bad route",
|
|
|
|
requestURI: "/foobar_bad",
|
|
|
|
wantStatus: 200, // Serves index.html by default.
|
|
|
|
wantContentType: "text/html; charset=utf-8",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "favicon.ico",
|
|
|
|
requestURI: "/favicon.ico",
|
|
|
|
wantStatus: 200,
|
2020-10-10 02:07:28 +00:00
|
|
|
wantContentType: "",
|
2020-10-09 14:58:30 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
req := &http.Request{RequestURI: tt.requestURI}
|
|
|
|
res := httptest.NewRecorder()
|
2020-10-21 22:32:00 +00:00
|
|
|
Handler(res, req)
|
2020-10-09 14:58:30 +00:00
|
|
|
assert.Equal(t, tt.wantStatus, res.Result().StatusCode)
|
2020-10-10 02:07:28 +00:00
|
|
|
if tt.wantContentType != "" {
|
|
|
|
assert.Equal(t, tt.wantContentType, res.Result().Header.Get("Content-Type"))
|
|
|
|
}
|
2020-10-09 14:58:30 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|