| 1 | package serve |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "net/http/httptest" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | |
| 9 | "reasonix/internal/config" |
| 10 | "reasonix/internal/control" |
| 11 | ) |
| 12 | |
| 13 | func TestServeIndexPageAndSessionDeepLink(t *testing.T) { |
| 14 | bc := NewBroadcaster() |
| 15 | ctrl := control.New(control.Options{Sink: bc}) |
| 16 | srv := httptest.NewServer(New(ctrl, bc, config.ServeConfig{}).Handler()) |
| 17 | defer srv.Close() |
| 18 | |
| 19 | for _, path := range []string{"/", "/sessions/reserved-session"} { |
| 20 | resp, err := http.Get(srv.URL + path) |
| 21 | if err != nil { |
| 22 | t.Fatal(err) |
| 23 | } |
| 24 | resp.Body.Close() |
| 25 | if resp.StatusCode != http.StatusOK { |
| 26 | t.Errorf("GET %s status = %d", path, resp.StatusCode) |
| 27 | } |
| 28 | if ct := resp.Header.Get("Content-Type"); !strings.Contains(ct, "text/html") { |
| 29 | t.Errorf("GET %s content-type = %q, want text/html", path, ct) |
| 30 | } |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | func TestServeWebPagesBootstrapFragmentTokenBeforeRequests(t *testing.T) { |
| 35 | for name, html := range map[string]string{ |
| 36 | "index": string(indexHTML), |
| 37 | "provider setup": string(providerSetupHTML), |
| 38 | } { |
| 39 | t.Run(name, func(t *testing.T) { |
| 40 | for _, want := range []string{ |
| 41 | "new URLSearchParams(window.location.hash.slice(1))", |
| 42 | "'/auth/token'", |
| 43 | "window.history.replaceState", |
| 44 | "window.fetch", |
| 45 | } { |
| 46 | if !strings.Contains(html, want) { |
| 47 | t.Fatalf("page missing fragment-token bootstrap %q", want) |
| 48 | } |
| 49 | } |
| 50 | }) |
| 51 | } |
| 52 | if !strings.Contains(string(indexHTML), "__authReady.then(connectEvents)") { |
| 53 | t.Fatal("serve index must delay SSE until fragment authentication completes") |
| 54 | } |
| 55 | } |
| 56 |