releases/tests/Paca.Tests/Browser/DeclarativeNetRequestPriorityTests.cs

82 lines
2.9 KiB
C#

using System.Collections.Generic;
using Paca.Browser.Extensions;
using Xunit;
namespace Paca.Tests.Browser;
public class DeclarativeNetRequestPriorityTests
{
[Fact]
public void HigherPriority_AllowRule_Overrides_LowerPriority_BlockRule()
{
var engine = new DeclarativeNetRequestEngine();
// 1. Static Rule: Priority 1 - Block doubleclick.net
engine.RegisterStaticRules("ext1", new List<DnrRule>
{
new DnrRule
{
Id = 1,
Priority = 1,
Action = new DnrRuleAction { Type = "block" },
Condition = new DnrRuleCondition { UrlFilter = "||doubleclick.net^" }
}
});
// 2. Dynamic Rule: Priority 2 - Allow doubleclick.net/allowed
engine.UpdateDynamicRules("ext1", null, new List<DnrRule>
{
new DnrRule
{
Id = 2,
Priority = 2,
Action = new DnrRuleAction { Type = "allow" },
Condition = new DnrRuleCondition { UrlFilter = "||doubleclick.net/allowed^" }
}
});
// 일반 픽셀은 Block
var blockDecision = engine.EvaluateRequest("https://ad.doubleclick.net/pixel", method: "GET", resourceType: "xmlhttprequest");
Assert.Equal(DnrRuleActionType.Block, blockDecision.ActionType);
Assert.Equal(1, blockDecision.MatchedRuleId);
// 허용 경로는 Priority 2인 Allow 룰이 Block을 오버라이드
var allowDecision = engine.EvaluateRequest("https://ad.doubleclick.net/allowed/test", method: "GET", resourceType: "xmlhttprequest");
Assert.Equal(DnrRuleActionType.Allow, allowDecision.ActionType);
Assert.Equal(2, allowDecision.MatchedRuleId);
}
[Fact]
public void SessionRules_HavePrecedence_Over_StaticRules()
{
var engine = new DeclarativeNetRequestEngine();
// Static: Block example.com
engine.RegisterStaticRules("ext1", new List<DnrRule>
{
new DnrRule
{
Id = 10,
Priority = 10,
Action = new DnrRuleAction { Type = "block" },
Condition = new DnrRuleCondition { UrlFilter = "||example.com^" }
}
});
// Session: AllowAllRequests for example.com (동일 priority에서도 Session이 Static을 압도)
engine.UpdateSessionRules("ext1", null, new List<DnrRule>
{
new DnrRule
{
Id = 20,
Priority = 10,
Action = new DnrRuleAction { Type = "allowAllRequests" },
Condition = new DnrRuleCondition { UrlFilter = "||example.com^" }
}
});
var decision = engine.EvaluateRequest("https://example.com/page", method: "GET", resourceType: "main_frame");
Assert.Equal(DnrRuleActionType.AllowAllRequests, decision.ActionType);
Assert.Equal(20, decision.MatchedRuleId);
}
}