oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package plan3test
2
3import (
4 "net/http/httptest"
5 "net/url"
6 "strings"
7 "testing"
8
9 "amadan.net/oficina/equipo/internal/chatmarkdown"
10)
11
12// A streaming reply is drawn by the server's renderer: the page posts the
13// text so far and gets back RenderDraft's HTML, from this origin, to a
14// member only.
15func TestConversationDraftRendersOnTheServer(t *testing.T) {
16 f := newFixture(t)
17 post := func(cookie bool, site, text string) *httptest.ResponseRecorder {
18 t.Helper()
19 r := httptest.NewRequest("POST", "/conversation/draft", strings.NewReader(url.Values{"text": {text}}.Encode()))
20 r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
21 r.Header.Set("Origin", origin)
22 if site != "" {
23 r.Header.Set("Sec-Fetch-Site", site)
24 }
25 if cookie {
26 r.AddCookie(f.Cookies[owner.Address])
27 }
28 w := httptest.NewRecorder()
29 f.Handler.ServeHTTP(w, r)
30 return w
31 }
32 text := "**Yes.** Two people see it:\n\n- **Guest:** the basket\n- **Cus"
33 w := post(true, "same-origin", text)
34 if w.Code != 200 || w.Body.String() != string(chatmarkdown.Labels{}.RenderDraft(text)) /* no fence, so no paste box words */ {
35 t.Fatalf("draft render: %d %q", w.Code, w.Body.String())
36 }
37 if !strings.HasPrefix(w.Header().Get("Content-Type"), "text/html") || w.Header().Get("Cache-Control") != "no-store" {
38 t.Fatal("draft headers", w.Header())
39 }
40 if w := post(true, "cross-site", text); w.Code != 403 {
41 t.Fatal("cross-site draft render", w.Code)
42 }
43 if w := post(true, "", text); w.Code != 403 {
44 t.Fatal("draft render without fetch metadata", w.Code)
45 }
46 if w := post(false, "same-origin", text); w.Code == 200 {
47 t.Fatal("draft render without a session")
48 }
49 if w := post(true, "same-origin", strings.Repeat("a", 300<<10)); w.Code == 200 {
50 t.Fatal("oversized draft rendered")
51 }
52}
53