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
84// The transport must respect a caller's tighter deadline, not just the lease
85// and the standard write budget. Removing ctx.Deadline assignment is RED.
86type deadlineCapture struct {
87 *httptest.ResponseRecorder
88 deadlines []time.Time
89}
90
91func (w *deadlineCapture) SetWriteDeadline(d time.Time) error {
92 if !d.IsZero() {
93 w.deadlines = append(w.deadlines, d)
94 }
95 return nil
96}
97func TestTeamBufferedEmissionUsesShorterRequestDeadline(t *testing.T) {
98 app := newSampleDocumentsInternalTestApp(t, "0")
99 s := app.Store()
100 owner, err := s.EnsurePerson(t.Context(), "owner@example.com")
101 if err != nil {
102 t.Fatal(err)
103 }
104 doc, err := s.Create(t.Context(), owner, "owned")
105 if err != nil {
106 t.Fatal(err)
107 }
108 rt, _, subject := testTeamRoster(t, s, app)
109 actor := localActor(owner, "owner@example.com")
110 deadline := time.Now().Add(time.Second)
111 ctx, cancel := context.WithDeadline(t.Context(), deadline)
112 defer cancel()
113 ctx = context.WithValue(ctx, accessContextKey{}, rt.resolveAccess(ctx, requestIdentity{Actor: actor, HomeSubject: subject}))
114 if _, err = s.GetFor(ctx, actor, doc.Sqid); err != nil {
115 t.Fatal(err)
116 }
117 w := &deadlineCapture{ResponseRecorder: httptest.NewRecorder()}
118 r := httptest.NewRequest("GET", "https://docs.example/d/"+doc.Sqid, nil).WithContext(ctx)
119 if err = writeGuardedBody(w, r, 200, []byte("guarded")); err != nil || w.Code != 200 {
120 t.Fatal(err, w.Code)
121 }
122 if len(w.deadlines) == 0 {
123 t.Fatal("no write deadline")
124 }
125 for _, got := range w.deadlines {
126 if got.After(deadline) {
127 t.Fatal("transport outlives caller deadline", got, deadline)
128 }
129 }
130}
131