oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package renderpage
2
3import (
4 "strings"
5 "testing"
6
7 "amadan.net/oficina/equipo/internal/runner/render"
8)
9
10// TestParseIsStrict: render_page's input is exactly tree (base or work),
11// up to 6 pages of path, viewport and full_page, and compare, which
12// renders both trees whichever tree is named or when none is; anything
13// else, a tree outside base and work included, is refused before it is
14// counted.
15func TestParseIsStrict(t *testing.T) {
16 page := `{"path": "/", "viewport": "desktop"}`
17 pages := func(n int) string { return "[" + strings.TrimSuffix(strings.Repeat(page+",", n), ",") + "]" }
18 for _, raw := range []string{
19 ``,
20 `null`,
21 `[]`,
22 `{}`,
23 `{"tree": "work"}`,
24 `{"tree": "work", "pages": []}`,
25 `{"tree": "work", "pages": null}`,
26 `{"tree": "work", "pages": ` + pages(7) + `}`,
27 `{"tree": "work", "pages": [{"path": "/1", "viewport": "phone"}, {"path": "/2", "viewport": "phone"}, {"path": "/3", "viewport": "phone"}, {"path": "/4", "viewport": "phone"}, {"path": "/5", "viewport": "phone"}, {"path": "/6", "viewport": "phone"}, {"path": "/7", "viewport": "phone"}]}`,
28 `{"tree": "work", "pages": ` + pages(1) + `, "extra": 1}`,
29 `{"tree": "work", "pages": [{"path": "/", "viewport": "desktop", "zoom": 2}]}`,
30 `{"tree": "work", "pages": ` + pages(1) + `} {}`,
31 `{"tree": "work", "pages": ` + pages(1) + `} x`,
32 `{"pages": ` + pages(1) + `}`,
33 `{"tree": "", "pages": ` + pages(1) + `}`,
34 `{"tree": "both", "pages": ` + pages(1) + `}`,
35 `{"tree": "main", "pages": ` + pages(1) + `}`,
36 `{"tree": "both", "pages": ` + pages(1) + `, "compare": true}`,
37 `{"tree": "main", "pages": ` + pages(1) + `, "compare": true}`,
38 `{"tree": "", "pages": ` + pages(1) + `, "compare": true}`,
39 `{"tree": "work", "pages": ` + pages(1) + `, "compare": "yes"}`,
40 `{"tree": "work", "pages": [{"viewport": "desktop"}]}`,
41 `{"tree": "work", "pages": [{"path": "/"}]}`,
42 `{"tree": "work", "pages": [{"path": "/", "viewport": "watch"}]}`,
43 `{"tree": "work", "pages": [{"path": "/", "viewport": "Desktop"}]}`,
44 `{"tree": "work", "pages": [{"path": "/", "viewport": "1440x900"}]}`,
45 `{"tree": "work", "pages": [{"path": "/", "viewport": "desktop", "full_page": "true"}]}`,
46 `{"tree": "work", "pages": [{"path": "/", "viewport": "desktop"}, {"path": "/", "viewport": "desktop", "full_page": true}]}`,
47 } {
48 if _, err := Parse([]byte(raw)); err == nil {
49 t.Errorf("accepted %s", raw)
50 }
51 }
52 for _, p := range []string{"", "index.html", "./index.html", "/../etc/passwd", "/a/../../b", "/a/./b", "/a//b", "//example.com/x", "/a\\b", "/a#top", "/a b", "/a\x00", "/a\n", "/é", "/%2e%2e/x", "/%2E%2e/x", "/a%2fb", "/a%5Cb", "/" + strings.Repeat("a", 1024), "http://example.com/", "/a?b#c"} {
53 raw := `{"tree": "work", "pages": [{"path": ` + quote(p) + `, "viewport": "phone"}]}`
54 if _, err := Parse([]byte(raw)); err == nil {
55 t.Errorf("accepted the path %q", p)
56 }
57 }
58 for _, c := range []struct {
59 raw string
60 want Request
61 }{
62 {`{"tree": "work", "pages": [{"path": "/", "viewport": "phone"}]}`, Request{Tree: TreeWork, Pages: []render.Page{{Path: "/", Viewport: render.Phone}}}},
63 {`{"tree": "base", "pages": [{"path": "/about/", "viewport": "tablet", "full_page": true}, {"path": "/about/", "viewport": "desktop"}], "compare": false}`, Request{Tree: TreeBase, Pages: []render.Page{{Path: "/about/", Viewport: render.Tablet, FullPage: true}, {Path: "/about/", Viewport: render.Desktop}}}},
64 {`{"pages": [{"path": "/caf%C3%A9.html?tab=2", "viewport": "desktop"}], "compare": true}`, Request{Tree: TreeBoth, Compare: true, Pages: []render.Page{{Path: "/caf%C3%A9.html?tab=2", Viewport: render.Desktop}}}},
65 // The spec's tree is base or work, and compare renders both
66 // (section 3, "Contract"): either tree with compare is a compare.
67 {`{"tree": "work", "pages": [{"path": "/a.html", "viewport": "desktop"}], "compare": true}`, Request{Tree: TreeBoth, Compare: true, Pages: []render.Page{{Path: "/a.html", Viewport: render.Desktop}}}},
68 {`{"tree": "base", "pages": [{"path": "/a.html", "viewport": "phone"}], "compare": true}`, Request{Tree: TreeBoth, Compare: true, Pages: []render.Page{{Path: "/a.html", Viewport: render.Phone}}}},
69 } {
70 got, err := Parse([]byte(c.raw))
71 if err != nil || got.Tree != c.want.Tree || got.Compare != c.want.Compare || len(got.Pages) != len(c.want.Pages) {
72 t.Errorf("%s: %+v %v", c.raw, got, err)
73 continue
74 }
75 for i := range got.Pages {
76 if got.Pages[i] != c.want.Pages[i] {
77 t.Errorf("%s: page %d %+v", c.raw, i, got.Pages[i])
78 }
79 }
80 }
81 var six []string
82 for i := range 6 {
83 six = append(six, `{"path": "/`+string(rune('a'+i))+`.html", "viewport": "desktop"}`)
84 }
85 if r, err := Parse([]byte(`{"tree": "work", "pages": [` + strings.Join(six, ",") + `]}`)); err != nil || len(r.Pages) != 6 {
86 t.Errorf("six pages: %+v %v", r, err)
87 }
88}
89
90func quote(s string) string {
91 var b strings.Builder
92 b.WriteByte('"')
93 for _, r := range s {
94 switch {
95 case r == '"' || r == '\\':
96 b.WriteByte('\\')
97 b.WriteRune(r)
98 case r < 0x20:
99 b.WriteString(`\u00`)
100 b.WriteByte("0123456789abcdef"[r>>4])
101 b.WriteByte("0123456789abcdef"[r&15])
102 default:
103 b.WriteRune(r)
104 }
105 }
106 b.WriteByte('"')
107 return b.String()
108}
109