Skip to content

Instantly share code, notes, and snippets.

@tugberkugurlu
Created August 11, 2016 13:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tugberkugurlu/ced21740cace9768e596bc9a7dd79543 to your computer and use it in GitHub Desktop.
Save tugberkugurlu/ced21740cace9768e596bc9a7dd79543 to your computer and use it in GitHub Desktop.
UseFakeMiddleware and UseCountingMiddleware extensions
namespace Tests
{
using AppFunc = Func<IDictionary<string, object>, Task>;
public static class AppBuilderExtensions
{
/// <summary>
/// Responds with the given status code (<paramref name="statusCode" />) and given response body (<paramref name="responseBody" />) as text/plain content.
/// </summary>
public static IAppBuilder UseFakeMiddleware(this IAppBuilder app, int statusCode, string responseBody)
{
return app.Use(new Func<AppFunc, AppFunc>(next => env =>
{
var context = new OwinContext(env);
context.Response.StatusCode = statusCode;
context.Response.Headers.Add("Content-Type", new [] { "text/plain" });
return context.Response.WriteAsync(responseBody);
}));
}
/// <summary>
/// A pass-through middleware to count the hits through <paramref name="increment" />.
/// </summary>
public static IAppBuilder UseCountingMiddleware(this IAppBuilder app, Action increment)
{
return app.Use(new Func<AppFunc, AppFunc>(next => env =>
{
increment();
return next(env);
}));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment