public async Task ReadAsync_ReadContentWithLargeBuffer_ParseMessageAndTrailers() { // Arrange var data = new byte[] { 0, 0, 0, 0, 1, 99, 128, 0, 0, 0, 0 }; var ms = new MemoryStream(data); var responseStream = new GrpcWebResponseStream(ms, new TestHttpHeaders()); // Act 1 var contentHeaderData = new byte[1024]; var read1 = await responseStream.ReadAsync(contentHeaderData); // Assert 1 Assert.AreEqual(5, read1); Assert.AreEqual(0, contentHeaderData[0]); Assert.AreEqual(0, contentHeaderData[1]); Assert.AreEqual(0, contentHeaderData[2]); Assert.AreEqual(0, contentHeaderData[3]); Assert.AreEqual(1, contentHeaderData[4]); // Act 2 var read2 = await responseStream.ReadAsync(contentHeaderData); // Assert 2 Assert.AreEqual(1, read2); // Act 2 var read3 = await responseStream.ReadAsync(contentHeaderData); // Assert 2 Assert.AreEqual(0, read3); }
public async Task ReadAsync_EmptyMessage_ParseMessageAndTrailers() { // Arrange var data = Convert.FromBase64String("AAAAAACAAAAAEA0KZ3JwYy1zdGF0dXM6IDA="); var trailingHeaders = new TestHttpHeaders(); var ms = new MemoryStream(data); var responseStream = new GrpcWebResponseStream(ms, trailingHeaders); // Act 1 var contentHeaderData = new byte[5]; var read1 = await responseStream.ReadAsync(contentHeaderData); // Assert 1 Assert.AreEqual(5, read1); Assert.AreEqual(0, contentHeaderData[0]); Assert.AreEqual(0, contentHeaderData[1]); Assert.AreEqual(0, contentHeaderData[2]); Assert.AreEqual(0, contentHeaderData[3]); Assert.AreEqual(0, contentHeaderData[4]); // Act 2 var read2 = await responseStream.ReadAsync(contentHeaderData); // Assert 2 Assert.AreEqual(0, read2); Assert.AreEqual(1, trailingHeaders.Count()); Assert.AreEqual("0", trailingHeaders.GetValues(GrpcProtocolConstants.StatusTrailer).Single()); }
public async Task ReadAsync_EmptyMessageAndTrailers_ParseMessageAndTrailers() { // Arrange var data = new byte[] { 0, 0, 0, 0, 0, 128, 0, 0, 0, 0 }; var trailingHeaders = new TestHttpHeaders(); var ms = new MemoryStream(data); var responseStream = new GrpcWebResponseStream(ms, trailingHeaders); // Act 1 var contentHeaderData = new byte[5]; var read1 = await responseStream.ReadAsync(contentHeaderData); // Assert 1 Assert.AreEqual(5, read1); Assert.AreEqual(0, contentHeaderData[0]); Assert.AreEqual(0, contentHeaderData[1]); Assert.AreEqual(0, contentHeaderData[2]); Assert.AreEqual(0, contentHeaderData[3]); Assert.AreEqual(0, contentHeaderData[4]); // Act 2 var read2 = await responseStream.ReadAsync(contentHeaderData); // Assert 2 Assert.AreEqual(0, read2); Assert.AreEqual(0, trailingHeaders.Count()); }
public async Task ReadAsync_HasContentAfterTrailers_Errors() { // Arrange var data = new byte[] { 0, 0, 0, 0, 0, 128, 0, 0, 0, 0, 1 }; var ms = new MemoryStream(data); var responseStream = new GrpcWebResponseStream(ms, new TestHttpHeaders()); // Act 1 var contentHeaderData = new byte[5]; var read1 = await responseStream.ReadAsync(contentHeaderData); // Assert 1 Assert.AreEqual(5, read1); Assert.AreEqual(0, contentHeaderData[0]); Assert.AreEqual(0, contentHeaderData[1]); Assert.AreEqual(0, contentHeaderData[2]); Assert.AreEqual(0, contentHeaderData[3]); Assert.AreEqual(0, contentHeaderData[4]); // Act 2 var ex = await ExceptionAssert.ThrowsAsync <InvalidOperationException>(() => responseStream.ReadAsync(contentHeaderData).AsTask()); // Assert 2 Assert.AreEqual("Unexpected data after trailers.", ex.Message); }