Permalink
Browse files

moving errorhandler code to nancy error pipeline

  • Loading branch information...
1 parent 7b9f57b commit 398e9c93d3eeccba5b8249996cf7b25704b67fbb @horsdal committed Mar 25, 2012
View
56 RestBucks.Tests/IntegrationSmokeTests.cs
@@ -6,6 +6,9 @@
using Nancy.Testing;
using RestBucks;
+ using RestBucks.Resources.Orders.Representations;
+
+ using Util;
[TestFixture]
public class IntegrationSmokeTests
@@ -23,6 +26,31 @@ public void AppCanCreateAndDeleteOrder()
{
var app = new Browser(new Bootstrapper());
+ var createdResponse = CreatedOrder(app);
+
+ Assert.That(createdResponse.StatusCode, Is.EqualTo(HttpStatusCode.Created));
+ Assert.That(createdResponse.Headers.Keys, Contains.Item("Location"));
+
+ var orderPath = GetOrderPath(createdResponse);
+
+ var getOrderResponse = app.Get(orderPath);
+ Assert.That(getOrderResponse.StatusCode, Is.EqualTo(HttpStatusCode.OK), string.Format("order at {0} not found", orderPath));
+
+ var deletedResponse = app.Delete(orderPath);
+ Assert.That(deletedResponse.StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
+
+ var getDeletedOrderResponse = app.Get(orderPath);
+ Assert.That(getDeletedOrderResponse.StatusCode, Is.EqualTo(HttpStatusCode.MovedPermanently));
+ }
+
+ private static string GetOrderPath(BrowserResponse createdResponse)
+ {
+ var orderPath = createdResponse.Headers["Location"].Remove(0, 12);
+ return orderPath;
+ }
+
+ private static BrowserResponse CreatedOrder(Browser app)
+ {
var createdResponse = app.Post("/orders/",
with =>
{
@@ -39,20 +67,28 @@ public void AppCanCreateAndDeleteOrder()
+ " </items>"
+ "</order>");
});
+ return createdResponse;
+ }
- Assert.That(createdResponse.StatusCode, Is.EqualTo(HttpStatusCode.Created));
- Assert.That(createdResponse.Headers.Keys, Contains.Item("Location"));
+ [Test]
+ public void AppReturnsBadRequestWhenCancelingPaidOrder()
+ {
+ var app = new Browser(new Bootstrapper());
- var orderPath = createdResponse.Headers["Location"].Remove(0, 12);
+ var createdResponse = CreatedOrder(app);
+ var orderPath = GetOrderPath(createdResponse);
- var getOrderResponse = app.Get(orderPath);
- Assert.That(getOrderResponse.StatusCode, Is.EqualTo(HttpStatusCode.OK), string.Format("order at {0} not found", orderPath));
-
- var deletedResponse = app.Delete(orderPath);
- Assert.That(deletedResponse.StatusCode, Is.EqualTo(HttpStatusCode.NoContent));
+ var paymnetResponse = app.Post(orderPath + "/payment/",
+ with =>
+ {
+ with.HttpRequest();
+ var xmlString = new PaymentRepresentation {CardNumber = "321", CardOwner = "Jose"}.ToXmlString();
+ with.Body(xmlString);
+ });
+ Assert.That(paymnetResponse.StatusCode, Is.EqualTo(HttpStatusCode.OK));
- var getDeletedOrderResponse = app.Get(orderPath);
- Assert.That(getDeletedOrderResponse.StatusCode, Is.EqualTo(HttpStatusCode.MovedPermanently));
+ var cancelResponse = app.Delete(orderPath);
+ Assert.That(cancelResponse.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
}
}
}
View
17 RestBucks/Bootstrapper.cs
@@ -10,6 +10,8 @@
using Castle.Windsor;
using Castle.Windsor.Installer;
+ using Infrastructure;
+
using NHibernate;
using NHibernate.Context;
@@ -33,9 +35,18 @@ protected override void ApplicationStartup(Castle.Windsor.IWindsorContainer cont
container.Resolve<Barista>();
- pipelines.BeforeRequest += c => CreateSession(container);
- pipelines.AfterRequest += c => CommitSession(container);
- pipelines.OnError += (c, e) => RollbackSession(container);
+ pipelines.BeforeRequest += ctx => CreateSession(container);
+ pipelines.AfterRequest += ctx => CommitSession(container);
+ pipelines.OnError += (ctx, ex) => RollbackSession(container);
+ pipelines.OnError += InvalidOrderOperationHandler;
+ }
+
+ private Response InvalidOrderOperationHandler(NancyContext ctx, Exception ex)
+ {
+ if (ex is InvalidOrderOperationException)
+ return ResponseHelpers.BadRequest(null, content: ex.Message);
+ else
+ return null;
}
private Response RollbackSession(IWindsorContainer container)
View
4 RestBucks/Infrastructure/InvalidOrderOperationException.cs
@@ -4,6 +4,10 @@
public class InvalidOrderOperationException : Exception
{
+ public InvalidOrderOperationException() : this("no message provided")
+ {
+ }
+
public InvalidOrderOperationException(string message)
: base(message)
{
View
20 RestBucks/Infrastructure/WebApi/RestBucksHttpErrorHandler.cs
@@ -1,20 +0,0 @@
-namespace RestBucks.Infrastructure.WebApi
-{
- using Castle.DynamicProxy;
-
- // TODO Move to Nancy error handling pipeline
- public class RestBucksHttpErrorHandler : IInterceptor
- {
- public void Intercept(IInvocation invocation)
- {
- try
- {
- invocation.Proceed();
- }
- catch (InvalidOrderOperationException ex)
- {
- invocation.ReturnValue = ResponseHelpers.BadRequest(null, content: ex.Message);
- }
- }
- }
-}
View
1 RestBucks/RestBucks.csproj
@@ -154,7 +154,6 @@
<Compile Include="Infrastructure\InvalidOrderOperationException.cs" />
<Compile Include="Infrastructure\RequestExtensions.cs" />
<Compile Include="Infrastructure\UriSegment.cs" />
- <Compile Include="Infrastructure\WebApi\RestBucksHttpErrorHandler.cs" />
<Compile Include="Resources\Orders\TrashHandler.cs" />
<Compile Include="Resources\Orders\Representations\OrderRepresentationMapper.cs" />
<Compile Include="Resources\Orders\Representations\PaymentRepresentation.cs" />

0 comments on commit 398e9c9

Please sign in to comment.