Created
August 11, 2016 13:41
-
-
Save tugberkugurlu/ced21740cace9768e596bc9a7dd79543 to your computer and use it in GitHub Desktop.
UseFakeMiddleware and UseCountingMiddleware extensions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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