package renderpage import ( "strings" "testing" "amadan.net/oficina/equipo/internal/runner/render" ) // TestParseIsStrict: render_page's input is exactly tree (base or work), // up to 6 pages of path, viewport and full_page, and compare, which // renders both trees whichever tree is named or when none is; anything // else, a tree outside base and work included, is refused before it is // counted. func TestParseIsStrict(t *testing.T) { page := `{"path": "/", "viewport": "desktop"}` pages := func(n int) string { return "[" + strings.TrimSuffix(strings.Repeat(page+",", n), ",") + "]" } for _, raw := range []string{ ``, `null`, `[]`, `{}`, `{"tree": "work"}`, `{"tree": "work", "pages": []}`, `{"tree": "work", "pages": null}`, `{"tree": "work", "pages": ` + pages(7) + `}`, `{"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"}]}`, `{"tree": "work", "pages": ` + pages(1) + `, "extra": 1}`, `{"tree": "work", "pages": [{"path": "/", "viewport": "desktop", "zoom": 2}]}`, `{"tree": "work", "pages": ` + pages(1) + `} {}`, `{"tree": "work", "pages": ` + pages(1) + `} x`, `{"pages": ` + pages(1) + `}`, `{"tree": "", "pages": ` + pages(1) + `}`, `{"tree": "both", "pages": ` + pages(1) + `}`, `{"tree": "main", "pages": ` + pages(1) + `}`, `{"tree": "both", "pages": ` + pages(1) + `, "compare": true}`, `{"tree": "main", "pages": ` + pages(1) + `, "compare": true}`, `{"tree": "", "pages": ` + pages(1) + `, "compare": true}`, `{"tree": "work", "pages": ` + pages(1) + `, "compare": "yes"}`, `{"tree": "work", "pages": [{"viewport": "desktop"}]}`, `{"tree": "work", "pages": [{"path": "/"}]}`, `{"tree": "work", "pages": [{"path": "/", "viewport": "watch"}]}`, `{"tree": "work", "pages": [{"path": "/", "viewport": "Desktop"}]}`, `{"tree": "work", "pages": [{"path": "/", "viewport": "1440x900"}]}`, `{"tree": "work", "pages": [{"path": "/", "viewport": "desktop", "full_page": "true"}]}`, `{"tree": "work", "pages": [{"path": "/", "viewport": "desktop"}, {"path": "/", "viewport": "desktop", "full_page": true}]}`, } { if _, err := Parse([]byte(raw)); err == nil { t.Errorf("accepted %s", raw) } } 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"} { raw := `{"tree": "work", "pages": [{"path": ` + quote(p) + `, "viewport": "phone"}]}` if _, err := Parse([]byte(raw)); err == nil { t.Errorf("accepted the path %q", p) } } for _, c := range []struct { raw string want Request }{ {`{"tree": "work", "pages": [{"path": "/", "viewport": "phone"}]}`, Request{Tree: TreeWork, Pages: []render.Page{{Path: "/", Viewport: render.Phone}}}}, {`{"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}}}}, {`{"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}}}}, // The spec's tree is base or work, and compare renders both // (section 3, "Contract"): either tree with compare is a compare. {`{"tree": "work", "pages": [{"path": "/a.html", "viewport": "desktop"}], "compare": true}`, Request{Tree: TreeBoth, Compare: true, Pages: []render.Page{{Path: "/a.html", Viewport: render.Desktop}}}}, {`{"tree": "base", "pages": [{"path": "/a.html", "viewport": "phone"}], "compare": true}`, Request{Tree: TreeBoth, Compare: true, Pages: []render.Page{{Path: "/a.html", Viewport: render.Phone}}}}, } { got, err := Parse([]byte(c.raw)) if err != nil || got.Tree != c.want.Tree || got.Compare != c.want.Compare || len(got.Pages) != len(c.want.Pages) { t.Errorf("%s: %+v %v", c.raw, got, err) continue } for i := range got.Pages { if got.Pages[i] != c.want.Pages[i] { t.Errorf("%s: page %d %+v", c.raw, i, got.Pages[i]) } } } var six []string for i := range 6 { six = append(six, `{"path": "/`+string(rune('a'+i))+`.html", "viewport": "desktop"}`) } if r, err := Parse([]byte(`{"tree": "work", "pages": [` + strings.Join(six, ",") + `]}`)); err != nil || len(r.Pages) != 6 { t.Errorf("six pages: %+v %v", r, err) } } func quote(s string) string { var b strings.Builder b.WriteByte('"') for _, r := range s { switch { case r == '"' || r == '\\': b.WriteByte('\\') b.WriteRune(r) case r < 0x20: b.WriteString(`\u00`) b.WriteByte("0123456789abcdef"[r>>4]) b.WriteByte("0123456789abcdef"[r&15]) default: b.WriteRune(r) } } b.WriteByte('"') return b.String() }