Skip to content

Commit

Permalink
(#255) De-activate Content Type checking by default
Browse files Browse the repository at this point in the history
  • Loading branch information
pardahlman committed Aug 19, 2017
1 parent 15c5b52 commit ccf57ab
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 10 deletions.
2 changes: 0 additions & 2 deletions RawRabbit.Enrichers.Protobuf/ProtobufPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ public static class ProtobufPlugin
/// <summary>
/// Replaces the default serializer with Protobuf.
/// </summary>
/// <param name="builder"></param>
/// <returns></returns>
public static IClientBuilder UseProtobuf(this IClientBuilder builder)
{
builder.Register(
Expand Down
33 changes: 30 additions & 3 deletions src/RawRabbit/Pipe/Middleware/BodyDeserializationMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class MessageDeserializationOptions
{
public Func<IPipeContext, Type> BodyTypeFunc { get; set; }
public Func<IPipeContext, string> BodyContentTypeFunc { get; set; }
public Func<IPipeContext, bool> ActivateContentTypeCheck{ get; set; }
public Func<IPipeContext, byte[]> BodyFunc { get; set; }
public Action<IPipeContext, object> PersistAction { get; set; }
}
Expand All @@ -21,6 +22,7 @@ public class BodyDeserializationMiddleware : Middleware
protected Func<IPipeContext, Type> MessageTypeFunc;
protected Func<IPipeContext, byte[]> BodyBytesFunc;
protected Func<IPipeContext, string> BodyContentTypeFunc { get; set; }
protected Func<IPipeContext, bool> ActivateContentTypeCheck { get; set; }
protected Action<IPipeContext, object> PersistAction;
private readonly ILog _logger = LogProvider.For<BodyDeserializationMiddleware>();

Expand All @@ -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))
Expand Down Expand Up @@ -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<bool>(ContentTypeCheck);
}
}
}
4 changes: 2 additions & 2 deletions src/RawRabbit/Pipe/PipeContextExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public static IModel GetTransientChannel(this IPipeContext context)

public static IBasicProperties GetBasicProperties(this IPipeContext context)
{
return context.Get<IBasicProperties>(PipeKey.BasicProperties) ?? GetDeliveryEventArgs(context)?.BasicProperties;
return context.Get<IBasicProperties>(PipeKey.BasicProperties);
}

public static BasicDeliverEventArgs GetDeliveryEventArgs(this IPipeContext context)
Expand Down Expand Up @@ -138,4 +138,4 @@ public static RawRabbitConfiguration GetClientConfiguration(this IPipeContext co
return context.Get<RawRabbitConfiguration>(PipeKey.ClientConfiguration);
}
}
}
}
7 changes: 4 additions & 3 deletions test/RawRabbit.IntegrationTests/Enrichers/ProtobufTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using RawRabbit.Exceptions;
using RawRabbit.Instantiation;
using RawRabbit.Pipe;
using RawRabbit.Pipe.Middleware;
using Xunit;

namespace RawRabbit.IntegrationTests.Enrichers
Expand Down Expand Up @@ -97,7 +98,7 @@ await protobufClient.SubscribeAsync<ProtoMessage>(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())
Expand All @@ -110,8 +111,8 @@ await jsonClient.RespondAsync<ProtoRequest, ProtoResponse>(request =>
/* Test */
/* Assert */
var e = await Assert.ThrowsAsync<MessageHandlerException>(() =>
protobufClient.RequestAsync<ProtoRequest, ProtoResponse>(new ProtoRequest())
);
protobufClient.RequestAsync<ProtoRequest, ProtoResponse>(new ProtoRequest { Id = Guid.NewGuid()}
, ctx => ctx.UseContentTypeCheck()));
}
}
}
Expand Down

0 comments on commit ccf57ab

Please sign in to comment.