@@ -13,6 +13,8 @@ import {executablePath} from 'puppeteer';
1313
1414import { detectDisplay , ensureBrowserConnected , launch } from '../src/browser.js' ;
1515
16+ import { serverHooks } from './server.js' ;
17+
1618describe ( 'browser' , ( ) => {
1719 it ( 'detects display does not crash' , ( ) => {
1820 detectDisplay ( ) ;
@@ -100,4 +102,88 @@ describe('browser', () => {
100102 await browser . close ( ) ;
101103 }
102104 } ) ;
105+
106+ describe ( 'Blocking' , ( ) => {
107+ const server = serverHooks ( ) ;
108+
109+ it ( 'blocks URLs in blocklist' , async ( ) => {
110+ server . addHtmlRoute ( '/allowed.html' , '<html><body>Allowed</body></html>' ) ;
111+ server . addHtmlRoute ( '/blocked.html' , '<html><body>Blocked</body></html>' ) ;
112+
113+ const browser = await launch ( {
114+ headless : true ,
115+ isolated : true ,
116+ executablePath : executablePath ( ) ,
117+ devtools : false ,
118+ blocklist : [ '*://*:*/blocked.html' ] ,
119+ } ) ;
120+ try {
121+ const page = await browser . newPage ( ) ;
122+
123+ // Access allowed URL
124+ await page . goto ( server . getRoute ( '/allowed.html' ) ) ;
125+ const content = await page . evaluate ( ( ) => document . body . textContent ) ;
126+ assert . strictEqual ( content , 'Allowed' ) ;
127+
128+ // Fetch of blocked URL from the page
129+ const fetchResult = await page . evaluate ( async url => {
130+ try {
131+ await fetch ( url ) ;
132+ return 'SUCCESS' ;
133+ } catch ( err ) {
134+ return err instanceof Error ? err . message : String ( err ) ;
135+ }
136+ } , server . getRoute ( '/blocked.html' ) ) ;
137+
138+ assert . strictEqual ( fetchResult , 'Failed to fetch' ) ;
139+ } finally {
140+ await browser . close ( ) ;
141+ }
142+ } ) ;
143+
144+ it (
145+ 'blocks URLs not in allowlist' ,
146+ { skip : 'Requires Chrome 149 or greater' } ,
147+ async ( ) => {
148+ server . addHtmlRoute (
149+ '/allowed.html' ,
150+ '<html><body>Allowed</body></html>' ,
151+ ) ;
152+ server . addHtmlRoute (
153+ '/blocked.html' ,
154+ '<html><body>Blocked</body></html>' ,
155+ ) ;
156+
157+ const browser = await launch ( {
158+ headless : true ,
159+ isolated : true ,
160+ executablePath : executablePath ( ) ,
161+ devtools : false ,
162+ allowlist : [ '*://*/allowed.html' ] ,
163+ } ) ;
164+ try {
165+ const page = await browser . newPage ( ) ;
166+
167+ // Access allowed URL
168+ await page . goto ( server . getRoute ( '/allowed.html' ) ) ;
169+ const content = await page . evaluate ( ( ) => document . body . textContent ) ;
170+ assert . strictEqual ( content , 'Allowed' ) ;
171+
172+ // Fetch of blocked URL from the page
173+ const fetchResult = await page . evaluate ( async url => {
174+ try {
175+ await fetch ( url ) ;
176+ return 'SUCCESS' ;
177+ } catch ( err ) {
178+ return err instanceof Error ? err . message : String ( err ) ;
179+ }
180+ } , server . getRoute ( '/blocked.html' ) ) ;
181+
182+ assert . strictEqual ( fetchResult , 'Failed to fetch' ) ;
183+ } finally {
184+ await browser . close ( ) ;
185+ }
186+ } ,
187+ ) ;
188+ } ) ;
103189} ) ;
0 commit comments