// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System.Collections.Generic; using System.IO; using System.Text.RegularExpressions; using Microsoft.AspNetCore.Rewrite.Internal; using Microsoft.AspNetCore.Rewrite.Internal.ApacheModRewrite; using Microsoft.AspNetCore.Rewrite.Internal.UrlActions; using Microsoft.AspNetCore.Rewrite.Internal.UrlMatches; using Microsoft.AspNetCore.Rewrite.Internal.IISUrlRewrite; using Xunit; namespace Microsoft.AspNetCore.Rewrite.Tests.UrlRewrite { public class FileParserTests { [Fact] public void RuleParse_ParseTypicalRule() { // arrange var xml = @" "; var expected = new List(); expected.Add(CreateTestRule(new List(), url: "^article/([0-9]+)/([_0-9a-z-]+)", name: "Rewrite to article.aspx", actionType: ActionType.Rewrite, pattern: "article.aspx?id={R:1}&title={R:2}")); // act var res = new UrlRewriteFileParser().Parse(new StringReader(xml)); // assert AssertUrlRewriteRuleEquality(expected, res); } [Fact] public void RuleParse_ParseSingleRuleWithSingleCondition() { // arrange var xml = @" "; var condList = new List(); condList.Add(new Condition { Input = new InputParser().ParseInputString("{HTTPS}"), Match = new RegexMatch(new Regex("^OFF$"), false) }); var expected = new List(); expected.Add(CreateTestRule(condList, url: "^article/([0-9]+)/([_0-9a-z-]+)", name: "Rewrite to article.aspx", actionType: ActionType.Rewrite, pattern: "article.aspx?id={R:1}&title={R:2}")); // act var res = new UrlRewriteFileParser().Parse(new StringReader(xml)); // assert AssertUrlRewriteRuleEquality(expected, res); } [Fact] public void RuleParse_ParseMultipleRules() { // arrange var xml = @" "; var condList = new List(); condList.Add(new Condition { Input = new InputParser().ParseInputString("{HTTPS}"), Match = new RegexMatch(new Regex("^OFF$"), false) }); var expected = new List(); expected.Add(CreateTestRule(condList, url: "^article/([0-9]+)/([_0-9a-z-]+)", name: "Rewrite to article.aspx", actionType: ActionType.Rewrite, pattern: "article.aspx?id={R:1}&title={R:2}")); expected.Add(CreateTestRule(condList, url: "^article/([0-9]+)/([_0-9a-z-]+)", name: "Rewrite to another article.aspx", actionType: ActionType.Rewrite, pattern: "article.aspx?id={R:1}&title={R:2}")); // act var res = new UrlRewriteFileParser().Parse(new StringReader(xml)); // assert AssertUrlRewriteRuleEquality(expected, res); } // Creates a rule with appropriate default values of the url rewrite rule. private IISUrlRewriteRule CreateTestRule(List conditions, LogicalGrouping condGrouping = LogicalGrouping.MatchAll, bool condTracking = false, string name = "", bool enabled = true, PatternSyntax patternSyntax = PatternSyntax.ECMAScript, bool stopProcessing = false, string url = "", bool ignoreCase = true, bool negate = false, ActionType actionType = ActionType.None, string pattern = "", bool appendQueryString = false, bool rewrittenUrl = false, RedirectType redirectType = RedirectType.Permanent ) { return new IISUrlRewriteRule(name, new RegexMatch(new Regex("^OFF$"), false), conditions, new RewriteAction(RuleResult.ContinueRules, new InputParser().ParseInputString(url), queryStringAppend: false)); } // TODO make rules comparable? private void AssertUrlRewriteRuleEquality(IList actual, IList expected) { Assert.Equal(actual.Count, expected.Count); for (var i = 0; i < actual.Count; i++) { var r1 = actual[i]; var r2 = expected[i]; Assert.Equal(r1.Name, r2.Name); if (r1.Conditions == null) { Assert.Equal(0, r2.Conditions.Count); } else if (r2.Conditions == null) { Assert.Equal(0, r1.Conditions.Count); } else { Assert.Equal(r1.Conditions.Count, r2.Conditions.Count); for (var j = 0; j < r1.Conditions.Count; j++) { var c1 = r1.Conditions[j]; var c2 = r2.Conditions[j]; Assert.Equal(c1.Input.PatternSegments.Count, c2.Input.PatternSegments.Count); } } Assert.Equal(r1.Action.GetType(), r2.Action.GetType()); Assert.Equal(r1.InitialMatch.GetType(), r2.InitialMatch.GetType()); } } } }