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 | <!DOCTYPE HTML> |
michael@0 | 2 | <html lang="en"> |
michael@0 | 3 | <head> |
michael@0 | 4 | <meta charset="utf8"> |
michael@0 | 5 | <title>Test for the network actor (POST request)</title> |
michael@0 | 6 | <script type="text/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> |
michael@0 | 7 | <script type="text/javascript;version=1.8" src="common.js"></script> |
michael@0 | 8 | <!-- Any copyright is dedicated to the Public Domain. |
michael@0 | 9 | - http://creativecommons.org/publicdomain/zero/1.0/ --> |
michael@0 | 10 | </head> |
michael@0 | 11 | <body> |
michael@0 | 12 | <p>Test for the network actor (POST request)</p> |
michael@0 | 13 | |
michael@0 | 14 | <iframe src="http://example.com/chrome/toolkit/devtools/webconsole/test/network_requests_iframe.html"></iframe> |
michael@0 | 15 | |
michael@0 | 16 | <script class="testbody" type="text/javascript;version=1.8"> |
michael@0 | 17 | SimpleTest.waitForExplicitFinish(); |
michael@0 | 18 | |
michael@0 | 19 | function startTest() |
michael@0 | 20 | { |
michael@0 | 21 | removeEventListener("load", startTest); |
michael@0 | 22 | |
michael@0 | 23 | attachConsole(["NetworkActivity"], onAttach, true); |
michael@0 | 24 | } |
michael@0 | 25 | |
michael@0 | 26 | function onAttach(aState, aResponse) |
michael@0 | 27 | { |
michael@0 | 28 | info("enable network request and response body logging"); |
michael@0 | 29 | |
michael@0 | 30 | onSetPreferences = onSetPreferences.bind(null, aState); |
michael@0 | 31 | aState.client.setPreferences({ |
michael@0 | 32 | "NetworkMonitor.saveRequestAndResponseBodies": true, |
michael@0 | 33 | }, onSetPreferences); |
michael@0 | 34 | } |
michael@0 | 35 | |
michael@0 | 36 | function onSetPreferences(aState, aResponse) |
michael@0 | 37 | { |
michael@0 | 38 | is(aResponse.updated.length, 1, "updated prefs length"); |
michael@0 | 39 | is(aResponse.updated[0], "NetworkMonitor.saveRequestAndResponseBodies", |
michael@0 | 40 | "updated prefs length"); |
michael@0 | 41 | |
michael@0 | 42 | info("test network POST request"); |
michael@0 | 43 | |
michael@0 | 44 | onNetworkEvent = onNetworkEvent.bind(null, aState); |
michael@0 | 45 | aState.dbgClient.addListener("networkEvent", onNetworkEvent); |
michael@0 | 46 | onNetworkEventUpdate = onNetworkEventUpdate.bind(null, aState); |
michael@0 | 47 | aState.dbgClient.addListener("networkEventUpdate", onNetworkEventUpdate); |
michael@0 | 48 | |
michael@0 | 49 | let iframe = document.querySelector("iframe").contentWindow; |
michael@0 | 50 | iframe.wrappedJSObject.testXhrPost(); |
michael@0 | 51 | } |
michael@0 | 52 | |
michael@0 | 53 | function onNetworkEvent(aState, aType, aPacket) |
michael@0 | 54 | { |
michael@0 | 55 | is(aPacket.from, aState.actor, "network event actor"); |
michael@0 | 56 | |
michael@0 | 57 | info("checking the network event packet"); |
michael@0 | 58 | |
michael@0 | 59 | let netActor = aPacket.eventActor; |
michael@0 | 60 | |
michael@0 | 61 | checkObject(netActor, { |
michael@0 | 62 | actor: /[a-z]/, |
michael@0 | 63 | startedDateTime: /^\d+\-\d+\-\d+T.+$/, |
michael@0 | 64 | url: /data\.json/, |
michael@0 | 65 | method: "POST", |
michael@0 | 66 | }); |
michael@0 | 67 | |
michael@0 | 68 | aState.netActor = netActor.actor; |
michael@0 | 69 | |
michael@0 | 70 | aState.dbgClient.removeListener("networkEvent", onNetworkEvent); |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | let updates = []; |
michael@0 | 74 | |
michael@0 | 75 | function onNetworkEventUpdate(aState, aType, aPacket) |
michael@0 | 76 | { |
michael@0 | 77 | info("received networkEventUpdate " + aPacket.updateType); |
michael@0 | 78 | is(aPacket.from, aState.netActor, "networkEventUpdate actor"); |
michael@0 | 79 | |
michael@0 | 80 | updates.push(aPacket.updateType); |
michael@0 | 81 | |
michael@0 | 82 | let expectedPacket = null; |
michael@0 | 83 | |
michael@0 | 84 | switch (aPacket.updateType) { |
michael@0 | 85 | case "requestHeaders": |
michael@0 | 86 | case "responseHeaders": |
michael@0 | 87 | ok(aPacket.headers > 0, "headers > 0"); |
michael@0 | 88 | ok(aPacket.headersSize > 0, "headersSize > 0"); |
michael@0 | 89 | break; |
michael@0 | 90 | case "requestCookies": |
michael@0 | 91 | expectedPacket = { |
michael@0 | 92 | cookies: 3, |
michael@0 | 93 | }; |
michael@0 | 94 | break; |
michael@0 | 95 | case "requestPostData": |
michael@0 | 96 | ok(aPacket.dataSize > 0, "dataSize > 0"); |
michael@0 | 97 | ok(!aPacket.discardRequestBody, "discardRequestBody"); |
michael@0 | 98 | break; |
michael@0 | 99 | case "responseStart": |
michael@0 | 100 | expectedPacket = { |
michael@0 | 101 | response: { |
michael@0 | 102 | httpVersion: /^HTTP\/\d\.\d$/, |
michael@0 | 103 | status: 200, |
michael@0 | 104 | statusText: "OK", |
michael@0 | 105 | headersSize: /^\d+$/, |
michael@0 | 106 | discardResponseBody: false, |
michael@0 | 107 | }, |
michael@0 | 108 | }; |
michael@0 | 109 | break; |
michael@0 | 110 | case "responseCookies": |
michael@0 | 111 | expectedPacket = { |
michael@0 | 112 | cookies: 0, |
michael@0 | 113 | }; |
michael@0 | 114 | break; |
michael@0 | 115 | case "responseContent": |
michael@0 | 116 | expectedPacket = { |
michael@0 | 117 | mimeType: "application/json", |
michael@0 | 118 | contentSize: /^\d+$/, |
michael@0 | 119 | discardResponseBody: false, |
michael@0 | 120 | }; |
michael@0 | 121 | break; |
michael@0 | 122 | case "eventTimings": |
michael@0 | 123 | expectedPacket = { |
michael@0 | 124 | totalTime: /^\d+$/, |
michael@0 | 125 | }; |
michael@0 | 126 | break; |
michael@0 | 127 | default: |
michael@0 | 128 | ok(false, "unknown network event update type: " + |
michael@0 | 129 | aPacket.updateType); |
michael@0 | 130 | return; |
michael@0 | 131 | } |
michael@0 | 132 | |
michael@0 | 133 | if (expectedPacket) { |
michael@0 | 134 | info("checking the packet content"); |
michael@0 | 135 | checkObject(aPacket, expectedPacket); |
michael@0 | 136 | } |
michael@0 | 137 | |
michael@0 | 138 | if (updates.indexOf("responseContent") > -1 && |
michael@0 | 139 | updates.indexOf("eventTimings") > -1) { |
michael@0 | 140 | aState.dbgClient.removeListener("networkEventUpdate", |
michael@0 | 141 | onNetworkEvent); |
michael@0 | 142 | |
michael@0 | 143 | onRequestHeaders = onRequestHeaders.bind(null, aState); |
michael@0 | 144 | aState.client.getRequestHeaders(aState.netActor, |
michael@0 | 145 | onRequestHeaders); |
michael@0 | 146 | } |
michael@0 | 147 | } |
michael@0 | 148 | |
michael@0 | 149 | function onRequestHeaders(aState, aResponse) |
michael@0 | 150 | { |
michael@0 | 151 | info("checking request headers"); |
michael@0 | 152 | |
michael@0 | 153 | ok(aResponse.headers.length > 0, "request headers > 0"); |
michael@0 | 154 | ok(aResponse.headersSize > 0, "request headersSize > 0"); |
michael@0 | 155 | |
michael@0 | 156 | checkHeadersOrCookies(aResponse.headers, { |
michael@0 | 157 | Referer: /network_requests_iframe\.html/, |
michael@0 | 158 | Cookie: /bug768096/, |
michael@0 | 159 | }); |
michael@0 | 160 | |
michael@0 | 161 | onRequestCookies = onRequestCookies.bind(null, aState); |
michael@0 | 162 | aState.client.getRequestCookies(aState.netActor, |
michael@0 | 163 | onRequestCookies); |
michael@0 | 164 | } |
michael@0 | 165 | |
michael@0 | 166 | function onRequestCookies(aState, aResponse) |
michael@0 | 167 | { |
michael@0 | 168 | info("checking request cookies"); |
michael@0 | 169 | |
michael@0 | 170 | is(aResponse.cookies.length, 3, "request cookies length"); |
michael@0 | 171 | |
michael@0 | 172 | checkHeadersOrCookies(aResponse.cookies, { |
michael@0 | 173 | foobar: "fooval", |
michael@0 | 174 | omgfoo: "bug768096", |
michael@0 | 175 | badcookie: "bug826798=st3fan", |
michael@0 | 176 | }); |
michael@0 | 177 | |
michael@0 | 178 | onRequestPostData = onRequestPostData.bind(null, aState); |
michael@0 | 179 | aState.client.getRequestPostData(aState.netActor, |
michael@0 | 180 | onRequestPostData); |
michael@0 | 181 | } |
michael@0 | 182 | |
michael@0 | 183 | function onRequestPostData(aState, aResponse) |
michael@0 | 184 | { |
michael@0 | 185 | info("checking request POST data"); |
michael@0 | 186 | |
michael@0 | 187 | checkObject(aResponse, { |
michael@0 | 188 | postData: { |
michael@0 | 189 | text: /^Hello world! foobaz barr.+foobaz barr$/, |
michael@0 | 190 | }, |
michael@0 | 191 | postDataDiscarded: false, |
michael@0 | 192 | }); |
michael@0 | 193 | |
michael@0 | 194 | is(aResponse.postData.text.length, 552, "postData text length"); |
michael@0 | 195 | |
michael@0 | 196 | onResponseHeaders = onResponseHeaders.bind(null, aState); |
michael@0 | 197 | aState.client.getResponseHeaders(aState.netActor, |
michael@0 | 198 | onResponseHeaders); |
michael@0 | 199 | } |
michael@0 | 200 | |
michael@0 | 201 | function onResponseHeaders(aState, aResponse) |
michael@0 | 202 | { |
michael@0 | 203 | info("checking response headers"); |
michael@0 | 204 | |
michael@0 | 205 | ok(aResponse.headers.length > 0, "response headers > 0"); |
michael@0 | 206 | ok(aResponse.headersSize > 0, "response headersSize > 0"); |
michael@0 | 207 | |
michael@0 | 208 | checkHeadersOrCookies(aResponse.headers, { |
michael@0 | 209 | "Content-Type": /^application\/(json|octet-stream)$/, |
michael@0 | 210 | "Content-Length": /^\d+$/, |
michael@0 | 211 | }); |
michael@0 | 212 | |
michael@0 | 213 | onResponseCookies = onResponseCookies.bind(null, aState); |
michael@0 | 214 | aState.client.getResponseCookies(aState.netActor, |
michael@0 | 215 | onResponseCookies); |
michael@0 | 216 | } |
michael@0 | 217 | |
michael@0 | 218 | function onResponseCookies(aState, aResponse) |
michael@0 | 219 | { |
michael@0 | 220 | info("checking response cookies"); |
michael@0 | 221 | |
michael@0 | 222 | is(aResponse.cookies.length, 0, "response cookies length"); |
michael@0 | 223 | |
michael@0 | 224 | onResponseContent = onResponseContent.bind(null, aState); |
michael@0 | 225 | aState.client.getResponseContent(aState.netActor, |
michael@0 | 226 | onResponseContent); |
michael@0 | 227 | } |
michael@0 | 228 | |
michael@0 | 229 | function onResponseContent(aState, aResponse) |
michael@0 | 230 | { |
michael@0 | 231 | info("checking response content"); |
michael@0 | 232 | |
michael@0 | 233 | checkObject(aResponse, { |
michael@0 | 234 | content: { |
michael@0 | 235 | text: /"test JSON data"/, |
michael@0 | 236 | }, |
michael@0 | 237 | contentDiscarded: false, |
michael@0 | 238 | }); |
michael@0 | 239 | |
michael@0 | 240 | onEventTimings = onEventTimings.bind(null, aState); |
michael@0 | 241 | aState.client.getEventTimings(aState.netActor, |
michael@0 | 242 | onEventTimings); |
michael@0 | 243 | } |
michael@0 | 244 | |
michael@0 | 245 | function onEventTimings(aState, aResponse) |
michael@0 | 246 | { |
michael@0 | 247 | info("checking event timings"); |
michael@0 | 248 | |
michael@0 | 249 | checkObject(aResponse, { |
michael@0 | 250 | timings: { |
michael@0 | 251 | blocked: /^-1|\d+$/, |
michael@0 | 252 | dns: /^-1|\d+$/, |
michael@0 | 253 | connect: /^-1|\d+$/, |
michael@0 | 254 | send: /^-1|\d+$/, |
michael@0 | 255 | wait: /^-1|\d+$/, |
michael@0 | 256 | receive: /^-1|\d+$/, |
michael@0 | 257 | }, |
michael@0 | 258 | totalTime: /^\d+$/, |
michael@0 | 259 | }); |
michael@0 | 260 | |
michael@0 | 261 | closeDebugger(aState, function() { |
michael@0 | 262 | SimpleTest.finish(); |
michael@0 | 263 | }); |
michael@0 | 264 | } |
michael@0 | 265 | |
michael@0 | 266 | addEventListener("load", startTest); |
michael@0 | 267 | </script> |
michael@0 | 268 | </body> |
michael@0 | 269 | </html> |