Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Building up default binder tests

  • Loading branch information...
commit 6ce0b82faa86588a71ee5917d5f001263275e111 1 parent fa744a3
@grumpydev grumpydev authored
View
51 src/Nancy.Tests/Unit/ModelBinding/DefaultBinderFixture.cs
@@ -6,6 +6,7 @@ namespace Nancy.Tests.Unit.ModelBinding
using FakeItEasy;
using Nancy.ModelBinding;
+ using Nancy.Tests.Fakes;
using Xunit;
@@ -26,5 +27,55 @@ public void Should_throw_if_body_deserializers_is_null()
result.ShouldBeOfType(typeof(ArgumentNullException));
}
+
+ [Fact]
+ public void Should_call_body_deserializer_if_one_matches()
+ {
+ var deserializer = A.Fake<IBodyDeserializer>();
+ A.CallTo(() => deserializer.CanDeserialize(null)).WithAnyArguments().Returns(true);
+ var binder = this.GetBinder(bodyDeserializers: new[] { deserializer });
+ var context = new NancyContext { Request = new FakeRequest("GET", "/") };
+ context.Request.Headers.Add("Content-Type", new[] { "application/xml" });
+
+ binder.Bind(context, this.GetType());
+
+ A.CallTo(() => deserializer.Deserialize(null, null, null)).WithAnyArguments()
+ .MustHaveHappened(Repeated.Exactly.Once);
+ }
+
+ [Fact]
+ public void Should_not_call_body_deserializer_if_none_matching()
+ {
+ var deserializer = A.Fake<IBodyDeserializer>();
+ A.CallTo(() => deserializer.CanDeserialize(null)).WithAnyArguments().Returns(false);
+ var binder = this.GetBinder(bodyDeserializers: new[] { deserializer });
+ var context = new NancyContext { Request = new FakeRequest("GET", "/") };
+ context.Request.Headers.Add("Content-Type", new[] { "application/xml" });
+
+ binder.Bind(context, this.GetType());
+
+ A.CallTo(() => deserializer.Deserialize(null, null, null)).WithAnyArguments()
+ .MustNotHaveHappened();
+ }
+
+ [Fact]
+ public void Should_pass_request_content_type_to_can_deserialize()
+ {
+ var deserializer = A.Fake<IBodyDeserializer>();
+ var binder = this.GetBinder(bodyDeserializers: new[] { deserializer });
+ var context = new NancyContext { Request = new FakeRequest("GET", "/") };
+ context.Request.Headers.Add("Content-Type", new[] { "application/xml" });
+
+ binder.Bind(context, this.GetType());
+
+ A.CallTo(() => deserializer.CanDeserialize("application/xml"))
+ .MustHaveHappened(Repeated.Exactly.Once);
+ }
+
+ private IBinder GetBinder(IEnumerable<ITypeConverter> typeConverters = null, IEnumerable<IBodyDeserializer> bodyDeserializers = null)
+ {
+ return new DefaultBinder(
+ typeConverters ?? new ITypeConverter[] { }, bodyDeserializers ?? new IBodyDeserializer[] { });
+ }
}
}
View
33 src/Nancy/ModelBinding/DefaultBinder.cs
@@ -87,15 +87,36 @@ public object Bind(NancyContext context, Type modelType)
private object DeserializeBody(NancyContext context, Type modelType)
{
- return null;
- //var contentType = context.Request.Headers["Content-Type"];
+ if (context == null || context.Request == null)
+ {
+ return null;
+ }
+
+ var contentType = this.GetContentType(context);
+
+ var bodyDeserializer = this.bodyDeserializers.Where(b => b.CanDeserialize(contentType)).FirstOrDefault();
+
+ return bodyDeserializer != null
+ ? bodyDeserializer.Deserialize(contentType, context.Request.Body, context)
+ : null;
+ }
- //if (string.IsNullOrEmpty(contentType))
+ private string GetContentType(NancyContext context)
+ {
+ if (context == null || context.Request == null)
+ {
+ return String.Empty;
+ }
- // foreach (var bodyDeserializer in bodyDeserializers.Where(b => b.CanDeserialize(contentType)))
- // {
+ IEnumerable<string> contentTypeHeaders;
+ context.Request.Headers.TryGetValue("Content-Type", out contentTypeHeaders);
+
+ if (contentTypeHeaders == null || !contentTypeHeaders.Any())
+ {
+ return string.Empty;
+ }
- // }
+ return contentTypeHeaders.First();
}
}
}
View
6 src/Nancy/ModelBinding/IBodyDeserializer.cs
@@ -1,5 +1,7 @@
namespace Nancy.ModelBinding
{
+ using System.IO;
+
/// <summary>
/// Provides a way to deserialize the contents of a request
/// into a bound model.
@@ -17,9 +19,9 @@ public interface IBodyDeserializer
/// Deserialize the request body to a model
/// </summary>
/// <param name="contentType">Content type to deserialize</param>
- /// <param name="body">Request body</param>
+ /// <param name="bodyStream">Request body stream</param>
/// <param name="context">Current context</param>
/// <returns>Model instance</returns>
- object Deserialize(string contentType, string body, NancyContext context);
+ object Deserialize(string contentType, Stream bodyStream, NancyContext context);
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.