This repository was archived by the owner on May 7, 2018. It is now read-only.
forked from shana/dotnet-httpclient35
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStreamContentTest.cs
More file actions
446 lines (380 loc) · 10.8 KB
/
Copy pathStreamContentTest.cs
File metadata and controls
446 lines (380 loc) · 10.8 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
//
// StreamContentTest.cs
//
// Authors:
// Marek Safar <marek.safar@gmail.com>
//
// Copyright (C) 2012 Xamarin Inc (http://www.xamarin.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections;
using System.Collections.Generic;
using DotNetHttp35;
using DotNetHttp35.Headers;
using System.IO;
using System.Net;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace MonoTests.System.Net.Http
{
extern alias tpl;
using tpl::System.Threading.Tasks;
using AggregateException = tpl::System.AggregateException;
[TestClass]
public class StreamContentTest
{
class StreamContentMock : StreamContent
{
public Func<long> OnTryComputeLength;
public Action OnSerializeToStreamAsync;
public StreamContentMock (Stream stream)
: base (stream)
{
}
protected override bool TryComputeLength (out long length)
{
if (OnTryComputeLength != null) {
length = OnTryComputeLength ();
return true;
}
return base.TryComputeLength (out length);
}
protected override Task SerializeToStreamAsync (Stream stream, TransportContext context)
{
if (OnSerializeToStreamAsync != null)
OnSerializeToStreamAsync ();
return base.SerializeToStreamAsync (stream, context);
}
}
class ExceptionStream : MemoryStream
{
public ExceptionStream ()
{
base.WriteByte (10);
base.Seek (0, SeekOrigin.Begin);
}
public override int Read (byte[] buffer, int offset, int count)
{
throw new ApplicationException ("Read");
}
public override byte[] GetBuffer ()
{
throw new ApplicationException ("GetBuffer");
}
}
class CannotSeekStream : MemoryStream
{
public CannotSeekStream ()
: base (new byte [11])
{
}
public override bool CanSeek {
get {
return false;
}
}
}
[TestMethod]
public void Ctor_Invalid ()
{
try {
new StreamContent (null);
Assert.Fail ("#1");
} catch (ArgumentNullException) {
}
try {
new StreamContent (new MemoryStream (), 0);
Assert.Fail ("#2");
} catch (ArgumentOutOfRangeException) {
}
}
[TestMethod]
public void Ctor ()
{
var ms = new MemoryStream ();
ms.WriteByte (44);
using (var m = new StreamContent (ms)) {
}
}
[TestMethod]
public void CopyToAsync_Invalid ()
{
var m = new MemoryStream ();
var sc = new StreamContent (new MemoryStream ());
try {
sc.CopyToAsync (null);
Assert.Fail ("#1");
} catch (ArgumentNullException) {
}
//
// For some reason does not work on .net
//
/*
sc = new StreamContent (new ExceptionStream ());
try {
sc.CopyToAsync (m).Wait ();
Assert.Fail ("#2");
} catch (AggregateException) {
}
*/
}
[TestMethod]
/*
* The .NET runtime hits the "#9" assertion.
* The test succeeds with Mono.
*/
[TestCategory ("NotWorking")]
public void CopyToAsync ()
{
var ms = new MemoryStream ();
ms.WriteByte (4);
ms.WriteByte (2);
ms.Seek (0, SeekOrigin.Begin);
var sc = new StreamContent (ms);
var dest = new MemoryStream ();
var task = sc.CopyToAsync (dest);
task.Wait ();
Assert.AreEqual (2, dest.Length, "#1");
bool hit = false;
dest = new MemoryStream ();
var scm = new StreamContentMock (new ExceptionStream ());
scm.OnSerializeToStreamAsync = () => { hit = true; };
task = scm.CopyToAsync (dest);
try {
task.Wait ();
Assert.Fail ("#9");
} catch (AggregateException) {
}
Assert.IsTrue (hit, "#10");
}
[TestMethod]
public void CopyToAsync_ClosedInput ()
{
var stream = new MemoryStream (new byte[] { 1 });
var content = new StreamContent (stream);
Assert.IsTrue (content.LoadIntoBufferAsync ().Wait (3000), "#1");
stream.Close ();
var stream_out = new MemoryStream (10);
Assert.IsTrue (content.CopyToAsync (stream_out).Wait (3000), "#2");
}
[TestMethod]
public void Headers ()
{
var ms = new MemoryStream ();
ms.WriteByte (4);
ms.WriteByte (2);
ms.Seek (0, SeekOrigin.Begin);
var sc = new StreamContent (ms);
var headers = sc.Headers;
Assert.AreEqual (2, headers.ContentLength, "#1");
headers.ContentLength = 400;
Assert.AreEqual (400, headers.ContentLength, "#1a");
headers.ContentLength = null;
var scm = new StreamContentMock (MemoryStream.Null);
scm.OnTryComputeLength = () => 330;
Assert.AreEqual (330, scm.Headers.ContentLength, "#2");
headers.Allow.Add ("a1");
headers.ContentEncoding.Add ("ce1");
headers.ContentLanguage.Add ("cl1");
headers.ContentLength = 23;
headers.ContentLocation = new Uri ("http://xamarin.com");
headers.ContentMD5 = new byte[] { 3, 5 };
headers.ContentRange = new ContentRangeHeaderValue (88, 444);
headers.ContentType = new MediaTypeHeaderValue ("multipart/*");
headers.Expires = new DateTimeOffset (DateTime.Today);
headers.LastModified = new DateTimeOffset (DateTime.Today);
headers.Add ("allow", "a2");
headers.Add ("content-encoding", "ce3");
headers.Add ("content-language", "cl2");
try {
headers.Add ("content-length", "444");
Assert.Fail ("content-length");
} catch (FormatException) {
}
try {
headers.Add ("content-location", "cl2");
Assert.Fail ("content-location");
} catch (FormatException) {
}
try {
headers.Add ("content-MD5", "cmd5");
Assert.Fail ("content-MD5");
} catch (FormatException) {
}
try {
headers.Add ("content-range", "133");
Assert.Fail ("content-range");
} catch (FormatException) {
}
try {
headers.Add ("content-type", "ctype");
Assert.Fail ("content-type");
} catch (FormatException) {
}
try {
headers.Add ("expires", "ctype");
Assert.Fail ("expires");
} catch (FormatException) {
}
try {
headers.Add ("last-modified", "lmo");
Assert.Fail ("last-modified");
} catch (FormatException) {
}
Assert.IsTrue (headers.Allow.SequenceEqual (
new[] {
"a1",
"a2"
}
));
Assert.IsTrue (headers.ContentEncoding.SequenceEqual (
new[] {
"ce1",
"ce3"
}
));
Assert.IsTrue (headers.ContentLanguage.SequenceEqual (
new[] {
"cl1",
"cl2"
}
));
Assert.AreEqual (23, headers.ContentLength);
Assert.AreEqual (new Uri ("http://xamarin.com"), headers.ContentLocation);
CollectionAssert.AreEqual (new byte[] { 3, 5 }, headers.ContentMD5);
Assert.AreEqual (new ContentRangeHeaderValue (88, 444), headers.ContentRange);
Assert.AreEqual (new MediaTypeHeaderValue ("multipart/*"), headers.ContentType);
Assert.AreEqual (new DateTimeOffset (DateTime.Today), headers.Expires);
Assert.AreEqual (new DateTimeOffset (DateTime.Today), headers.LastModified);
}
[TestMethod]
public void Headers_ToString ()
{
var sc = new StreamContent (new MemoryStream ());
var headers = sc.Headers;
headers.ContentMD5 = new byte[] { 3, 5 };
Assert.AreEqual ("Content-MD5: AwU=\r\n", headers.ToString (), "#1");
}
[TestMethod]
public void Headers_Invalid ()
{
var sc = new StreamContent (MemoryStream.Null);
var h = sc.Headers;
try {
h.Add ("Age", "");
Assert.Fail ("#1");
} catch (InvalidOperationException) {
}
}
[TestMethod]
public void Headers_Multi ()
{
var sc = new StreamContent (MemoryStream.Null);
var headers = sc.Headers;
headers.Add ("Allow", "");
headers.Add ("Allow", "a , b, c");
Assert.AreEqual (3, headers.Allow.Count, "#1a");
Assert.IsTrue (headers.Allow.SequenceEqual (
new[] { "a", "b", "c" }
), "#1b");
}
[TestMethod]
public void LoadIntoBuffer ()
{
var ms = new MemoryStream ();
ms.WriteByte (4);
ms.Seek (0, SeekOrigin.Begin);
var sc = new StreamContent (ms);
Assert.IsTrue (sc.LoadIntoBufferAsync (400).Wait (200));
}
[TestMethod]
public void LoadIntoBuffer_BufferOverflow ()
{
var ms = new MemoryStream ();
ms.Write (new byte[10000], 0, 10000);
ms.Seek (0, SeekOrigin.Begin);
var sc = new StreamContent (ms);
try {
Assert.IsTrue (sc.LoadIntoBufferAsync (50).Wait (200));
Assert.Fail ("#1");
} catch (AggregateException e) {
Assert.IsTrue (e.InnerException is HttpRequestException, "#2");
}
}
[TestMethod]
public void ReadAsByteArrayAsync ()
{
var ms = new MemoryStream ();
ms.WriteByte (4);
ms.WriteByte (55);
var sc = new StreamContent (ms);
var res = sc.ReadAsByteArrayAsync ().Result;
Assert.AreEqual (0, res.Length, "#1");
ms.Seek (0, SeekOrigin.Begin);
sc = new StreamContent (ms);
res = sc.ReadAsByteArrayAsync ().Result;
Assert.AreEqual (2, res.Length, "#10");
Assert.AreEqual (55, res[1], "#11");
}
[TestMethod]
public void ReadAsString ()
{
var ms = new MemoryStream ();
ms.WriteByte (77);
ms.WriteByte (55);
ms.Seek (0, SeekOrigin.Begin);
var sc = new StreamContent (ms);
var res = sc.ReadAsStringAsync ().Result;
Assert.AreEqual ("M7", res, "#1");
}
[TestMethod]
public void ReadAsStream ()
{
var ms = new MemoryStream ();
ms.WriteByte (77);
ms.WriteByte (55);
ms.Seek (0, SeekOrigin.Begin);
var sc = new StreamContent (ms);
var res = sc.ReadAsStreamAsync ().Result;
Assert.AreEqual (77, res.ReadByte (), "#1");
}
[TestMethod]
public void ReadAsStreamAsync_ClosedInput ()
{
var stream = new MemoryStream (new byte[] { 1 });
var content = new StreamContent (stream);
Assert.IsTrue (content.LoadIntoBufferAsync ().Wait (3000), "#1");
stream.Close ();
var stream_read = content.ReadAsStreamAsync ().Result;
Assert.IsTrue (stream_read.CanSeek, "#2");
Assert.AreEqual (0, stream_read.Position, "#3");
Assert.AreEqual (1, stream_read.Length, "#4");
}
[TestMethod]
public void ContentLengthAfterLoad ()
{
var sc = new StreamContent (new CannotSeekStream ());
Assert.IsTrue (sc.LoadIntoBufferAsync ().Wait (3000), "#1");
Assert.AreEqual (11, sc.Headers.ContentLength, "#2");
}
}
}