11import * as assert from 'node:assert/strict' ;
22import { describe , it } from 'node:test' ;
33import {
4+ isRemoteAllowed ,
45 matchHostname ,
56 matchPathname ,
67 matchPattern ,
@@ -13,6 +14,7 @@ describe('remote-pattern', () => {
1314 const url2 = new URL ( 'http://preview.docs.astro.build:8080/' ) ;
1415 const url3 = new URL ( 'https://astro.build/' ) ;
1516 const url4 = new URL ( 'https://example.co/' ) ;
17+ const url5 = new URL ( 'data:text/plain;base64,SGVsbG8sIFdvcmxkIQ==' ) ;
1618
1719 describe ( 'remote pattern matchers' , ( ) => {
1820 it ( 'matches protocol' , async ( ) => {
@@ -22,6 +24,7 @@ describe('remote-pattern', () => {
2224 // defined, true/false
2325 assert . equal ( matchProtocol ( url1 , 'http' ) , false ) ;
2426 assert . equal ( matchProtocol ( url1 , 'https' ) , true ) ;
27+ assert . equal ( matchProtocol ( url5 , 'data' ) , true ) ;
2528 } ) ;
2629
2730 it ( 'matches port' , async ( ) => {
@@ -123,6 +126,36 @@ describe('remote-pattern', () => {
123126 } ) ,
124127 false ,
125128 ) ;
129+
130+ assert . equal (
131+ matchPattern ( url5 , {
132+ protocol : 'data' ,
133+ } ) ,
134+ true ,
135+ ) ;
136+ } ) ;
137+ } ) ;
138+
139+ describe ( 'remote is allowed' , ( ) => {
140+ it ( 'allows remote URLs based on patterns' , async ( ) => {
141+ const patterns = { domains : [ ] , remotePatterns : [
142+ {
143+ protocol : 'https' ,
144+ hostname : '**.astro.build' ,
145+ pathname : '/en/**' ,
146+ } ,
147+ {
148+ protocol : 'http' ,
149+ hostname : 'preview.docs.astro.build' ,
150+ port : '8080' ,
151+ } ,
152+ ] } ;
153+
154+ assert . equal ( isRemoteAllowed ( url1 , patterns ) , true ) ;
155+ assert . equal ( isRemoteAllowed ( url2 , patterns ) , true ) ;
156+ assert . equal ( isRemoteAllowed ( url3 , patterns ) , false ) ;
157+ assert . equal ( isRemoteAllowed ( url4 , patterns ) , false ) ;
158+ assert . equal ( isRemoteAllowed ( url5 , patterns ) , false ) ;
126159 } ) ;
127160 } ) ;
128161} ) ;
0 commit comments