Skip to content

A free, open-source alternative to Burp Suite: an intercepting proxy and web app security testing suite with Repeater, Intruder, Collaborator and a built-in OWASP ZAP scanner. Linux, AGPL-3.0.

6.3 KB
Raw Blame
package opensuite.ui;

import opensuite.testkit.Expect;
import opensuite.testkit.Harness;

/** Tests the Pretty view's body formatting for read-only messages. */
public final class PrettyPrintTest {

    public void run() {
        Harness.suite("pretty print");

        Harness.test("pretty lines are numbered by the message's own lines", () -> {
            String raw = "HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n<html><body>hello</body></html>\n";
            String pretty = PrettyPrint.message(raw, "text/html");
            int[] n = PrettyPrint.sourceLines(raw, pretty);
            Expect.isTrue(n != null, "an account of the lines is given");
            Expect.isTrue(n.length == pretty.replace("\r\n", "\n").split("\n", -1).length, "one entry a pretty line");
            Expect.isTrue(n[0] == 1 && n[1] == 2 && n[2] == 3 && n[3] == 4, "the head and the first body line keep their numbers");
            for (int k = 4; k < n.length - 1; k++) Expect.isTrue(n[k] == 0, "a line the formatting added has no number");
            Expect.isTrue(n[n.length - 1] == 5, "the line after the body is the fifth");
            Expect.isTrue(PrettyPrint.sourceLines(raw, raw) == null, "the same text needs no account");
            Expect.isTrue(PrettyPrint.sourceLines("a\nb", "a\nc") == null, "text that differs in more than white space gets none");
        });

        Harness.test("a very long line is broken for display only", () -> {
            String raw = "abcdefghij";
            Expect.equal(PrettyPrint.wrapLongLines(raw, 4), "abcd\nefgh\nij", "broken at the limit");
            Expect.equal(PrettyPrint.wrapLongLines("ab\ncd", 4), "ab\ncd", "short lines stay");
            int[] n = PrettyPrint.sourceLines(raw, PrettyPrint.wrapLongLines(raw, 4));
            Expect.isTrue(n != null && n.length == 3, "an account of the display lines");
            Expect.isTrue(n[0] == 1 && n[1] == 0 && n[2] == 0, "continuations carry no source number");
        });

        Harness.test("JSON is indented one member per line with no space after the colon", () -> {
            String out = PrettyPrint.json("{\"a\": 1, \"b\": [true, null], \"c\": {\"d\": \"x\"}}");
            Expect.equal(out, "{\n  \"a\":1,\n  \"b\":[\n    true,\n    null\n  ],\n  \"c\":{\n    \"d\":\"x\"\n  }\n}", "nested layout");
        });

        Harness.test("JSON strings keep their spaces, brackets and escaped quotes", () -> {
            String out = PrettyPrint.json("{\"k\":\"a, {b} \\\" [c]: d\"}");
            Expect.equal(out, "{\n  \"k\":\"a, {b} \\\" [c]: d\"\n}", "string content untouched");
        });

        Harness.test("an empty JSON object opens and closes on separate lines", () -> {
            Expect.equal(PrettyPrint.json("{\"args\":{},\"n\":2}"), "{\n  \"args\":{\n  },\n  \"n\":2\n}", "empty object");
        });

        Harness.test("unbalanced or non-JSON text is returned unchanged", () -> {
            Expect.equal(PrettyPrint.json("{\"a\":[1,2}"), "{\"a\":[1,2}", "a bracket closed by the wrong kind leaves the nesting unbalanced");
            Expect.equal(PrettyPrint.json("{\"a\":1"), "{\"a\":1", "missing close brace");
            Expect.equal(PrettyPrint.json("hello"), "hello", "not JSON");
            Expect.equal(PrettyPrint.json("{\"a\":\"open"), "{\"a\":\"open", "unterminated string");
        });

        Harness.test("a style sheet is set out one declaration to a line", () -> {
            String out = PrettyPrint.markup("<html><style>html{color-scheme:light dark;background:light-dark(#eee,#222)}\nbody { margin : 0 ; font: 1em 'a;b' }</style></html>");
            Expect.equal(out, String.join("\n",
                    "<html>",
                    "  <style>",
                    "    html{",
                    "      color-scheme:light dark;",
                    "      background:light-dark(#eee,#222)",
                    "    }",
                    "    body{",
                    "      margin:0;",
                    "      font:1em 'a;b'",
                    "    }",
                    "  </style>",
                    "</html>"), "style sheet");
        });

        Harness.test("nested rules, comments and urls survive the setting out", () -> {
            StringBuilder out = new StringBuilder();
            PrettyPrint.css("@media (min-width:1px){a{b:url(x;y}z)}}/* c;d */p{q:r}", out, 0);
            Expect.equal(out.toString(), String.join("\n", "",
                    "@media (min-width:1px){",
                    "  a{",
                    "    b:url(x;y}z)",
                    "  }",
                    "}",
                    "/* c;d */p{",
                    "  q:r",
                    "}"), "nested");
        });

        Harness.test("markup nests tags and puts text on its own line", () -> {
            String out = PrettyPrint.markup("<!doctype html><html><head><meta charset=utf-8><title>Hi there</title></head><body><p>x</p></body></html>");
            Expect.equal(out, "<!doctype html><html>\n  <head>\n    <meta charset=utf-8>\n    <title>\n      Hi there\n    </title>\n  </head>\n  <body>\n    <p>\n      x\n    </p>\n  </body>\n</html>", "indented document");
        });

        Harness.test("a '>' inside a quoted attribute does not end the tag", () -> {
            String out = PrettyPrint.markup("<a title=\"x > y\"><b>t</b></a>");
            Expect.isTrue(out.startsWith("<a title=\"x > y\">\n  <b>"), "attribute kept whole");
        });

        Harness.test("script content is kept as written", () -> {
            String out = PrettyPrint.markup("<div><script>if (a < b) { go(); }</script></div>");
            Expect.isTrue(out.contains("\n    if (a < b) { go(); }\n  </script>"), "script body not parsed as tags");
        });

        Harness.test("a whole message keeps its head and formats only the body", () -> {
            String msg = "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n{\"a\":1}";
            Expect.equal(PrettyPrint.message(msg, "application/json"),
                    "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\n\r\n{\n  \"a\":1\n}", "head untouched");
            String plain = "HTTP/1.1 200 OK\r\n\r\njust text";
            Expect.equal(PrettyPrint.message(plain, "text/plain"), plain, "plain text untouched");
            String noBody = "GET / HTTP/1.1\r\nHost: x\r\n\r\n";
            Expect.equal(PrettyPrint.message(noBody, ""), noBody, "no body");
        });
    }
}