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); }
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); } }
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); }
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; }