public void ToArrayWithSomeFullSegmentsWorks()
        {
            var inputSize = (MinimumSegmentSize * 2) + 1;
            var input     = Enumerable.Range(0, inputSize).Select(i => (byte)i).ToArray();

            using (var bufferWriter = new MemoryBufferWriter(MinimumSegmentSize))
            {
                bufferWriter.Write(input, 0, input.Length);
                Assert.Equal(input.Length, bufferWriter.Length);

                var data = bufferWriter.ToArray();
                Assert.Equal(input, data);
            }
        }
        public void WriteByteWorksIfFirstByteInNewSegment()
        {
            var inputSize = MinimumSegmentSize;
            var input     = Enumerable.Range(0, inputSize).Select(i => (byte)i).ToArray();

            using (var bufferWriter = new MemoryBufferWriter(MinimumSegmentSize))
            {
                bufferWriter.Write(input, 0, input.Length);
                Assert.Equal(16, bufferWriter.Length);
                bufferWriter.WriteByte(16);
                Assert.Equal(17, bufferWriter.Length);

                var data = bufferWriter.ToArray();
                Assert.Equal(input, data.Take(16));
                Assert.Equal(16, data[16]);
            }
        }
        public void WriteSpanWorksAtNonZeroOffset()
        {
            using (var bufferWriter = new MemoryBufferWriter())
            {
                bufferWriter.WriteByte(1);
                bufferWriter.Write(new byte[] { 2, 3, 4 }.AsSpan());

                Assert.Equal(4, bufferWriter.Length);

                var data = bufferWriter.ToArray();
                Assert.Equal(4, data.Length);
                Assert.Equal(1, data[0]);
                Assert.Equal(2, data[1]);
                Assert.Equal(3, data[2]);
                Assert.Equal(4, data[3]);
            }
        }
Esempio n. 4
0
        public void WriteNegotiateResponseWithNullTransferFormats()
        {
            using (MemoryBufferWriter writer = new MemoryBufferWriter())
            {
                NegotiateProtocol.WriteResponse(new NegotiationResponse
                {
                    AvailableTransports = new List <AvailableTransport>
                    {
                        new AvailableTransport()
                    }
                }, writer);

                string json = Encoding.UTF8.GetString(writer.ToArray());

                Assert.Equal("{\"negotiateVersion\":0,\"availableTransports\":[{\"transport\":null,\"transferFormats\":[]}]}", json);
            }
        }
        public void CopyToWithExactlyFullSegmentsWorks()
        {
            var inputSize = MinimumSegmentSize * 2;
            var input     = Enumerable.Range(0, inputSize).Select(i => (byte)i).ToArray();

            using (var bufferWriter = new MemoryBufferWriter(MinimumSegmentSize))
            {
                bufferWriter.Write(input, 0, input.Length);
                Assert.Equal(input.Length, bufferWriter.Length);

                using (var destination = new MemoryBufferWriter())
                {
                    bufferWriter.CopyTo(destination);
                    var data = destination.ToArray();
                    Assert.Equal(input, data);
                }
            }
        }
        public void WriteByteWorksIfSegmentHasSpace()
        {
            var input = new byte[] { 11, 12, 13 };

            using (var bufferWriter = new MemoryBufferWriter())
            {
                bufferWriter.Write(input, 0, input.Length);
                bufferWriter.WriteByte(14);

                Assert.Equal(4, bufferWriter.Length);

                var data = bufferWriter.ToArray();
                Assert.Equal(4, data.Length);
                Assert.Equal(11, data[0]);
                Assert.Equal(12, data[1]);
                Assert.Equal(13, data[2]);
                Assert.Equal(14, data[3]);
            }
        }
Esempio n. 7
0
        public void GlobalSetup()
        {
            using (var memoryBufferWriter = new MemoryBufferWriter())
            {
                HandshakeProtocol.WriteRequestMessage(new HandshakeRequestMessage("json", 1), memoryBufferWriter);
                _handshakeRequestResult = new ReadResult(new ReadOnlySequence <byte>(memoryBufferWriter.ToArray()), false, false);
            }

            _pipe = new TestDuplexPipe();

            var connection = new DefaultConnectionContext(Guid.NewGuid().ToString(), _pipe, _pipe);

            _hubConnectionContext = new HubConnectionContext(connection, Timeout.InfiniteTimeSpan, NullLoggerFactory.Instance);

            _successHubProtocolResolver = new TestHubProtocolResolver(new JsonHubProtocol());
            _failureHubProtocolResolver = new TestHubProtocolResolver(null);
            _userIdProvider             = new TestUserIdProvider();
            _supportedProtocols         = new List <string> {
                "json"
            };
        }
