Bug 1269468 - fix netmonitor Copy as cURL: adjusting requestData calls to current implementation of onRequestPostData; r=Honza
MozReview-Commit-ID: AOYbsV4AljW
--- a/devtools/client/netmonitor/src/utils/request-utils.js
+++ b/devtools/client/netmonitor/src/utils/request-utils.js
@@ -436,17 +436,17 @@ async function updateFormDataSections(pr
requestPostDataAvailable,
} = request;
if (requestHeadersAvailable && !requestHeaders) {
requestHeaders = await connector.requestData(id, "requestHeaders");
}
if (requestPostDataAvailable && !requestPostData) {
- requestPostData = await connector.requestData(id, "requestPostData");
+ ({ requestPostData } = await connector.requestData(id, "requestPostData"));
}
if (!formDataSections && requestHeaders && requestPostData &&
requestHeadersFromUploadStream) {
formDataSections = await getFormDataSections(
requestHeaders,
requestHeadersFromUploadStream,
requestPostData,
--- a/devtools/client/netmonitor/src/widgets/RequestListContextMenu.js
+++ b/devtools/client/netmonitor/src/widgets/RequestListContextMenu.js
@@ -281,17 +281,17 @@ class RequestListContextMenu {
let string = params
.map(param => param.name + (param.value ? "=" + param.value : ""))
.join(Services.appinfo.OS === "WINNT" ? "\r\n" : "\n");
// 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") {
string = string.replace(/\r/g, "");
}
}
copyString(string);
}
@@ -299,17 +299,17 @@ class RequestListContextMenu {
/**
* Copy a cURL command from the currently selected item.
*/
async copyAsCurl(id, url, method, httpVersion, requestHeaders, requestPostData) {
requestHeaders = requestHeaders ||
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 = {
url,
method,
headers: requestHeaders.headers,
httpVersion,
postDataText: requestPostData ? requestPostData.postData.text : "",
--- a/devtools/client/netmonitor/test/browser_net_copy_as_curl.js
+++ b/devtools/client/netmonitor/test/browser_net_copy_as_curl.js
@@ -20,70 +20,104 @@ add_task(async function() {
}
// Header param is formatted as -H "Header: value" or -H 'Header: value'
function header(h) {
return "-H " + quote(h);
}
// Construct the expected command
- const EXPECTED_RESULT = [
+ const BASE_RESULT = [
"curl " + quote(SIMPLE_SJS),
"--compressed",
header("Host: example.com"),
header("User-Agent: " + navigator.userAgent),
header("Accept: */*"),
header("Accept-Language: " + navigator.language),
header("X-Custom-Header-1: Custom value"),
header("X-Custom-Header-2: 8.8.8.8"),
header("X-Custom-Header-3: Mon, 3 Mar 2014 11:11:11 GMT"),
header("Referer: " + CURL_URL),
header("Connection: keep-alive"),
header("Pragma: no-cache"),
header("Cache-Control: no-cache")
];
- let { document } = monitor.panelWin;
-
- let wait = waitForNetworkEvents(monitor, 1);
- await ContentTask.spawn(tab.linkedBrowser, SIMPLE_SJS, async function(url) {
- content.wrappedJSObject.performRequest(url);
- });
- await wait;
+ const COOKIE_PARTIAL_RESULT = [
+ header("Cookie: bob=true; tom=cool")
+ ];
- EventUtils.sendMouseEvent({ type: "mousedown" },
- document.querySelectorAll(".request-list-item")[0]);
- EventUtils.sendMouseEvent({ type: "contextmenu" },
- document.querySelectorAll(".request-list-item")[0]);
+ const POST_PAYLOAD = "Plaintext value as a payload";
+ const POST_PARTIAL_RESULT = [
+ "--data " + quote(POST_PAYLOAD),
+ header("Content-Type: text/plain;charset=UTF-8")
+ ];
- 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, no cookies (first request)
+ await performRequest(null);
+ await testClipboardContent(BASE_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);
+ // GET request, cookies set by previous response
+ await performRequest(null);
+ await testClipboardContent([
+ ...BASE_RESULT,
+ ...COOKIE_PARTIAL_RESULT
+ ]);
- // 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.");
+ }
});
--- a/devtools/client/netmonitor/test/html_copy-as-curl.html
+++ b/devtools/client/netmonitor/test/html_copy-as-curl.html
@@ -7,27 +7,28 @@
<meta charset="utf-8"/>
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="0" />
<title>Network Monitor test page</title>
</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>
</html>