@@ -21,6 +21,7 @@ import { expect, test, vi, Mock } from "vitest";
21
21
22
22
import {
23
23
applyActions ,
24
+ fetchActions ,
24
25
callCreateAssessment ,
25
26
callListFirewallPolicies ,
26
27
createPartialEventWithSiteInfo ,
@@ -39,6 +40,8 @@ import {
39
40
EdgeResponseInit ,
40
41
FirewallPolicy ,
41
42
Event ,
43
+ applyPreRequestActions ,
44
+ applyPostResponseActions ,
42
45
} from "./index" ;
43
46
44
47
import { FetchApiRequest , FetchApiResponse } from "./fetchApi" ;
@@ -83,7 +86,8 @@ class TestContext extends RecaptchaContext {
83
86
log_messages : Array < [ LogLevel , string [ ] ] > = [ ] ;
84
87
85
88
constructor ( config : RecaptchaConfig ) {
86
- super ( config ) ;
89
+ // Deep copy the config so it can be modified independently.
90
+ super ( JSON . parse ( JSON . stringify ( config ) ) ) ;
87
91
}
88
92
89
93
createRequest ( url : string , options : any ) : EdgeRequest {
@@ -344,6 +348,7 @@ test("ApplyActions-setHeader", async () => {
344
348
expect ( resp . text ( ) ) . toEqual ( "<HTML>Hello World</HTML>" ) ;
345
349
expect ( fetch ) . toHaveBeenCalledTimes ( 1 ) ;
346
350
} ) ;
351
+
347
352
test ( "ApplyActions-redirect" , async ( ) => {
348
353
const context = new TestContext ( testConfig ) ;
349
354
const req = new FetchApiRequest ( "https://www.example.com/originalreq" ) ;
@@ -1087,6 +1092,7 @@ test("processRequest-dump", async () => {
1087
1092
[ "debug" , [ "[rpc] listFirewallPolicies (ok)" ] ] ,
1088
1093
[ "debug" , [ "local assessment succeeded" ] ] ,
1089
1094
[ "debug" , [ "terminalAction: allow" ] ] ,
1095
+ [ "debug" , [ "Applying response actions, ignoring request action" ] ] ,
1090
1096
] ,
1091
1097
exceptions : [ ] ,
1092
1098
create_assessment_headers : [ ] ,
@@ -1369,3 +1375,123 @@ test("DebugTrace-format", () => {
1369
1375
"list_firewall_policies_status=ok;policy_count=10;site_key_used=session;site_keys_present=asce;empty_config=apikey,endpoint;performance_counters=" ,
1370
1376
) ;
1371
1377
} ) ;
1378
+
1379
+ test ( "fetchActions-localAssessment" , async ( ) => {
1380
+ const context = new TestContext ( testConfig ) ;
1381
+ context . config . sessionJsInjectPath = "/teste2e;/another/path" ;
1382
+ const req = new FetchApiRequest ( "https://www.example.com/teste2e" ) ;
1383
+ const testPolicies = [
1384
+ {
1385
+ name : "test-policy" ,
1386
+ description : "test-description" ,
1387
+ path : "/teste2e" ,
1388
+ actions : [ { block : { } } ] ,
1389
+ } ,
1390
+ {
1391
+ name : "test-policy2" ,
1392
+ description : "test-description2" ,
1393
+ path : "/teste2e" ,
1394
+ actions : [ { redirect : { } } ] ,
1395
+ } ,
1396
+ ] ;
1397
+ vi . stubGlobal ( "fetch" , vi . fn ( ) ) ;
1398
+ ( fetch as Mock ) . mockImplementationOnce ( ( ) =>
1399
+ Promise . resolve ( {
1400
+ status : 200 ,
1401
+ headers : new Headers ( ) ,
1402
+ json : ( ) => Promise . resolve ( { firewallPolicies : testPolicies } ) ,
1403
+ } ) ,
1404
+ ) ;
1405
+ const actions = await fetchActions ( context , req ) ;
1406
+ expect ( actions ) . toEqual ( [
1407
+ {
1408
+ injectjs : { } ,
1409
+ } ,
1410
+ {
1411
+ block : { } ,
1412
+ } ,
1413
+ ] ) ;
1414
+ expect ( fetch ) . toHaveBeenCalledTimes ( 1 ) ;
1415
+ } ) ;
1416
+
1417
+ test ( "fetchActions-createAssessment" , async ( ) => {
1418
+ const context = new TestContext ( testConfig ) ;
1419
+ const req = new FetchApiRequest ( "https://www.example.com/testlocal" ) ;
1420
+ vi . stubGlobal ( "fetch" , vi . fn ( ) ) ;
1421
+ ( fetch as Mock )
1422
+ . mockImplementationOnce ( ( req ) => {
1423
+ expect ( req . url ) . toEqual ( "https://recaptchaenterprise.googleapis.com/v1/projects/12345/fetchFirewallPolicies" ) ;
1424
+ const testPolicies = [
1425
+ {
1426
+ name : "test-policy" ,
1427
+ description : "test-description" ,
1428
+ path : "/testlocal" ,
1429
+ condition : "test-condition" ,
1430
+ actions : [ { block : { } } ] ,
1431
+ } ,
1432
+ ] ;
1433
+ Promise . resolve ( {
1434
+ status : 200 ,
1435
+ headers : new Headers ( ) ,
1436
+ json : ( ) => Promise . resolve ( { firewallPolicies : testPolicies } ) ,
1437
+ } ) ;
1438
+ } )
1439
+ . mockImplementationOnce ( ( req ) => {
1440
+ expect ( req . url ) . toEqual ( "https://recaptchaenterprise.googleapis.com/v1/projects/12345/assessments?key=abc123" ) ;
1441
+ return Promise . resolve ( {
1442
+ status : 200 ,
1443
+ headers : new Headers ( ) ,
1444
+ json : ( ) =>
1445
+ Promise . resolve ( {
1446
+ name : "projects/12345/assessments/1234567890" ,
1447
+ firewallPolicyAssessment : {
1448
+ firewallPolicy : {
1449
+ actions : [ { block : { } } ] ,
1450
+ } ,
1451
+ } ,
1452
+ } ) ,
1453
+ } ) ;
1454
+ } ) ;
1455
+ const actions = await fetchActions ( context , req ) ;
1456
+ expect ( fetch ) . toHaveBeenCalledTimes ( 2 ) ;
1457
+ expect ( actions ) . toEqual ( [
1458
+ {
1459
+ block : { } ,
1460
+ } ,
1461
+ ] ) ;
1462
+ } ) ;
1463
+
1464
+ test ( "applyPreRequestActions - terminal" , async ( ) => {
1465
+ const context = new TestContext ( testConfig ) ;
1466
+ const req = new FetchApiRequest ( "https://www.example.com/teste2e" ) ;
1467
+ const resp = ( await applyPreRequestActions ( context , req , [ { block : { } } ] ) ) as EdgeResponse ;
1468
+ expect ( resp . status ) . toEqual ( 403 ) ;
1469
+ } ) ;
1470
+
1471
+ test ( "applyPreRequestActions - non-terminal" , async ( ) => {
1472
+ const context = new TestContext ( testConfig ) ;
1473
+ const req = new FetchApiRequest ( "https://www.example.com/teste2e" ) ;
1474
+ expect ( req . getHeader ( "my-custom-header" ) ) . toBeNull ( ) ;
1475
+ const resp = await applyPreRequestActions ( context , req , [
1476
+ {
1477
+ setHeader : {
1478
+ key : "my-custom-header" ,
1479
+ value : "test123" ,
1480
+ } ,
1481
+ } ,
1482
+ ] ) ;
1483
+ expect ( resp ) . toBeNull ( ) ;
1484
+ expect ( req . getHeader ( "my-custom-header" ) ) . toEqual ( "test123" ) ;
1485
+ } ) ;
1486
+
1487
+ test ( "applyPostResponseActions" , async ( ) => {
1488
+ const context = new TestContext ( testConfig ) ;
1489
+ const inputResp : Promise < EdgeResponse > = Promise . resolve ( {
1490
+ status : 200 ,
1491
+ headers : new Headers ( ) ,
1492
+ text : ( ) => "<HTML>Hello World</HTML>" ,
1493
+ } ) ;
1494
+
1495
+ const resp = await applyPostResponseActions ( context , await inputResp , [ { injectjs : { } } ] ) ;
1496
+ expect ( await resp . text ( ) ) . toEqual ( '<HTML><script src="test.js"/>Hello World</HTML>' ) ;
1497
+ } ) ;
0 commit comments