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

HeaderUtilitiesTest.cs « test « Headers « Http « src - github.com/dotnet/aspnetcore.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a467bcaf77adedf59686d3a4d020de3438cafb2e (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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// 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.Globalization;
using Microsoft.Extensions.Primitives;
using Xunit;

namespace Microsoft.Net.Http.Headers
{
    public class HeaderUtilitiesTest
    {
        private const string Rfc1123Format = "r";

        [Theory]
        [MemberData(nameof(TestValues))]
        public void ReturnsSameResultAsRfc1123String(DateTimeOffset dateTime, bool quoted)
        {
            var formatted = dateTime.ToString(Rfc1123Format);
            var expected = quoted ? $"\"{formatted}\"" : formatted;
            var actual = HeaderUtilities.FormatDate(dateTime, quoted);

            Assert.Equal(expected, actual);
        }

        public static TheoryData<DateTimeOffset, bool> TestValues
        {
            get
            {
                var data = new TheoryData<DateTimeOffset, bool>();

                var date = new DateTimeOffset(new DateTime(2018, 1, 1, 1, 1, 1));

                foreach (var quoted in new[] { true, false })
                {
                    data.Add(date, quoted);

                    for (var i = 1; i < 60; i++)
                    {
                        data.Add(date.AddSeconds(i), quoted);
                        data.Add(date.AddMinutes(i), quoted);
                    }

                    for (var i = 1; i < DateTime.DaysInMonth(date.Year, date.Month); i++)
                    {
                        data.Add(date.AddDays(i), quoted);
                    }

                    for (var i = 1; i < 11; i++)
                    {
                        data.Add(date.AddMonths(i), quoted);
                    }

                    for (var i = 1; i < 5; i++)
                    {
                        data.Add(date.AddYears(i), quoted);
                    }
                }

                return data;
            }
        }

        [Theory]
        [InlineData("h=1", "h", 1)]
        [InlineData("directive1=3, directive2=10", "directive1", 3)]
        [InlineData("directive1   =45, directive2=80", "directive1", 45)]
        [InlineData("directive1=   89   , directive2=22", "directive1", 89)]
        [InlineData("directive1=   89   , directive2= 42", "directive2", 42)]
        [InlineData("directive1=   89   , directive= 42", "directive", 42)]
        [InlineData("directive1,,,,,directive2 = 42 ", "directive2", 42)]
        [InlineData("directive1=;,directive2 = 42 ", "directive2", 42)]
        [InlineData("directive1;;,;;,directive2 = 42 ", "directive2", 42)]
        [InlineData("directive1=value;q=0.6,directive2 = 42 ", "directive2", 42)]
        public void TryParseSeconds_Succeeds(string headerValues, string targetValue, int expectedValue)
        {
            TimeSpan? value;
            Assert.True(HeaderUtilities.TryParseSeconds(new StringValues(headerValues), targetValue, out value));
            Assert.Equal(TimeSpan.FromSeconds(expectedValue), value);
        }

        [Theory]
        [InlineData("", "")]
        [InlineData(null, null)]
        [InlineData("h=", "h")]
        [InlineData("directive1=, directive2=10", "directive1")]
        [InlineData("directive1   , directive2=80", "directive1")]
        [InlineData("h=10", "directive")]
        [InlineData("directive1", "directive")]
        [InlineData("directive1,,,,,,,", "directive")]
        [InlineData("h=directive", "directive")]
        [InlineData("directive1, directive2=80", "directive")]
        [InlineData("directive1=;, directive2=10", "directive1")]
        [InlineData("directive1;directive2=10", "directive2")]
        public void TryParseSeconds_Fails(string headerValues, string targetValue)
        {
            TimeSpan? value;
            Assert.False(HeaderUtilities.TryParseSeconds(new StringValues(headerValues), targetValue, out value));
        }

        [Theory]
        [InlineData(0)]
        [InlineData(1)]
        [InlineData(1234567890)]
        [InlineData(long.MaxValue)]
        public void FormatNonNegativeInt64_MatchesToString(long value)
        {
            Assert.Equal(value.ToString(CultureInfo.InvariantCulture), HeaderUtilities.FormatNonNegativeInt64(value));
        }

        [Theory]
        [InlineData(-1)]
        [InlineData(-1234567890)]
        [InlineData(long.MinValue)]
        public void FormatNonNegativeInt64_Throws_ForNegativeValues(long value)
        {
            Assert.Throws<ArgumentOutOfRangeException>(() => HeaderUtilities.FormatNonNegativeInt64(value));
        }

        [Theory]
        [InlineData("h", "h", true)]
        [InlineData("h=", "h", true)]
        [InlineData("h=1", "h", true)]
        [InlineData("H", "h", true)]
        [InlineData("H=", "h", true)]
        [InlineData("H=1", "h", true)]
        [InlineData("h", "H", true)]
        [InlineData("h=", "H", true)]
        [InlineData("h=1", "H", true)]
        [InlineData("directive1, directive=10", "directive1", true)]
        [InlineData("directive1=, directive=10", "directive1", true)]
        [InlineData("directive1=3, directive=10", "directive1", true)]
        [InlineData("directive1   , directive=80", "directive1", true)]
        [InlineData("   directive1, directive=80", "directive1", true)]
        [InlineData("directive1   =45, directive=80", "directive1", true)]
        [InlineData("directive1=   89   , directive=22", "directive1", true)]
        [InlineData("directive1, directive", "directive", true)]
        [InlineData("directive1, directive=", "directive", true)]
        [InlineData("directive1, directive=10", "directive", true)]
        [InlineData("directive1=3, directive", "directive", true)]
        [InlineData("directive1=3, directive=", "directive", true)]
        [InlineData("directive1=3, directive=10", "directive", true)]
        [InlineData("directive1=   89   , directive= 42", "directive", true)]
        [InlineData("directive1=   89   , directive = 42", "directive", true)]
        [InlineData("directive1,,,,,directive2 = 42 ", "directive2", true)]
        [InlineData("directive1;;,;;,directive2 = 42 ", "directive2", true)]
        [InlineData("directive1=;,directive2 = 42 ", "directive2", true)]
        [InlineData("directive1=value;q=0.6,directive2 = 42 ", "directive2", true)]
        [InlineData(null, null, false)]
        [InlineData(null, "", false)]
        [InlineData("", null, false)]
        [InlineData("", "", false)]
        [InlineData("h=10", "directive", false)]
        [InlineData("directive1", "directive", false)]
        [InlineData("directive1,,,,,,,", "directive", false)]
        [InlineData("h=directive", "directive", false)]
        [InlineData("directive1, directive2=80", "directive", false)]
        [InlineData("directive1;, directive2=80", "directive", false)]
        [InlineData("directive1=value;q=0.6;directive2 = 42 ", "directive2", false)]
        public void ContainsCacheDirective_MatchesExactValue(string headerValues, string targetValue, bool contains)
        {
            Assert.Equal(contains, HeaderUtilities.ContainsCacheDirective(new StringValues(headerValues), targetValue));
        }

        [Theory]
        [InlineData("")]
        [InlineData(null)]
        [InlineData("-1")]
        [InlineData("a")]
        [InlineData("1.1")]
        [InlineData("9223372036854775808")] // long.MaxValue + 1
        public void TryParseNonNegativeInt64_Fails(string valueString)
        {
            long value = 1;
            Assert.False(HeaderUtilities.TryParseNonNegativeInt64(valueString, out value));
            Assert.Equal(0, value);
        }

        [Theory]
        [InlineData("0", 0)]
        [InlineData("9223372036854775807", 9223372036854775807)] // long.MaxValue
        public void TryParseNonNegativeInt64_Succeeds(string valueString, long expected)
        {
            long value = 1;
            Assert.True(HeaderUtilities.TryParseNonNegativeInt64(valueString, out value));
            Assert.Equal(expected, value);
        }

        [Theory]
        [InlineData("")]
        [InlineData(null)]
        [InlineData("-1")]
        [InlineData("a")]
        [InlineData("1.1")]
        [InlineData("1,000")]
        [InlineData("2147483648")] // int.MaxValue + 1
        public void TryParseNonNegativeInt32_Fails(string valueString)
        {
            int value = 1;
            Assert.False(HeaderUtilities.TryParseNonNegativeInt32(valueString, out value));
            Assert.Equal(0, value);
        }

        [Theory]
        [InlineData("0", 0)]
        [InlineData("2147483647", 2147483647)] // int.MaxValue
        public void TryParseNonNegativeInt32_Succeeds(string valueString, long expected)
        {
            int value = 1;
            Assert.True(HeaderUtilities.TryParseNonNegativeInt32(valueString, out value));
            Assert.Equal(expected, value);
        }

        [Theory]
        [InlineData("\"hello\"", "hello")]
        [InlineData("\"hello", "\"hello")]
        [InlineData("hello\"", "hello\"")]
        [InlineData("\"\"hello\"\"", "\"hello\"")]
        public void RemoveQuotes_BehaviorCheck(string input, string expected)
        {
            var actual = HeaderUtilities.RemoveQuotes(input);

            Assert.Equal(expected, actual);
        }
        [Theory]
        [InlineData("\"hello\"", true)]
        [InlineData("\"hello", false)]
        [InlineData("hello\"", false)]
        [InlineData("\"\"hello\"\"", true)]
        public void IsQuoted_BehaviorCheck(string input, bool expected)
        {
            var actual = HeaderUtilities.IsQuoted(input);

            Assert.Equal(expected, actual);
        }

        [Theory]
        [InlineData("value", "value")]
        [InlineData("\"value\"", "value")]
        [InlineData("\"hello\\\\\"", "hello\\")]
        [InlineData("\"hello\\\"\"", "hello\"")]
        [InlineData("\"hello\\\"foo\\\\bar\\\\baz\\\\\"", "hello\"foo\\bar\\baz\\")]
        [InlineData("\"quoted value\"", "quoted value")]
        [InlineData("\"quoted\\\"valuewithquote\"", "quoted\"valuewithquote")]
        [InlineData("\"hello\\\"", "hello\\")]
        public void UnescapeAsQuotedString_BehaviorCheck(string input, string expected)
        {
            var actual = HeaderUtilities.UnescapeAsQuotedString(input);

            Assert.Equal(expected, actual);
        }

        [Theory]
        [InlineData("value", "\"value\"")]
        [InlineData("23", "\"23\"")]
        [InlineData(";;;", "\";;;\"")]
        [InlineData("\"value\"", "\"\\\"value\\\"\"")]
        [InlineData("unquoted \"value", "\"unquoted \\\"value\"")]
        [InlineData("value\\morevalues\\evenmorevalues", "\"value\\\\morevalues\\\\evenmorevalues\"")]
        // We have to assume that the input needs to be quoted here
        [InlineData("\"\"double quoted string\"\"", "\"\\\"\\\"double quoted string\\\"\\\"\"")]
        [InlineData("\t", "\"\t\"")]
        public void SetAndEscapeValue_BehaviorCheck(string input, string expected)
        {
            var actual = HeaderUtilities.EscapeAsQuotedString(input);

            Assert.Equal(expected, actual);
        }

        [Theory]
        [InlineData("\n")]
        [InlineData("\b")]
        [InlineData("\r")]
        public void SetAndEscapeValue_ControlCharactersThrowFormatException(string input)
        {
            Assert.Throws<FormatException>(() => { var actual = HeaderUtilities.EscapeAsQuotedString(input); });
        }

        [Fact]
        public void SetAndEscapeValue_ThrowsFormatExceptionOnDelCharacter()
        {
            Assert.Throws<FormatException>(() => { var actual = HeaderUtilities.EscapeAsQuotedString($"{(char)0x7F}"); });
        }
    }
}