This repository has been archived by the owner on Nov 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 104
/
MediaTypeFormatterExtensionsTests.cs
106 lines (94 loc) · 5.52 KB
/
MediaTypeFormatterExtensionsTests.cs
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
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Formatting.Mocks;
using System.Net.Http.Headers;
using Microsoft.TestCommon;
using Xunit;
using Assert = Microsoft.TestCommon.AssertEx;
namespace System.Net.Http.Formatting
{
public class MediaTypeFormatterExtensionsTests
{
[Fact]
[Trait("Description", "MediaTypeFormatterExtensionMethods is public and static.")]
public void TypeIsCorrect()
{
Assert.Type.HasProperties(typeof(MediaTypeFormatterExtensions), TypeAssert.TypeProperties.IsPublicVisibleClass | TypeAssert.TypeProperties.IsStatic);
}
[Fact]
[Trait("Description", "AddQueryStringMapping(MediaTypeFormatter, string, string, MediaTypeHeaderValue) throws for null 'this'.")]
public void AddQueryStringMappingThrowsWithNullThis()
{
MediaTypeFormatter formatter = null;
Assert.ThrowsArgumentNull(() => formatter.AddQueryStringMapping("name", "value", new MediaTypeHeaderValue("application/xml")), "formatter");
}
[Fact]
[Trait("Description", "AddQueryStringMapping(MediaTypeFormatter, string, string, string) throws for null 'this'.")]
public void AddQueryStringMapping1ThrowsWithNullThis()
{
MediaTypeFormatter formatter = null;
Assert.ThrowsArgumentNull(() => formatter.AddQueryStringMapping("name", "value", "application/xml"), "formatter");
}
[Fact]
[Trait("Description", "AddMediaRangeMapping(MediaTypeFormatter, MediaTypeHeaderValue, MediaTypeHeaderValue) throws for null 'this'.")]
public void AddMediaRangeMappingThrowsWithNullThis()
{
MediaTypeFormatter formatter = null;
Assert.ThrowsArgumentNull(() => formatter.AddMediaRangeMapping(new MediaTypeHeaderValue("application/*"), new MediaTypeHeaderValue("application/xml")), "formatter");
}
[Fact]
[Trait("Description", "AddMediaRangeMapping(MediaTypeFormatter, string, string) throws for null 'this'.")]
public void AddMediaRangeMapping1ThrowsWithNullThis()
{
MediaTypeFormatter formatter = null;
Assert.ThrowsArgumentNull(() => formatter.AddMediaRangeMapping("application/*", "application/xml"), "formatter");
}
[Fact]
[Trait("Description", "AddRequestHeaderMapping(MediaTypeFormatter, string, string, StringComparison, bool, MediaTypeHeaderValue) throws for null 'this'.")]
public void AddRequestHeaderMappingThrowsWithNullThis()
{
MediaTypeFormatter formatter = null;
Assert.ThrowsArgumentNull(() => formatter.AddRequestHeaderMapping("name", "value", StringComparison.CurrentCulture, true, new MediaTypeHeaderValue("application/xml")), "formatter");
}
[Fact]
[Trait("Description", "AddRequestHeaderMapping(MediaTypeFormatter, string, string, StringComparison, bool, MediaTypeHeaderValue) adds formatter on 'this'.")]
public void AddRequestHeaderMappingAddsSuccessfully()
{
MediaTypeFormatter formatter = new MockMediaTypeFormatter();
Assert.Equal(0, formatter.MediaTypeMappings.Count);
formatter.AddRequestHeaderMapping("name", "value", StringComparison.CurrentCulture, true, new MediaTypeHeaderValue("application/xml"));
IEnumerable<RequestHeaderMapping> mappings = formatter.MediaTypeMappings.OfType<RequestHeaderMapping>();
Assert.Equal(1, mappings.Count());
RequestHeaderMapping mapping = mappings.ElementAt(0);
Assert.Equal("name", mapping.HeaderName);
Assert.Equal("value", mapping.HeaderValue);
Assert.Equal(StringComparison.CurrentCulture, mapping.HeaderValueComparison);
Assert.Equal(true, mapping.IsValueSubstring);
Assert.Equal(new MediaTypeHeaderValue("application/xml"), mapping.MediaType);
}
[Fact]
[Trait("Description", "AddRequestHeaderMapping(MediaTypeFormatter, string, string, StringComparison, bool, string) throws for null 'this'.")]
public void AddRequestHeaderMapping1ThrowsWithNullThis()
{
MediaTypeFormatter formatter = null;
Assert.ThrowsArgumentNull(() => formatter.AddRequestHeaderMapping("name", "value", StringComparison.CurrentCulture, true, "application/xml"), "formatter");
}
[Fact]
[Trait("Description", "AddRequestHeaderMapping(MediaTypeFormatter, string, string, StringComparison, bool, string) adds formatter on 'this'.")]
public void AddRequestHeaderMapping1AddsSuccessfully()
{
MediaTypeFormatter formatter = new MockMediaTypeFormatter();
Assert.Equal(0, formatter.MediaTypeMappings.Count);
formatter.AddRequestHeaderMapping("name", "value", StringComparison.CurrentCulture, true, "application/xml");
IEnumerable<RequestHeaderMapping> mappings = formatter.MediaTypeMappings.OfType<RequestHeaderMapping>();
Assert.Equal(1, mappings.Count());
RequestHeaderMapping mapping = mappings.ElementAt(0);
Assert.Equal("name", mapping.HeaderName);
Assert.Equal("value", mapping.HeaderValue);
Assert.Equal(StringComparison.CurrentCulture, mapping.HeaderValueComparison);
Assert.Equal(true, mapping.IsValueSubstring);
Assert.Equal(new MediaTypeHeaderValue("application/xml"), mapping.MediaType);
}
}
}