Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* Any copyright is dedicated to the Public Domain. |
michael@0 | 2 | http://creativecommons.org/publicdomain/zero/1.0/ */ |
michael@0 | 3 | |
michael@0 | 4 | let gPanelWin; |
michael@0 | 5 | let gPanelDoc; |
michael@0 | 6 | |
michael@0 | 7 | const ADD_QUERY = "t1=t2"; |
michael@0 | 8 | const ADD_HEADER = "Test-header: true"; |
michael@0 | 9 | const ADD_POSTDATA = "t3=t4"; |
michael@0 | 10 | |
michael@0 | 11 | /** |
michael@0 | 12 | * Tests if resending a request works. |
michael@0 | 13 | */ |
michael@0 | 14 | |
michael@0 | 15 | function test() { |
michael@0 | 16 | initNetMonitor(POST_DATA_URL).then(([aTab, aDebuggee, aMonitor]) => { |
michael@0 | 17 | info("Starting test... "); |
michael@0 | 18 | |
michael@0 | 19 | gPanelWin = aMonitor.panelWin; |
michael@0 | 20 | gPanelDoc = gPanelWin.document; |
michael@0 | 21 | |
michael@0 | 22 | let { NetMonitorView } = gPanelWin; |
michael@0 | 23 | let { RequestsMenu } = NetMonitorView; |
michael@0 | 24 | let TAB_UPDATED = aMonitor.panelWin.EVENTS.TAB_UPDATED; |
michael@0 | 25 | let CUSTOMREQUESTVIEW_POPULATED = aMonitor.panelWin.EVENTS.CUSTOMREQUESTVIEW_POPULATED; |
michael@0 | 26 | |
michael@0 | 27 | RequestsMenu.lazyUpdate = false; |
michael@0 | 28 | |
michael@0 | 29 | waitForNetworkEvents(aMonitor, 0, 2).then(() => { |
michael@0 | 30 | let origItem = RequestsMenu.getItemAtIndex(0); |
michael@0 | 31 | RequestsMenu.selectedItem = origItem; |
michael@0 | 32 | |
michael@0 | 33 | waitFor(aMonitor.panelWin, TAB_UPDATED).then(() => { |
michael@0 | 34 | // add a new custom request cloned from selected request |
michael@0 | 35 | RequestsMenu.cloneSelectedRequest(); |
michael@0 | 36 | return waitFor(aMonitor.panelWin, CUSTOMREQUESTVIEW_POPULATED); |
michael@0 | 37 | }).then(() => { |
michael@0 | 38 | testCustomForm(origItem.attachment); |
michael@0 | 39 | |
michael@0 | 40 | let customItem = RequestsMenu.selectedItem; |
michael@0 | 41 | testCustomItem(customItem, origItem); |
michael@0 | 42 | |
michael@0 | 43 | // edit the custom request |
michael@0 | 44 | editCustomForm(() => { |
michael@0 | 45 | testCustomItemChanged(customItem, origItem); |
michael@0 | 46 | |
michael@0 | 47 | waitForNetworkEvents(aMonitor, 0, 1).then(() => { |
michael@0 | 48 | let sentItem = RequestsMenu.selectedItem; |
michael@0 | 49 | testSentRequest(sentItem.attachment, origItem.attachment); |
michael@0 | 50 | finishUp(aMonitor); |
michael@0 | 51 | }); |
michael@0 | 52 | // send the new request |
michael@0 | 53 | RequestsMenu.sendCustomRequest(); |
michael@0 | 54 | }); |
michael@0 | 55 | }); |
michael@0 | 56 | }); |
michael@0 | 57 | |
michael@0 | 58 | aDebuggee.performRequests(); |
michael@0 | 59 | }); |
michael@0 | 60 | } |
michael@0 | 61 | |
michael@0 | 62 | function testCustomItem(aItem, aOrigItem) { |
michael@0 | 63 | let method = aItem.target.querySelector(".requests-menu-method").value; |
michael@0 | 64 | let origMethod = aOrigItem.target.querySelector(".requests-menu-method").value; |
michael@0 | 65 | is(method, origMethod, "menu item is showing the same method as original request"); |
michael@0 | 66 | |
michael@0 | 67 | let file = aItem.target.querySelector(".requests-menu-file").value; |
michael@0 | 68 | let origFile = aOrigItem.target.querySelector(".requests-menu-file").value; |
michael@0 | 69 | is(file, origFile, "menu item is showing the same file name as original request"); |
michael@0 | 70 | |
michael@0 | 71 | let domain = aItem.target.querySelector(".requests-menu-domain").value; |
michael@0 | 72 | let origDomain = aOrigItem.target.querySelector(".requests-menu-domain").value; |
michael@0 | 73 | is(domain, origDomain, "menu item is showing the same domain as original request"); |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | function testCustomItemChanged(aItem, aOrigItem) { |
michael@0 | 77 | let file = aItem.target.querySelector(".requests-menu-file").value; |
michael@0 | 78 | let expectedFile = aOrigItem.target.querySelector(".requests-menu-file").value + "&" + ADD_QUERY; |
michael@0 | 79 | |
michael@0 | 80 | is(file, expectedFile, "menu item is updated to reflect url entered in form"); |
michael@0 | 81 | } |
michael@0 | 82 | |
michael@0 | 83 | /* |
michael@0 | 84 | * Test that the New Request form was populated correctly |
michael@0 | 85 | */ |
michael@0 | 86 | function testCustomForm(aData) { |
michael@0 | 87 | is(gPanelDoc.getElementById("custom-method-value").value, aData.method, |
michael@0 | 88 | "new request form showing correct method"); |
michael@0 | 89 | |
michael@0 | 90 | is(gPanelDoc.getElementById("custom-url-value").value, aData.url, |
michael@0 | 91 | "new request form showing correct url"); |
michael@0 | 92 | |
michael@0 | 93 | let query = gPanelDoc.getElementById("custom-query-value"); |
michael@0 | 94 | is(query.value, "foo=bar\nbaz=42\ntype=urlencoded", |
michael@0 | 95 | "new request form showing correct query string"); |
michael@0 | 96 | |
michael@0 | 97 | let headers = gPanelDoc.getElementById("custom-headers-value").value.split("\n"); |
michael@0 | 98 | for (let {name, value} of aData.requestHeaders.headers) { |
michael@0 | 99 | ok(headers.indexOf(name + ": " + value) >= 0, "form contains header from request"); |
michael@0 | 100 | } |
michael@0 | 101 | |
michael@0 | 102 | let postData = gPanelDoc.getElementById("custom-postdata-value"); |
michael@0 | 103 | is(postData.value, aData.requestPostData.postData.text, |
michael@0 | 104 | "new request form showing correct post data"); |
michael@0 | 105 | } |
michael@0 | 106 | |
michael@0 | 107 | /* |
michael@0 | 108 | * Add some params and headers to the request form |
michael@0 | 109 | */ |
michael@0 | 110 | function editCustomForm(callback) { |
michael@0 | 111 | gPanelWin.focus(); |
michael@0 | 112 | |
michael@0 | 113 | let query = gPanelDoc.getElementById("custom-query-value"); |
michael@0 | 114 | query.addEventListener("focus", function onFocus() { |
michael@0 | 115 | query.removeEventListener("focus", onFocus, false); |
michael@0 | 116 | |
michael@0 | 117 | // add params to url query string field |
michael@0 | 118 | type(["VK_RETURN"]); |
michael@0 | 119 | type(ADD_QUERY); |
michael@0 | 120 | |
michael@0 | 121 | let headers = gPanelDoc.getElementById("custom-headers-value"); |
michael@0 | 122 | headers.addEventListener("focus", function onFocus() { |
michael@0 | 123 | headers.removeEventListener("focus", onFocus, false); |
michael@0 | 124 | |
michael@0 | 125 | // add a header |
michael@0 | 126 | type(["VK_RETURN"]); |
michael@0 | 127 | type(ADD_HEADER); |
michael@0 | 128 | |
michael@0 | 129 | let postData = gPanelDoc.getElementById("custom-postdata-value"); |
michael@0 | 130 | postData.addEventListener("focus", function onFocus() { |
michael@0 | 131 | postData.removeEventListener("focus", onFocus, false); |
michael@0 | 132 | |
michael@0 | 133 | // add to POST data |
michael@0 | 134 | type(ADD_POSTDATA); |
michael@0 | 135 | callback(); |
michael@0 | 136 | }, false); |
michael@0 | 137 | postData.focus(); |
michael@0 | 138 | }, false); |
michael@0 | 139 | headers.focus(); |
michael@0 | 140 | }, false); |
michael@0 | 141 | query.focus(); |
michael@0 | 142 | } |
michael@0 | 143 | |
michael@0 | 144 | /* |
michael@0 | 145 | * Make sure newly created event matches expected request |
michael@0 | 146 | */ |
michael@0 | 147 | function testSentRequest(aData, aOrigData) { |
michael@0 | 148 | is(aData.method, aOrigData.method, "correct method in sent request"); |
michael@0 | 149 | is(aData.url, aOrigData.url + "&" + ADD_QUERY, "correct url in sent request"); |
michael@0 | 150 | |
michael@0 | 151 | let hasHeader = aData.requestHeaders.headers.some((header) => { |
michael@0 | 152 | return (header.name + ": " + header.value) == ADD_HEADER; |
michael@0 | 153 | }) |
michael@0 | 154 | ok(hasHeader, "new header added to sent request"); |
michael@0 | 155 | |
michael@0 | 156 | is(aData.requestPostData.postData.text, |
michael@0 | 157 | aOrigData.requestPostData.postData.text + ADD_POSTDATA, |
michael@0 | 158 | "post data added to sent request"); |
michael@0 | 159 | } |
michael@0 | 160 | |
michael@0 | 161 | |
michael@0 | 162 | function type(aString) { |
michael@0 | 163 | for (let ch of aString) { |
michael@0 | 164 | EventUtils.synthesizeKey(ch, {}, gPanelWin); |
michael@0 | 165 | } |
michael@0 | 166 | } |
michael@0 | 167 | |
michael@0 | 168 | function finishUp(aMonitor) { |
michael@0 | 169 | gPanelWin = null; |
michael@0 | 170 | gPanelDoc = null; |
michael@0 | 171 | |
michael@0 | 172 | teardown(aMonitor).then(finish); |
michael@0 | 173 | } |