Esempio n. 8
0
        public void Setup()
        {
            var buffer = new byte[MessageLength];

            Random.NextBytes(buffer);
            using (var writer = new MemoryBufferWriter())
            {
                BinaryMessageFormatter.WriteLengthPrefix(buffer.Length, writer);
                writer.Write(buffer);
                _binaryInput = writer.ToArray();
            }

            buffer = new byte[MessageLength];
            Random.NextBytes(buffer);
            using (var writer = new MemoryBufferWriter())
            {
                writer.Write(buffer);
                TextMessageFormatter.WriteRecordSeparator(writer);

                _textInput = writer.ToArray();
            }
        }
Esempio n. 9
0
        public void WriteMessage(HubMessage message, bool camelCase, NullValueHandling nullValueHandling, string expectedOutput)
        {
            expectedOutput = Frame(expectedOutput);

            var protocolOptions = new JsonHubProtocolOptions
            {
                PayloadSerializerSettings = new JsonSerializerSettings()
                {
                    NullValueHandling = nullValueHandling,
                    ContractResolver  = camelCase ? new CamelCasePropertyNamesContractResolver() : new DefaultContractResolver()
                }
            };

            var protocol = new JsonHubProtocol(Options.Create(protocolOptions));

            using (var writer = new MemoryBufferWriter())
            {
                protocol.WriteMessage(message, writer);
                var json = Encoding.UTF8.GetString(writer.ToArray());

                Assert.Equal(expectedOutput, json);
            }
        }
Esempio n. 10
0
    public static byte[] WriteCompletionMessage(MemoryBufferWriter writer, string protocolName)
    {
        // Written as a MessagePack 'arr' containing at least these items:
        // * A 'str': The name of the HubProtocol used for the serialization of the Completion Message
        // * [A serialized Completion Message which is a 'bin']
        // Any additional items are discarded.

        var completionMessage = writer.DetachAndReset();
        var msgPackWriter     = new MessagePackWriter(writer);

        msgPackWriter.WriteArrayHeader(2);
        msgPackWriter.Write(protocolName);

        msgPackWriter.WriteBinHeader(completionMessage.ByteLength);
        foreach (var segment in completionMessage.Segments)
        {
            msgPackWriter.WriteRaw(segment.Span);
        }
        completionMessage.Dispose();

        msgPackWriter.Flush();
        return(writer.ToArray());
    }
Esempio n. 11
0
        public void GlobalSetup()
        {
            using (var writer = new MemoryBufferWriter())
            {
                HandshakeProtocol.WriteResponseMessage(HandshakeResponseMessage.Empty, writer);
                var handshakeResponseResult = new ReadResult(new ReadOnlySequence <byte>(writer.ToArray()), false, false);

                _pipe = new TestDuplexPipe();
                _pipe.AddReadResult(new ValueTask <ReadResult>(handshakeResponseResult));
            }

            _tcs = new TaskCompletionSource <ReadResult>();
            _pipe.AddReadResult(new ValueTask <ReadResult>(_tcs.Task));

            var connection = new TestConnection();

            // prevents keep alive time being activated
            connection.Features.Set <IConnectionInherentKeepAliveFeature>(new TestConnectionInherentKeepAliveFeature());
            connection.Transport = _pipe;

            var protocol = Protocol == "json" ? (IHubProtocol) new JsonHubProtocol() : new MessagePackHubProtocol();

            _hubConnection = new HubConnection(() => connection, protocol, new NullLoggerFactory());
            _hubConnection.StartAsync().GetAwaiter().GetResult();

            _arguments = new object[ArgumentCount];
            for (int i = 0; i < _arguments.Length; i++)
            {
                _arguments[i] = "Hello world!";
            }
        }
Esempio n. 12
0
        public void GlobalSetup()
        {
            using (var writer = new MemoryBufferWriter())
            {
                HandshakeProtocol.WriteResponseMessage(HandshakeResponseMessage.Empty, writer);
                _handshakeResponseResult = new ReadResult(new ReadOnlySequence <byte>(writer.ToArray()), false, false);
            }

            _pipe = new TestDuplexPipe();

            var connection = new TestConnection();

            // prevents keep alive time being activated
            connection.Features.Set <IConnectionInherentKeepAliveFeature>(new TestConnectionInherentKeepAliveFeature());
            connection.Transport = _pipe;

            _hubConnection = new HubConnection(() => connection, new JsonHubProtocol(), new NullLoggerFactory());
        }