예제 #1
0
    public void Should_parse_RewriteMapSegment_and_successfully_evaluate_result()
    {
        const string expectedMapName = "apiMap";
        const string expectedKey     = "api.test.com";
        const string expectedValue   = "test.com/api";
        var          map             = new IISRewriteMap(expectedMapName);

        map[expectedKey] = expectedValue;
        var maps = new IISRewriteMapCollection {
            map
        };

        var inputString = $"{{{expectedMapName}:{{R:1}}}}";
        var pattern     = new InputParser(maps, false).ParseInputString(inputString, UriMatchPart.Path);

        Assert.Equal(1, pattern.PatternSegments.Count);

        var segment           = pattern.PatternSegments.Single();
        var rewriteMapSegment = segment as RewriteMapSegment;

        Assert.NotNull(rewriteMapSegment);

        var result = rewriteMapSegment.Evaluate(CreateTestRewriteContext(), CreateRewriteMapRuleMatch(expectedKey).BackReferences, CreateRewriteMapConditionMatch(inputString).BackReferences);

        Assert.Equal(expectedValue, result);
    }
예제 #2
0
    public void Should_throw_FormatException_if_rewrite_map_not_found()
    {
        const string definedMapName   = "testMap";
        const string undefinedMapName = "apiMap";
        var          map  = new IISRewriteMap(definedMapName);
        var          maps = new IISRewriteMapCollection {
            map
        };

        Assert.Throws <FormatException>(() => new InputParser(maps, false).ParseInputString($"{{{undefinedMapName}:{{R:1}}}}", UriMatchPart.Path));
    }
예제 #3
0
    public static IISRewriteMapCollection?Parse(XElement xmlRoot)
    {
        if (xmlRoot == null)
        {
            throw new ArgumentNullException(nameof(xmlRoot));
        }

        var mapsElement = xmlRoot.Descendants(RewriteTags.RewriteMaps).SingleOrDefault();

        if (mapsElement == null)
        {
            return(null);
        }

        var rewriteMaps = new IISRewriteMapCollection();

        foreach (var mapElement in mapsElement.Elements(RewriteTags.RewriteMap))
        {
            var map = new IISRewriteMap(mapElement.Attribute(RewriteTags.Name)?.Value !);
            foreach (var addElement in mapElement.Elements(RewriteTags.Add))
            {
                map[addElement.Attribute(RewriteTags.Key) !.Value.ToLowerInvariant()] = addElement.Attribute(RewriteTags.Value) !.Value;