oficina / slides Public

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

Plain git — no account needed to clone.

Download

Download this file

1package slidestest
2
3import (
4 "bufio"
5 "bytes"
6 "compress/gzip"
7 "context"
8 "crypto/rand"
9 "net/http"
10 "net/http/httptest"
11 "strings"
12 "testing"
13 "time"
14)
15
16// A process that is stopping ends every open stream at once, and answers
17// a stream opened after that with a clean end and a reconnection delay
18// (internal/slides/drain.go). net/http's graceful shutdown waits for
19// requests in progress and cancels none of their contexts, so an open
20// editor or projector held the shutdown for its whole ten seconds and the
21// process exited 1 (Docs #94; cmd/slides's
22// TestStoppingWithAStreamOpenExitsAtOnce is the process half). Both kinds:
23// the deck's and the presentation's, which the share link's also is.
24func TestStreamsEndWhenTheAppDrains(t *testing.T) {
25 a := newApp(t)
26 cookies := signedIn(t, a, allowedAddress)
27 path := sampleDeck(t, a, cookies)
28 session, _ := startPresenting(t, a, path, cookies)
29 srv := httptest.NewServer(a)
30 defer srv.Close()
31
32 open := func(target string) (*http.Response, *bufio.Reader) {
33 t.Helper()
34 ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
35 t.Cleanup(cancel)
36 req, err := http.NewRequestWithContext(ctx, http.MethodGet, srv.URL+target, nil)
37 if err != nil {
38 t.Fatal(err)
39 }
40 for _, c := range cookies {
41 req.AddCookie(c)
42 }
43 res, err := http.DefaultClient.Do(req)
44 if err != nil {
45 t.Fatalf("open %s: %v", target, err)
46 }
47 t.Cleanup(func() { res.Body.Close() })
48 if res.StatusCode != http.StatusOK {
49 t.Fatalf("GET %s: %d, want 200", target, res.StatusCode)
50 }
51 return res, bufio.NewReader(res.Body)
52 }
53 ended := func(r *bufio.Reader) <-chan struct{} {
54 done := make(chan struct{})
55 go func() {
56 for {
57 if _, err := r.ReadString('\n'); err != nil {
58 close(done)
59 return
60 }
61 }
62 }()
63 return done
64 }
65
66 _, deck := open(path + "/stream")
67 nextEvent(t, deck, "update")
68 _, talk := open("/p/" + session + "/stream")
69 nextSlideEvent(t, talk)
70 deckEnded, talkEnded := ended(deck), ended(talk)
71
72 a.app.Drain()
73 for name, done := range map[string]<-chan struct{}{"the deck's": deckEnded, "the presentation's": talkEnded} {
74 select {
75 case <-done:
76 case <-time.After(2 * time.Second):
77 t.Errorf("%s stream was still open after the app drained", name)
78 }
79 }
80
81 // Opened while stopping: a clean end, at once, with a reconnection
82 // delay. Not an error status, which would end a projector's
83 // EventSource for good.
84 for _, target := range []string{path + "/stream", "/p/" + session + "/stream"} {
85 res, body := open(target)
86 got := make(chan string, 1)
87 go func() {
88 var b strings.Builder
89 for {
90 line, err := body.ReadString('\n')
91 b.WriteString(line)
92 if err != nil {
93 got <- b.String()
94 return
95 }
96 }
97 }()
98 select {
99 case all := <-got:
100 if !strings.Contains(all, "retry: ") || strings.Contains(all, "event: ") {
101 t.Errorf("GET %s while draining said %q, want only a reconnection delay", target, all)
102 }
103 case <-time.After(2 * time.Second):
104 t.Errorf("GET %s while draining stayed open", target)
105 }
106 _ = res
107 }
108 a.app.Drain() // twice is harmless
109}
110
111// The editor gzips a large save so that more of them fit a keepalive
112// request, the one kind WebKit still sends while a link the page opened
113// is loading (fixtures/save-queue.mjs, Docs #94). The server takes the
114// body gunzipped, under the same limits: a body expanding past a
115// megabyte is refused as too large, a body that is not gzip is refused
116// (400, as in Docs),
117// and so is any other encoding.
118func TestAPostedUpdateMayBeGzipped(t *testing.T) {
119 a := newApp(t)
120 cookies := signedIn(t, a, allowedAddress)
121 path := createDeck(t, a, cookies, "Compressed")
122 log, _ := getUpdates(t, a, path, 0, cookies)
123 m := newMirror(t, sqidOf(path), log)
124 update := m.ops(t, `[{"op":"add_slide","id":"gzipslide01","layout":"flow","template":"title"}]`)
125
126 send := func(body []byte, encoding string) int {
127 t.Helper()
128 req := httptest.NewRequest(http.MethodPost, path+"/updates", bytes.NewReader(body))
129 req.Header.Set("Content-Type", "application/octet-stream")
130 req.Header.Set("Origin", testOrigin)
131 req.Header.Set("Sec-Fetch-Site", "same-origin")
132 if encoding != "" {
133 req.Header.Set("Content-Encoding", encoding)
134 }
135 for _, c := range cookies {
136 req.AddCookie(c)
137 }
138 rec := httptest.NewRecorder()
139 a.ServeHTTP(rec, req)
140 return rec.Code
141 }
142 gz := func(b []byte) []byte {
143 var out bytes.Buffer
144 w := gzip.NewWriter(&out)
145 _, _ = w.Write(b)
146 _ = w.Close()
147 return out.Bytes()
148 }
149
150 if code := send(gz(update), "gzip"); code != http.StatusNoContent {
151 t.Fatalf("a gzipped update: %d, want 204", code)
152 }
153 mustGet(t, a, path+"/slides/gzipslide01.html", http.StatusOK, cookies)
154
155 if code := send(gz(make([]byte, 2<<20)), "gzip"); code != http.StatusRequestEntityTooLarge {
156 t.Errorf("a gzipped body expanding past the limit: %d, want 413", code)
157 }
158 noise := make([]byte, (1<<20)+1024)
159 _, _ = rand.Read(noise)
160 if code := send(noise, "gzip"); code != http.StatusRequestEntityTooLarge && code != http.StatusBadRequest {
161 t.Errorf("an oversized gzip body: %d, want 413 or 400", code)
162 }
163 // 400, as in Docs: the body is not gzip at all, which is not the 422 of
164 // an update the core refuses.
165 if code := send([]byte("not gzip at all"), "gzip"); code != http.StatusBadRequest {
166 t.Errorf("a body that is not gzip: %d, want 400", code)
167 }
168 if code := send(gz(update)[:20], "gzip"); code != http.StatusBadRequest {
169 t.Errorf("a truncated gzip body: %d, want 400", code)
170 }
171 if code := send(update, "br"); code != http.StatusUnsupportedMediaType {
172 t.Errorf("an unsupported encoding: %d, want 415", code)
173 }
174}
175