1
+ <!DOCTYPE html>
2
+ < title > Ensures that prefetch works with documents</ title >
3
+ < script src ="/resources/testharness.js "> </ script >
4
+ < script src ="/resources/testharnessreport.js "> </ script >
5
+ < script src ="/common/utils.js "> </ script >
6
+ < script src ="/common/get-host-info.sub.js "> </ script >
7
+ < script src ="/common/dispatcher/dispatcher.js "> </ script >
8
+ < script src ="resources/prefetch-helper.js "> </ script >
9
+ < body >
10
+ < script >
11
+
12
+ const { ORIGIN , REMOTE_ORIGIN , HTTP_NOTSAMESITE_ORIGIN } = get_host_info ( ) ;
13
+ const origins = {
14
+ "same origin" : ORIGIN ,
15
+ "same site" : REMOTE_ORIGIN ,
16
+ "different site" : HTTP_NOTSAMESITE_ORIGIN
17
+ }
18
+ const loaders = {
19
+ image : {
20
+ file : 'square.png' ,
21
+ type : 'image/png' ,
22
+ load : href => {
23
+ const image = document . createElement ( 'img' ) ;
24
+ image . src = href ;
25
+ document . body . appendChild ( image ) ;
26
+ return new Promise ( resolve => image . addEventListener ( 'load' , resolve ) ) ;
27
+ }
28
+ } ,
29
+ script : {
30
+ file : 'dummy.js' ,
31
+ type : 'application/javascript' ,
32
+ load : href => {
33
+ const script = document . createElement ( 'script' ) ;
34
+ script . src = href ;
35
+ document . body . appendChild ( script ) ;
36
+ return new Promise ( resolve => script . addEventListener ( 'load' , resolve ) ) ;
37
+ }
38
+ } ,
39
+ style : {
40
+ file : 'dummy.css' ,
41
+ type : 'text/css' ,
42
+ load : href => {
43
+ const link = document . createElement ( 'link' ) ;
44
+ link . href = href ;
45
+ link . rel = "stylesheet" ;
46
+ document . body . appendChild ( link ) ;
47
+ return new Promise ( resolve => link . addEventListener ( 'load' , resolve ) ) ;
48
+ }
49
+ } ,
50
+ document : {
51
+ file : 'empty.html' ,
52
+ type : 'text/html' ,
53
+ load : href => {
54
+ const iframe = document . createElement ( "iframe" ) ;
55
+ iframe . src = href ;
56
+ document . body . appendChild ( iframe ) ;
57
+ return new Promise ( resolve => iframe . addEventListener ( "load" , resolve ) ) ;
58
+ }
59
+ }
60
+ } ;
61
+
62
+ async function prefetch_document ( origin , as , t ) {
63
+ const { href, uid} = await prefetch ( {
64
+ file : "prefetch-exec.html" ,
65
+ type : "text/html" ,
66
+ corssOrigin : "anonymous" ,
67
+ origin : origins [ origin ] , as} ) ;
68
+ const popup = window . open ( href ) ;
69
+ const remoteContext = new RemoteContext ( uid ) ;
70
+ t . add_cleanup ( ( ) => popup . close ( ) ) ;
71
+ const result = await remoteContext . execute_script ( ( ) => "OK" ) ;
72
+ assert_equals ( result , "OK" ) ;
73
+ return await get_prefetch_info ( href ) ;
74
+ }
75
+
76
+ async function test_prefetch_document ( { origin, as} , expected ) {
77
+ promise_test ( async t => {
78
+ const requests = await prefetch_document ( origin , as , t ) ;
79
+ const did_consume = requests . length === 1 ? "consumed" : "not consumed" ;
80
+ assert_equals ( did_consume , expected ) ;
81
+ } , `Prefetching a document (${ origin } , as="${ as } ") should be ${ expected } ` ) ;
82
+ }
83
+
84
+ test_prefetch_document ( { origin : "same origin" } , "consumed" ) ;
85
+ test_prefetch_document ( { origin : "same site" } , "consumed" ) ;
86
+ test_prefetch_document ( { as : "document" , origin : "different site" } , "not consumed" ) ;
87
+
88
+ promise_test ( async t => {
89
+ const { href, uid} = await prefetch ( {
90
+ file : "prefetch-exec.html" ,
91
+ type : "text/html" ,
92
+ corssOrigin : "anonymous" ,
93
+ origin : ORIGIN } ) ;
94
+ const popup = window . open ( href + "&cache_bust=" + token ( ) ) ;
95
+ const remoteContext = new RemoteContext ( uid ) ;
96
+ t . add_cleanup ( ( ) => popup . close ( ) ) ;
97
+ await remoteContext . execute_script ( ( ) => "OK" ) ;
98
+ const results = await get_prefetch_info ( href ) ;
99
+ assert_equals ( results . length , 2 ) ;
100
+ assert_equals ( results [ 0 ] . headers . accept , results [ 1 ] . headers . accept ) ;
101
+ } , "Document prefetch should send the exact Accept header as navigation" )
102
+
103
+ </ script >
104
+ </ body >
0 commit comments