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

NancyContext available everywhere #551

Merged
merged 15 commits into from
Mar 14, 2012
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/Nancy.Demo.Hosting.Aspnet/CustomErrorHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
public class CustomErrorHandler : IErrorHandler
{
/// <summary>
/// Whether then
/// Check if the error handler can handle errors of the provided status code.
/// </summary>
/// <param name="statusCode">Status code</param>
/// <param name="context">The <see cref="NancyContext"/> instance of the current request.</param>
/// <returns>True if handled, false otherwise</returns>
public bool HandlesStatusCode(HttpStatusCode statusCode)
public bool HandlesStatusCode(HttpStatusCode statusCode, NancyContext context)
{
return statusCode == HttpStatusCode.ImATeapot;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Nancy.Testing/PassThroughErrorHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Nancy.Testing
{
public class PassThroughErrorHandler : IErrorHandler
{
public bool HandlesStatusCode(HttpStatusCode statusCode)
public bool HandlesStatusCode(HttpStatusCode statusCode, NancyContext context)
{
return statusCode == HttpStatusCode.InternalServerError;
}
Expand Down
8 changes: 8 additions & 0 deletions src/Nancy.Tests/Fakes/FakeRoutePatternMatchResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public FakeRoutePatternMatchResult(Action<FakeRoutePatternMatchResultConfigurato
closure.Invoke(configurator);
}

public NancyContext Context { get; private set; }

public bool IsMatch { get; private set; }

public DynamicDictionary Parameters { get; private set; }
Expand All @@ -39,6 +41,12 @@ public FakeRoutePatternMatchResultConfigurator(FakeRoutePatternMatchResult fakeR
this.fakeRoutePatternMatchResult = fakeRoutePatternMatchResult;
}

public FakeRoutePatternMatchResultConfigurator Context(NancyContext context)
{
this.fakeRoutePatternMatchResult.Context = context;
return this;
}

public FakeRoutePatternMatchResultConfigurator IsMatch(bool isMatch)
{
this.fakeRoutePatternMatchResult.IsMatch = isMatch;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public DefaultErrorHandlerFixture()
[InlineData(HttpStatusCode.Unauthorized)]
public void Should_not_handle_non_error_codes(HttpStatusCode code)
{
var result = this.errorHandler.HandlesStatusCode(code);
var result = this.errorHandler.HandlesStatusCode(code, null);

result.ShouldBeFalse();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
namespace Nancy.Tests.Unit.ModelBinding
{
using System;

using FakeItEasy;

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

using Xunit;

public class DefaultModelBinderLocatorFixture
{
private DefaultBinder defaultBinder;
private readonly DefaultBinder defaultBinder;

/// <summary>
/// Initializes a new instance of the <see cref="T:System.Object"/> class.
Expand All @@ -36,7 +33,7 @@ public void Should_return_default_binder_if_no_specific_binder_exists()
A.CallTo(() => fakeBinder.CanBind(A<Type>.Ignored)).Returns(false);
var locator = new DefaultModelBinderLocator(new IModelBinder[] { fakeBinder }, this.defaultBinder);

var result = locator.GetBinderForType(typeof(Model));
var result = locator.GetBinderForType(typeof(Model), A<NancyContext>.Ignored);

result.ShouldBeSameAs(this.defaultBinder);
}
Expand All @@ -48,7 +45,7 @@ public void Should_not_return_a_binder_that_returns_false_for_canbind()
A.CallTo(() => fakeBinder.CanBind(A<Type>.Ignored)).Returns(false);
var locator = new DefaultModelBinderLocator(new IModelBinder[] { fakeBinder }, this.defaultBinder);

var result = locator.GetBinderForType(typeof(Model));
var result = locator.GetBinderForType(typeof(Model), A<NancyContext>.Ignored);

result.ShouldNotBeSameAs(fakeBinder);
}
Expand All @@ -60,7 +57,7 @@ public void Should_return_a_binder_that_returns_true_for_canbind()
A.CallTo(() => fakeBinder.CanBind(A<Type>.Ignored)).Returns(true);
var locator = new DefaultModelBinderLocator(new IModelBinder[] { fakeBinder }, this.defaultBinder);

var result = locator.GetBinderForType(typeof(Model));
var result = locator.GetBinderForType(typeof(Model), A<NancyContext>.Ignored);

result.ShouldBeSameAs(fakeBinder);
}
Expand All @@ -70,7 +67,7 @@ public void Should_be_able_to_bind_interfaces()
{
var binder = new InterfaceModelBinder();
var locator = new DefaultModelBinderLocator(new IModelBinder[] { binder }, this.defaultBinder);
var locatedBinder = locator.GetBinderForType(typeof(Concrete));
var locatedBinder = locator.GetBinderForType(typeof(Concrete), A<NancyContext>.Ignored);

var result = locatedBinder.Bind(null, typeof(Concrete)) as IAmAnInterface;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,86 +1,123 @@
namespace Nancy.Tests.Unit.ModelBinding
{
using System;

using FakeItEasy;

using Nancy.ModelBinding;

using Xunit;

public class DynamicModelBinderAdapterFixture
{
[Fact]
public void Should_throw_if_locator_is_null()
{
// Given, When
var result = Record.Exception(() => new DynamicModelBinderAdapter(null, new NancyContext()));

// Then
result.ShouldBeOfType(typeof(ArgumentNullException));
}

[Fact]
public void Should_throw_if_context_is_null()
{
// Given, When
var result = Record.Exception(() => new DynamicModelBinderAdapter(A.Fake<IModelBinderLocator>(), null));

// Then
result.ShouldBeOfType(typeof(ArgumentNullException));
}

[Fact]
public void Should_pass_type_to_locator_when_cast_implcitly()
{
// Given
var fakeModelBinder = A.Fake<IModelBinder>();
var returnModel = new Model();
A.CallTo(() => fakeModelBinder.Bind(null, null, null)).WithAnyArguments().Returns(returnModel);

var fakeLocator = A.Fake<IModelBinderLocator>();
A.CallTo(() => fakeLocator.GetBinderForType(A<Type>.Ignored)).Returns(fakeModelBinder);
A.CallTo(() => fakeLocator.GetBinderForType(A<Type>.Ignored, A<NancyContext>.Ignored)).Returns(fakeModelBinder);
dynamic adapter = new DynamicModelBinderAdapter(fakeLocator, new NancyContext());

// When
Model result = adapter;

A.CallTo(() => fakeLocator.GetBinderForType(typeof(Model))).MustHaveHappened(Repeated.Exactly.Once);
// Then
A.CallTo(() => fakeLocator.GetBinderForType(typeof(Model), A<NancyContext>.Ignored)).MustHaveHappened(Repeated.Exactly.Once);
}

[Fact]
public void Should_invoke_binder_with_context()
{
// Given
var context = new NancyContext();

var fakeModelBinder = A.Fake<IModelBinder>();
var returnModel = new Model();
A.CallTo(() => fakeModelBinder.Bind(null, null, null)).WithAnyArguments().Returns(returnModel);

var fakeLocator = A.Fake<IModelBinderLocator>();
A.CallTo(() => fakeLocator.GetBinderForType(A<Type>.Ignored, context)).Returns(fakeModelBinder);
dynamic adapter = new DynamicModelBinderAdapter(fakeLocator, context);

// When
Model result = adapter;

// Then
A.CallTo(() => fakeLocator.GetBinderForType(A<Type>.Ignored, context)).MustHaveHappened(Repeated.Exactly.Once);
}

[Fact]
public void Should_pass_type_to_locator_when_cast_explicitly()
{
// Given
var fakeModelBinder = A.Fake<IModelBinder>();
var returnModel = new Model();
A.CallTo(() => fakeModelBinder.Bind(null, null, null)).WithAnyArguments().Returns(returnModel);

var fakeLocator = A.Fake<IModelBinderLocator>();
A.CallTo(() => fakeLocator.GetBinderForType(A<Type>.Ignored)).Returns(fakeModelBinder);
A.CallTo(() => fakeLocator.GetBinderForType(A<Type>.Ignored, A<NancyContext>.Ignored)).Returns(fakeModelBinder);
dynamic adapter = new DynamicModelBinderAdapter(fakeLocator, new NancyContext());

// When
var result = (Model)adapter;

A.CallTo(() => fakeLocator.GetBinderForType(typeof(Model))).MustHaveHappened(Repeated.Exactly.Once);
// Then
A.CallTo(() => fakeLocator.GetBinderForType(typeof(Model), A<NancyContext>.Ignored)).MustHaveHappened(Repeated.Exactly.Once);
}

[Fact]
public void Should_return_object_from_binder_if_binder_doesnt_return_null()
{
// Given
var fakeModelBinder = A.Fake<IModelBinder>();
var returnModel = new Model();
A.CallTo(() => fakeModelBinder.Bind(null, null, null)).WithAnyArguments().Returns(returnModel);

var fakeLocator = A.Fake<IModelBinderLocator>();
A.CallTo(() => fakeLocator.GetBinderForType(A<Type>.Ignored)).Returns(fakeModelBinder);
A.CallTo(() => fakeLocator.GetBinderForType(A<Type>.Ignored, A<NancyContext>.Ignored)).Returns(fakeModelBinder);
dynamic adapter = new DynamicModelBinderAdapter(fakeLocator, new NancyContext());

// When
Model result = adapter;

// Then
result.ShouldNotBeNull();
result.ShouldBeSameAs(returnModel);
}

[Fact]
public void Should_throw_if_locator_does_not_return_binder()
{
// Given
var fakeLocator = A.Fake<IModelBinderLocator>();
A.CallTo(() => fakeLocator.GetBinderForType(A<Type>.Ignored)).Returns(null);
A.CallTo(() => fakeLocator.GetBinderForType(A<Type>.Ignored, A<NancyContext>.Ignored)).Returns(null);
dynamic adapter = new DynamicModelBinderAdapter(fakeLocator, new NancyContext());

// When
var result = Record.Exception(() => (Model)adapter);

// Then
result.ShouldBeOfType(typeof(ModelBindingException));
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/Nancy.Tests/Unit/NancyEngineFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public NancyEngineFixture()
this.context = new NancyContext();
this.errorHandler = A.Fake<IErrorHandler>();

A.CallTo(() => errorHandler.HandlesStatusCode(A<HttpStatusCode>.Ignored)).Returns(false);
A.CallTo(() => errorHandler.HandlesStatusCode(A<HttpStatusCode>.Ignored, A<NancyContext>.Ignored)).Returns(false);

contextFactory = A.Fake<INancyContextFactory>();
A.CallTo(() => contextFactory.Create()).Returns(context);
Expand Down Expand Up @@ -499,7 +499,7 @@ public void Should_ask_error_handler_if_it_can_handle_status_code()
this.engine.HandleRequest(request);

// Then
A.CallTo(() => this.errorHandler.HandlesStatusCode(A<HttpStatusCode>.Ignored)).MustHaveHappened(Repeated.Exactly.Once);
A.CallTo(() => this.errorHandler.HandlesStatusCode(A<HttpStatusCode>.Ignored, A<NancyContext>.Ignored)).MustHaveHappened(Repeated.Exactly.Once);
}

[Fact]
Expand All @@ -520,7 +520,7 @@ public void Should_invoke_error_handler_if_supported_status_code()
{
// Given
var request = new Request("GET", "/", "http");
A.CallTo(() => this.errorHandler.HandlesStatusCode(A<HttpStatusCode>.Ignored)).Returns(true);
A.CallTo(() => this.errorHandler.HandlesStatusCode(A<HttpStatusCode>.Ignored, A<NancyContext>.Ignored)).Returns(true);

// When
this.engine.HandleRequest(request);
Expand Down
Loading