Exemplo n.º 1
0
        public void TestConfigurableHeaders()
        {
            var message = IntegrationMessageBuilder <byte[]>
                          .WithPayload(Encoding.UTF8.GetBytes("Hello"))
                          .SetHeader("foo", "bar")
                          .SetHeader("baz", "quxx")
                          .SetHeader("contentType", "text/plain")
                          .Build();

            var headers  = new string[] { "foo" };
            var embedded = EmbeddedHeaderUtils.EmbedHeaders(new MessageValues(message), EmbeddedHeaderUtils.HeadersToEmbed(headers));

            Assert.Equal(0xff, embedded[0]);
            var embeddedString = Encoding.UTF8.GetString(embedded);

            Assert.Equal("\u0002\u000BcontentType\u0000\u0000\u0000\u000C\"text/plain\"\u0003foo\u0000\u0000\u0000\u0005\"bar\"Hello", embeddedString.Substring(1));
            var extracted = EmbeddedHeaderUtils.ExtractHeaders(IntegrationMessageBuilder <byte[]> .WithPayload(embedded).Build(), false);

            Assert.Equal("Hello", Encoding.UTF8.GetString((byte[])extracted.Payload));
            Assert.Equal("bar", extracted["foo"]);
            Assert.Null(extracted["baz"]);
            Assert.Equal("text/plain", extracted["contentType"]);
            Assert.Null(extracted["timestamp"]);

            var extractedWithRequestHeaders = EmbeddedHeaderUtils.ExtractHeaders(IntegrationMessageBuilder <byte[]> .WithPayload(embedded).Build(), true);

            Assert.Equal("bar", extractedWithRequestHeaders["foo"]);
            Assert.Null(extractedWithRequestHeaders["baz"]);
            Assert.Equal("text/plain", extractedWithRequestHeaders["contentType"]);
            Assert.NotNull(extractedWithRequestHeaders["timestamp"]);
            Assert.NotNull(extractedWithRequestHeaders["id"]);
            Assert.IsType <long>(extractedWithRequestHeaders["timestamp"]);
            Assert.IsType <string>(extractedWithRequestHeaders["id"]);
        }
        public void HandlerThrowsExceptionExecutorChannel()
        {
            services.AddSingleton <IDestinationResolver <IMessageChannel>, DefaultMessageChannelDestinationResolver>();
            services.AddSingleton <IMessageBuilderFactory, DefaultMessageBuilderFactory>();
            services.AddSingleton <IMessageChannel>((p) => new DirectChannel(p.GetService <IApplicationContext>(), "errorChannel"));
            var provider = services.BuildServiceProvider();

            var defaultErrorChannel = provider.GetService <IMessageChannel>() as DirectChannel;
            var channel             = new TaskSchedulerChannel(provider.GetService <IApplicationContext>(), TaskScheduler.Default);
            var resultHandler       = new ResultHandler(latch);
            var throwingHandler     = new ThrowMessageExceptionHandler();

            channel.Subscribe(throwingHandler);
            defaultErrorChannel.Subscribe(resultHandler);
            var message = IntegrationMessageBuilder.WithPayload("test").Build();

            channel.Send(message);
            Assert.True(latch.Wait(10000));
            var errorMessage = resultHandler.LastMessage;

            Assert.IsType <MessagingException>(errorMessage.Payload);
            var exceptionPayload = (MessagingException)errorMessage.Payload;

            Assert.IsType <NotSupportedException>(exceptionPayload.InnerException);
            Assert.Same(message, exceptionPayload.FailedMessage);
            Assert.NotEqual(Thread.CurrentThread.ManagedThreadId, resultHandler.LastThread.ManagedThreadId);
        }
        public void HandlerThrowsExceptionPublishSubscribeWithoutScheduler()
        {
            var provider = services.BuildServiceProvider();
            var channel  = new PublishSubscribeChannel(provider.GetService <IApplicationContext>());
            var handler  = new ThrowingHandler();

            channel.Subscribe(handler);
            var message = IntegrationMessageBuilder.WithPayload("test").Build();

            Assert.Throws <MessageDeliveryException>(() => channel.Send(message));
        }
        public void MutableFromImmutableMutate()
        {
            var message1 = IntegrationMessageBuilder <string> .WithPayload("test").SetHeader("foo", "bar").Build();

            var message2 = new MutableIntegrationMessageBuilderFactory().FromMessage(message1).SetHeader("another", 1).Build();

            Assert.Equal("bar", message2.Headers["foo"]);
            Assert.Equal(message1.Headers.Id, message2.Headers.Id);
            Assert.NotEqual(message1, message2);
            Assert.False(message2 == message1);
        }
