From b5c93c12360e46a1df6a4fd2f538f13b783f6f1f Mon Sep 17 00:00:00 2001 From: Tim Clem Date: Sat, 28 Apr 2012 12:44:12 -0700 Subject: [PATCH] Test out requesthandler --- Burr.Tests/Burr.Tests.csproj | 1 + Burr.Tests/Http/RequestHandlerTests.cs | 57 +++++++++++++++++++++++++ Burr.Tests/Http/ResponseHandlerTests.cs | 2 +- Burr/Burr.csproj | 1 + Burr/Http/Connection.cs | 20 --------- Burr/Http/RequestHandler.cs | 24 +++++++++++ Burr/Http/ResponseHandler.cs | 6 +-- 7 files changed, 85 insertions(+), 26 deletions(-) create mode 100644 Burr.Tests/Http/RequestHandlerTests.cs create mode 100644 Burr/Http/RequestHandler.cs diff --git a/Burr.Tests/Burr.Tests.csproj b/Burr.Tests/Burr.Tests.csproj index 8d152b40..1fcd4b25 100644 --- a/Burr.Tests/Burr.Tests.csproj +++ b/Burr.Tests/Burr.Tests.csproj @@ -56,6 +56,7 @@ + diff --git a/Burr.Tests/Http/RequestHandlerTests.cs b/Burr.Tests/Http/RequestHandlerTests.cs new file mode 100644 index 00000000..7922e595 --- /dev/null +++ b/Burr.Tests/Http/RequestHandlerTests.cs @@ -0,0 +1,57 @@ +using System; +using System.Threading.Tasks; +using Burr.Http; +using FluentAssertions; +using Moq; +using Xunit; + +namespace Burr.Tests.Http +{ + public class RequestHandlerTests + { + public class MockRequestHandler : RequestHandler + { + public MockRequestHandler(IApplication app) + : base(app) + { + } + + protected override void Before(Env env) + { + BeforeWasCalled = true; + } + + public bool BeforeWasCalled { get; private set; } + } + + public class TheConstructor + { + [Fact] + public void ThrowsForBadArguments() + { + Assert.Throws(() => new MockRequestHandler(null)); + } + } + + public class TheCallMethod + { + [Fact] + public async Task InvokesBefore() + { + var env = new Mock>(); + var app = new Mock(); + var handler = new MockRequestHandler(app.Object); + app.Setup(x => x.Call(env.Object)) + .Returns(Task.FromResult(app.Object)) + .Callback(() => + { + handler.BeforeWasCalled.Should().BeTrue(); + }); + + await handler.Call(env.Object); + + app.Verify(x => x.Call(env.Object)); + } + } + } +} diff --git a/Burr.Tests/Http/ResponseHandlerTests.cs b/Burr.Tests/Http/ResponseHandlerTests.cs index f5b0e06a..dcd43db9 100644 --- a/Burr.Tests/Http/ResponseHandlerTests.cs +++ b/Burr.Tests/Http/ResponseHandlerTests.cs @@ -1,9 +1,9 @@ using System; using System.Threading.Tasks; using Burr.Http; +using FluentAssertions; using Moq; using Xunit; -using FluentAssertions; namespace Burr.Tests.Http { diff --git a/Burr/Burr.csproj b/Burr/Burr.csproj index 418db02b..b61fa224 100644 --- a/Burr/Burr.csproj +++ b/Burr/Burr.csproj @@ -44,6 +44,7 @@ Code + diff --git a/Burr/Http/Connection.cs b/Burr/Http/Connection.cs index eaf9e727..49e2a38a 100644 --- a/Burr/Http/Connection.cs +++ b/Burr/Http/Connection.cs @@ -2,10 +2,8 @@ using System.Collections.Generic; using System.Linq; using System.Net.Http; -using System.Text; using System.Threading.Tasks; using Burr.Helpers; -using Burr.SimpleJSON; namespace Burr.Http { @@ -196,22 +194,4 @@ namespace Burr.Http return this; } } - - public abstract class RequestHandler : IApplication - { - protected RequestHandler(IApplication app) - { - App = app; - } - - protected IApplication App { get; private set; } - - public async Task Call(Env env) - { - Before(env); - return await App.Call(env); - } - - protected abstract void Before(Env env); - } } diff --git a/Burr/Http/RequestHandler.cs b/Burr/Http/RequestHandler.cs new file mode 100644 index 00000000..e8273d68 --- /dev/null +++ b/Burr/Http/RequestHandler.cs @@ -0,0 +1,24 @@ +using System.Threading.Tasks; +using Burr.Helpers; + +namespace Burr.Http +{ + public abstract class RequestHandler : IApplication + { + protected RequestHandler(IApplication app) + { + Ensure.ArgumentNotNull(app, "app"); + App = app; + } + + protected IApplication App { get; private set; } + + public async Task Call(Env env) + { + Before(env); + return await App.Call(env); + } + + protected abstract void Before(Env env); + } +} diff --git a/Burr/Http/ResponseHandler.cs b/Burr/Http/ResponseHandler.cs index 666ab274..76e98c59 100644 --- a/Burr/Http/ResponseHandler.cs +++ b/Burr/Http/ResponseHandler.cs @@ -1,8 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using System.Threading.Tasks; using Burr.Helpers; namespace Burr.Http