public async Task Should_be_able_to_process_two_messages_from_the_same_receive() { var serializer = new JsonTransportSerializer(); var encoder = new SimpleProtocolEncoder(serializer); var msg1 = new TransportMessage("Test1"); var msg2 = new TransportMessage("Test2"); var sender = Substitute.For <ISendState>(); await encoder.EncodeMessage(sender, msg1); await encoder.EncodeMessage(sender, msg2); var buffer = new byte[65535]; var bufferOffset = 0; foreach (var call in sender.ReceivedCalls()) { var buf = call.GetArguments()[0].As <byte[]>(); var offset = call.GetArguments()[1].As <int>(); var len = call.GetArguments()[2].As <int>(); Buffer.BlockCopy(buf, offset, buffer, bufferOffset, len); bufferOffset += len; } var state = new TestReceiveState(); state.AddBuffer(buffer, 0, bufferOffset); state.WriteOffset = bufferOffset; var sut = new SimpleProtocolDecoder(serializer); var actual1 = (await sut.Decode(state)).As <TransportMessage>(); var actual2 = (await sut.Decode(state)).As <TransportMessage>(); actual1.Body.Should().Be("Test1"); actual2.Body.Should().Be("Test2"); }
public void Should_be_able_to_serialize_messages() { var headers = new Dictionary <string, string>(); var sut = new JsonTransportSerializer(); var buffer = sut.Serialize("Hello world", headers); var message = sut.Deserialize(buffer, 0, buffer.Length, headers); message.Should().Be("Hello world"); }
public void Should_tell_if_the_contentType_header_is_missing() { var headers = new Dictionary <string, string>(); var sut = new JsonTransportSerializer(); var buffer = sut.Serialize("Hello world", headers); headers.Clear(); Action actual = () => sut.Deserialize(buffer, 0, buffer.Length, headers); actual.Should().Throw <JsonSerializationException>().And.Message.Should().Contain("Type-Name"); }
public async Task Should_be_able_to_encode_ack() { var serializer = new JsonTransportSerializer(); var expectedId = Guid.NewGuid(); var sender = Substitute.For <ISendState>(); var sut = new SimpleProtocolEncoder(serializer); await sut.EncodeAck(sender, expectedId); var sendBuffer = sender.ReceivedCalls().First().GetArguments()[0].As <byte[]>(); var guidBytes = new byte[16]; Buffer.BlockCopy(sendBuffer, 1, guidBytes, 0, 16); var actual = new Guid(guidBytes); actual.Should().Be(expectedId); }
public async Task Should_be_able_to_decode_an_nak() { var serializer = new JsonTransportSerializer(); var expectedId = Guid.NewGuid(); var state = new TestReceiveState(); var buf = new byte[17]; buf[0] = 3; //nak var guidBytes = expectedId.ToByteArray(); Buffer.BlockCopy(guidBytes, 0, buf, 1, 16); state.AddBuffer(buf, 0, buf.Length); state.WriteOffset = 17; var sut = new SimpleProtocolDecoder(serializer); var message = await sut.Decode(state); message.As <Nak>().MessageId.Should().Be(expectedId); }
public async Task Should_be_able_to_serialize_content() { var serialiser = new JsonTransportSerializer(); var sender = Substitute.For <ISendState>(); var msg = new TransportMessage("Hello world"); var sut = new SimpleProtocolEncoder(serialiser); await sut.EncodeMessage(sender, msg); var args = sender.ReceivedCalls().First().GetArguments(); var headerBytes = args[0].As <byte[]>(); FindHeader("Content-Type", "application/json", headerBytes).Should().BeTrue(); FindHeader("Content-Length", msg.Headers["Content-Length"], headerBytes).Should().BeTrue(); FindHeader("Type-Name", msg.Headers["Type-Name"], headerBytes).Should().BeTrue(); args = sender.ReceivedCalls().Last().GetArguments(); var len = (int)args[2]; Encoding.UTF8.GetString(args[0].As <byte[]>(), 0, len).Should().Be("\"Hello world\""); }
public async Task Should_be_able_to_encode_and_decode() { var serialiser = new JsonTransportSerializer(); var sender = Substitute.For <ISendState>(); var msg = new TransportMessage("Hello world"); var sut = new SimpleProtocolEncoder(serialiser); await sut.EncodeMessage(sender, msg); var headerCall = sender.ReceivedCalls().First().GetArguments(); var bodyCall = sender.ReceivedCalls().Last().GetArguments(); var receiveState = new TestReceiveState(); receiveState.AddBuffer(headerCall[0].As <byte[]>(), 0, headerCall[2].As <int>()); receiveState.AddBuffer(bodyCall[0].As <byte[]>(), 0, bodyCall[2].As <int>()); var decoder = new SimpleProtocolDecoder(serialiser); var actual = await decoder.Decode(receiveState); var actualMessage = actual as TransportMessage; actualMessage.Body.Should().Be("Hello world"); }