Exemplo n.º 5
0
        public void TestNoExceptionEnhancement()
        {
            dispatcher.AddHandler(handlerMock.Object);
            var dontReplaceThisMessage = IntegrationMessageBuilder.WithPayload("x").Build();

            handlerMock.Setup((h) => h.HandleMessage(messageMock.Object)).Throws(new MessagingException(dontReplaceThisMessage, "Mock Exception"));
            var ex = Assert.Throws <MessagingException>(() => dispatcher.Dispatch(messageMock.Object));

            Assert.Equal("Mock Exception", ex.Message);
            Assert.Equal(dontReplaceThisMessage, ex.FailedMessage);
        }
Exemplo n.º 6
0
        public void TestHeaderEmbeddingMissingHeader()
        {
            var message = IntegrationMessageBuilder <byte[]>
                          .WithPayload(Encoding.UTF8.GetBytes("Hello"))
                          .SetHeader("foo", "bar")
                          .Build();

            var embedded = EmbeddedHeaderUtils.EmbedHeaders(new MessageValues(message), "foo", "baz");

            Assert.Equal(0xff, embedded[0]);
            var embeddedString = Encoding.UTF8.GetString(embedded);

            Assert.Equal("\u0001\u0003foo\u0000\u0000\u0000\u0005\"bar\"Hello", embeddedString.Substring(1));
        }
        public void ArrayWithRequestReplyHandler()
        {
            handler.ReturnValue = new string[] { "foo", "bar" };
            var channel = new QueueChannel(provider.GetService <IApplicationContext>());
            var message = IntegrationMessageBuilder.WithPayload("test").SetReplyChannel(channel).Build();

            handler.HandleMessage(message);
            var reply1 = channel.Receive(0);
            var reply2 = channel.Receive(0);

            Assert.NotNull(reply1);
            Assert.Null(reply2);
            Assert.IsType <string[]>(reply1.Payload);
            Assert.Equal(2, ((string[])reply1.Payload).Length);
        }
Exemplo n.º 8
0
        public void TestUnicodeHeader()
        {
            var message = IntegrationMessageBuilder <byte[]>
                          .WithPayload(Encoding.UTF8.GetBytes("Hello"))
                          .SetHeader("foo", "bar")
                          .SetHeader("baz", "ØØØØØØØØ")
                          .Build();

            var embedded = EmbeddedHeaderUtils.EmbedHeaders(new MessageValues(message), "foo", "baz");

            Assert.Equal(0xff, embedded[0]);
            var embeddedString = Encoding.UTF8.GetString(embedded);

            Assert.Equal("\u0002\u0003foo\u0000\u0000\u0000\u0005\"bar\"\u0003baz\u0000\u0000\u0000\u0012\"ØØØØØØØØ\"Hello", embeddedString.Substring(1));

            var extracted = EmbeddedHeaderUtils.ExtractHeaders(IntegrationMessageBuilder <byte[]> .WithPayload(embedded).Build(), false);

            Assert.Equal("Hello", Encoding.UTF8.GetString((byte[])extracted.Payload));
            Assert.Equal("bar", extracted["foo"]);
            Assert.Equal("ØØØØØØØØ", extracted["baz"]);
        }
Exemplo n.º 9
0
 public IMessage ToMessage()
 {
     return(IntegrationMessageBuilder.WithPayload(Payload).CopyHeaders(Headers).Build());
 }