2023-01-26 15:25:17 +00:00
|
|
|
package base_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2023-03-01 21:57:30 +00:00
|
|
|
"context"
|
2023-01-26 15:25:17 +00:00
|
|
|
"embed"
|
|
|
|
"html/template"
|
2023-03-01 21:57:30 +00:00
|
|
|
"net"
|
2023-01-26 15:25:17 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2023-03-01 21:57:30 +00:00
|
|
|
"path"
|
2023-01-26 15:25:17 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/matrix-org/dendrite/internal"
|
2023-03-22 08:21:32 +00:00
|
|
|
"github.com/matrix-org/dendrite/internal/httputil"
|
|
|
|
basepkg "github.com/matrix-org/dendrite/setup/base"
|
2023-01-26 15:25:17 +00:00
|
|
|
"github.com/matrix-org/dendrite/setup/config"
|
2023-03-22 08:21:32 +00:00
|
|
|
"github.com/matrix-org/dendrite/setup/process"
|
2023-01-26 15:25:17 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
//go:embed static/*.gotmpl
|
|
|
|
var staticContent embed.FS
|
|
|
|
|
2023-03-01 21:57:30 +00:00
|
|
|
func TestLandingPage_Tcp(t *testing.T) {
|
2023-01-26 15:25:17 +00:00
|
|
|
// generate the expected result
|
|
|
|
tmpl := template.Must(template.ParseFS(staticContent, "static/*.gotmpl"))
|
|
|
|
expectedRes := &bytes.Buffer{}
|
|
|
|
err := tmpl.ExecuteTemplate(expectedRes, "index.gotmpl", map[string]string{
|
|
|
|
"Version": internal.VersionString(),
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2023-03-22 08:21:32 +00:00
|
|
|
processCtx := process.NewProcessContext()
|
|
|
|
routers := httputil.NewRouters()
|
|
|
|
cfg := config.Dendrite{}
|
|
|
|
cfg.Defaults(config.DefaultOpts{Generate: true, SingleDatabase: true})
|
2023-01-26 15:25:17 +00:00
|
|
|
|
|
|
|
// hack: create a server and close it immediately, just to get a random port assigned
|
|
|
|
s := httptest.NewServer(nil)
|
|
|
|
s.Close()
|
|
|
|
|
|
|
|
// start base with the listener and wait for it to be started
|
2023-03-01 21:57:30 +00:00
|
|
|
address, err := config.HTTPAddress(s.URL)
|
|
|
|
assert.NoError(t, err)
|
2023-03-22 08:21:32 +00:00
|
|
|
go basepkg.SetupAndServeHTTP(processCtx, &cfg, routers, address, nil, nil)
|
2023-01-26 15:25:17 +00:00
|
|
|
time.Sleep(time.Millisecond * 10)
|
|
|
|
|
|
|
|
// When hitting /, we should be redirected to /_matrix/static, which should contain the landing page
|
|
|
|
req, err := http.NewRequest(http.MethodGet, s.URL, nil)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// do the request
|
|
|
|
resp, err := s.Client().Do(req)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
|
|
|
|
|
|
|
// read the response
|
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
_, err = buf.ReadFrom(resp.Body)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// Using .String() for user friendly output
|
|
|
|
assert.Equal(t, expectedRes.String(), buf.String(), "response mismatch")
|
|
|
|
}
|
2023-03-01 21:57:30 +00:00
|
|
|
|
|
|
|
func TestLandingPage_UnixSocket(t *testing.T) {
|
|
|
|
// generate the expected result
|
|
|
|
tmpl := template.Must(template.ParseFS(staticContent, "static/*.gotmpl"))
|
|
|
|
expectedRes := &bytes.Buffer{}
|
|
|
|
err := tmpl.ExecuteTemplate(expectedRes, "index.gotmpl", map[string]string{
|
|
|
|
"Version": internal.VersionString(),
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2023-03-22 08:21:32 +00:00
|
|
|
processCtx := process.NewProcessContext()
|
|
|
|
routers := httputil.NewRouters()
|
|
|
|
cfg := config.Dendrite{}
|
|
|
|
cfg.Defaults(config.DefaultOpts{Generate: true, SingleDatabase: true})
|
2023-03-01 21:57:30 +00:00
|
|
|
|
|
|
|
tempDir := t.TempDir()
|
|
|
|
socket := path.Join(tempDir, "socket")
|
|
|
|
// start base with the listener and wait for it to be started
|
2023-03-16 07:51:21 +00:00
|
|
|
address, err := config.UnixSocketAddress(socket, "755")
|
2023-03-01 21:57:30 +00:00
|
|
|
assert.NoError(t, err)
|
2023-03-22 08:21:32 +00:00
|
|
|
go basepkg.SetupAndServeHTTP(processCtx, &cfg, routers, address, nil, nil)
|
2023-03-01 21:57:30 +00:00
|
|
|
time.Sleep(time.Millisecond * 100)
|
|
|
|
|
|
|
|
client := &http.Client{
|
|
|
|
Transport: &http.Transport{
|
|
|
|
DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
|
|
|
|
return net.Dial("unix", socket)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
resp, err := client.Get("http://unix/")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
|
|
|
|
|
|
|
// read the response
|
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
_, err = buf.ReadFrom(resp.Body)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// Using .String() for user friendly output
|
|
|
|
assert.Equal(t, expectedRes.String(), buf.String(), "response mismatch")
|
|
|
|
}
|