1
+ // Copyright (c) .NET Foundation. All rights reserved.
2
+ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3
+
4
+ using System . Net ;
5
+ using System . Net . Http ;
6
+ using System . Text ;
7
+ using System . Threading . Tasks ;
8
+ using Microsoft . AspNetCore . Mvc . Formatters . Xml ;
9
+ using Microsoft . AspNetCore . Testing . xunit ;
10
+ using Xunit ;
11
+
12
+ namespace Microsoft . AspNetCore . Mvc . FunctionalTests
13
+ {
14
+ /// <summary>
15
+ /// These tests are for scenarios when <see cref="MvcOptions.RespectBrowserAcceptHeader"/> is <c>False</c>, which is the default.
16
+ /// </summary>
17
+ public class DoNotRespectBrowserAcceptHeaderTests : IClassFixture < MvcTestFixture < FormatterWebSite . Startup > >
18
+ {
19
+ public DoNotRespectBrowserAcceptHeaderTests ( MvcTestFixture < FormatterWebSite . Startup > fixture )
20
+ {
21
+ Client = fixture . Client ;
22
+ }
23
+
24
+ public HttpClient Client { get ; }
25
+
26
+ [ Theory ]
27
+ [ InlineData ( "application/xml,*/*;q=0.2" ) ]
28
+ [ InlineData ( "application/xml,*/*" ) ]
29
+ public async Task AllMediaRangeAcceptHeader_FirstFormatterInListWritesResponse ( string acceptHeader )
30
+ {
31
+ // Arrange
32
+ var request = RequestWithAccept ( "http://localhost/DoNotRespectBrowserAcceptHeader/EmployeeInfo" , acceptHeader ) ;
33
+
34
+ // Act
35
+ var response = await Client . SendAsync ( request ) ;
36
+
37
+ // Assert
38
+ Assert . Equal ( HttpStatusCode . OK , response . StatusCode ) ;
39
+ Assert . NotNull ( response . Content ) ;
40
+ Assert . NotNull ( response . Content . Headers . ContentType ) ;
41
+ Assert . Equal ( "application/json; charset=utf-8" , response . Content . Headers . ContentType . ToString ( ) ) ;
42
+ var responseData = await response . Content . ReadAsStringAsync ( ) ;
43
+ Assert . Equal ( "{\" id\" :10,\" name\" :\" John\" }" , responseData ) ;
44
+ }
45
+
46
+ [ ConditionalTheory ]
47
+ // Mono issue - https://github.com/aspnet/External/issues/18
48
+ [ FrameworkSkipCondition ( RuntimeFrameworks . Mono ) ]
49
+ [ InlineData ( "application/xml,*/*;q=0.2" ) ]
50
+ [ InlineData ( "application/xml,*/*" ) ]
51
+ public async Task AllMediaRangeAcceptHeader_ProducesAttributeIsHonored ( string acceptHeader )
52
+ {
53
+ // Arrange
54
+ var request = RequestWithAccept (
55
+ "http://localhost/DoNotRespectBrowserAcceptHeader/EmployeeInfoWithProduces" ,
56
+ acceptHeader ) ;
57
+ var expectedResponseData =
58
+ "<DoNotRespectBrowserAcceptHeaderController.Employee xmlns:i=\" http://www.w3.org/2001/XMLSchema-instance\" " +
59
+ " xmlns=\" http://schemas.datacontract.org/2004/07/FormatterWebSite.Controllers\" ><Id>20</Id><Name>Mike" +
60
+ "</Name></DoNotRespectBrowserAcceptHeaderController.Employee>" ;
61
+
62
+ // Act
63
+ var response = await Client . SendAsync ( request ) ;
64
+
65
+ // Assert
66
+ Assert . Equal ( HttpStatusCode . OK , response . StatusCode ) ;
67
+ Assert . NotNull ( response . Content ) ;
68
+ Assert . NotNull ( response . Content . Headers . ContentType ) ;
69
+ Assert . Equal ( "application/xml; charset=utf-8" , response . Content . Headers . ContentType . ToString ( ) ) ;
70
+ var responseData = await response . Content . ReadAsStringAsync ( ) ;
71
+ XmlAssert . Equal ( expectedResponseData , responseData ) ;
72
+ }
73
+
74
+ [ ConditionalTheory ]
75
+ // Mono issue - https://github.com/aspnet/External/issues/18
76
+ [ FrameworkSkipCondition ( RuntimeFrameworks . Mono ) ]
77
+ [ InlineData ( "application/xml,*/*;q=0.2" ) ]
78
+ [ InlineData ( "application/xml,*/*" ) ]
79
+ public async Task AllMediaRangeAcceptHeader_WithContentTypeHeader_ContentTypeIsIgnored ( string acceptHeader )
80
+ {
81
+ // Arrange
82
+ var requestData =
83
+ "<DoNotRespectBrowserAcceptHeaderController.Employee xmlns:i=\" http://www.w3.org/2001/XMLSchema-instance\" " +
84
+ " xmlns=\" http://schemas.datacontract.org/2004/07/FormatterWebSite.Controllers\" ><Id>35</Id><Name>Jimmy" +
85
+ "</Name></DoNotRespectBrowserAcceptHeaderController.Employee>" ;
86
+ var expectedResponseData = @"{""id"":35,""name"":""Jimmy""}" ;
87
+ var request = RequestWithAccept ( "http://localhost/DoNotRespectBrowserAcceptHeader/CreateEmployee" , acceptHeader ) ;
88
+ request . Content = new StringContent ( requestData , Encoding . UTF8 , "application/xml" ) ;
89
+ request . Method = HttpMethod . Post ;
90
+
91
+ // Act
92
+ var response = await Client . SendAsync ( request ) ;
93
+
94
+ // Assert
95
+ Assert . Equal ( HttpStatusCode . OK , response . StatusCode ) ;
96
+ Assert . NotNull ( response . Content ) ;
97
+ Assert . NotNull ( response . Content . Headers . ContentType ) ;
98
+
99
+ // Site uses default output formatter (ignores Accept header) because that header contained a wildcard match.
100
+ Assert . Equal ( "application/json; charset=utf-8" , response . Content . Headers . ContentType . ToString ( ) ) ;
101
+
102
+ var responseData = await response . Content . ReadAsStringAsync ( ) ;
103
+ Assert . Equal ( expectedResponseData , responseData ) ;
104
+ }
105
+
106
+ [ ConditionalTheory ]
107
+ // Mono issue - https://github.com/aspnet/External/issues/18
108
+ [ FrameworkSkipCondition ( RuntimeFrameworks . Mono ) ]
109
+ [ InlineData ( "application/xml,application/json;q=0.2" ) ]
110
+ [ InlineData ( "application/xml,application/json" ) ]
111
+ public async Task AllMediaRangeAcceptHeader_WithExactMatch_ReturnsExpectedContent ( string acceptHeader )
112
+ {
113
+ // Arrange
114
+ var requestData =
115
+ "<DoNotRespectBrowserAcceptHeaderController.Employee xmlns:i=\" http://www.w3.org/2001/XMLSchema-instance\" " +
116
+ " xmlns=\" http://schemas.datacontract.org/2004/07/FormatterWebSite.Controllers\" ><Id>35</Id><Name>Jimmy" +
117
+ "</Name></DoNotRespectBrowserAcceptHeaderController.Employee>" ;
118
+ var request = RequestWithAccept ( "http://localhost/DoNotRespectBrowserAcceptHeader/CreateEmployee" , acceptHeader ) ;
119
+ request . Content = new StringContent ( requestData , Encoding . UTF8 , "application/xml" ) ;
120
+ request . Method = HttpMethod . Post ;
121
+
122
+ // Act
123
+ var response = await Client . SendAsync ( request ) ;
124
+
125
+ // Assert
126
+ Assert . Equal ( HttpStatusCode . OK , response . StatusCode ) ;
127
+ Assert . NotNull ( response . Content ) ;
128
+ Assert . NotNull ( response . Content . Headers . ContentType ) ;
129
+ Assert . Equal ( "application/xml; charset=utf-8" , response . Content . Headers . ContentType . ToString ( ) ) ;
130
+ var responseData = await response . Content . ReadAsStringAsync ( ) ;
131
+ Assert . Equal ( requestData , responseData ) ;
132
+ }
133
+
134
+ private static HttpRequestMessage RequestWithAccept ( string url , string accept )
135
+ {
136
+ var request = new HttpRequestMessage ( HttpMethod . Get , url ) ;
137
+ request . Headers . Add ( "Accept" , accept ) ;
138
+
139
+ return request ;
140
+ }
141
+ }
142
+ }
0 commit comments