Do not announce robot account token before account ID is available
[chromium-blink-merge.git] / chrome / test / data / extensions / api_test / webrequest / test_auth_required.js
blob500eaab4d599f7339c5e88f4e430f011f262c6ce
1 // Copyright (c) 2011 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.
5 // Generates a unique authentication URL so each test can run
6 // without hitting the HTTP authentication cache. Each test
7 // must use a unique realm, however.
8 function getURLAuthRequired(realm) {
9   return getServerURL('auth-basic/' + realm + '/subpath?realm=' + realm);
12 runTests([
13   // onAuthRequired is not a blocking function in this variant.
14   function authRequiredNonBlocking() {
15     var realm = 'nonblock';
16     var url = getURLAuthRequired(realm);
17     expect(
18       [  // events
19         { label: "onBeforeRequest",
20           event: "onBeforeRequest",
21           details: {
22             url: url,
23             frameUrl: url
24           }
25         },
26         { label: "onBeforeSendHeaders",
27           event: "onBeforeSendHeaders",
28           details: {
29             url: url,
30             // Note: no requestHeaders because we don't ask for them.
31           },
32         },
33         { label: "onSendHeaders",
34           event: "onSendHeaders",
35           details: {
36             url: url,
37           }
38         },
39         { label: "onHeadersReceived",
40           event: "onHeadersReceived",
41           details: {
42             url: url,
43             responseHeadersExist: true,
44             statusLine: "HTTP/1.1 401 Unauthorized"
45           }
46         },
47         { label: "onAuthRequired",
48           event: "onAuthRequired",
49           details: {
50             url: url,
51             isProxy: false,
52             scheme: "basic",
53             realm: realm,
54             challenger: {host: testServer, port: testServerPort},
55             responseHeadersExist: true,
56             statusLine: "HTTP/1.1 401 Unauthorized",
57           }
58         },
59         { label: "onResponseStarted",
60           event: "onResponseStarted",
61           details: {
62             url: url,
63             fromCache: false,
64             statusCode: 401,
65             ip: "127.0.0.1",
66             responseHeadersExist: true,
67             statusLine: "HTTP/1.1 401 Unauthorized",
68           }
69         },
70         { label: "onCompleted",
71           event: "onCompleted",
72           details: {
73             url: url,
74             fromCache: false,
75             statusCode: 401,
76             ip: "127.0.0.1",
77             responseHeadersExist: true,
78             statusLine: "HTTP/1.1 401 Unauthorized",
79           }
80         },
81       ],
82       [  // event order
83         ["onBeforeRequest", "onBeforeSendHeaders", "onSendHeaders",
84          "onHeadersReceived", "onAuthRequired", "onResponseStarted",
85          "onCompleted"]
86       ],
87       {urls: ["<all_urls>"]}, ["responseHeaders"]);
88     navigateAndWait(url);
89   },
91   // onAuthRequired is a blocking function but takes no action in this variant.
92   function authRequiredSyncNoAction() {
93     var realm = 'syncnoaction';
94     var url = getURLAuthRequired(realm);
95     expect(
96       [  // events
97         { label: "onBeforeRequest",
98           event: "onBeforeRequest",
99           details: {
100             url: url,
101             frameUrl: url
102           }
103         },
104         { label: "onBeforeSendHeaders",
105           event: "onBeforeSendHeaders",
106           details: {
107             url: url,
108             // Note: no requestHeaders because we don't ask for them.
109           },
110         },
111         { label: "onSendHeaders",
112           event: "onSendHeaders",
113           details: {
114             url: url,
115           }
116         },
117         { label: "onHeadersReceived",
118           event: "onHeadersReceived",
119           details: {
120             url: url,
121             responseHeadersExist: true,
122             statusLine: "HTTP/1.1 401 Unauthorized"
123           }
124         },
125         { label: "onAuthRequired",
126           event: "onAuthRequired",
127           details: {
128             url: url,
129             isProxy: false,
130             scheme: "basic",
131             realm: realm,
132             challenger: {host: testServer, port: testServerPort},
133             responseHeadersExist: true,
134             statusLine: "HTTP/1.1 401 Unauthorized",
135           }
136         },
137         { label: "onResponseStarted",
138           event: "onResponseStarted",
139           details: {
140             url: url,
141             fromCache: false,
142             statusCode: 401,
143             ip: "127.0.0.1",
144             responseHeadersExist: true,
145             statusLine: "HTTP/1.1 401 Unauthorized",
146           }
147         },
148         { label: "onCompleted",
149           event: "onCompleted",
150           details: {
151             url: url,
152             fromCache: false,
153             statusCode: 401,
154             ip: "127.0.0.1",
155             responseHeadersExist: true,
156             statusLine: "HTTP/1.1 401 Unauthorized",
157           }
158         },
159       ],
160       [  // event order
161         ["onBeforeRequest", "onBeforeSendHeaders", "onSendHeaders",
162          "onHeadersReceived", "onAuthRequired", "onResponseStarted",
163          "onCompleted"]
164       ],
165       {urls: ["<all_urls>"]}, ["blocking", "responseHeaders"]);
166     navigateAndWait(url);
167   },
169   // onAuthRequired is a blocking function that cancels the auth attempt.
170   function authRequiredSyncCancelAuth() {
171     var realm = 'synccancel';
172     var url = getURLAuthRequired(realm);
173     expect(
174       [  // events
175         { label: "onBeforeRequest",
176           event: "onBeforeRequest",
177           details: {
178             url: url,
179             frameUrl: url
180           },
181           retval: {}
182         },
183         { label: "onBeforeSendHeaders",
184           event: "onBeforeSendHeaders",
185           details: {
186             url: url,
187             // Note: no requestHeaders because we don't ask for them.
188           },
189           retval: {}
190         },
191         { label: "onSendHeaders",
192           event: "onSendHeaders",
193           details: {
194             url: url,
195           }
196         },
197         { label: "onHeadersReceived",
198           event: "onHeadersReceived",
199           details: {
200             url: url,
201             responseHeadersExist: true,
202             statusLine: "HTTP/1.1 401 Unauthorized"
203           }
204         },
205         { label: "onAuthRequired",
206           event: "onAuthRequired",
207           details: {
208             url: url,
209             isProxy: false,
210             scheme: "basic",
211             realm: realm,
212             challenger: {host: testServer, port: testServerPort},
213             responseHeadersExist: true,
214             statusLine: "HTTP/1.1 401 Unauthorized",
215           },
216           retval: {cancel: true}
217         },
218         { label: "onResponseStarted",
219           event: "onResponseStarted",
220           details: {
221             url: url,
222             fromCache: false,
223             statusCode: 401,
224             ip: "127.0.0.1",
225             responseHeadersExist: true,
226             statusLine: "HTTP/1.1 401 Unauthorized",
227           }
228         },
229         { label: "onCompleted",
230           event: "onCompleted",
231           details: {
232             url: url,
233             fromCache: false,
234             statusCode: 401,
235             ip: "127.0.0.1",
236             responseHeadersExist: true,
237             statusLine: "HTTP/1.1 401 Unauthorized",
238           }
239         },
240       ],
241       [  // event order
242         ["onBeforeRequest", "onBeforeSendHeaders", "onSendHeaders",
243          "onHeadersReceived", "onAuthRequired", "onResponseStarted",
244          "onCompleted"]
245       ],
246       {urls: ["<all_urls>"]},
247       ["responseHeaders", "blocking"]);
248     navigateAndWait(url);
249   },
251   // onAuthRequired is a blocking function that sets authentication credentials.
252   function authRequiredSyncSetAuth() {
253     var realm = 'syncsetauth';
254     var url = getURLAuthRequired(realm);
255     expect(
256       [  // events
257         { label: "onBeforeRequest",
258           event: "onBeforeRequest",
259           details: {
260             url: url,
261             frameUrl: url
262           },
263           retval: {}
264         },
265         { label: "onBeforeSendHeaders",
266           event: "onBeforeSendHeaders",
267           details: {
268             url: url,
269             // Note: no requestHeaders because we don't ask for them.
270           },
271           retval: {}
272         },
273         { label: "onSendHeaders",
274           event: "onSendHeaders",
275           details: {
276             url: url,
277           }
278         },
279         { label: "onHeadersReceived",
280           event: "onHeadersReceived",
281           details: {
282             url: url,
283             responseHeadersExist: true,
284             statusLine: "HTTP/1.1 401 Unauthorized"
285           }
286         },
287         { label: "onAuthRequired",
288           event: "onAuthRequired",
289           details: {
290             url: url,
291             isProxy: false,
292             scheme: "basic",
293             realm: realm,
294             challenger: {host: testServer, port: testServerPort},
295             responseHeadersExist: true,
296             statusLine: "HTTP/1.1 401 Unauthorized",
297           },
298           retval: {authCredentials: {username: "foo", password: "secret"}}
299         },
300         { label: "onResponseStarted",
301           event: "onResponseStarted",
302           details: {
303             url: url,
304             fromCache: false,
305             statusCode: 200,
306             ip: "127.0.0.1",
307             responseHeadersExist: true,
308             statusLine: "HTTP/1.1 200 OK",
309           }
310         },
311         { label: "onCompleted",
312           event: "onCompleted",
313           details: {
314             url: url,
315             fromCache: false,
316             statusCode: 200,
317             ip: "127.0.0.1",
318             responseHeadersExist: true,
319             statusLine: "HTTP/1.1 200 OK",
320           }
321         },
322       ],
323       [  // event order
324         ["onBeforeRequest", "onBeforeSendHeaders", "onSendHeaders",
325          "onHeadersReceived", "onAuthRequired", "onResponseStarted",
326          "onCompleted"]
327       ],
328       {urls: ["<all_urls>"]},
329       ["responseHeaders", "blocking"]);
330     navigateAndWait(url);
331   },
333   // onAuthRequired is an async function but takes no action in this variant.
334   function authRequiredAsyncNoAction() {
335     var realm = 'asyncnoaction';
336     var url = getURLAuthRequired(realm);
337     expect(
338       [  // events
339         { label: "onBeforeRequest",
340           event: "onBeforeRequest",
341           details: {
342             url: url,
343             frameUrl: url
344           }
345         },
346         { label: "onBeforeSendHeaders",
347           event: "onBeforeSendHeaders",
348           details: {
349             url: url,
350             // Note: no requestHeaders because we don't ask for them.
351           },
352         },
353         { label: "onSendHeaders",
354           event: "onSendHeaders",
355           details: {
356             url: url,
357           }
358         },
359         { label: "onHeadersReceived",
360           event: "onHeadersReceived",
361           details: {
362             url: url,
363             responseHeadersExist: true,
364             statusLine: "HTTP/1.1 401 Unauthorized"
365           }
366         },
367         { label: "onAuthRequired",
368           event: "onAuthRequired",
369           details: {
370             url: url,
371             isProxy: false,
372             scheme: "basic",
373             realm: realm,
374             challenger: {host: testServer, port: testServerPort},
375             responseHeadersExist: true,
376             statusLine: "HTTP/1.1 401 Unauthorized",
377           }
378         },
379         { label: "onResponseStarted",
380           event: "onResponseStarted",
381           details: {
382             url: url,
383             fromCache: false,
384             statusCode: 401,
385             ip: "127.0.0.1",
386             responseHeadersExist: true,
387             statusLine: "HTTP/1.1 401 Unauthorized",
388           }
389         },
390         { label: "onCompleted",
391           event: "onCompleted",
392           details: {
393             url: url,
394             fromCache: false,
395             statusCode: 401,
396             ip: "127.0.0.1",
397             responseHeadersExist: true,
398             statusLine: "HTTP/1.1 401 Unauthorized",
399           }
400         },
401       ],
402       [  // event order
403         ["onBeforeRequest", "onBeforeSendHeaders", "onSendHeaders",
404          "onHeadersReceived", "onAuthRequired", "onResponseStarted",
405          "onCompleted"]
406       ],
407       {urls: ["<all_urls>"]}, ["asyncBlocking", "responseHeaders"]);
408     navigateAndWait(url);
409   },
411   // onAuthRequired is an async function that cancels the auth attempt.
412   function authRequiredAsyncCancelAuth() {
413     var realm = 'asynccancel';
414     var url = getURLAuthRequired(realm);
415     expect(
416       [  // events
417         { label: "onBeforeRequest",
418           event: "onBeforeRequest",
419           details: {
420             url: url,
421             frameUrl: url
422           },
423           retval: {}
424         },
425         { label: "onBeforeSendHeaders",
426           event: "onBeforeSendHeaders",
427           details: {
428             url: url,
429             // Note: no requestHeaders because we don't ask for them.
430           },
431           retval: {}
432         },
433         { label: "onSendHeaders",
434           event: "onSendHeaders",
435           details: {
436             url: url,
437           }
438         },
439         { label: "onHeadersReceived",
440           event: "onHeadersReceived",
441           details: {
442             url: url,
443             responseHeadersExist: true,
444             statusLine: "HTTP/1.1 401 Unauthorized"
445           }
446         },
447         { label: "onAuthRequired",
448           event: "onAuthRequired",
449           details: {
450             url: url,
451             isProxy: false,
452             scheme: "basic",
453             realm: realm,
454             challenger: {host: testServer, port: testServerPort},
455             responseHeadersExist: true,
456             statusLine: "HTTP/1.1 401 Unauthorized",
457           },
458           retval: {cancel: true}
459         },
460         { label: "onResponseStarted",
461           event: "onResponseStarted",
462           details: {
463             url: url,
464             fromCache: false,
465             statusCode: 401,
466             ip: "127.0.0.1",
467             responseHeadersExist: true,
468             statusLine: "HTTP/1.1 401 Unauthorized",
469           }
470         },
471         { label: "onCompleted",
472           event: "onCompleted",
473           details: {
474             url: url,
475             fromCache: false,
476             statusCode: 401,
477             ip: "127.0.0.1",
478             responseHeadersExist: true,
479             statusLine: "HTTP/1.1 401 Unauthorized",
480           }
481         },
482       ],
483       [  // event order
484         ["onBeforeRequest", "onBeforeSendHeaders", "onSendHeaders",
485          "onHeadersReceived", "onAuthRequired", "onResponseStarted",
486          "onCompleted"]
487       ],
488       {urls: ["<all_urls>"]},
489       ["responseHeaders", "asyncBlocking"]);
490     navigateAndWait(url);
491   },
493   // onAuthRequired is an async function that sets authentication credentials.
494   function authRequiredAsyncSetAuth() {
495     var realm = 'asyncsetauth';
496     var url = getURLAuthRequired(realm);
497     expect(
498       [  // events
499         { label: "onBeforeRequest",
500           event: "onBeforeRequest",
501           details: {
502             url: url,
503             frameUrl: url
504           },
505           retval: {}
506         },
507         { label: "onBeforeSendHeaders",
508           event: "onBeforeSendHeaders",
509           details: {
510             url: url,
511             // Note: no requestHeaders because we don't ask for them.
512           },
513           retval: {}
514         },
515         { label: "onSendHeaders",
516           event: "onSendHeaders",
517           details: {
518             url: url,
519           }
520         },
521         { label: "onHeadersReceived",
522           event: "onHeadersReceived",
523           details: {
524             url: url,
525             responseHeadersExist: true,
526             statusLine: "HTTP/1.1 401 Unauthorized"
527           }
528         },
529         { label: "onAuthRequired",
530           event: "onAuthRequired",
531           details: {
532             url: url,
533             isProxy: false,
534             scheme: "basic",
535             realm: realm,
536             challenger: {host: testServer, port: testServerPort},
537             responseHeadersExist: true,
538             statusLine: "HTTP/1.1 401 Unauthorized",
539           },
540           retval: {authCredentials: {username: "foo", password: "secret"}}
541         },
542         { label: "onResponseStarted",
543           event: "onResponseStarted",
544           details: {
545             url: url,
546             fromCache: false,
547             statusCode: 200,
548             ip: "127.0.0.1",
549             responseHeadersExist: true,
550             statusLine: "HTTP/1.1 200 OK",
551           }
552         },
553         { label: "onCompleted",
554           event: "onCompleted",
555           details: {
556             url: url,
557             fromCache: false,
558             statusCode: 200,
559             ip: "127.0.0.1",
560             responseHeadersExist: true,
561             statusLine: "HTTP/1.1 200 OK",
562           }
563         },
564       ],
565       [  // event order
566         ["onBeforeRequest", "onBeforeSendHeaders", "onSendHeaders",
567          "onHeadersReceived", "onAuthRequired", "onResponseStarted",
568          "onCompleted"]
569       ],
570       {urls: ["<all_urls>"]},
571       ["responseHeaders", "asyncBlocking"]);
572     navigateAndWait(url);
573   },