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

HeaderDictionaryTests.cs « test « Http « Http « src - github.com/dotnet/aspnetcore.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 27de61aec55dd2cd3ef40e357a236d0fdc2cef31 (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
// 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.Extensions.Primitives;
using Xunit;

namespace Microsoft.AspNetCore.Http;

public class HeaderDictionaryTests
{
    public static TheoryData HeaderSegmentData => new TheoryData<IEnumerable<string>>
        {
          new[] { "Value1", "Value2", "Value3", "Value4" },
          new[] { "Value1", "", "Value3", "Value4" },
          new[] { "Value1", "", "", "Value4" },
          new[] { "Value1", "", null, "Value4" },
          new[] { "", "", "", "" },
          new[] { "", null, "", null },
        };

    [Fact]
    public void PropertiesAreAccessible()
    {
        var headers = new HeaderDictionary(
            new Dictionary<string, StringValues>(StringComparer.OrdinalIgnoreCase)
            {
                    { "Header1", "Value1" }
            });

        Assert.Single(headers);
        Assert.Equal<string>(new[] { "Header1" }, headers.Keys);
        Assert.True(headers.ContainsKey("header1"));
        Assert.False(headers.ContainsKey("header2"));
        Assert.Equal("Value1", headers["header1"]);
        Assert.Equal(new[] { "Value1" }, headers["header1"].ToArray());
    }

    [Theory]
    [MemberData(nameof(HeaderSegmentData))]
    public void EmptyHeaderSegmentsAreIgnored(IEnumerable<string> segments)
    {
        var header = string.Join(",", segments);

        var headers = new HeaderDictionary(
           new Dictionary<string, StringValues>(StringComparer.OrdinalIgnoreCase)
           {
                    { "Header1",  header},
           });

        var result = headers.GetCommaSeparatedValues("Header1");
        var expectedResult = segments.Where(s => !string.IsNullOrEmpty(s));

        Assert.Equal(expectedResult, result);
    }

    [Fact]
    public void EmptyQuotedHeaderSegmentsAreIgnored()
    {
        var headers = new HeaderDictionary(
           new Dictionary<string, StringValues>(StringComparer.OrdinalIgnoreCase)
           {
                    { "Header1",  "Value1,\"\",,Value2" },
           });

        var result = headers.GetCommaSeparatedValues("Header1");
        Assert.Equal(new[] { "Value1", "Value2" }, result);
    }

    [Fact]
    public void ReadActionsWorkWhenReadOnly()
    {
        var headers = new HeaderDictionary(
            new Dictionary<string, StringValues>(StringComparer.OrdinalIgnoreCase)
            {
                    { "Header1", "Value1" }
            });

        headers.IsReadOnly = true;

        Assert.Single(headers);
        Assert.Equal<string>(new[] { "Header1" }, headers.Keys);
        Assert.True(headers.ContainsKey("header1"));
        Assert.False(headers.ContainsKey("header2"));
        Assert.Equal("Value1", headers["header1"]);
        Assert.Equal(new[] { "Value1" }, headers["header1"].ToArray());
    }

    [Fact]
    public void WriteActionsThrowWhenReadOnly()
    {
        var headers = new HeaderDictionary();
        headers.IsReadOnly = true;

        Assert.Throws<InvalidOperationException>(() => headers["header1"] = "value1");
        Assert.Throws<InvalidOperationException>(() => ((IDictionary<string, StringValues>)headers)["header1"] = "value1");
        Assert.Throws<InvalidOperationException>(() => headers.ContentLength = 12);
        Assert.Throws<InvalidOperationException>(() => headers.Add(new KeyValuePair<string, StringValues>("header1", "value1")));
        Assert.Throws<InvalidOperationException>(() => headers.Add("header1", "value1"));
        Assert.Throws<InvalidOperationException>(() => headers.Clear());
        Assert.Throws<InvalidOperationException>(() => headers.Remove(new KeyValuePair<string, StringValues>("header1", "value1")));
        Assert.Throws<InvalidOperationException>(() => headers.Remove("header1"));
    }

    [Fact]
    public void GetCommaSeparatedValues_WorksForUnquotedHeaderValuesEndingWithSpace()
    {
        var headers = new HeaderDictionary
            {
                { "Via", "value " },
            };

        var result = headers.GetCommaSeparatedValues("Via");

        Assert.Equal(new[] { "value " }, result);
    }
}