示例#1
0
        public void ReceiveResponse(out ITransportHeaders responseHeaders, out Stream responseStream)
        {
            // transport signature
            if (!MatchPreamble())
            {
                BinaryWireProtocol.ThrowException(BinaryWireProtocol.StatusCode.InvalidMessageFormat);
            }
            // operation opcode
            byte operation = _reader.ReadByte();

            if (operation != BinaryWireProtocol.OperationType.Reply)
            {
                BinaryWireProtocol.ThrowException(BinaryWireProtocol.StatusCode.InvalidMessageFormat);
            }
            // content length
            int contentLength = _reader.ReadInt32();

            // response headers
            responseHeaders = ReadHeaders();
            // set special headers
            responseHeaders[BinaryWireProtocol.WellKnownHeaders.ConnectionId] = _connectionId;

            // create stream for response reading
            if (contentLength == -1)
            {
                responseStream = new ChunkedReadStream(_bufferedStream);
            }
            else
            {
                responseStream = new FixedReadStream(_bufferedStream, contentLength);
            }
        }
示例#2
0
 public void SetLength_ThrowsNotSupportedException()
 {
     using (var stream = new ChunkedReadStream(new[] { new byte[0] }))
     {
         Assert.Throws <NotSupportedException>(() => { stream.SetLength(0); });
     }
 }
示例#3
0
        public void Dispose_ThenReadThrowsObjectDisposedException()
        {
            var stream = new ChunkedReadStream(new[] { new byte[0] });

            stream.Dispose();
            Assert.Throws <ObjectDisposedException>(() => stream.Read(new byte[10], 0, 10));
        }
示例#4
0
 public void PositionSet_ThrowsNotSupportedException()
 {
     using (var stream = new ChunkedReadStream(new[] { new byte[0] }))
     {
         Assert.Throws <NotSupportedException>(() => { stream.Position = 0; });
     }
 }
示例#5
0
 public void LengthnGet_ThrowsNotSupportedException()
 {
     using (var stream = new ChunkedReadStream(new[] { new byte[0] }))
     {
         Assert.Throws <NotSupportedException>(() => { var temp = stream.Length; });
     }
 }
示例#6
0
 public void Seek_ThrowsNotSupportedException()
 {
     using (var stream = new ChunkedReadStream(new[] { new byte[0] }))
     {
         Assert.Throws <NotSupportedException>(() => stream.Seek(0, SeekOrigin.Begin));
     }
 }
示例#7
0
 public void CanSeek_ReturnsFalse()
 {
     using (var stream = new ChunkedReadStream(new[] { new byte[0] }))
     {
         Assert.IsFalse(stream.CanSeek);
     }
 }
示例#8
0
 public void Write_ThrowsNotSupportedException()
 {
     using (var stream = new ChunkedReadStream(new[] { new byte[0] }))
     {
         Assert.Throws <NotSupportedException>(() => stream.Write(new byte[1], 0, 1));
     }
 }
示例#9
0
 public void CanRead_ReturnsTrue()
 {
     using (var stream = new ChunkedReadStream(new[] { new byte[0] }))
     {
         Assert.IsTrue(stream.CanRead);
     }
 }
示例#10
0
 public void Flush_DoesNothing()
 {
     using (var stream = new ChunkedReadStream(new[] { new byte[0] }))
     {
         // Just to make sure this doesn't throw an exception or anything like that.
         stream.Flush();
     }
 }
示例#11
0
 public void Read_EmptyAndNullByteArrays()
 {
     using (var stream = new ChunkedReadStream(new[] { new byte[0], null, new byte[0] }))
     {
         var buffer = new byte[1];
         for (var i = 0; i < 10; i++)
         {
             Assert.AreEqual(0, stream.Read(buffer, 0, buffer.Length));
         }
     }
 }
示例#12
0
        public void Dispose_AlsoDisposesEnumerator()
        {
            var enumeratorMock = new Mock <IEnumerator <byte[]> >();

            enumeratorMock.Setup(x => x.Dispose()).Verifiable();

            var stream = new ChunkedReadStream(enumeratorMock.Object);

            stream.Dispose();

            enumeratorMock.Verify();
        }
示例#13
0
        public void Read_SimpleString_TwoChunks()
        {
            var expected = "Hello world!";
            var expectedBytes = Encoding.UTF8.GetBytes(expected);
            var chunkEnumerator = new[] { expectedBytes.Take(5).ToArray(), expectedBytes.Skip(5).ToArray() }.AsEnumerable().GetEnumerator();

            using (var stream = new ChunkedReadStream(chunkEnumerator))
            {
                byte[] actualBytes;
                using (var memoryStream = new MemoryStream())
                {
                    stream.CopyTo(memoryStream);
                    actualBytes = memoryStream.ToArray();
                }

                var actual = Encoding.UTF8.GetString(actualBytes);
                Assert.AreEqual(expected, actual);
            }
        }
示例#14
0
        public void ReceiveRequest(out ITransportHeaders requestHeaders, out Stream requestStream)
        {
            // transport signature
            if (!MatchPreamble())
            {
                BinaryWireProtocol.ThrowException(BinaryWireProtocol.StatusCode.InvalidMessageFormat);
            }
            // operation opcode
            byte operation = _reader.ReadByte();

            if (operation != BinaryWireProtocol.OperationType.Request && operation != BinaryWireProtocol.OperationType.OneWayRequest)
            {
                BinaryWireProtocol.ThrowException(BinaryWireProtocol.StatusCode.InvalidMessageFormat);
            }
            // content length
            int contentLength = _reader.ReadInt32();
            // request uri
            string requestUri = _reader.ReadString();

            if (!CheckRequestUri(requestUri))
            {
                BinaryWireProtocol.ThrowException(BinaryWireProtocol.StatusCode.InvalidRequestUri);
            }
            // request headers
            requestHeaders = ReadHeaders();
            // set special headers
            requestHeaders[BinaryWireProtocol.WellKnownHeaders.ConnectionId] = _connectionId;
            requestHeaders[BinaryWireProtocol.WellKnownHeaders.RequestUri]   = requestUri;

            // create stream for request reading
            if (contentLength == -1)
            {
                requestStream = new ChunkedReadStream(_bufferedStream);
            }
            else
            {
                requestStream = new FixedReadStream(_bufferedStream, contentLength);
            }

            // set client principal
            RemotingService.ClientPrincipal = _transport.ClientPrincipal;
        }
示例#15
0
        public void Read_ChunkSizesMismatchReadCount()
        {
            var chunks = new[]
            {
                new byte[] { 1, 2, 3 },
                new byte[] { 4 },
                new byte[] { 5, 6, 7 },
            };
            var chunkEnumerator = chunks.AsEnumerable().GetEnumerator();

            using (var stream = new ChunkedReadStream(chunkEnumerator))
            {
                byte[] tempBuffer = new byte[8];
                Assert.AreEqual(2, stream.Read(tempBuffer, offset: 0, count: 2));
                Assert.AreEqual(3, stream.Read(tempBuffer, offset: 2, count: 3));
                Assert.AreEqual(2, stream.Read(tempBuffer, offset: 5, count: 3));
                Assert.AreEqual(0, stream.Read(tempBuffer, offset: 7, count: 1));
                CollectionAssert.AreEqual(chunks.SelectMany(c => c), tempBuffer.Take(7));
            }
        }