| 1 | package sheets |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "net/http" |
| 6 | "net/http/httptest" |
| 7 | "net/url" |
| 8 | "os" |
| 9 | "regexp" |
| 10 | "testing" |
| 11 | ) |
| 12 | |
| 13 | // Opt-in fixture for tools/drive/menu-actions.mjs: actual server-rendered menus, |
| 14 | // with a synthetic owner and sheet. No session cookie is exported. |
| 15 | func TestBrowserMenuFixture(t *testing.T) { |
| 16 | path := os.Getenv("SHEETS_MENU_FIXTURE") |
| 17 | if path == "" { |
| 18 | t.Skip("set SHEETS_MENU_FIXTURE to render the browser fixture") |
| 19 | } |
| 20 | app, cookies := signIn(t) |
| 21 | created := do(t, app, as(postForm("/sheets", url.Values{}), cookies)) |
| 22 | page := do(t, app, as(httptest.NewRequest(http.MethodGet, created.Header().Get("Location"), nil), cookies)) |
| 23 | if page.Code != http.StatusOK { |
| 24 | t.Fatalf("sheet page: %d", page.Code) |
| 25 | } |
| 26 | assets := map[string]string{} |
| 27 | for _, match := range regexp.MustCompile(`(?:src|href)="(/static/[^" ]+)"`).FindAllStringSubmatch(page.Body.String(), -1) { |
| 28 | asset := do(t, app, as(httptest.NewRequest(http.MethodGet, match[1], nil), cookies)) |
| 29 | if asset.Code != http.StatusOK { |
| 30 | t.Fatalf("asset %s: %d", match[1], asset.Code) |
| 31 | } |
| 32 | assets[match[1]] = asset.Body.String() |
| 33 | } |
| 34 | data, err := json.Marshal(map[string]any{"html": page.Body.String(), "assets": assets}) |
| 35 | if err != nil { |
| 36 | t.Fatal(err) |
| 37 | } |
| 38 | if err := os.WriteFile(path, data, 0600); err != nil { |
| 39 | t.Fatal(err) |
| 40 | } |
| 41 | } |
| 42 | |