Welcome to mirror list, hosted at ThFree Co, Russian Federation.

MatcherAssert.cs « Matching « UnitTests « test « Routing « Http « src - github.com/dotnet/aspnetcore.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 35c3ab87e4c37b606647416781e37d1929b12538 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Xunit.Sdk;

namespace Microsoft.AspNetCore.Routing.Matching
{
    internal static class MatcherAssert
    {
        public static void AssertRouteValuesEqual(object expectedValues, RouteValueDictionary actualValues)
        {
            AssertRouteValuesEqual(new RouteValueDictionary(expectedValues), actualValues);
        }

        public static void AssertRouteValuesEqual(RouteValueDictionary expectedValues, RouteValueDictionary actualValues)
        {
            if (expectedValues.Count != actualValues.Count ||
                !expectedValues.OrderBy(kvp => kvp.Key).SequenceEqual(actualValues.OrderBy(kvp => kvp.Key)))
            {
                throw new XunitException(
                    $"Expected values:{FormatRouteValues(expectedValues)} Actual values: {FormatRouteValues(actualValues)}.");
            }
        }

        public static void AssertMatch(HttpContext httpContext, Endpoint expected)
        {
            AssertMatch(httpContext, expected, new RouteValueDictionary());
        }

        public static void AssertMatch(HttpContext httpContext, Endpoint expected, bool ignoreValues)
        {
            AssertMatch(httpContext, expected, new RouteValueDictionary(), ignoreValues);
        }

        public static void AssertMatch(HttpContext httpContext, Endpoint expected, object values)
        {
            AssertMatch(httpContext, expected, new RouteValueDictionary(values));
        }

        public static void AssertMatch(HttpContext httpContext, Endpoint expected, string[] keys, string[] values)
        {
            keys = keys ?? Array.Empty<string>();
            values = values ?? Array.Empty<string>();

            if (keys.Length != values.Length)
            {
                throw new XunitException("Keys and Values must be the same length.");
            }

            var zipped = keys.Zip(values, (k, v) => new KeyValuePair<string, object>(k, v));
            AssertMatch(httpContext, expected, new RouteValueDictionary(zipped));
        }

        public static void AssertMatch(
            HttpContext httpContext,
            Endpoint expected,
            RouteValueDictionary values,
            bool ignoreValues = false)
        {
            if (httpContext.GetEndpoint() == null)
            {
                throw new XunitException($"Was expected to match '{expected.DisplayName}' but did not match.");
            }

            var actualValues = httpContext.Request.RouteValues;

            if (actualValues == null)
            {
                throw new XunitException("RouteValues is null.");
            }

            if (!object.ReferenceEquals(expected, httpContext.GetEndpoint()))
            {
                throw new XunitException(
                    $"Was expected to match '{expected.DisplayName}' but matched " +
                    $"'{httpContext.GetEndpoint().DisplayName}' with values: {FormatRouteValues(actualValues)}.");
            }

            if (!ignoreValues)
            {
                // Note: this comparison is intended for unit testing, and is stricter than necessary to make tests
                // more precise. Route value comparisons in product code are more flexible than a simple .Equals.
                if (values.Count != actualValues.Count ||
                    !values.OrderBy(kvp => kvp.Key).SequenceEqual(actualValues.OrderBy(kvp => kvp.Key)))
                {
                    throw new XunitException(
                        $"Was expected to match '{expected.DisplayName}' with values {FormatRouteValues(values)} but matched " +
                        $"values: {FormatRouteValues(actualValues)}.");
                }
            }
        }

        public static void AssertNotMatch(HttpContext httpContext)
        {
            if (httpContext.GetEndpoint() != null)
            {
                throw new XunitException(
                    $"Was expected not to match '{httpContext.GetEndpoint().DisplayName}' " +
                    $"but matched with values: {FormatRouteValues(httpContext.Request.RouteValues)}.");
            }
        }

        private static string FormatRouteValues(RouteValueDictionary values)
        {
            return values == null ? "{}" : "{" + string.Join(", ", values.Select(kvp => $"{kvp.Key} = '{kvp.Value}'")) + "}";
        }
    }
}