netflux-homepage/handler/handler_test.go

121 lines
3.1 KiB
Go
Raw Normal View History

2022-06-06 16:48:53 +00:00
package handler_test
import (
2024-11-19 18:55:01 +00:00
"io"
2022-06-06 16:48:53 +00:00
"net/http"
"net/http/httptest"
"testing"
"git.netflux.io/rob/netflux-homepage/handler"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestHandler(t *testing.T) {
const (
matrixHostname = "foo.example.com:443"
matrixBaseURL = "https://foo.example.com"
)
testCases := []struct {
name string
method string
path string
wantContentType string
wantStatusCode int
wantBody string
}{
{
name: "GET /.well-known/matrix/server",
method: http.MethodGet,
path: "/.well-known/matrix/server",
wantContentType: "application/json",
wantStatusCode: http.StatusOK,
wantBody: `{"m.server": "foo.example.com:443"}`,
},
{
name: "GET /.well-known/matrix/client",
method: http.MethodGet,
path: "/.well-known/matrix/client",
wantContentType: "application/json",
wantStatusCode: http.StatusOK,
wantBody: `{"m.homeserver": {"base_url": "https://foo.example.com"}}`,
},
2024-11-20 19:45:19 +00:00
{
name: "GET /robots.txt",
method: http.MethodGet,
path: "/robots.txt",
wantContentType: "text/plain",
wantStatusCode: http.StatusOK,
wantBody: "Allow: /",
},
2024-11-19 18:55:01 +00:00
{
name: "GET /test.html",
method: http.MethodGet,
path: "/test.html",
wantContentType: "text/html; charset=utf-8",
wantStatusCode: http.StatusOK,
wantBody: "<html>\n <head>\n <title>Test</title>\n </head>\n</html>\n",
},
2022-06-06 16:48:53 +00:00
{
name: "GET /",
method: http.MethodGet,
path: "/",
2024-11-20 19:45:19 +00:00
wantContentType: "text/html; charset=utf-8",
2022-06-06 16:48:53 +00:00
wantStatusCode: http.StatusOK,
2024-11-20 19:45:19 +00:00
wantBody: "<html>\n <head>\n <title>Homepage</title>\n </head>\n</html>\n",
2022-06-06 16:48:53 +00:00
},
2022-06-07 03:04:33 +00:00
{
name: "HEAD /",
method: http.MethodHead,
path: "/",
2024-11-20 19:45:19 +00:00
wantContentType: "text/html; charset=utf-8",
2022-06-07 03:04:33 +00:00
wantStatusCode: http.StatusOK,
wantBody: "",
},
2022-06-06 18:23:49 +00:00
{
name: "page not found",
method: http.MethodGet,
path: "/foo",
wantStatusCode: http.StatusNotFound,
wantBody: "404 page not found",
},
{
name: "wrong method",
method: http.MethodPost,
path: "/",
wantStatusCode: http.StatusMethodNotAllowed,
2024-11-19 18:55:01 +00:00
wantBody: "Method Not Allowed\n",
2022-06-06 18:23:49 +00:00
},
2022-06-06 16:48:53 +00:00
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
req := httptest.NewRequest(tc.method, tc.path, nil)
rec := httptest.NewRecorder()
2024-11-19 18:55:01 +00:00
h := handler.New(
handler.Params{
MatrixHostname: matrixHostname,
MatrixBaseURL: matrixBaseURL,
RootPath: "testdata/static/",
},
)
2022-06-06 16:48:53 +00:00
h.ServeHTTP(rec, req)
resp := rec.Result()
defer resp.Body.Close()
assert.Equal(t, tc.wantStatusCode, resp.StatusCode)
2022-06-06 18:23:49 +00:00
if tc.wantContentType != "" {
assert.Equal(t, tc.wantContentType, resp.Header.Get("content-type"))
}
2024-11-19 18:55:01 +00:00
respBody, err := io.ReadAll(resp.Body)
2022-06-07 03:04:33 +00:00
require.NoError(t, err)
2024-11-20 19:45:19 +00:00
2022-06-07 03:04:33 +00:00
assert.Contains(t, string(respBody), tc.wantBody)
2022-06-06 16:48:53 +00:00
})
}
}