@@ -42,6 +42,61 @@ public void GetHeaderValueOrDefault_ReturnsExpectedResult()
42
42
Assert . Equal ( "One" , value ) ;
43
43
}
44
44
45
+ [ Theory ]
46
+ [ InlineData ( "\" UTF-8\" " ) ]
47
+ [ InlineData ( "utf-8" ) ]
48
+ [ InlineData ( "'Utf-8'" ) ]
49
+ [ InlineData ( "'UTF-8'" ) ]
50
+ public void NormalizeContentTypeCharsetHeader_ContentTypeHeader_ValidCharSet ( string charSet )
51
+ {
52
+ HttpRequestMessage request = new HttpRequestMessage ( HttpMethod . Post , "http://functions/{test:alpha}/test?name=Amy" ) ;
53
+ string input = "{ name: 'body1', nestedObject: { name: 'body2' } }" ;
54
+ request . Content = new StringContent ( input ) ;
55
+ request . Content . Headers . ContentType = new MediaTypeHeaderValue ( "application/json" ) ;
56
+ request . Content . Headers . ContentType . Parameters . Add ( new NameValueHeaderValue ( "charset" , charSet ) ) ;
57
+ request . NormalizeContentTypeCharsetHeader ( ) ;
58
+
59
+ string expectedHeader = GetExpectedContenTypeHeader ( charSet ) ;
60
+ string actualHeader = request . Content . Headers . ContentType . ToString ( ) ;
61
+ Assert . Equal ( expectedHeader , actualHeader ) ;
62
+ }
63
+
64
+ [ Theory ]
65
+ [ InlineData ( "" ) ]
66
+ [ InlineData ( null ) ]
67
+ public void NormalizeContentTypeCharsetHeader_ContentTypeHeader_Set_CharSet_Empty ( string charSet )
68
+ {
69
+ HttpRequestMessage request = new HttpRequestMessage ( HttpMethod . Post , "http://functions/{test:alpha}/test?name=Amy" ) ;
70
+ string input = "{ name: 'body1', nestedObject: { name: 'body2' } }" ;
71
+ request . Content = new StringContent ( input ) ;
72
+ request . Content . Headers . ContentType = new MediaTypeHeaderValue ( "application/json" ) ;
73
+ request . Content . Headers . ContentType . Parameters . Add ( new NameValueHeaderValue ( "charset" , charSet ) ) ;
74
+ request . NormalizeContentTypeCharsetHeader ( ) ;
75
+
76
+ string expectedHeader = GetExpectedContenTypeHeader ( charSet ) ;
77
+ string actualHeader = request . Content . Headers . ContentType . ToString ( ) ;
78
+ Assert . Equal ( expectedHeader , actualHeader ) ;
79
+ }
80
+
81
+ [ Fact ]
82
+ public void NormalizeContentTypeCharsetHeader_ContentTypeHeader_Default ( )
83
+ {
84
+ HttpRequestMessage request = new HttpRequestMessage ( HttpMethod . Post , "http://functions/{test:alpha}/test?name=Amy" ) ;
85
+ request . Content = new StringContent ( "test" ) ;
86
+ request . NormalizeContentTypeCharsetHeader ( ) ;
87
+ string expectedHeader = "text/plain; charset=utf-8" ;
88
+ string actualHeader = request . Content . Headers . ContentType . ToString ( ) ;
89
+ Assert . Equal ( expectedHeader , actualHeader ) ;
90
+ }
91
+
92
+ [ Fact ]
93
+ public void NormalizeContentTypeCharsetHeader_NoHeaders ( )
94
+ {
95
+ HttpRequestMessage request = new HttpRequestMessage ( HttpMethod . Post , "http://functions/{test:alpha}/test?name=Amy" ) ;
96
+ request . NormalizeContentTypeCharsetHeader ( ) ;
97
+ Assert . Null ( request . Content ) ;
98
+ }
99
+
45
100
[ Fact ]
46
101
public void IsAntaresInternalRequest_ReturnsExpectedResult ( )
47
102
{
@@ -171,5 +226,15 @@ public void HasAuthorizationLevel_ReturnsExpectedValue()
171
226
request . SetProperty ( ScriptConstants . AzureFunctionsHttpRequestAuthorizationDisabledKey , true ) ;
172
227
Assert . True ( request . HasAuthorizationLevel ( AuthorizationLevel . Admin ) ) ;
173
228
}
229
+
230
+ private static string GetExpectedContenTypeHeader ( string charSet )
231
+ {
232
+ if ( string . IsNullOrEmpty ( charSet ) )
233
+ {
234
+ return "application/json; charset" ;
235
+ }
236
+ char [ ] trimQuotes = new [ ] { '\' ' , '\" ' } ;
237
+ return "application/json; charset=" + charSet . Trim ( trimQuotes ) ;
238
+ }
174
239
}
175
240
}
0 commit comments