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

HostStringTest.cs « test « Http.Abstractions « Http « src - github.com/dotnet/aspnetcore.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c672ccfc80f3aa025133976b229c5d65f8c4b262 (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// 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 Microsoft.AspNetCore.Testing;
using Microsoft.Extensions.Primitives;
using Xunit;

namespace Microsoft.AspNetCore.Http
{
    public class HostStringTests
    {
        [Theory]
        [InlineData(0)]
        [InlineData(-1)]
        public void CtorThrows_IfPortIsNotGreaterThanZero(int port)
        {
            // Act and Assert
            ExceptionAssert.ThrowsArgumentOutOfRange(() => new HostString("localhost", port), "port", "The value must be greater than zero.");
        }

        [Theory]
        [InlineData("localhost", "localhost")]
        [InlineData("1.2.3.4", "1.2.3.4")]
        [InlineData("[2001:db8:a0b:12f0::1]", "[2001:db8:a0b:12f0::1]")]
        [InlineData("本地主機", "本地主機")]
        [InlineData("localhost:5000", "localhost")]
        [InlineData("1.2.3.4:5000", "1.2.3.4")]
        [InlineData("[2001:db8:a0b:12f0::1]:5000", "[2001:db8:a0b:12f0::1]")]
        [InlineData("本地主機:5000", "本地主機")]
        public void Domain_ExtractsHostFromValue(string sourceValue, string expectedDomain)
        {
            // Arrange
            var hostString = new HostString(sourceValue);

            // Act
            var result = hostString.Host;

            // Assert
            Assert.Equal(expectedDomain, result);
        }

        [Theory]
        [InlineData("localhost", null)]
        [InlineData("1.2.3.4", null)]
        [InlineData("[2001:db8:a0b:12f0::1]", null)]
        [InlineData("本地主機", null)]
        [InlineData("localhost:5000", 5000)]
        [InlineData("1.2.3.4:5000", 5000)]
        [InlineData("[2001:db8:a0b:12f0::1]:5000", 5000)]
        [InlineData("本地主機:5000", 5000)]
        public void Port_ExtractsPortFromValue(string sourceValue, int? expectedPort)
        {
            // Arrange
            var hostString = new HostString(sourceValue);

            // Act
            var result = hostString.Port;

            // Assert
            Assert.Equal(expectedPort, result);
        }

        [Theory]
        [InlineData("localhost:BLAH")]
        public void Port_ExtractsInvalidPortFromValue(string sourceValue)
        {
            // Arrange
            var hostString = new HostString(sourceValue);

            // Act
            var result = hostString.Port;

            // Assert
            Assert.Null(result);
        }

        [Theory]
        [InlineData("localhost", 5000, "localhost", 5000)]
        [InlineData("1.2.3.4", 5000, "1.2.3.4", 5000)]
        [InlineData("[2001:db8:a0b:12f0::1]", 5000, "[2001:db8:a0b:12f0::1]", 5000)]
        [InlineData("2001:db8:a0b:12f0::1", 5000, "[2001:db8:a0b:12f0::1]", 5000)]
        [InlineData("本地主機", 5000, "本地主機", 5000)]
        public void Ctor_CreatesFromHostAndPort(string sourceHost, int sourcePort, string expectedHost, int expectedPort)
        {
            // Arrange
            var hostString = new HostString(sourceHost, sourcePort);

            // Act
            var host = hostString.Host;
            var port = hostString.Port;

            // Assert
            Assert.Equal(expectedHost, host);
            Assert.Equal(expectedPort, port);
        }

        [Fact]
        public void Equals_EmptyHostStringAndDefaultHostString()
        {
            // Act and Assert
            Assert.Equal(default(HostString), new HostString(string.Empty));
            Assert.Equal(default(HostString), new HostString(string.Empty));
            // explicitly checking == operator
            Assert.True(new HostString(string.Empty) == default(HostString));
            Assert.True(default(HostString) == new HostString(string.Empty));
        }

        [Fact]
        public void NotEquals_DefaultHostStringAndNonNullHostString()
        {
            // Arrange
            var hostString = new HostString("example.com");

            // Act and Assert
            Assert.NotEqual(default(HostString), hostString);
        }

        [Fact]
        public void NotEquals_EmptyHostStringAndNonNullHostString()
        {
            // Arrange
            var hostString = new HostString("example.com");

            // Act and Assert
            Assert.NotEqual(hostString, new HostString(string.Empty));
        }

        [Theory]
        [InlineData("localHost", "localhost")]
        [InlineData("localHost", "*")] // Any - Used by HttpSys
        [InlineData("localhost:9090", "localHost")]
        [InlineData("example.com:443", "example.com")]
        [InlineData("foo.eXample.com:443", "*.exampLe.com")]
        [InlineData("f.eXample.com:443", "*.exampLe.com")]
        [InlineData("a.b.c.eXample.com:443", "*.exampLe.com")]
        [InlineData("127.0.0.1", "127.0.0.1")]
        [InlineData("127.0.0.1:443", "127.0.0.1")]
        [InlineData("xn--c1yn36f:443", "xn--c1yn36f")]
        [InlineData("點看", "點看")]
        [InlineData("[::ABC]", "[::aBc]")]
        [InlineData("[::1]:80", "[::1]")]
        [InlineData("[::1]:", "[::1]")]
        [InlineData("::1", "[::1]")]
        public void HostMatches(string host, string pattern)
        {
            Assert.True(HostString.MatchesAny(host, new StringSegment[] { pattern }));
        }

        [Theory]
        [InlineData("example.com", "localhost")]
        [InlineData("localhost:9090", "example.com")]
        [InlineData(":80", "localhost")]
        [InlineData(":", "localhost")]
        [InlineData("example.com:443", "*.example.com")]
        [InlineData(".example.com:443", "*.example.com")]
        [InlineData("foo.com:443", "*.example.com")]
        [InlineData("foo.example.com.bar:443", "*.example.com")]
        [InlineData(".com:443", "*.com")]
        [InlineData("xn--c1yn36f:443", "點看")]
        [InlineData("[::1", "[::1]")]
        [InlineData("[::1:80", "[::1]")]
        [InlineData("::1", "::1")] // Brackets are added to the host before the comparison
        public void HostDoesntMatch(string host, string pattern)
        {
            Assert.False(HostString.MatchesAny(host, new StringSegment[] { pattern }));
        }

        [Fact]
        public void HostMatchThrowsForBadPort()
        {
            Assert.Throws<FormatException>(() => HostString.MatchesAny("example.com:1abc", new StringSegment[] { "example.com" }));
        }
    }
}