oficina / docs Public

Clone
git clone https://amadan.net/oficina/docs

Plain git — no account needed to clone.

Download

Download this file

1package docs
2
3import (
4 "bytes"
5 "context"
6 "net/http"
7 "net/http/httptest"
8 "strconv"
9 "testing"
10 "time"
11)
12
13type teamDeadlineRecorder struct {
14 *httptest.ResponseRecorder
15 afterWrite func()
16}
17
18func (w *teamDeadlineRecorder) SetWriteDeadline(time.Time) error { return nil }
19func (w *teamDeadlineRecorder) Write(b []byte) (int, error) {
20 n, e := w.ResponseRecorder.Write(b)
21 if w.afterWrite != nil {
22 f := w.afterWrite
23 w.afterWrite = nil
24 f()
25 }
26 return n, e
27}
28func TestTeamBufferedEmissionRefusesLateAndAbortsPartial(t *testing.T) {
29 for _, late := range []bool{false, true} {
30 t.Run(strconv.FormatBool(late), func(t *testing.T) {
31 app := newSampleDocumentsInternalTestApp(t, "0")
32 s := app.Store()
33 ctx := t.Context()
34 owner, e := s.EnsurePerson(ctx, "owner@example.com")
35 if e != nil {
36 t.Fatal(e)
37 }
38 doc, e := s.Create(ctx, owner, "owned")
39 if e != nil {
40 t.Fatal(e)
41 }
42 rt, clock, subject := testTeamRoster(t, s, app)
43 actor := localActor(owner, "owner@example.com")
44 ctx = context.WithValue(ctx, accessContextKey{}, rt.resolveAccess(ctx, requestIdentity{Actor: actor, HomeSubject: subject}))
45 if _, e = s.GetFor(ctx, actor, doc.Sqid); e != nil {
46 t.Fatal(e)
47 }
48 r := httptest.NewRequest("GET", "https://docs.example/d/"+doc.Sqid, nil).WithContext(ctx)
49 w := &teamDeadlineRecorder{ResponseRecorder: httptest.NewRecorder()}
50 w.Header().Set("Docs-Seq", "50")
51 body := bytes.Repeat([]byte{'x'}, 3*64<<10)
52 if late {
53 clock.step(60 * time.Second)
54 } else {
55 w.afterWrite = func() { clock.step(60 * time.Second) }
56 }
57 aborted := false
58 func() {
59 defer func() {
60 if v := recover(); v != nil {
61 if v != http.ErrAbortHandler {
62 panic(v)
63 }
64 aborted = true
65 }
66 }()
67 if e = writeGuardedBody(w, r, 0, body); e != nil {
68 t.Fatal(e)
69 }
70 }()
71 if late {
72 if aborted || w.Code != 503 || w.Header().Get("Docs-Seq") != "" || bytes.Contains(w.Body.Bytes(), body[:64]) {
73 t.Fatal("late refusal emitted private bytes or sequence")
74 }
75 } else {
76 if !aborted || w.Code != 200 || w.Body.Len() != 64<<10 || w.Header().Get("Content-Length") != strconv.Itoa(len(body)) {
77 t.Fatal("partial body was not explicitly aborted with exact length")
78 }
79 }
80 })
81 }
82}
83