From ccf57abc43280152b36424c53aff2da323da008e Mon Sep 17 00:00:00 2001 From: pardahlman Date: Sun, 20 Aug 2017 00:07:01 +0200 Subject: [PATCH] (#255) De-activate Content Type checking by default --- .../ProtobufPlugin.cs | 2 -- .../BodyDeserializationMiddleware.cs | 33 +++++++++++++++++-- src/RawRabbit/Pipe/PipeContextExtension.cs | 4 +-- .../Enrichers/ProtobufTests.cs | 7 ++-- 4 files changed, 36 insertions(+), 10 deletions(-) diff --git a/RawRabbit.Enrichers.Protobuf/ProtobufPlugin.cs b/RawRabbit.Enrichers.Protobuf/ProtobufPlugin.cs index 0d5a379e..5aa61bb4 100644 --- a/RawRabbit.Enrichers.Protobuf/ProtobufPlugin.cs +++ b/RawRabbit.Enrichers.Protobuf/ProtobufPlugin.cs @@ -9,8 +9,6 @@ public static class ProtobufPlugin /// /// Replaces the default serializer with Protobuf. /// - /// - /// public static IClientBuilder UseProtobuf(this IClientBuilder builder) { builder.Register( diff --git a/src/RawRabbit/Pipe/Middleware/BodyDeserializationMiddleware.cs b/src/RawRabbit/Pipe/Middleware/BodyDeserializationMiddleware.cs index aebfe8df..5087ca15 100644 --- a/src/RawRabbit/Pipe/Middleware/BodyDeserializationMiddleware.cs +++ b/src/RawRabbit/Pipe/Middleware/BodyDeserializationMiddleware.cs @@ -11,6 +11,7 @@ public class MessageDeserializationOptions { public Func BodyTypeFunc { get; set; } public Func BodyContentTypeFunc { get; set; } + public Func ActivateContentTypeCheck{ get; set; } public Func BodyFunc { get; set; } public Action PersistAction { get; set; } } @@ -21,6 +22,7 @@ public class BodyDeserializationMiddleware : Middleware protected Func MessageTypeFunc; protected Func BodyBytesFunc; protected Func BodyContentTypeFunc { get; set; } + protected Func ActivateContentTypeCheck { get; set; } protected Action PersistAction; private readonly ILog _logger = LogProvider.For(); @@ -31,20 +33,29 @@ public BodyDeserializationMiddleware(ISerializer serializer, MessageDeserializat BodyBytesFunc = options?.BodyFunc ?? (context =>context.GetDeliveryEventArgs()?.Body); PersistAction = options?.PersistAction ?? ((context, msg) => context.Properties.TryAdd(PipeKey.Message, msg)); BodyContentTypeFunc = options?.BodyContentTypeFunc ?? (context => context.GetDeliveryEventArgs()?.BasicProperties.ContentType); + ActivateContentTypeCheck = options?.ActivateContentTypeCheck ?? (context => context.GetContentTypeCheckActivated()); } public override async Task InvokeAsync(IPipeContext context, CancellationToken token) { - var msgContentType = GetMessageContentType(context); - if (!CanSerializeMessage(msgContentType)) + if (ContentTypeCheckActivated(context)) { - throw new SerializationException($"Registered serializer supports {Serializer.ContentType}, recieved message uses {msgContentType}."); + var msgContentType = GetMessageContentType(context); + if (!CanSerializeMessage(msgContentType)) + { + throw new SerializationException($"Registered serializer supports {Serializer.ContentType}, recieved message uses {msgContentType}."); + } } var message = GetMessage(context); SaveInContext(context, message); await Next.InvokeAsync(context, token); } + protected virtual bool ContentTypeCheckActivated(IPipeContext context) + { + return ActivateContentTypeCheck?.Invoke(context) ?? false; + } + protected virtual bool CanSerializeMessage(string msgContentType) { if (string.IsNullOrEmpty(msgContentType)) @@ -96,4 +107,20 @@ protected virtual void SaveInContext(IPipeContext context, object message) PersistAction?.Invoke(context, message); } } + + public static class BodyDeserializationMiddlewareExtensions + { + private const string ContentTypeCheck = "Deserialization:ContentType:Check"; + + public static IPipeContext UseContentTypeCheck(this IPipeContext context, bool check = true) + { + context.Properties.TryAdd(ContentTypeCheck, check); + return context; + } + + public static bool GetContentTypeCheckActivated(this IPipeContext context) + { + return context.Get(ContentTypeCheck); + } + } } diff --git a/src/RawRabbit/Pipe/PipeContextExtension.cs b/src/RawRabbit/Pipe/PipeContextExtension.cs index 43d8a8f9..2130dfaa 100644 --- a/src/RawRabbit/Pipe/PipeContextExtension.cs +++ b/src/RawRabbit/Pipe/PipeContextExtension.cs @@ -109,7 +109,7 @@ public static IModel GetTransientChannel(this IPipeContext context) public static IBasicProperties GetBasicProperties(this IPipeContext context) { - return context.Get(PipeKey.BasicProperties) ?? GetDeliveryEventArgs(context)?.BasicProperties; + return context.Get(PipeKey.BasicProperties); } public static BasicDeliverEventArgs GetDeliveryEventArgs(this IPipeContext context) @@ -138,4 +138,4 @@ public static RawRabbitConfiguration GetClientConfiguration(this IPipeContext co return context.Get(PipeKey.ClientConfiguration); } } -} \ No newline at end of file +} diff --git a/test/RawRabbit.IntegrationTests/Enrichers/ProtobufTests.cs b/test/RawRabbit.IntegrationTests/Enrichers/ProtobufTests.cs index fb4f3b3b..eeef81b7 100644 --- a/test/RawRabbit.IntegrationTests/Enrichers/ProtobufTests.cs +++ b/test/RawRabbit.IntegrationTests/Enrichers/ProtobufTests.cs @@ -6,6 +6,7 @@ using RawRabbit.Exceptions; using RawRabbit.Instantiation; using RawRabbit.Pipe; +using RawRabbit.Pipe.Middleware; using Xunit; namespace RawRabbit.IntegrationTests.Enrichers @@ -97,7 +98,7 @@ await protobufClient.SubscribeAsync(msg => } [Fact] - public async Task Should_Throw_Exception_If_Responder_Can_Not_Deserialize_Request() + public async Task Should_Throw_Exception_If_Responder_Can_Not_Deserialize_Request_And_Content_Type_Check_Is_Activated() { using (var protobufClient = RawRabbitFactory.CreateTestClient(new RawRabbitOptions { Plugins = p => p.UseProtobuf() })) using (var jsonClient = RawRabbitFactory.CreateTestClient()) @@ -110,8 +111,8 @@ await jsonClient.RespondAsync(request => /* Test */ /* Assert */ var e = await Assert.ThrowsAsync(() => - protobufClient.RequestAsync(new ProtoRequest()) - ); + protobufClient.RequestAsync(new ProtoRequest { Id = Guid.NewGuid()} + , ctx => ctx.UseContentTypeCheck())); } } }