- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / api_test / webrequest / test_declarative1.js
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 var onRequest = chrome.declarativeWebRequest.onRequest;
6 var AddResponseHeader =
7     chrome.declarativeWebRequest.AddResponseHeader;
8 var RequestMatcher = chrome.declarativeWebRequest.RequestMatcher;
9 var CancelRequest = chrome.declarativeWebRequest.CancelRequest;
10 var RedirectByRegEx = chrome.declarativeWebRequest.RedirectByRegEx;
11 var RedirectRequest = chrome.declarativeWebRequest.RedirectRequest;
12 var RedirectToTransparentImage =
13     chrome.declarativeWebRequest.RedirectToTransparentImage;
14 var RedirectToEmptyDocument =
15     chrome.declarativeWebRequest.RedirectToEmptyDocument;
16 var SetRequestHeader =
17     chrome.declarativeWebRequest.SetRequestHeader;
18 var RemoveRequestHeader =
19     chrome.declarativeWebRequest.RemoveRequestHeader;
20 var RemoveResponseHeader =
21     chrome.declarativeWebRequest.RemoveResponseHeader;
22 var IgnoreRules =
23     chrome.declarativeWebRequest.IgnoreRules;
24 var AddRequestCookie = chrome.declarativeWebRequest.AddRequestCookie;
25 var AddResponseCookie = chrome.declarativeWebRequest.AddResponseCookie;
26 var EditRequestCookie = chrome.declarativeWebRequest.EditRequestCookie;
27 var EditResponseCookie = chrome.declarativeWebRequest.EditResponseCookie;
28 var RemoveRequestCookie = chrome.declarativeWebRequest.RemoveRequestCookie;
29 var RemoveResponseCookie = chrome.declarativeWebRequest.RemoveResponseCookie;
30
31 // Constants as functions, not to be called until after runTests.
32 function getURLHttpSimple() {
33   return getServerURL("extensions/api_test/webrequest/simpleLoad/a.html");
34 }
35
36 function getURLHttpSimpleB() {
37   return getServerURL("extensions/api_test/webrequest/simpleLoad/b.html");
38 }
39
40 function getURLHttpComplex() {
41   return getServerURL(
42       "extensions/api_test/webrequest/complexLoad/a.html");
43 }
44
45 function getURLHttpRedirectTest() {
46   return getServerURL(
47       "extensions/api_test/webrequest/declarative/a.html");
48 }
49
50 function getURLHttpWithHeaders() {
51   return getServerURL(
52       "extensions/api_test/webrequest/declarative/headers.html");
53 }
54
55 function getURLOfHTMLWithThirdParty() {
56   // Returns the URL of a HTML document with a third-party resource.
57   return getServerURL(
58       "extensions/api_test/webrequest/declarative/third-party.html");
59 }
60
61 // Shared test sections.
62 function cancelThirdPartyExpected() {
63     return [
64       { label: "onBeforeRequest",
65         event: "onBeforeRequest",
66         details: {
67           url: getURLOfHTMLWithThirdParty(),
68           frameUrl: getURLOfHTMLWithThirdParty()
69         }
70       },
71       { label: "onBeforeSendHeaders",
72         event: "onBeforeSendHeaders",
73         details: {url: getURLOfHTMLWithThirdParty()}
74       },
75       { label: "onSendHeaders",
76         event: "onSendHeaders",
77         details: {url: getURLOfHTMLWithThirdParty()}
78       },
79       { label: "onHeadersReceived",
80         event: "onHeadersReceived",
81         details: {
82           url: getURLOfHTMLWithThirdParty(),
83           statusLine: "HTTP/1.1 200 OK"
84         }
85       },
86       { label: "onResponseStarted",
87         event: "onResponseStarted",
88         details: {
89           url: getURLOfHTMLWithThirdParty(),
90           fromCache: false,
91           ip: "127.0.0.1",
92           statusCode: 200,
93           statusLine: "HTTP/1.1 200 OK"
94         }
95       },
96       { label: "onCompleted",
97         event: "onCompleted",
98         details: {
99           fromCache: false,
100           ip: "127.0.0.1",
101           url: getURLOfHTMLWithThirdParty(),
102           statusCode: 200,
103           statusLine: "HTTP/1.1 200 OK"
104         }
105       },
106       { label: "img-onBeforeRequest",
107         event: "onBeforeRequest",
108         details: {
109           type: "image",
110           url: "http://non_existing_third_party.com/image.png",
111           frameUrl: getURLOfHTMLWithThirdParty()
112         }
113       },
114       { label: "img-onErrorOccurred",
115         event: "onErrorOccurred",
116         details: {
117           error: "net::ERR_BLOCKED_BY_CLIENT",
118           fromCache: false,
119           type: "image",
120           url: "http://non_existing_third_party.com/image.png"
121         }
122       },
123     ];
124 }
125
126 function cancelThirdPartyExpectedOrder() {
127     return [
128       ["onBeforeRequest", "onBeforeSendHeaders", "onSendHeaders",
129        "onHeadersReceived", "onResponseStarted", "onCompleted"],
130       ["img-onBeforeRequest", "img-onErrorOccurred"]
131     ];
132 }
133
134 runTests([
135
136   function testCancelRequest() {
137     ignoreUnexpected = true;
138     expect(
139       [
140         { label: "onErrorOccurred",
141           event: "onErrorOccurred",
142           details: {
143             url: getURLHttpWithHeaders(),
144             fromCache: false,
145             error: "net::ERR_BLOCKED_BY_CLIENT"
146           }
147         },
148       ],
149       [ ["onErrorOccurred"] ]);
150     onRequest.addRules(
151       [ {'conditions': [
152            new RequestMatcher({
153              'url': {
154                  'pathSuffix': ".html",
155                  'ports': [testServerPort, [1000, 2000]],
156                  'schemes': ["http"]
157              },
158              'resourceType': ["main_frame"],
159              'contentType': ["text/plain"],
160              'excludeContentType': ["image/png"],
161              'responseHeaders': [{ nameContains: ["content", "type"] }],
162              'excludeResponseHeaders': [{ valueContains: "nonsense" }],
163              'stages': ["onHeadersReceived", "onAuthRequired"] })],
164          'actions': [new CancelRequest()]}
165       ],
166       function() {navigateAndWait(getURLHttpWithHeaders());}
167     );
168   },
169
170   // Postpone cancelling of the request until onHeadersReceived by using
171   // 'stages'. If not for the stages, the request would be already cancelled
172   // during onBeforeRequest.
173   function testPostponeCancelRequest() {
174     ignoreUnexpected = false;
175     expect(
176       [
177         { label: "onBeforeRequest",
178           event: "onBeforeRequest",
179           details: {
180             url: getURLHttpWithHeaders(),
181             frameUrl: getURLHttpWithHeaders()
182           }
183         },
184         { label: "onBeforeSendHeaders",
185           event: "onBeforeSendHeaders",
186           details: {
187             url: getURLHttpWithHeaders(),
188           }
189         },
190         { label: "onSendHeaders",
191           event: "onSendHeaders",
192           details: {
193             url: getURLHttpWithHeaders(),
194           }
195         },
196         { label: "onHeadersReceived",
197           event: "onHeadersReceived",
198           details: {
199             statusLine: "HTTP/1.1 200 OK",
200             url: getURLHttpWithHeaders(),
201           }
202         },
203         { label: "onErrorOccurred",
204           event: "onErrorOccurred",
205           details: {
206             url: getURLHttpWithHeaders(),
207             fromCache: false,
208             error: "net::ERR_BLOCKED_BY_CLIENT"
209           }
210         },
211       ],
212       [ ["onBeforeRequest", "onBeforeSendHeaders", "onSendHeaders",
213          "onHeadersReceived", "onErrorOccurred"] ]);
214     onRequest.addRules(
215       [ {'conditions': [
216            new RequestMatcher({ 'stages': ["onHeadersReceived"] })],
217          'actions': [new CancelRequest()]}
218       ],
219       function() {navigateAndWait(getURLHttpWithHeaders());}
220     );
221   },
222   // Tests that "thirdPartyForCookies: true" matches third party requests.
223   function testThirdParty() {
224     ignoreUnexpected = false;
225     expect(cancelThirdPartyExpected(), cancelThirdPartyExpectedOrder());
226     onRequest.addRules(
227       [ {'conditions': [new RequestMatcher({thirdPartyForCookies: true})],
228          'actions': [new chrome.declarativeWebRequest.CancelRequest()]},],
229       function() {navigateAndWait(getURLOfHTMLWithThirdParty());}
230     );
231   },
232
233   // Tests that "thirdPartyForCookies: false" matches first party requests,
234   // by cancelling all requests, and overriding the cancelling rule only for
235   // requests matching "thirdPartyForCookies: false".
236   function testFirstParty() {
237     ignoreUnexpected = false;
238     expect(cancelThirdPartyExpected(), cancelThirdPartyExpectedOrder());
239     onRequest.addRules(
240       [ {'priority': 2,
241          'conditions': [
242            new RequestMatcher({thirdPartyForCookies: false})
243          ],
244          'actions': [
245            new chrome.declarativeWebRequest.IgnoreRules({
246               lowerPriorityThan: 2 })
247          ]
248         },
249         {'priority': 1,
250          'conditions': [new RequestMatcher({})],
251          'actions': [new chrome.declarativeWebRequest.CancelRequest()]
252         },
253       ],
254       function() {navigateAndWait(getURLOfHTMLWithThirdParty());}
255     );
256   },
257
258   function testFirstPartyForCookiesUrl() {
259     // This is an end-to-end test for firstPartyForCookies. The choice of URL to
260     // navigate to is purely arbitrary.
261     ignoreUnexpected = false;
262     expect(
263       [
264         { label: "onBeforeRequest",
265           event: "onBeforeRequest",
266           details: {
267             url: getURLOfHTMLWithThirdParty(),
268             frameUrl: getURLOfHTMLWithThirdParty()
269           }
270         },
271         { label: "onErrorOccurred",
272           event: "onErrorOccurred",
273           details: {
274             url: getURLOfHTMLWithThirdParty(),
275             fromCache: false,
276             error: "net::ERR_BLOCKED_BY_CLIENT"
277           }
278         },
279       ],
280       [ ["onBeforeRequest", "onErrorOccurred"] ]);
281     onRequest.addRules(
282       [ {'conditions': [
283            new RequestMatcher({
284              firstPartyForCookiesUrl: {
285                hostEquals: testServer
286              }
287            })
288          ],
289          'actions': [new chrome.declarativeWebRequest.CancelRequest()]
290         },
291       ],
292       function() {navigateAndWait(getURLOfHTMLWithThirdParty());}
293     );
294   },
295
296   function testRedirectRequest() {
297     ignoreUnexpected = true;
298     expect(
299       [
300         { label: "onBeforeRequest-a",
301           event: "onBeforeRequest",
302           details: {
303             type: "main_frame",
304             url: getURLHttpComplex(),
305             frameUrl: getURLHttpComplex()
306           },
307         },
308         { label: "onBeforeRedirect",
309           event: "onBeforeRedirect",
310           details: {
311             url: getURLHttpComplex(),
312             redirectUrl: getURLHttpSimple(),
313             statusLine: "",
314             statusCode: -1,
315             fromCache: false,
316           }
317         },
318         { label: "onBeforeRequest-b",
319           event: "onBeforeRequest",
320           details: {
321             type: "main_frame",
322             url: getURLHttpSimple(),
323             frameUrl: getURLHttpSimple(),
324           },
325         },
326         { label: "onCompleted",
327           event: "onCompleted",
328           details: {
329             ip: "127.0.0.1",
330             url: getURLHttpSimple(),
331             fromCache: false,
332             statusCode: 200,
333             statusLine: "HTTP/1.1 200 OK",
334           }
335         },
336       ],
337       [ ["onBeforeRequest-a", "onBeforeRedirect", "onBeforeRequest-b",
338          "onCompleted"] ]);
339
340     onRequest.addRules(
341       [ {'conditions': [new RequestMatcher({'url': {'pathSuffix': ".html"}})],
342          'actions': [
343              new RedirectRequest({'redirectUrl': getURLHttpSimple()})]}
344       ],
345       function() {navigateAndWait(getURLHttpComplex());}
346     );
347   },
348
349   function testRedirectRequest2() {
350     ignoreUnexpected = true;
351     expect(
352       [
353         { label: "onCompleted",
354           event: "onCompleted",
355           details: {
356             ip: "127.0.0.1",
357             url: getURLHttpRedirectTest(),
358             fromCache: false,
359             statusCode: 200,
360             statusLine: "HTTP/1.1 200 OK",
361           }
362         },
363         // We cannot wait for onCompleted signals because these are not sent
364         // for data:// URLs.
365         { label: "onBeforeRedirect-1",
366           event: "onBeforeRedirect",
367           details: {
368             url: getServerURL(
369                 "extensions/api_test/webrequest/declarative/image.png"),
370             redirectUrl: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEA" +
371                 "AAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJ" +
372                 "ggg==",
373             fromCache: false,
374             statusCode: -1,
375             statusLine: "",
376             type: "image",
377           }
378         },
379         { label: "onBeforeRedirect-2",
380           event: "onBeforeRedirect",
381           details: {
382             frameId: 1,
383             parentFrameId: 0,
384             url: getServerURL(
385                 "extensions/api_test/webrequest/declarative/frame.html"),
386             redirectUrl: "data:text/html,",
387             fromCache: false,
388             statusCode: -1,
389             statusLine: "",
390             type: "sub_frame",
391           }
392         },
393       ],
394       [ ["onCompleted"], ["onBeforeRedirect-1"], ["onBeforeRedirect-2"] ]);
395
396     onRequest.addRules(
397       [ {conditions: [
398              new RequestMatcher({url: {pathSuffix: "image.png"}})],
399          actions: [new RedirectToTransparentImage()]},
400         {conditions: [
401              new RequestMatcher({url: {pathSuffix: "frame.html"}})],
402          actions: [new RedirectToEmptyDocument()]},
403       ],
404       function() {navigateAndWait(getURLHttpRedirectTest());}
405     );
406   },
407
408   function testRedirectByRegEx() {
409     ignoreUnexpected = true;
410     expect(
411       [
412         { label: "onCompleted",
413           event: "onCompleted",
414           details: {
415             ip: "127.0.0.1",
416             url: getURLHttpSimpleB(),
417             fromCache: false,
418             statusCode: 200,
419             statusLine: "HTTP/1.1 200 OK",
420           }
421         },
422       ],
423       [ ["onCompleted"] ]);
424
425     onRequest.addRules(
426       [ {conditions: [new RequestMatcher({url: {pathSuffix: ".html"}})],
427          actions: [
428              new RedirectByRegEx({from: "^(.*)/a.html$", to: "$1/b.html"})]}
429       ],
430       function() {navigateAndWait(getURLHttpSimple());}
431     );
432   },
433
434   function testRegexFilter() {
435     ignoreUnexpected = true;
436     expect(
437       [
438         { label: "onErrorOccurred",
439           event: "onErrorOccurred",
440           details: {
441             url: getURLHttpSimple(),
442             fromCache: false,
443             error: "net::ERR_BLOCKED_BY_CLIENT"
444           }
445         },
446       ],
447       [ ["onErrorOccurred"] ]);
448     onRequest.addRules(
449       [ {'conditions': [
450            new RequestMatcher({
451              'url': {
452                  'urlMatches': 'simple[A-Z].*a\.html$',
453                  'schemes': ["http"]
454              },
455            })],
456          'actions': [new CancelRequest()]}
457       ],
458       function() {navigateAndWait(getURLHttpSimple());}
459     );
460   },
461   ]);