public async Task DoesNotClose_UnderlyingStream_OnDisposingWriter() { // Arrange var stream = new TestMemoryStream(); var writer = new HttpResponseStreamWriter(stream, Encoding.UTF8); // Act await writer.WriteAsync("Hello"); writer.Close(); // Assert Assert.Equal(0, stream.CloseCallCount); }
public async Task DoesNotDispose_UnderlyingStream_OnDisposingWriter() { // Arrange var stream = new TestMemoryStream(); var writer = new HttpResponseStreamWriter(stream, Encoding.UTF8); // Act await writer.WriteAsync("Hello world"); writer.Dispose(); // Assert Assert.Equal(0, stream.DisposeCallCount); }
public async Task FlushesBuffer_OnFlushAsync(int byteLength) { // Arrange var stream = new TestMemoryStream(); var writer = new HttpResponseStreamWriter(stream, Encoding.UTF8); await writer.WriteAsync(new string('a', byteLength)); // Act await writer.FlushAsync(); // Assert Assert.Equal(1, stream.FlushAsyncCallCount); Assert.Equal(byteLength, stream.Length); }
public async Task DoesNotFlush_UnderlyingStream_OnClosingWriter() { // Arrange var stream = new TestMemoryStream(); var writer = new HttpResponseStreamWriter(stream, Encoding.UTF8); // Act await writer.WriteAsync("Hello"); writer.Close(); // Assert Assert.Equal(0, stream.FlushCallCount); Assert.Equal(0, stream.FlushAsyncCallCount); }
public async Task WriteCharArrayAsync_WritesToStream(int byteLength) { // Arrange var stream = new TestMemoryStream(); var writer = new HttpResponseStreamWriter(stream, Encoding.UTF8); // Act using (writer) { await writer.WriteAsync((new string('a', byteLength)).ToCharArray()); } // Assert Assert.Equal(byteLength, stream.Length); }
public async Task DoesNotWriteBOM() { // Arrange var memoryStream = new MemoryStream(); var encodingWithBOM = new UTF8Encoding(encoderShouldEmitUTF8Identifier: true); var writer = new HttpResponseStreamWriter(memoryStream, encodingWithBOM); var expectedData = new byte[] { 97, 98, 99, 100 }; // without BOM // Act using (writer) { await writer.WriteAsync("abcd"); } // Assert Assert.Equal(expectedData, memoryStream.ToArray()); }
public async Task FlushesBuffer_ButNotStream_OnFlushAsync(int byteLength) { // Arrange var stream = new TestMemoryStream(); var writer = new HttpResponseStreamWriter(stream, Encoding.UTF8); await writer.WriteAsync(new string('a', byteLength)); var expectedWriteCount = Math.Ceiling((double)byteLength / HttpResponseStreamWriter.DefaultBufferSize); // Act await writer.FlushAsync(); // Assert Assert.Equal(0, stream.FlushAsyncCallCount); Assert.Equal(expectedWriteCount, stream.WriteAsyncCallCount); Assert.Equal(byteLength, stream.Length); }
public async Task WritesData_InExpectedEncoding(string data, string encodingName) { // Arrange var encoding = Encoding.GetEncoding(encodingName); var expectedBytes = encoding.GetBytes(data); var stream = new MemoryStream(); var writer = new HttpResponseStreamWriter(stream, encoding); // Act using (writer) { await writer.WriteAsync(data); } // Assert Assert.Equal(expectedBytes, stream.ToArray()); }
public async Task WriteCharAsync_WritesToStream(int byteLength) { // Arrange var stream = new TestMemoryStream(); var writer = new HttpResponseStreamWriter(stream, Encoding.UTF8); // Act using (writer) { for (var i = 0; i < byteLength; i++) { await writer.WriteAsync('a'); } } // Assert Assert.Equal(byteLength, stream.Length); }
public async Task FlushesBuffer_OnClose(int byteLength) { // Arrange var stream = new TestMemoryStream(); var writer = new HttpResponseStreamWriter(stream, Encoding.UTF8); await writer.WriteAsync(new string('a', byteLength)); // Act #if DNX451 writer.Close(); #else writer.Dispose(); #endif // Assert Assert.Equal(0, stream.FlushCallCount); Assert.Equal(0, stream.FlushAsyncCallCount); Assert.Equal(byteLength, stream.Length); }
public async Task WritesData_OfDifferentLength_InExpectedEncoding( char character, int charCount, string encodingName) { // Arrange var encoding = Encoding.GetEncoding(encodingName); string data = new string(character, charCount); var expectedBytes = encoding.GetBytes(data); var stream = new MemoryStream(); var writer = new HttpResponseStreamWriter(stream, encoding); // Act using (writer) { await writer.WriteAsync(data); } // Assert Assert.Equal(expectedBytes, stream.ToArray()); }
public async Task FlushesBuffer_OnDispose(int byteLength) { // Arrange var stream = new TestMemoryStream(); var writer = new HttpResponseStreamWriter(stream, Encoding.UTF8); await writer.WriteAsync(new string('a', byteLength)); // Act writer.Dispose(); // Assert Assert.Equal(0, stream.FlushCallCount); Assert.Equal(0, stream.FlushAsyncCallCount); Assert.Equal(byteLength, stream.Length); }