Bug 1269468 - fix netmonitor Copy as cURL: adjusting requestData calls to current implementation of onRequestPostData; r=Honza

MozReview-Commit-ID: AOYbsV4AljW

--HG--
extra : rebase_source : 338e57e5eac9716a3a2075afa1e44e2cb7dc0e10
This commit is contained in:
glowka 2018-03-09 00:14:23 +01:00
parent 2684cc4b23
commit d3ff20c01f
4 changed files with 82 additions and 47 deletions

View File

@ -441,7 +441,7 @@ async function updateFormDataSections(props) {
}
if (requestPostDataAvailable && !requestPostData) {
requestPostData = await connector.requestData(id, "requestPostData");
({ requestPostData } = await connector.requestData(id, "requestPostData"));
}
if (!formDataSections && requestHeaders && requestPostData &&

View File

@ -286,7 +286,7 @@ class RequestListContextMenu {
// Fall back to raw payload.
if (!string) {
requestPostData = requestPostData ||
await this.props.connector.requestData(id, "requestPostData").requestPostData;
(await this.props.connector.requestData(id, "requestPostData")).requestPostData;
string = requestPostData.postData.text;
if (Services.appinfo.OS !== "WINNT") {
@ -304,7 +304,7 @@ class RequestListContextMenu {
await this.props.connector.requestData(id, "requestHeaders");
requestPostData = requestPostData ||
await this.props.connector.requestData(id, "requestPostData").requestPostData;
(await this.props.connector.requestData(id, "requestPostData")).requestPostData;
// Create a sanitized object for the Curl command generator.
let data = {

View File

@ -25,7 +25,7 @@ add_task(async function() {
}
// Construct the expected command
const EXPECTED_RESULT = [
const BASE_RESULT = [
"curl " + quote(SIMPLE_SJS),
"--compressed",
header("Host: example.com"),
@ -41,49 +41,83 @@ add_task(async function() {
header("Cache-Control: no-cache")
];
let { document } = monitor.panelWin;
const COOKIE_PARTIAL_RESULT = [
header("Cookie: bob=true; tom=cool")
];
let wait = waitForNetworkEvents(monitor, 1);
await ContentTask.spawn(tab.linkedBrowser, SIMPLE_SJS, async function(url) {
content.wrappedJSObject.performRequest(url);
});
await wait;
const POST_PAYLOAD = "Plaintext value as a payload";
const POST_PARTIAL_RESULT = [
"--data '" + POST_PAYLOAD + "'",
header("Content-Type: text/plain;charset=UTF-8")
];
EventUtils.sendMouseEvent({ type: "mousedown" },
document.querySelectorAll(".request-list-item")[0]);
EventUtils.sendMouseEvent({ type: "contextmenu" },
document.querySelectorAll(".request-list-item")[0]);
// GET request, no cookies (first request)
await performRequest(null);
await testClipboardContent(BASE_RESULT);
await waitForClipboardPromise(function setup() {
monitor.panelWin.parent.document
.querySelector("#request-list-context-copy-as-curl").click();
}, function validate(result) {
if (typeof result !== "string") {
return false;
}
// GET request, cookies set by previous response
await performRequest(null);
await testClipboardContent([
...BASE_RESULT,
...COOKIE_PARTIAL_RESULT
]);
// Different setups may produce the same command, but with the
// parameters in a different order in the commandline (which is fine).
// Here we confirm that the commands are the same even in that case.
// This monster regexp parses the command line into an array of arguments,
// recognizing quoted args with matching quotes and escaped quotes inside:
// [ "curl 'url'", "--standalone-arg", "-arg-with-quoted-string 'value\'s'" ]
let matchRe = /[-A-Za-z1-9]+(?: ([\"'])(?:\\\1|.)*?\1)?/g;
let actual = result.match(matchRe);
// Must begin with the same "curl 'URL'" segment
if (!actual || EXPECTED_RESULT[0] != actual[0]) {
return false;
}
// Must match each of the params in the middle (headers and --compressed)
return EXPECTED_RESULT.length === actual.length &&
EXPECTED_RESULT.every(param => actual.includes(param));
});
info("Clipboard contains a cURL command for the currently selected item's url.");
// POST request
await performRequest(POST_PAYLOAD);
await testClipboardContent([
...BASE_RESULT,
...COOKIE_PARTIAL_RESULT,
...POST_PARTIAL_RESULT
]);
await teardown(monitor);
async function performRequest(payload) {
let wait = waitForNetworkEvents(monitor, 1);
await ContentTask.spawn(tab.linkedBrowser, {
url: SIMPLE_SJS, payload_: payload
}, async function({url, payload_}) {
content.wrappedJSObject.performRequest(url, payload_);
});
await wait;
}
async function testClipboardContent(expectedResult) {
let { document } = monitor.panelWin;
const items = document.querySelectorAll(".request-list-item");
EventUtils.sendMouseEvent({ type: "mousedown" }, items[items.length - 1]);
EventUtils.sendMouseEvent({ type: "contextmenu" },
document.querySelectorAll(".request-list-item")[0]);
await waitForClipboardPromise(function setup() {
monitor.panelWin.parent.document
.querySelector("#request-list-context-copy-as-curl").click();
}, function validate(result) {
if (typeof result !== "string") {
return false;
}
// Different setups may produce the same command, but with the
// parameters in a different order in the commandline (which is fine).
// Here we confirm that the commands are the same even in that case.
// This monster regexp parses the command line into an array of arguments,
// recognizing quoted args with matching quotes and escaped quotes inside:
// [ "curl 'url'", "--standalone-arg", "-arg-with-quoted-string 'value\'s'" ]
let matchRe = /[-A-Za-z1-9]+(?: ([\"'])(?:\\\1|.)*?\1)?/g;
let actual = result.match(matchRe);
// Must begin with the same "curl 'URL'" segment
if (!actual || expectedResult[0] != actual[0]) {
return false;
}
// Must match each of the params in the middle (headers and --compressed)
return expectedResult.length === actual.length &&
expectedResult.every(param => actual.includes(param));
});
info("Clipboard contains a cURL command for the currently selected item's url.");
}
});

View File

@ -12,20 +12,21 @@
</head>
<body>
<p>Performing a GET request</p>
<p>Performing a GET or POST request</p>
<script type="text/javascript">
/* exported performRequest */
"use strict";
function performRequest(url) {
function performRequest(url, payload) {
let xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
let method = payload ? "POST" : "GET";
xhr.open(method, url, true);
xhr.setRequestHeader("Accept-Language", window.navigator.language);
xhr.setRequestHeader("X-Custom-Header-1", "Custom value");
xhr.setRequestHeader("X-Custom-Header-2", "8.8.8.8");
xhr.setRequestHeader("X-Custom-Header-3", "Mon, 3 Mar 2014 11:11:11 GMT");
xhr.send(null);
xhr.send(payload);
}
</script>
</body>