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.

4.5 KB
Raw Blame
package opensuite.ui;

import opensuite.core.Bytes;
import opensuite.core.HttpMessage;
import opensuite.core.HttpService;
import opensuite.testkit.Expect;
import opensuite.testkit.Harness;

import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import java.util.ArrayList;
import java.util.List;

/** The message editor's right-click menu: its items in the reference's order, and the request a pasted URL makes. */
public final class EditorMenuTest {

    private static List<String> labels(JPopupMenu menu) {
        List<String> out = new ArrayList<>();
        for (java.awt.Component c : menu.getComponents()) out.add(c instanceof JMenuItem mi ? mi.getText() : "-");
        return out;
    }

    public void run() {
        Harness.suite("editor menu");

        Harness.test("a request shown beside a response offers what is done with the response, and the marked items carry icons", () -> {
            List<String>[] got = new List[2];
            boolean[] icons = new boolean[2];
            try {
                javax.swing.SwingUtilities.invokeAndWait(() -> {
                    HttpService svc = new HttpService("a.test", 443, true);
                    MessageEditor req = new MessageEditor(null, MessageEditor.REQUEST, true);
                    MessageEditor resp = new MessageEditor(null, MessageEditor.RESPONSE, false);
                    new MessagePair(req, resp);
                    req.setMessage(svc, new HttpMessage(Bytes.bytes("GET /p HTTP/1.1\r\nHost: a.test\r\n\r\n")));
                    got[0] = labels(req.buildMenu());
                    resp.setMessage(svc, new HttpMessage(Bytes.bytes("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n")));
                    JPopupMenu menu = req.buildMenu();
                    got[1] = labels(menu);
                    for (java.awt.Component c : menu.getComponents()) {
                        if (!(c instanceof JMenuItem mi)) continue;
                        if (mi.getText().equals("Save item")) icons[0] = mi.getIcon() != null;
                        if (mi.getText().equals("Request in browser")) icons[1] = mi.getIcon() != null;
                    }
                });
            } catch (Exception e) { throw new RuntimeException(e); }
            Expect.isTrue(!got[0].contains("Open response in browser") && !got[0].contains("Copy links in response"), "nothing for a response before there is one");
            Expect.isTrue(got[1].contains("Open response in browser") && got[1].contains("Copy links in response"), "the response items once there is one");
            Expect.isTrue(icons[0], "Save item carries an icon");
            Expect.isTrue(!icons[1], "Request in browser carries none");
        });

        Harness.test("an editable request has the reference's editing and saving items, in its order", () -> {
            List<String>[] got = new List[1];
            try {
                javax.swing.SwingUtilities.invokeAndWait(() -> {
                    MessageEditor ed = new MessageEditor(null, MessageEditor.REQUEST, true);
                    ed.setMessage(new HttpService("a.test", 443, true), new HttpMessage(Bytes.bytes("GET /p?x=1 HTTP/1.1\r\nHost: a.test\r\n\r\n")));
                    ed.setToolDocs("Repeater");
                    ed.setHistorySaver(() -> { });
                    got[0] = labels(ed.buildMenu());
                });
            } catch (Exception e) { throw new RuntimeException(e); }
            Expect.equal(got[0], List.of(
                    "Request in browser", "-",
                    "Change request method", "Cut", "Paste host / URL as request", "Copy", "Copy prettified",
                    "Save selected text to file", "Paste", "Paste text from file", "URL-encode as you type",
                    "Convert selection", "Change body encoding", "-",
                    "Save entire history", "Copy URL", "Copy as curl command (bash)", "Save item", "-",
                    "Message editor documentation", "Repeater documentation"), "items");
        });

        Harness.test("a host or a URL becomes a GET for it; other text does not", () -> {
            Expect.equal(new String(MessageEditor.requestFor("https://a.test/x/y?q=1").raw()), "GET /x/y?q=1 HTTP/1.1\r\nHost: a.test\r\n\r\n", "url");
            Expect.equal(new String(MessageEditor.requestFor("a.test:8080").raw()), "GET / HTTP/1.1\r\nHost: a.test:8080\r\n\r\n", "host and port");
            Expect.isTrue(MessageEditor.requestFor("two words") == null, "not an address");
            Expect.isTrue(MessageEditor.requestFor("ftp://a.test/") == null, "not http");
        });
    }
}