| 1 | package sheets |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "net/http/httptest" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | func TestExportNamesTheFileAfterTheSheet(t *testing.T) { |
| 11 | app, cookies := signIn(t) |
| 12 | s, _ := newSheet(t, app, cookies, "Presupuesto — año") |
| 13 | |
| 14 | rec := do(t, app, as(httptest.NewRequest(http.MethodGet, |
| 15 | "/sheets/"+itoaPos(int(s.ID))+"/export.xlsx", nil), cookies)) |
| 16 | if rec.Code != http.StatusOK { |
| 17 | t.Fatalf("export = %d: %s", rec.Code, rec.Body.String()) |
| 18 | } |
| 19 | // A real workbook, not an error page rendered with a 200. |
| 20 | if b := rec.Body.Bytes(); len(b) < 4 || b[0] != 'P' || b[1] != 'K' { |
| 21 | t.Fatalf("body is not a zip: %d bytes", len(b)) |
| 22 | } |
| 23 | |
| 24 | cd := rec.Header().Get("Content-Disposition") |
| 25 | // The ASCII fallback: every client understands it, and it must not |
| 26 | // carry a quote or a newline that could split the header. |
| 27 | if !strings.Contains(cd, `filename="Presupuesto ao.xlsx"`) { |
| 28 | t.Errorf("no usable ASCII filename in %q", cd) |
| 29 | } |
| 30 | // The extended form: without it, a suite called Oficina mangles the |
| 31 | // first Spanish sheet title anybody exports. |
| 32 | if !strings.Contains(cd, "filename*=UTF-8''") { |
| 33 | t.Errorf("no RFC 5987 filename in %q", cd) |
| 34 | } |
| 35 | if !strings.Contains(cd, "%C3%B1") { // the n-tilde of "año" |
| 36 | t.Errorf("the extended filename lost its non-ASCII characters: %q", cd) |
| 37 | } |
| 38 | // A sealed document must not sit in a shared cache: the body is |
| 39 | // every plaintext cell in the sheet. |
| 40 | if rec.Header().Get("Cache-Control") != "no-store" { |
| 41 | t.Errorf("Cache-Control = %q, want no-store", rec.Header().Get("Cache-Control")) |
| 42 | } |
| 43 | } |
| 44 | |