3
3
4
4
using System ;
5
5
using System . Collections . Generic ;
6
+ using System . Globalization ;
6
7
using System . Text ;
7
8
using Microsoft . AspNetCore . Http ;
8
9
using Microsoft . AspNetCore . Server . Kestrel ;
@@ -149,75 +150,88 @@ public void ThrowsWhenClearingHeadersAfterReadOnlyIsSet()
149
150
Assert . Throws < InvalidOperationException > ( ( ) => dictionary . Clear ( ) ) ;
150
151
}
151
152
152
- [ Fact ]
153
- public void ThrowsWhenAddingContentLengthWithNonNumericValue ( )
153
+ [ Theory ]
154
+ [ MemberData ( nameof ( BadContentLengths ) ) ]
155
+ public void ThrowsWhenAddingContentLengthWithNonNumericValue ( string contentLength )
154
156
{
155
157
var headers = new FrameResponseHeaders ( ) ;
156
158
var dictionary = ( IDictionary < string , StringValues > ) headers ;
157
159
158
- Assert . Throws < InvalidOperationException > ( ( ) => dictionary . Add ( "Content-Length" , new [ ] { "bad" } ) ) ;
160
+ var exception = Assert . Throws < InvalidOperationException > ( ( ) => dictionary . Add ( "Content-Length" , new [ ] { contentLength } ) ) ;
161
+ Assert . Equal ( $ "Invalid Content-Length: \" { contentLength } \" . Value must be a positive integral number.", exception . Message ) ;
159
162
}
160
163
161
- [ Fact ]
162
- public void ThrowsWhenSettingContentLengthToNonNumericValue ( )
164
+ [ Theory ]
165
+ [ MemberData ( nameof ( BadContentLengths ) ) ]
166
+ public void ThrowsWhenSettingContentLengthToNonNumericValue ( string contentLength )
163
167
{
164
168
var headers = new FrameResponseHeaders ( ) ;
165
169
var dictionary = ( IDictionary < string , StringValues > ) headers ;
166
170
167
- Assert . Throws < InvalidOperationException > ( ( ) => ( ( IHeaderDictionary ) headers ) [ "Content-Length" ] = "bad" ) ;
171
+ var exception = Assert . Throws < InvalidOperationException > ( ( ) => ( ( IHeaderDictionary ) headers ) [ "Content-Length" ] = contentLength ) ;
172
+ Assert . Equal ( $ "Invalid Content-Length: \" { contentLength } \" . Value must be a positive integral number.", exception . Message ) ;
168
173
}
169
174
170
- [ Fact ]
171
- public void ThrowsWhenSettingRawContentLengthToNonNumericValue ( )
175
+ [ Theory ]
176
+ [ MemberData ( nameof ( BadContentLengths ) ) ]
177
+ public void ThrowsWhenSettingRawContentLengthToNonNumericValue ( string contentLength )
172
178
{
173
179
var headers = new FrameResponseHeaders ( ) ;
174
180
175
- Assert . Throws < InvalidOperationException > ( ( ) => headers . SetRawContentLength ( "bad" , Encoding . ASCII . GetBytes ( "bad" ) ) ) ;
181
+ var exception = Assert . Throws < InvalidOperationException > ( ( ) => headers . SetRawContentLength ( contentLength , Encoding . ASCII . GetBytes ( contentLength ) ) ) ;
182
+ Assert . Equal ( $ "Invalid Content-Length: \" { contentLength } \" . Value must be a positive integral number.", exception . Message ) ;
176
183
}
177
184
178
- [ Fact ]
179
- public void ThrowsWhenAssigningHeaderContentLengthToNonNumericValue ( )
185
+ [ Theory ]
186
+ [ MemberData ( nameof ( BadContentLengths ) ) ]
187
+ public void ThrowsWhenAssigningHeaderContentLengthToNonNumericValue ( string contentLength )
180
188
{
181
189
var headers = new FrameResponseHeaders ( ) ;
182
- Assert . Throws < InvalidOperationException > ( ( ) => headers . HeaderContentLength = "bad" ) ;
190
+
191
+ var exception = Assert . Throws < InvalidOperationException > ( ( ) => headers . HeaderContentLength = contentLength ) ;
192
+ Assert . Equal ( $ "Invalid Content-Length: \" { contentLength } \" . Value must be a positive integral number.", exception . Message ) ;
183
193
}
184
194
185
- [ Fact ]
186
- public void ContentLengthValueCanBeReadAsLongAfterAddingHeader ( )
195
+ [ Theory ]
196
+ [ MemberData ( nameof ( GoodContentLengths ) ) ]
197
+ public void ContentLengthValueCanBeReadAsLongAfterAddingHeader ( string contentLength )
187
198
{
188
199
var headers = new FrameResponseHeaders ( ) ;
189
200
var dictionary = ( IDictionary < string , StringValues > ) headers ;
190
- dictionary . Add ( "Content-Length" , "42" ) ;
201
+ dictionary . Add ( "Content-Length" , contentLength ) ;
191
202
192
- Assert . Equal ( 42 , headers . HeaderContentLengthValue ) ;
203
+ Assert . Equal ( ParseLong ( contentLength ) , headers . HeaderContentLengthValue ) ;
193
204
}
194
205
195
- [ Fact ]
196
- public void ContentLengthValueCanBeReadAsLongAfterSettingHeader ( )
206
+ [ Theory ]
207
+ [ MemberData ( nameof ( GoodContentLengths ) ) ]
208
+ public void ContentLengthValueCanBeReadAsLongAfterSettingHeader ( string contentLength )
197
209
{
198
210
var headers = new FrameResponseHeaders ( ) ;
199
211
var dictionary = ( IDictionary < string , StringValues > ) headers ;
200
- dictionary [ "Content-Length" ] = "42" ;
212
+ dictionary [ "Content-Length" ] = contentLength ;
201
213
202
- Assert . Equal ( 42 , headers . HeaderContentLengthValue ) ;
214
+ Assert . Equal ( ParseLong ( contentLength ) , headers . HeaderContentLengthValue ) ;
203
215
}
204
216
205
- [ Fact ]
206
- public void ContentLengthValueCanBeReadAsLongAfterSettingRawHeader ( )
217
+ [ Theory ]
218
+ [ MemberData ( nameof ( GoodContentLengths ) ) ]
219
+ public void ContentLengthValueCanBeReadAsLongAfterSettingRawHeader ( string contentLength )
207
220
{
208
221
var headers = new FrameResponseHeaders ( ) ;
209
- headers . SetRawContentLength ( "42" , Encoding . ASCII . GetBytes ( "42" ) ) ;
222
+ headers . SetRawContentLength ( contentLength , Encoding . ASCII . GetBytes ( contentLength ) ) ;
210
223
211
- Assert . Equal ( 42 , headers . HeaderContentLengthValue ) ;
224
+ Assert . Equal ( ParseLong ( contentLength ) , headers . HeaderContentLengthValue ) ;
212
225
}
213
226
214
- [ Fact ]
215
- public void ContentLengthValueCanBeReadAsLongAfterAssigningHeader ( )
227
+ [ Theory ]
228
+ [ MemberData ( nameof ( GoodContentLengths ) ) ]
229
+ public void ContentLengthValueCanBeReadAsLongAfterAssigningHeader ( string contentLength )
216
230
{
217
231
var headers = new FrameResponseHeaders ( ) ;
218
- headers . HeaderContentLength = "42" ;
232
+ headers . HeaderContentLength = contentLength ;
219
233
220
- Assert . Equal ( 42 , headers . HeaderContentLengthValue ) ;
234
+ Assert . Equal ( ParseLong ( contentLength ) , headers . HeaderContentLengthValue ) ;
221
235
}
222
236
223
237
[ Fact ]
@@ -243,5 +257,33 @@ public void ContentLengthValueClearedWhenHeadersCleared()
243
257
244
258
Assert . Equal ( null , headers . HeaderContentLengthValue ) ;
245
259
}
260
+
261
+ private static long ParseLong ( string value )
262
+ {
263
+ return long . Parse ( value , NumberStyles . AllowLeadingWhite | NumberStyles . AllowTrailingWhite , CultureInfo . InvariantCulture ) ;
264
+ }
265
+
266
+ public static TheoryData < string > GoodContentLengths => new TheoryData < string >
267
+ {
268
+ "0" ,
269
+ "00" ,
270
+ "042" ,
271
+ "42" ,
272
+ long . MaxValue . ToString ( CultureInfo . InvariantCulture )
273
+ } ;
274
+
275
+ public static TheoryData < string > BadContentLengths => new TheoryData < string >
276
+ {
277
+ "" ,
278
+ " " ,
279
+ " 42" ,
280
+ "42 " ,
281
+ "bad" ,
282
+ "!" ,
283
+ "!42" ,
284
+ "42!" ,
285
+ "42,000" ,
286
+ "42.000" ,
287
+ } ;
246
288
}
247
- }
289
+ }
0 commit comments