@@ -8,6 +8,19 @@ const host = 'http://127.0.0.1:8080';
88const server = `${ host } /test/fixtures` ;
99const mainSuite = suite ( 'quicklink tests' ) ;
1010
11+ const isExternalURL = url => {
12+ try {
13+ const parsed = new URL ( url ) ;
14+
15+ // "null" origins cover about:blank, data:, etc.
16+ if ( parsed . origin === 'null' ) return false ;
17+
18+ return parsed . origin !== host ;
19+ } catch ( _ ) {
20+ return false ;
21+ }
22+ } ;
23+
1124// Default 1000 ms
1225const sleep = ( ms = 1000 ) => new Promise ( resolve => {
1326 setTimeout ( resolve , ms ) ;
@@ -22,6 +35,28 @@ const puppeteerOptions = {
2235mainSuite . before ( async context => {
2336 context . browser = await puppeteer . launch ( puppeteerOptions ) ;
2437 context . page = await context . browser . newPage ( ) ;
38+
39+ // Mock external network calls so tests do not reach the internet.
40+ context . handleRequest = null ;
41+
42+ await context . page . setRequestInterception ( true ) ;
43+ context . page . on ( 'request' , req => {
44+ const url = req . url ( ) ;
45+
46+ if ( isExternalURL ( url ) ) {
47+ return req . respond ( {
48+ status : 200 ,
49+ contentType : 'text/plain' ,
50+ body : 'mocked external response' ,
51+ } ) ;
52+ }
53+
54+ if ( typeof context . handleRequest === 'function' ) {
55+ return context . handleRequest ( req ) ;
56+ }
57+
58+ return req . continue ( ) ;
59+ } ) ;
2560} ) ;
2661
2762mainSuite . after ( async context => {
@@ -107,10 +142,10 @@ mainSuite('should only prefetch links if allowed in origins list', async context
107142 responseURLs . push ( resp . url ( ) ) ;
108143 } ) ;
109144 await context . page . goto ( `${ server } /test-allow-origin.html` ) ;
110- await sleep ( 1000 ) ;
145+ await sleep ( ) ;
111146 assert . instance ( responseURLs , Array ) ;
112147
113- // => origins: ['github.githubassets.com']
148+ // => origins: ['github.githubassets.com', 'example.com' ]
114149 assert . not . ok ( responseURLs . includes ( `${ server } /2.html` ) ) ;
115150 assert . ok ( responseURLs . includes ( 'https://example.com/1.html' ) ) ;
116151 assert . ok ( responseURLs . includes ( 'https://github.githubassets.com/images/spinners/octocat-spinner-32.gif' ) ) ;
@@ -262,20 +297,17 @@ mainSuite('should not exceed the `limit` total', async context => {
262297mainSuite ( 'should respect the `throttle` concurrency' , async context => {
263298 const URLs = [ ] ; // Note: Page makes 4 requests
264299
265- // Make HTML requests take a long time
266- // ~> so that we can ensure throttling occurs
267- await context . page . setRequestInterception ( true ) ;
268-
269- context . page . on ( 'request' , async req => {
300+ // Make HTML requests take a long time so that we can ensure throttling occurs
301+ context . handleRequest = async req => {
270302 const url = req . url ( ) ;
271303 if ( / t e s t \/ f i x t u r e s \/ \d + \. h t m l $ / i. test ( url ) ) {
272304 await sleep ( 100 ) ;
273305 URLs . push ( url ) ;
274306 return req . respond ( { status : 200 } ) ;
275307 }
276308
277- req . continue ( ) ;
278- } ) ;
309+ return req . continue ( ) ;
310+ } ;
279311
280312 await context . page . goto ( `${ server } /test-throttle.html` ) ;
281313
@@ -288,6 +320,8 @@ mainSuite('should respect the `throttle` concurrency', async context => {
288320 // Note: Parallel requests, w/ 50ms buffer
289321 await sleep ( 250 ) ;
290322 assert . is ( URLs . length , 4 ) ;
323+
324+ context . handleRequest = null ;
291325} ) ;
292326
293327mainSuite ( 'should prefetch using a custom function to build the URL' , async context => {
0 commit comments