package server import ( "context" "io" "net/http" "net/http/httptest" "strings" "testing" "time" "github.com/coder/websocket" "github.com/D-Modem/bbs/internal/api" ) // webTerm is a server with the web terminal page, on e's BBS. func webTerm(t *testing.T, e *env) *httptest.Server { srv := New(e.b, nil) as := &api.Server{B: e.b, S: api.Settings{SessionTTL: time.Hour, Terminal: true, Rate: 100, Burst: 100}, Terminal: srv.WebTerminal} ts := httptest.NewServer(as.Handler()) t.Cleanup(ts.Close) return ts } // get is url's response and body, redirects not followed. func get(t *testing.T, url string) (*http.Response, string) { t.Helper() c := &http.Client{CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }} resp, err := c.Get(url) if err != nil { t.Fatal(err) } defer resp.Body.Close() b, _ := io.ReadAll(resp.Body) return resp, string(b) } // TestWebTerminal: the page and its files, and a browser's terminal on the // WebSocket logging in to the text BBS. func TestWebTerminal(t *testing.T) { e := start(t, nil, &Listener{Name: "modem", Kind: "raw", Address: "modem.sock", ANSI: "no"}) ts := webTerm(t, e) resp, body := get(t, ts.URL+"/") if resp.StatusCode != http.StatusFound || resp.Header.Get("Location") != "/terminal" { t.Fatalf("/: %d %v", resp.StatusCode, resp.Header) } resp, body = get(t, ts.URL+"/terminal") csp := resp.Header.Get("Content-Security-Policy") if resp.StatusCode != 200 || !strings.Contains(body, "Test BBS") || !strings.Contains(body, `src="/terminal/xterm.js"`) || !strings.Contains(csp, "script-src 'self'") || !strings.Contains(csp, "frame-ancestors 'none'") || strings.Contains(body, "