예제 #1
0
        public void TestMissingContentType()
        {
            var bytes             = Encoding.UTF8.GetBytes("{\"name\" : \"foo\" }");
            var messageProperties = new MessageHeaders();
            var message           = Message.Create(bytes, messageProperties);
            var j2Converter       = new JsonMessageConverter();
            var typeMapper        = new DefaultTypeMapper
            {
                DefaultType = typeof(Foo)
            };

            j2Converter.TypeMapper = typeMapper;
            var foo = j2Converter.FromMessage(message, null);

            Assert.IsType <Foo>(foo);
            var accessor = RabbitHeaderAccessor.GetMutableAccessor(messageProperties);

            accessor.ContentType = null;

            foo = j2Converter.FromMessage(message, null);
            Assert.IsType <Foo>(foo);
            j2Converter.AssumeSupportedContentType = false;
            foo = j2Converter.FromMessage(message, null);
            Assert.Same(foo, bytes);
        }
예제 #2
0
        public void TestDelegationOutbound()
        {
            var converter        = new ContentTypeDelegatingMessageConverter();
            var messageConverter = new JsonMessageConverter();

            converter.AddDelegate("foo/bar", messageConverter);
            converter.AddDelegate(MessageHeaders.CONTENT_TYPE_JSON, messageConverter);

            var props = new RabbitHeaderAccessor();
            var foo   = new Foo
            {
                FooString = "bar"
            };

            props.ContentType = "foo/bar";
            var message = converter.ToMessage(foo, props.MessageHeaders);

            Assert.Equal(MessageHeaders.CONTENT_TYPE_JSON, message.Headers.ContentType());
            Assert.Equal("{\"fooString\":\"bar\"}", Encoding.UTF8.GetString((byte[])message.Payload));
            var converted = converter.FromMessage <Foo>(message);

            Assert.Equal("bar", converted.FooString);

            props = new RabbitHeaderAccessor
            {
                ContentType = MessageHeaders.CONTENT_TYPE_JSON
            };
            message = converter.ToMessage(foo, props.MessageHeaders);
            Assert.Equal("{\"fooString\":\"bar\"}", Encoding.UTF8.GetString((byte[])message.Payload));
            converted = converter.FromMessage <Foo>(message);
            Assert.Equal("bar", converted.FooString);

            converter = new ContentTypeDelegatingMessageConverter(null); // no default
            props     = new RabbitHeaderAccessor();
            try
            {
                converter.ToMessage(foo, props.MessageHeaders);
                throw new Exception("Expected exception");
            }
            catch (Exception e)
            {
                Assert.IsType <MessageConversionException>(e);
                Assert.Contains("No delegate converter", e.Message);
            }
        }
예제 #3
0
        public void TestDefaultType()
        {
            var bytes             = Encoding.UTF8.GetBytes("{\"name\" : \"foo\" }");
            var messageProperties = new RabbitHeaderAccessor
            {
                ContentType = "application/json"
            };
            var message     = Message.Create(bytes, messageProperties.MessageHeaders);
            var converter   = new JsonMessageConverter();
            var classMapper = new DefaultTypeMapper
            {
                DefaultType = typeof(Foo)
            };

            converter.TypeMapper = classMapper;
            var foo = converter.FromMessage(message, null);

            Assert.IsType <Foo>(foo);
        }
예제 #4
0
        public JsonMessageConverterTest()
        {
            converter = new JsonMessageConverter();
            trade     = new SimpleTrade
            {
                AccountName = "Acct1",
                BuyRequest  = true,
                OrderType   = "Market",
                Price       = 103.30M,
                Quantity    = 100,
                RequestId   = "R123",
                Ticker      = "VMW",
                UserName    = "******"
            };
            jsonConverterWithDefaultType = new JsonMessageConverter();
            var classMapper = new DefaultTypeMapper
            {
                DefaultType = typeof(Foo)
            };

            jsonConverterWithDefaultType.TypeMapper = classMapper;
        }