forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReadOnlyStreamWithEncodingPreambleTest.cs
More file actions
75 lines (63 loc) · 2.92 KB
/
Copy pathReadOnlyStreamWithEncodingPreambleTest.cs
File metadata and controls
75 lines (63 loc) · 2.92 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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
using Microsoft.TestCommon;
namespace System.Net.Http.Internal
{
public class ReadOnlyStreamWithEncodingPreambleTest
{
[Theory]
[EncodingData]
public void StreamWithoutPreamble(Encoding encoding, bool includePreambleInInputStream)
{
using (MemoryStream inputStream = new MemoryStream())
{
// Arrange
string message = "Hello, world" + Environment.NewLine // English
+ "こんにちは、世界" + Environment.NewLine // Japanese
+ "مرحبا، العالم"; // Arabic
byte[] preamble = encoding.GetPreamble();
byte[] encodedMessage = encoding.GetBytes(message);
if (includePreambleInInputStream)
{
inputStream.Write(preamble, 0, preamble.Length);
}
inputStream.Write(encodedMessage, 0, encodedMessage.Length);
byte[] expectedBytes = new byte[preamble.Length + encodedMessage.Length];
preamble.CopyTo(expectedBytes, 0);
encodedMessage.CopyTo(expectedBytes, preamble.Length);
inputStream.Seek(0, SeekOrigin.Begin);
using (ReadOnlyStreamWithEncodingPreamble wrapperStream = new ReadOnlyStreamWithEncodingPreamble(inputStream, encoding))
{
// Act
int totalRead = 0;
byte[] readBuffer = new byte[expectedBytes.Length];
while (totalRead < readBuffer.Length)
{
int read = wrapperStream.Read(readBuffer, totalRead, readBuffer.Length - totalRead);
totalRead += read;
if (read == 0)
break;
}
// Assert
Assert.Equal(expectedBytes.Length, totalRead);
Assert.Equal(expectedBytes, readBuffer);
Assert.Equal(0, wrapperStream.Read(readBuffer, 0, 1)); // Make sure there are no stray bytes left in the stream
}
}
}
class EncodingDataAttribute : DataAttribute
{
public override IEnumerable<object[]> GetData(MethodInfo methodUnderTest, Type[] parameterTypes)
{
return new MatrixTheoryDataSet<Encoding, bool>(
new[] { Encoding.UTF7, Encoding.UTF8, Encoding.BigEndianUnicode, Encoding.Unicode, Encoding.UTF32, Encoding.ASCII },
new[] { false, true }
);
}
}
}
}