| 1 | package docs_test |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "compress/gzip" |
| 6 | "context" |
| 7 | "crypto/rand" |
| 8 | "net/http" |
| 9 | "net/http/httptest" |
| 10 | "testing" |
| 11 | |
| 12 | "github.com/carlosframework/docs/internal/docs" |
| 13 | ) |
| 14 | |
| 15 | // The editor gzips a large save so that more of them fit a keepalive |
| 16 | // request, the one kind WebKit still sends while a link the page opened is |
| 17 | // loading (fixtures/save-queue.mjs). The server takes the body gunzipped, |
| 18 | // with the same limits as ever: a gzip body that expands past a megabyte |
| 19 | // is refused as too large, not read into memory, and a body that is not |
| 20 | // gzip, or any other encoding, is refused rather than stored. |
| 21 | func TestAPostedUpdateMayBeGzipped(t *testing.T) { |
| 22 | app, err := docs.NewForTest(t) |
| 23 | if err != nil { |
| 24 | t.Fatal(err) |
| 25 | } |
| 26 | ctx := context.Background() |
| 27 | doc, err := app.Store().Create(ctx, app.TestPerson(t, "gzip@example.com"), "Compressed") |
| 28 | if err != nil { |
| 29 | t.Fatal(err) |
| 30 | } |
| 31 | cookies := signInCookies(t, app, "gzip@example.com") |
| 32 | post := func(body []byte, encoding string) int { |
| 33 | t.Helper() |
| 34 | req := httptest.NewRequest(http.MethodPost, "/d/"+doc.Sqid+"/updates", bytes.NewReader(body)) |
| 35 | req.Header.Set("Content-Type", "application/octet-stream") |
| 36 | req.Header.Set("Sec-Fetch-Site", "same-origin") |
| 37 | for _, c := range cookies { |
| 38 | req.AddCookie(c) |
| 39 | } |
| 40 | if encoding != "" { |
| 41 | req.Header.Set("Content-Encoding", encoding) |
| 42 | } |
| 43 | rec := httptest.NewRecorder() |
| 44 | app.Handler().ServeHTTP(rec, req) |
| 45 | return rec.Code |
| 46 | } |
| 47 | gz := func(b []byte) []byte { |
| 48 | var out bytes.Buffer |
| 49 | w := gzip.NewWriter(&out) |
| 50 | _, _ = w.Write(b) |
| 51 | _ = w.Close() |
| 52 | return out.Bytes() |
| 53 | } |
| 54 | |
| 55 | if code := post(gz(docs.ReadFragmentFixture(t, "hello")), "gzip"); code != http.StatusNoContent { |
| 56 | t.Fatalf("a gzipped update: %d, want 204", code) |
| 57 | } |
| 58 | text, err := app.Store().Text(ctx, doc) |
| 59 | if err != nil { |
| 60 | t.Fatal(err) |
| 61 | } |
| 62 | if text != "hello world" { |
| 63 | t.Errorf("Text = %q after a gzipped update, want %q", text, "hello world") |
| 64 | } |
| 65 | |
| 66 | // Two megabytes of zeros gzip to a few kilobytes: well under the limit |
| 67 | // on the wire, and refused for what it expands to. |
| 68 | if code := post(gz(make([]byte, 2<<20)), "gzip"); code != http.StatusRequestEntityTooLarge { |
| 69 | t.Errorf("a gzipped body expanding past the limit: %d, want 413", code) |
| 70 | } |
| 71 | // Over the limit on the wire, gzip or not. |
| 72 | noise := make([]byte, (1<<20)+1024) |
| 73 | _, _ = rand.Read(noise) |
| 74 | if code := post(noise, "gzip"); code != http.StatusRequestEntityTooLarge && code != http.StatusBadRequest { |
| 75 | t.Errorf("an oversized gzip body: %d, want 413 or 400", code) |
| 76 | } |
| 77 | if code := post([]byte("not gzip at all"), "gzip"); code != http.StatusBadRequest { |
| 78 | t.Errorf("a body that is not gzip: %d, want 400", code) |
| 79 | } |
| 80 | if code := post(gz(docs.ReadFragmentFixture(t, "hello"))[:20], "gzip"); code != http.StatusBadRequest { |
| 81 | t.Errorf("a truncated gzip body: %d, want 400", code) |
| 82 | } |
| 83 | if code := post(docs.ReadFragmentFixture(t, "hello"), "br"); code != http.StatusUnsupportedMediaType { |
| 84 | t.Errorf("an unsupported encoding: %d, want 415", code) |
| 85 | } |
| 86 | if code := post(docs.ReadFragmentFixture(t, "hello"), ""); code != http.StatusNoContent { |
| 87 | t.Errorf("an uncompressed update, as ever: %d, want 204", code) |
| 88 | } |
| 89 | } |
| 90 | |