check / check (push) Canceled after 0s
setupSPA took the file name from request.URL.Path[1:], which panics
when the path is empty, as it is for an absolute-form request line
with no path ("GET http://host HTTP/1.1"). It now trims a leading "/"
instead; an empty name fails to read, so the request gets the index
page, as "/" already does.
The new test in internal/server sends such a request and expects 200
and the index page. export_test.go gives it a Server whose router has
only the web client route.
Model: opus-5-5
42 lines
932 B
Go
42 lines
932 B
Go
package server_test
|
|
|
|
import (
|
|
"io/fs"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"sneak.berlin/go/neoirc/internal/server"
|
|
"sneak.berlin/go/neoirc/web"
|
|
)
|
|
|
|
// An absolute-form request line with no path, such as
|
|
// "GET http://example.com HTTP/1.1", leaves URL.Path empty.
|
|
func TestSPAServesIndexForEmptyPath(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
request := httptest.NewRequestWithContext(
|
|
t.Context(), http.MethodGet, "http://example.com", nil,
|
|
)
|
|
if request.URL.Path != "" {
|
|
t.Fatalf("URL.Path is %q, want it empty", request.URL.Path)
|
|
}
|
|
|
|
recorder := httptest.NewRecorder()
|
|
|
|
server.NewTestSPAServer().ServeHTTP(recorder, request)
|
|
|
|
if recorder.Code != http.StatusOK {
|
|
t.Fatalf("status %d, want %d", recorder.Code, http.StatusOK)
|
|
}
|
|
|
|
indexHTML, err := fs.ReadFile(web.Dist, "dist/index.html")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if recorder.Body.String() != string(indexHTML) {
|
|
t.Errorf("body is not the index page")
|
|
}
|
|
}
|