Permalink
Browse files

Merge pull request #770 from grumpydev/ConfigurableModuleImprovements

Configurable module improvements
  • Loading branch information...
2 parents e5f8dc1 + 354af64 commit 0a5b5510d4b2584f3c4e13333c589e8512c4d31d @thecodejunkie thecodejunkie committed Oct 2, 2012
@@ -85,7 +85,7 @@ public ConfigurableNancyModuleConfigurator Before(BeforePipeline before)
/// <remarks>This will add a route with a condition that is always evaluates to <see langword="true"/> and an action that returns <see cref="HttpStatusCode.OK"/>.</remarks>
public ConfigurableNancyModuleConfigurator Delete(string path)
{
- return this.Delete(path, condition => true, action => HttpStatusCode.OK);
+ return this.Delete(path, condition => true, (action, module) => HttpStatusCode.OK);
}
/// <summary>
@@ -95,7 +95,7 @@ public ConfigurableNancyModuleConfigurator Delete(string path)
/// <param name="action">The action that should be invoked by the route.</param>
/// <returns>An instance to the current <see cref="ConfigurableNancyModuleConfigurator"/>.</returns>
/// <remarks>This will add a route with a condition that is always evaluates to <see langword="true"/>.</remarks>
- public ConfigurableNancyModuleConfigurator Delete(string path, Func<object, dynamic> action)
+ public ConfigurableNancyModuleConfigurator Delete(string path, Func<object, NancyModule, dynamic> action)
{
return this.Delete(path, condition => true, action);
}
@@ -107,7 +107,7 @@ public ConfigurableNancyModuleConfigurator Delete(string path, Func<object, dyna
/// <param name="condition">The condition that has to be furfilled in order for the route to be invoked</param>
/// <param name="action">The action that should be invoked by the route.</param>
/// <returns>An instance to the current <see cref="ConfigurableNancyModuleConfigurator"/>.</returns>
- public ConfigurableNancyModuleConfigurator Delete(string path, Func<NancyContext, bool> condition, Func<object, dynamic> action)
+ public ConfigurableNancyModuleConfigurator Delete(string path, Func<NancyContext, bool> condition, Func<object, NancyModule, dynamic> action)
{
this.module.Delete[path, GetSafeRouteCondition(condition)] = GetSafeRouteAction(action);
return this;
@@ -121,7 +121,7 @@ public ConfigurableNancyModuleConfigurator Delete(string path, Func<NancyContext
/// <remarks>This will add a route with a condition that is always evaluates to <see langword="true"/> and an action that returns <see cref="HttpStatusCode.OK"/>.</remarks>
public ConfigurableNancyModuleConfigurator Get(string path)
{
- return this.Get(path, condition => true, action => HttpStatusCode.OK);
+ return this.Get(path, condition => true, (action, module) => HttpStatusCode.OK);
}
/// <summary>
@@ -131,7 +131,7 @@ public ConfigurableNancyModuleConfigurator Get(string path)
/// <param name="action">The action that should be invoked by the route.</param>
/// <returns>An instance to the current <see cref="ConfigurableNancyModuleConfigurator"/>.</returns>
/// <remarks>This will add a route with a condition that is always evaluates to <see langword="true"/>.</remarks>
- public ConfigurableNancyModuleConfigurator Get(string path, Func<object, dynamic> action)
+ public ConfigurableNancyModuleConfigurator Get(string path, Func<object, NancyModule, dynamic> action)
{
return this.Get(path, condition => true, action);
}
@@ -143,7 +143,7 @@ public ConfigurableNancyModuleConfigurator Get(string path, Func<object, dynamic
/// <param name="condition">The condition that has to be furfilled in order for the route to be invoked</param>
/// <param name="action">The action that should be invoked by the route.</param>
/// <returns>An instance to the current <see cref="ConfigurableNancyModuleConfigurator"/>.</returns>
- public ConfigurableNancyModuleConfigurator Get(string path, Func<NancyContext, bool> condition, Func<object, dynamic> action)
+ public ConfigurableNancyModuleConfigurator Get(string path, Func<NancyContext, bool> condition, Func<object, NancyModule, dynamic> action)
{
this.module.Get[path, GetSafeRouteCondition(condition)] = GetSafeRouteAction(action);
return this;
@@ -157,7 +157,7 @@ public ConfigurableNancyModuleConfigurator Get(string path, Func<NancyContext, b
/// <remarks>This will add a route with a condition that is always evaluates to <see langword="true"/> and an action that returns <see cref="HttpStatusCode.OK"/>.</remarks>
public ConfigurableNancyModuleConfigurator Patch(string path)
{
- return this.Patch(path, condition => true, action => HttpStatusCode.OK);
+ return this.Patch(path, condition => true, (action, module) => HttpStatusCode.OK);
}
/// <summary>
@@ -167,7 +167,7 @@ public ConfigurableNancyModuleConfigurator Patch(string path)
/// <param name="action">The action that should be invoked by the route.</param>
/// <returns>An instance to the current <see cref="ConfigurableNancyModuleConfigurator"/>.</returns>
/// <remarks>This will add a route with a condition that is always evaluates to <see langword="true"/>.</remarks>
- public ConfigurableNancyModuleConfigurator Patch(string path, Func<object, dynamic> action)
+ public ConfigurableNancyModuleConfigurator Patch(string path, Func<object, NancyModule, dynamic> action)
{
return this.Patch(path, condition => true, action);
}
@@ -179,7 +179,7 @@ public ConfigurableNancyModuleConfigurator Patch(string path, Func<object, dynam
/// <param name="condition">The condition that has to be furfilled in order for the route to be invoked</param>
/// <param name="action">The action that should be invoked by the route.</param>
/// <returns>An instance to the current <see cref="ConfigurableNancyModuleConfigurator"/>.</returns>
- public ConfigurableNancyModuleConfigurator Patch(string path, Func<NancyContext, bool> condition, Func<object, dynamic> action)
+ public ConfigurableNancyModuleConfigurator Patch(string path, Func<NancyContext, bool> condition, Func<object, NancyModule, dynamic> action)
{
this.module.Patch[path, GetSafeRouteCondition(condition)] = GetSafeRouteAction(action);
return this;
@@ -193,7 +193,7 @@ public ConfigurableNancyModuleConfigurator Patch(string path, Func<NancyContext,
/// <remarks>This will add a route with a condition that is always evaluates to <see langword="true"/> and an action that returns <see cref="HttpStatusCode.OK"/>.</remarks>
public ConfigurableNancyModuleConfigurator Post(string path)
{
- return this.Post(path, condition => true, action => HttpStatusCode.OK);
+ return this.Post(path, condition => true, (action, module) => HttpStatusCode.OK);
}
/// <summary>
@@ -203,7 +203,7 @@ public ConfigurableNancyModuleConfigurator Post(string path)
/// <param name="action">The action that should be invoked by the route.</param>
/// <returns>An instance to the current <see cref="ConfigurableNancyModuleConfigurator"/>.</returns>
/// <remarks>This will add a route with a condition that is always evaluates to <see langword="true"/>.</remarks>
- public ConfigurableNancyModuleConfigurator Post(string path, Func<object, dynamic> action)
+ public ConfigurableNancyModuleConfigurator Post(string path, Func<object, NancyModule, dynamic> action)
{
return this.Post(path, condition => true, action);
}
@@ -215,7 +215,7 @@ public ConfigurableNancyModuleConfigurator Post(string path, Func<object, dynami
/// <param name="condition">The condition that has to be furfilled in order for the route to be invoked</param>
/// <param name="action">The action that should be invoked by the route.</param>
/// <returns>An instance to the current <see cref="ConfigurableNancyModuleConfigurator"/>.</returns>
- public ConfigurableNancyModuleConfigurator Post(string path, Func<NancyContext, bool> condition, Func<object, dynamic> action)
+ public ConfigurableNancyModuleConfigurator Post(string path, Func<NancyContext, bool> condition, Func<object, NancyModule, dynamic> action)
{
this.module.Post[path, GetSafeRouteCondition(condition)] = GetSafeRouteAction(action);
return this;
@@ -229,7 +229,7 @@ public ConfigurableNancyModuleConfigurator Post(string path, Func<NancyContext,
/// <remarks>This will add a route with a condition that is always evaluates to <see langword="true"/> and an action that returns <see cref="HttpStatusCode.OK"/>.</remarks>
public ConfigurableNancyModuleConfigurator Put(string path)
{
- return this.Put(path, condition => true, action => HttpStatusCode.OK);
+ return this.Put(path, condition => true, (action, module) => HttpStatusCode.OK);
}
/// <summary>
@@ -239,7 +239,7 @@ public ConfigurableNancyModuleConfigurator Put(string path)
/// <param name="action">The action that should be invoked by the route.</param>
/// <returns>An instance to the current <see cref="ConfigurableNancyModuleConfigurator"/>.</returns>
/// <remarks>This will add a route with a condition that is always evaluates to <see langword="true"/>.</remarks>
- public ConfigurableNancyModuleConfigurator Put(string path, Func<object, dynamic> action)
+ public ConfigurableNancyModuleConfigurator Put(string path, Func<object, NancyModule, dynamic> action)
{
return this.Put(path, condition => true, action);
}
@@ -251,7 +251,7 @@ public ConfigurableNancyModuleConfigurator Put(string path, Func<object, dynamic
/// <param name="condition">The condition that has to be furfilled in order for the route to be invoked</param>
/// <param name="action">The action that should be invoked by the route.</param>
/// <returns>An instance to the current <see cref="ConfigurableNancyModuleConfigurator"/>.</returns>
- public ConfigurableNancyModuleConfigurator Put(string path, Func<NancyContext, bool> condition, Func<object, dynamic> action)
+ public ConfigurableNancyModuleConfigurator Put(string path, Func<NancyContext, bool> condition, Func<object, NancyModule, dynamic> action)
{
this.module.Post[path, GetSafeRouteCondition(condition)] = GetSafeRouteAction(action);
return this;
@@ -265,7 +265,7 @@ public ConfigurableNancyModuleConfigurator Put(string path, Func<NancyContext, b
/// <remarks>This will add a route with a condition that is always evaluates to <see langword="true"/> and an action that returns <see cref="HttpStatusCode.OK"/>.</remarks>
public ConfigurableNancyModuleConfigurator Options(string path)
{
- return this.Patch(path, condition => true, action => HttpStatusCode.OK);
+ return this.Patch(path, condition => true, (action, module) => HttpStatusCode.OK);
}
/// <summary>
@@ -275,7 +275,7 @@ public ConfigurableNancyModuleConfigurator Options(string path)
/// <param name="action">The action that should be invoked by the route.</param>
/// <returns>An instance to the current <see cref="ConfigurableNancyModuleConfigurator"/>.</returns>
/// <remarks>This will add a route with a condition that is always evaluates to <see langword="true"/>.</remarks>
- public ConfigurableNancyModuleConfigurator Options(string path, Func<object, dynamic> action)
+ public ConfigurableNancyModuleConfigurator Options(string path, Func<object, NancyModule, dynamic> action)
{
return this.Options(path, condition => true, action);
}
@@ -287,15 +287,20 @@ public ConfigurableNancyModuleConfigurator Options(string path, Func<object, dyn
/// <param name="condition">The condition that has to be furfilled in order for the route to be invoked</param>
/// <param name="action">The action that should be invoked by the route.</param>
/// <returns>An instance to the current <see cref="ConfigurableNancyModuleConfigurator"/>.</returns>
- public ConfigurableNancyModuleConfigurator Options(string path, Func<NancyContext, bool> condition, Func<object, dynamic> action)
+ public ConfigurableNancyModuleConfigurator Options(string path, Func<NancyContext, bool> condition, Func<object, NancyModule, dynamic> action)
{
this.module.Options[path, GetSafeRouteCondition(condition)] = GetSafeRouteAction(action);
return this;
}
- private static Func<object, dynamic> GetSafeRouteAction(Func<object, dynamic> action)
+ private Func<object, dynamic> GetSafeRouteAction(Func<object, NancyModule, dynamic> action)
{
- return action ?? (x => HttpStatusCode.OK);
+ if (action == null)
+ {
+ return x => HttpStatusCode.OK;
+ }
+
+ return x => action.Invoke(x, this.module);
}
private static Func<NancyContext, bool> GetSafeRouteCondition(Func<NancyContext, bool> condition)
@@ -19,7 +19,7 @@ public void Should_return_int_value_from_get_route_as_response_with_status_code_
// Given
var module = new ConfigurableNancyModule(with =>
{
- with.Get("/int", x => 200);
+ with.Get("/int", (x,m) => 200);
});
var browser = new Browser(with =>
@@ -40,7 +40,7 @@ public void Should_return_string_value_from_get_route_as_response_with_content_s
// Given
var module = new ConfigurableNancyModule(with =>
{
- with.Get("/string", x => "hello");
+ with.Get("/string", (x, m) => "hello");
});
var browser = new Browser(with =>
@@ -61,7 +61,7 @@ public void Should_return_httpstatuscode_value_from_get_route_as_response_with_c
// Given
var module = new ConfigurableNancyModule(with =>
{
- with.Get("/httpstatuscode", x => HttpStatusCode.Accepted);
+ with.Get("/httpstatuscode", (x, m) => HttpStatusCode.Accepted);
});
var browser = new Browser(with =>
@@ -82,7 +82,7 @@ public void Should_return_action_value_as_response_with_content_set_as_value()
// Given
var module = new ConfigurableNancyModule(with =>
{
- with.Get("/action", x =>
+ with.Get("/action", (x, m) =>
{
Action<Stream> result = stream =>
{
@@ -116,7 +116,7 @@ public void Should_add_negotiated_headers_to_response()
var module = new ConfigurableNancyModule(with =>
{
- with.Get("/headers", x =>
+ with.Get("/headers", (x, m) =>
{
var context =
new NancyContext { NegotiationContext = new NegotiationContext() };
@@ -154,7 +154,7 @@ public void Should_apply_default_accept_when_no_accept_header_sent()
with.Module(new ConfigurableNancyModule(x =>
{
- x.Get("/", parameters =>
+ x.Get("/", (parameters, module) =>
{
var context =
new NancyContext { NegotiationContext = new NegotiationContext() };
@@ -184,7 +184,7 @@ public void Should_boost_html_priority_if_set_to_the_same_priority_as_others()
with.Module(new ConfigurableNancyModule(x =>
{
- x.Get("/", parameters =>
+ x.Get("/", (parameters, module) =>
{
var context =
new NancyContext { NegotiationContext = new NegotiationContext() };
@@ -223,7 +223,7 @@ public void Should_override_with_extension()
with.Module(new ConfigurableNancyModule(x =>
{
- x.Get("/test", parameters =>
+ x.Get("/test", (parameters, module) =>
{
var context =
new NancyContext { NegotiationContext = new NegotiationContext() };
@@ -434,7 +434,7 @@ public void Should_throw_exception_if_view_location_fails()
{
with.ResponseProcessor<ViewProcessor>();
- with.Module(new FakeModuleInvalidViewName());
+ with.Module(new ConfigurableNancyModule(x => x.Get("/FakeModuleInvalidViewName", CreateNegotiatedResponse(neg => neg.WithView("blahblahblah")))));
});
// When
@@ -479,21 +479,17 @@ public void Should_use_next_processor_if_processor_returns_null()
Assert.True(bodyResult.StartsWith("application/xml"), string.Format("Body should have started with 'application/xml' but was actually '{0}'", bodyResult));
}
- private static Func<dynamic, dynamic> CreateNegotiatedResponse(Action<Negotiator> action = null)
+ private static Func<dynamic, NancyModule, dynamic> CreateNegotiatedResponse(Action<Negotiator> action = null)
{
- var context =
- new NancyContext { NegotiationContext = new NegotiationContext() };
-
- var negotiator =
- new Negotiator(context);
+ return (parameters, module) =>
+ {
+ var negotiator = new Negotiator(module.Context);
- if (action != null)
- {
- action.Invoke(negotiator);
- }
+ if (action != null)
+ {
+ action.Invoke(negotiator);
+ }
- return parameters =>
- {
return negotiator;
};
}
@@ -584,13 +580,5 @@ public Response Process(MediaRange requestedMediaRange, dynamic model, NancyCont
return (string) model;
}
}
-
- public class FakeModuleInvalidViewName : NancyModule
- {
- public FakeModuleInvalidViewName()
- {
- Get["/FakeModuleInvalidViewName"] = _ => View["blahblahblah"];
- }
- }
}
}

0 comments on commit 0a5b551

Please sign in to comment.