|
1 /* vim:set ts=2 sw=2 sts=2 et: */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 // Tests that the network panel works. |
|
7 |
|
8 const TEST_URI = "http://example.com/browser/browser/devtools/webconsole/test/test-console.html"; |
|
9 const TEST_IMG = "http://example.com/browser/browser/devtools/webconsole/test/test-image.png"; |
|
10 const TEST_ENCODING_ISO_8859_1 = "http://example.com/browser/browser/devtools/webconsole/test/test-encoding-ISO-8859-1.html"; |
|
11 |
|
12 const TEST_IMG_BASE64 = |
|
13 "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABHNCSVQICAgIfAhkiAAAAVRJREFU" + |
|
14 "OI2lk7FLw0AUxr+YpC1CBqcMWfsvCCLdXFzqEJCgDl1EQRGxg9AhSBEJONhFhG52UCuFDjq5dxD8" + |
|
15 "FwoO0qGDOBQkl7vLOeWa2EQDffDBvTu+373Hu1OEEJgntGgxGD6J+7fLXKbt5VNUyhsKAChRBQcP" + |
|
16 "FVFeWskFGH694mZroCQqCLlAwPxcgJBP254CmAD5B7C7dgHLMLF3uzoL4DQEod+Z5sP1FizDxGgy" + |
|
17 "BqfhLID9AahX29J89bwPFgMsSEAQglAf9WobhPpScbPXr4FQHyzIADTsDizDRMPuIOC+zEeTMZo9" + |
|
18 "BwH3EfAMACccbtfGaDKGZZg423yUZrdrg3EqxQlPr0BTdTR7joREN2uqnlBmCwW1hIJagtev4f3z" + |
|
19 "A16/JvfiigMSYyzqJXlw/XKUyOORMUaBor6YavgdjKa8xGOnidadmwtwsnMu18q83/kHSou+bFND" + |
|
20 "Dr4AAAAASUVORK5CYII="; |
|
21 |
|
22 let testDriver; |
|
23 |
|
24 function test() { |
|
25 addTab(TEST_URI); |
|
26 browser.addEventListener("load", function onLoad() { |
|
27 browser.removeEventListener("load", onLoad, true); |
|
28 openConsole(null, testNetworkPanel); |
|
29 }, true); |
|
30 } |
|
31 |
|
32 function testNetworkPanel() { |
|
33 testDriver = testGen(); |
|
34 testDriver.next(); |
|
35 } |
|
36 |
|
37 function checkIsVisible(aPanel, aList) { |
|
38 for (let id in aList) { |
|
39 let node = aPanel.document.getElementById(id); |
|
40 let isVisible = aList[id]; |
|
41 is(node.style.display, (isVisible ? "block" : "none"), id + " isVisible=" + isVisible); |
|
42 } |
|
43 } |
|
44 |
|
45 function checkNodeContent(aPanel, aId, aContent) { |
|
46 let node = aPanel.document.getElementById(aId); |
|
47 if (node == null) { |
|
48 ok(false, "Tried to access node " + aId + " that doesn't exist!"); |
|
49 } |
|
50 else if (node.textContent.indexOf(aContent) != -1) { |
|
51 ok(true, "checking content of " + aId); |
|
52 } |
|
53 else { |
|
54 ok(false, "Got false value for " + aId + ": " + node.textContent + " doesn't have " + aContent); |
|
55 } |
|
56 } |
|
57 |
|
58 function checkNodeKeyValue(aPanel, aId, aKey, aValue) { |
|
59 let node = aPanel.document.getElementById(aId); |
|
60 |
|
61 let headers = node.querySelectorAll("th"); |
|
62 for (let i = 0; i < headers.length; i++) { |
|
63 if (headers[i].textContent == (aKey + ":")) { |
|
64 is(headers[i].nextElementSibling.textContent, aValue, |
|
65 "checking content of " + aId + " for key " + aKey); |
|
66 return; |
|
67 } |
|
68 } |
|
69 |
|
70 ok(false, "content check failed for " + aId + ", key " + aKey); |
|
71 } |
|
72 |
|
73 function testGen() { |
|
74 let hud = HUDService.getHudByWindow(content); |
|
75 let filterBox = hud.ui.filterBox; |
|
76 |
|
77 let httpActivity = { |
|
78 updates: [], |
|
79 discardRequestBody: true, |
|
80 discardResponseBody: true, |
|
81 startedDateTime: (new Date()).toISOString(), |
|
82 request: { |
|
83 url: "http://www.testpage.com", |
|
84 method: "GET", |
|
85 cookies: [], |
|
86 headers: [ |
|
87 { name: "foo", value: "bar" }, |
|
88 ], |
|
89 }, |
|
90 response: { |
|
91 headers: [], |
|
92 content: {}, |
|
93 cookies: [], |
|
94 }, |
|
95 timings: {}, |
|
96 }; |
|
97 |
|
98 let networkPanel = hud.ui.openNetworkPanel(filterBox, httpActivity); |
|
99 |
|
100 is(filterBox._netPanel, networkPanel, |
|
101 "Network panel stored on the anchor object"); |
|
102 |
|
103 networkPanel._onUpdate = function() { |
|
104 networkPanel._onUpdate = null; |
|
105 executeSoon(function() { |
|
106 testDriver.next(); |
|
107 }); |
|
108 }; |
|
109 |
|
110 yield undefined; |
|
111 |
|
112 info("test 1"); |
|
113 |
|
114 checkIsVisible(networkPanel, { |
|
115 requestCookie: false, |
|
116 requestFormData: false, |
|
117 requestBody: false, |
|
118 responseContainer: false, |
|
119 responseBody: false, |
|
120 responseNoBody: false, |
|
121 responseImage: false, |
|
122 responseImageCached: false |
|
123 }); |
|
124 |
|
125 checkNodeContent(networkPanel, "header", "http://www.testpage.com"); |
|
126 checkNodeContent(networkPanel, "header", "GET"); |
|
127 checkNodeKeyValue(networkPanel, "requestHeadersContent", "foo", "bar"); |
|
128 |
|
129 // Test request body. |
|
130 info("test 2: request body"); |
|
131 httpActivity.discardRequestBody = false; |
|
132 httpActivity.request.postData = { text: "hello world" }; |
|
133 networkPanel.update(); |
|
134 |
|
135 checkIsVisible(networkPanel, { |
|
136 requestBody: true, |
|
137 requestFormData: false, |
|
138 requestCookie: false, |
|
139 responseContainer: false, |
|
140 responseBody: false, |
|
141 responseNoBody: false, |
|
142 responseImage: false, |
|
143 responseImageCached: false |
|
144 }); |
|
145 checkNodeContent(networkPanel, "requestBodyContent", "hello world"); |
|
146 |
|
147 // Test response header. |
|
148 info("test 3: response header"); |
|
149 httpActivity.timings.wait = 10; |
|
150 httpActivity.response.httpVersion = "HTTP/3.14"; |
|
151 httpActivity.response.status = 999; |
|
152 httpActivity.response.statusText = "earthquake win"; |
|
153 httpActivity.response.content.mimeType = "text/html"; |
|
154 httpActivity.response.headers.push( |
|
155 { name: "Content-Type", value: "text/html" }, |
|
156 { name: "leaveHouses", value: "true" } |
|
157 ); |
|
158 |
|
159 networkPanel.update(); |
|
160 |
|
161 checkIsVisible(networkPanel, { |
|
162 requestBody: true, |
|
163 requestFormData: false, |
|
164 requestCookie: false, |
|
165 responseContainer: true, |
|
166 responseBody: false, |
|
167 responseNoBody: false, |
|
168 responseImage: false, |
|
169 responseImageCached: false |
|
170 }); |
|
171 |
|
172 checkNodeContent(networkPanel, "header", "HTTP/3.14 999 earthquake win"); |
|
173 checkNodeKeyValue(networkPanel, "responseHeadersContent", "leaveHouses", "true"); |
|
174 checkNodeContent(networkPanel, "responseHeadersInfo", "10ms"); |
|
175 |
|
176 info("test 4"); |
|
177 |
|
178 httpActivity.discardResponseBody = false; |
|
179 httpActivity.timings.receive = 2; |
|
180 networkPanel.update(); |
|
181 |
|
182 checkIsVisible(networkPanel, { |
|
183 requestBody: true, |
|
184 requestCookie: false, |
|
185 requestFormData: false, |
|
186 responseContainer: true, |
|
187 responseBody: false, |
|
188 responseNoBody: false, |
|
189 responseImage: false, |
|
190 responseImageCached: false |
|
191 }); |
|
192 |
|
193 info("test 5"); |
|
194 |
|
195 httpActivity.updates.push("responseContent", "eventTimings"); |
|
196 networkPanel.update(); |
|
197 |
|
198 checkNodeContent(networkPanel, "responseNoBodyInfo", "2ms"); |
|
199 checkIsVisible(networkPanel, { |
|
200 requestBody: true, |
|
201 requestCookie: false, |
|
202 responseContainer: true, |
|
203 responseBody: false, |
|
204 responseNoBody: true, |
|
205 responseImage: false, |
|
206 responseImageCached: false |
|
207 }); |
|
208 |
|
209 networkPanel.panel.hidePopup(); |
|
210 |
|
211 // Second run: Test for cookies and response body. |
|
212 info("test 6: cookies and response body"); |
|
213 httpActivity.request.cookies.push( |
|
214 { name: "foo", value: "bar" }, |
|
215 { name: "hello", value: "world" } |
|
216 ); |
|
217 httpActivity.response.content.text = "get out here"; |
|
218 |
|
219 networkPanel = hud.ui.openNetworkPanel(filterBox, httpActivity); |
|
220 is(filterBox._netPanel, networkPanel, |
|
221 "Network panel stored on httpActivity object"); |
|
222 |
|
223 networkPanel._onUpdate = function() { |
|
224 networkPanel._onUpdate = null; |
|
225 executeSoon(function() { |
|
226 testDriver.next(); |
|
227 }); |
|
228 }; |
|
229 |
|
230 yield undefined; |
|
231 |
|
232 checkIsVisible(networkPanel, { |
|
233 requestBody: true, |
|
234 requestFormData: false, |
|
235 requestCookie: true, |
|
236 responseContainer: true, |
|
237 responseCookie: false, |
|
238 responseBody: true, |
|
239 responseNoBody: false, |
|
240 responseImage: false, |
|
241 responseImageCached: false |
|
242 }); |
|
243 |
|
244 checkNodeKeyValue(networkPanel, "requestCookieContent", "foo", "bar"); |
|
245 checkNodeKeyValue(networkPanel, "requestCookieContent", "hello", "world"); |
|
246 checkNodeContent(networkPanel, "responseBodyContent", "get out here"); |
|
247 checkNodeContent(networkPanel, "responseBodyInfo", "2ms"); |
|
248 |
|
249 networkPanel.panel.hidePopup(); |
|
250 |
|
251 // Third run: Test for response cookies. |
|
252 info("test 6b: response cookies"); |
|
253 httpActivity.response.cookies.push( |
|
254 { name: "foobar", value: "boom" }, |
|
255 { name: "foobaz", value: "omg" } |
|
256 ); |
|
257 |
|
258 networkPanel = hud.ui.openNetworkPanel(filterBox, httpActivity); |
|
259 is(filterBox._netPanel, networkPanel, |
|
260 "Network panel stored on httpActivity object"); |
|
261 |
|
262 networkPanel._onUpdate = function() { |
|
263 networkPanel._onUpdate = null; |
|
264 executeSoon(function() { |
|
265 testDriver.next(); |
|
266 }); |
|
267 }; |
|
268 |
|
269 yield undefined; |
|
270 |
|
271 checkIsVisible(networkPanel, { |
|
272 requestBody: true, |
|
273 requestFormData: false, |
|
274 requestCookie: true, |
|
275 responseContainer: true, |
|
276 responseCookie: true, |
|
277 responseBody: true, |
|
278 responseNoBody: false, |
|
279 responseImage: false, |
|
280 responseImageCached: false, |
|
281 responseBodyFetchLink: false, |
|
282 }); |
|
283 |
|
284 checkNodeKeyValue(networkPanel, "responseCookieContent", "foobar", "boom"); |
|
285 checkNodeKeyValue(networkPanel, "responseCookieContent", "foobaz", "omg"); |
|
286 |
|
287 networkPanel.panel.hidePopup(); |
|
288 |
|
289 // Check image request. |
|
290 info("test 7: image request"); |
|
291 httpActivity.response.headers[1].value = "image/png"; |
|
292 httpActivity.response.content.mimeType = "image/png"; |
|
293 httpActivity.response.content.text = TEST_IMG_BASE64; |
|
294 httpActivity.request.url = TEST_IMG; |
|
295 |
|
296 networkPanel = hud.ui.openNetworkPanel(filterBox, httpActivity); |
|
297 networkPanel._onUpdate = function() { |
|
298 networkPanel._onUpdate = null; |
|
299 executeSoon(function() { |
|
300 testDriver.next(); |
|
301 }); |
|
302 }; |
|
303 |
|
304 yield undefined; |
|
305 |
|
306 checkIsVisible(networkPanel, { |
|
307 requestBody: true, |
|
308 requestFormData: false, |
|
309 requestCookie: true, |
|
310 responseContainer: true, |
|
311 responseBody: false, |
|
312 responseNoBody: false, |
|
313 responseImage: true, |
|
314 responseImageCached: false, |
|
315 responseBodyFetchLink: false, |
|
316 }); |
|
317 |
|
318 let imgNode = networkPanel.document.getElementById("responseImageNode"); |
|
319 is(imgNode.getAttribute("src"), "data:image/png;base64," + TEST_IMG_BASE64, |
|
320 "Displayed image is correct"); |
|
321 |
|
322 function checkImageResponseInfo() { |
|
323 checkNodeContent(networkPanel, "responseImageInfo", "2ms"); |
|
324 checkNodeContent(networkPanel, "responseImageInfo", "16x16px"); |
|
325 } |
|
326 |
|
327 // Check if the image is loaded already. |
|
328 imgNode.addEventListener("load", function onLoad() { |
|
329 imgNode.removeEventListener("load", onLoad, false); |
|
330 checkImageResponseInfo(); |
|
331 networkPanel.panel.hidePopup(); |
|
332 testDriver.next(); |
|
333 }, false); |
|
334 yield undefined; |
|
335 |
|
336 // Check cached image request. |
|
337 info("test 8: cached image request"); |
|
338 httpActivity.response.httpVersion = "HTTP/1.1"; |
|
339 httpActivity.response.status = 304; |
|
340 httpActivity.response.statusText = "Not Modified"; |
|
341 |
|
342 networkPanel = hud.ui.openNetworkPanel(filterBox, httpActivity); |
|
343 networkPanel._onUpdate = function() { |
|
344 networkPanel._onUpdate = null; |
|
345 executeSoon(function() { |
|
346 testDriver.next(); |
|
347 }); |
|
348 }; |
|
349 |
|
350 yield undefined; |
|
351 |
|
352 checkIsVisible(networkPanel, { |
|
353 requestBody: true, |
|
354 requestFormData: false, |
|
355 requestCookie: true, |
|
356 responseContainer: true, |
|
357 responseBody: false, |
|
358 responseNoBody: false, |
|
359 responseImage: false, |
|
360 responseImageCached: true |
|
361 }); |
|
362 |
|
363 let imgNode = networkPanel.document.getElementById("responseImageCachedNode"); |
|
364 is(imgNode.getAttribute("src"), "data:image/png;base64," + TEST_IMG_BASE64, |
|
365 "Displayed image is correct"); |
|
366 |
|
367 networkPanel.panel.hidePopup(); |
|
368 |
|
369 // Test sent form data. |
|
370 info("test 9: sent form data"); |
|
371 httpActivity.request.postData.text = [ |
|
372 "Content-Type: application/x-www-form-urlencoded", |
|
373 "Content-Length: 59", |
|
374 "name=rob&age=20" |
|
375 ].join("\n"); |
|
376 |
|
377 networkPanel = hud.ui.openNetworkPanel(filterBox, httpActivity); |
|
378 networkPanel._onUpdate = function() { |
|
379 networkPanel._onUpdate = null; |
|
380 executeSoon(function() { |
|
381 testDriver.next(); |
|
382 }); |
|
383 }; |
|
384 |
|
385 yield undefined; |
|
386 |
|
387 checkIsVisible(networkPanel, { |
|
388 requestBody: false, |
|
389 requestFormData: true, |
|
390 requestCookie: true, |
|
391 responseContainer: true, |
|
392 responseBody: false, |
|
393 responseNoBody: false, |
|
394 responseImage: false, |
|
395 responseImageCached: true |
|
396 }); |
|
397 |
|
398 checkNodeKeyValue(networkPanel, "requestFormDataContent", "name", "rob"); |
|
399 checkNodeKeyValue(networkPanel, "requestFormDataContent", "age", "20"); |
|
400 networkPanel.panel.hidePopup(); |
|
401 |
|
402 // Test no space after Content-Type: |
|
403 info("test 10: no space after Content-Type header in post data"); |
|
404 httpActivity.request.postData.text = "Content-Type:application/x-www-form-urlencoded\n"; |
|
405 |
|
406 networkPanel = hud.ui.openNetworkPanel(filterBox, httpActivity); |
|
407 networkPanel._onUpdate = function() { |
|
408 networkPanel._onUpdate = null; |
|
409 executeSoon(function() { |
|
410 testDriver.next(); |
|
411 }); |
|
412 }; |
|
413 |
|
414 yield undefined; |
|
415 |
|
416 checkIsVisible(networkPanel, { |
|
417 requestBody: false, |
|
418 requestFormData: true, |
|
419 requestCookie: true, |
|
420 responseContainer: true, |
|
421 responseBody: false, |
|
422 responseNoBody: false, |
|
423 responseImage: false, |
|
424 responseImageCached: true |
|
425 }); |
|
426 |
|
427 networkPanel.panel.hidePopup(); |
|
428 |
|
429 // Test cached data. |
|
430 |
|
431 info("test 11: cached data"); |
|
432 |
|
433 httpActivity.request.url = TEST_ENCODING_ISO_8859_1; |
|
434 httpActivity.response.headers[1].value = "application/json"; |
|
435 httpActivity.response.content.mimeType = "application/json"; |
|
436 httpActivity.response.content.text = "my cached data is here!"; |
|
437 |
|
438 networkPanel = hud.ui.openNetworkPanel(filterBox, httpActivity); |
|
439 networkPanel._onUpdate = function() { |
|
440 networkPanel._onUpdate = null; |
|
441 executeSoon(function() { |
|
442 testDriver.next(); |
|
443 }); |
|
444 }; |
|
445 |
|
446 yield undefined; |
|
447 |
|
448 checkIsVisible(networkPanel, { |
|
449 requestBody: false, |
|
450 requestFormData: true, |
|
451 requestCookie: true, |
|
452 responseContainer: true, |
|
453 responseBody: false, |
|
454 responseBodyCached: true, |
|
455 responseNoBody: false, |
|
456 responseImage: false, |
|
457 responseImageCached: false |
|
458 }); |
|
459 |
|
460 checkNodeContent(networkPanel, "responseBodyCachedContent", |
|
461 "my cached data is here!"); |
|
462 |
|
463 networkPanel.panel.hidePopup(); |
|
464 |
|
465 // Test a response with a content type that can't be displayed in the |
|
466 // NetworkPanel. |
|
467 info("test 12: unknown content type"); |
|
468 httpActivity.response.headers[1].value = "application/x-shockwave-flash"; |
|
469 httpActivity.response.content.mimeType = "application/x-shockwave-flash"; |
|
470 |
|
471 networkPanel = hud.ui.openNetworkPanel(filterBox, httpActivity); |
|
472 networkPanel._onUpdate = function() { |
|
473 networkPanel._onUpdate = null; |
|
474 executeSoon(function() { |
|
475 testDriver.next(); |
|
476 }); |
|
477 }; |
|
478 |
|
479 yield undefined; |
|
480 |
|
481 checkIsVisible(networkPanel, { |
|
482 requestBody: false, |
|
483 requestFormData: true, |
|
484 requestCookie: true, |
|
485 responseContainer: true, |
|
486 responseBody: false, |
|
487 responseBodyCached: false, |
|
488 responseBodyUnknownType: true, |
|
489 responseNoBody: false, |
|
490 responseImage: false, |
|
491 responseImageCached: false |
|
492 }); |
|
493 |
|
494 let responseString = |
|
495 WCU_l10n.getFormatStr("NetworkPanel.responseBodyUnableToDisplay.content", |
|
496 ["application/x-shockwave-flash"]); |
|
497 checkNodeContent(networkPanel, "responseBodyUnknownTypeContent", responseString); |
|
498 networkPanel.panel.hidePopup(); |
|
499 |
|
500 /* |
|
501 |
|
502 // This test disabled. See bug 603620. |
|
503 |
|
504 // Test if the NetworkPanel figures out the content type based on an URL as |
|
505 // well. |
|
506 delete httpActivity.response.header["Content-Type"]; |
|
507 httpActivity.url = "http://www.test.com/someCrazyFile.swf?done=right&ending=txt"; |
|
508 |
|
509 networkPanel = hud.ui.openNetworkPanel(filterBox, httpActivity); |
|
510 networkPanel.isDoneCallback = function NP_doneCallback() { |
|
511 networkPanel.isDoneCallback = null; |
|
512 testDriver.next(); |
|
513 } |
|
514 |
|
515 yield undefined; |
|
516 |
|
517 checkIsVisible(networkPanel, { |
|
518 requestBody: false, |
|
519 requestFormData: true, |
|
520 requestCookie: true, |
|
521 responseContainer: true, |
|
522 responseBody: false, |
|
523 responseBodyCached: false, |
|
524 responseBodyUnknownType: true, |
|
525 responseNoBody: false, |
|
526 responseImage: false, |
|
527 responseImageCached: false |
|
528 }); |
|
529 |
|
530 // Systems without Flash installed will return an empty string here. Ignore. |
|
531 if (networkPanel.document.getElementById("responseBodyUnknownTypeContent").textContent !== "") |
|
532 checkNodeContent(networkPanel, "responseBodyUnknownTypeContent", responseString); |
|
533 else |
|
534 ok(true, "Flash not installed"); |
|
535 |
|
536 networkPanel.panel.hidePopup(); */ |
|
537 |
|
538 // All done! |
|
539 testDriver = null; |
|
540 executeSoon(finishTest); |
|
541 |
|
542 yield undefined; |
|
543 } |