Skip to content
This repository has been archived by the owner on Jan 24, 2021. It is now read-only.

Commit

Permalink
Building up default binder tests
Browse files Browse the repository at this point in the history
  • Loading branch information
grumpydev committed Apr 1, 2011
1 parent fa744a3 commit 6ce0b82
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 8 deletions.
51 changes: 51 additions & 0 deletions src/Nancy.Tests/Unit/ModelBinding/DefaultBinderFixture.cs
Expand Up @@ -6,6 +6,7 @@ namespace Nancy.Tests.Unit.ModelBinding
using FakeItEasy;

using Nancy.ModelBinding;
using Nancy.Tests.Fakes;

using Xunit;

Expand All @@ -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[] { });
}
}
}
33 changes: 27 additions & 6 deletions src/Nancy/ModelBinding/DefaultBinder.cs
Expand Up @@ -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();
}
}
}
6 changes: 4 additions & 2 deletions 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.
Expand All @@ -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);
}
}

0 comments on commit 6ce0b82

Please sign in to comment.