public void UseWebApi_UsesAdapterAndConfigServices() { var config = new HttpConfiguration(); var bufferPolicySelector = new Mock <IHostBufferPolicySelector>().Object; Mock <IExceptionLogger> loggerMock = new Mock <IExceptionLogger>(); Mock <IExceptionHandler> handlerMock = new Mock <IExceptionHandler>(); config.Services.Replace(typeof(IHostBufferPolicySelector), bufferPolicySelector); config.Services.Replace(typeof(IExceptionLogger), loggerMock.Object); config.Services.Replace(typeof(IExceptionHandler), handlerMock.Object); IExceptionLogger exceptionLogger = null; IExceptionHandler exceptionHandler = null; var appBuilder = new Mock <IAppBuilder>(); appBuilder .Setup(ab => ab.Use( typeof(HttpMessageHandlerAdapter), It.Is <HttpMessageHandlerOptions>((o) => ((HttpServer)o.MessageHandler).Configuration == config && o.BufferPolicySelector == bufferPolicySelector))) .Callback <object, object[]>((i, args) => { HttpMessageHandlerOptions options = (HttpMessageHandlerOptions)args[0]; exceptionLogger = options.ExceptionLogger; exceptionHandler = options.ExceptionHandler; }) .Returns(appBuilder.Object) .Verifiable(); IAppBuilder returnedAppBuilder = appBuilder.Object.UseWebApi(config); Assert.Equal(appBuilder.Object, returnedAppBuilder); appBuilder.Verify(); AssertDelegatesTo(loggerMock, exceptionLogger); AssertDelegatesTo(handlerMock, exceptionHandler); }
private static IAppBuilder UseMessageHandler(this IAppBuilder builder, HttpMessageHandlerOptions options) { Contract.Assert(builder != null); Contract.Assert(options != null); return builder.Use(typeof(HttpMessageHandlerAdapter), options); }
public async Task UseWebApiWithHttpServer_UsesAdapterAndConfigServices() { // Arrange using (CancellationTokenSource tokenSource = CreateCancellationTokenSource()) { HttpConfiguration config = new HttpConfiguration(); IHostBufferPolicySelector bufferPolicySelector = new Mock <IHostBufferPolicySelector>().Object; Mock <IExceptionLogger> loggerMock = new Mock <IExceptionLogger>(); Mock <IExceptionHandler> handlerMock = new Mock <IExceptionHandler>(); config.Services.Replace(typeof(IHostBufferPolicySelector), bufferPolicySelector); config.Services.Replace(typeof(IExceptionLogger), loggerMock.Object); config.Services.Replace(typeof(IExceptionHandler), handlerMock.Object); HttpServer httpServer = new Mock <HttpServer>(config).Object; IExceptionLogger exceptionLogger = null; IExceptionHandler exceptionHandler = null; Mock <IAppBuilder> appBuilderMock = new Mock <IAppBuilder>(); CancellationToken expectedAppDisposing = tokenSource.Token; IDictionary <string, object> properties = new Dictionary <string, object> { { "host.OnAppDisposing", expectedAppDisposing } }; appBuilderMock.SetupGet(b => b.Properties).Returns(properties); appBuilderMock .Setup( ab => ab.Use( typeof(HttpMessageHandlerAdapter), It.Is <HttpMessageHandlerOptions>( (o) => o.MessageHandler == httpServer && o.BufferPolicySelector == bufferPolicySelector && o.AppDisposing == expectedAppDisposing ) ) ) .Callback <object, object[]>( (i, args) => { HttpMessageHandlerOptions options = (HttpMessageHandlerOptions)args[0]; exceptionLogger = options.ExceptionLogger; exceptionHandler = options.ExceptionHandler; } ) .Returns(appBuilderMock.Object) .Verifiable(); // Act IAppBuilder returnedAppBuilder = appBuilderMock.Object.UseWebApi(httpServer); // Assert Assert.Equal(appBuilderMock.Object, returnedAppBuilder); appBuilderMock.Verify(); await AssertDelegatesToAsync(loggerMock, exceptionLogger); await AssertDelegatesToAsync(handlerMock, exceptionHandler); } }
public void ExceptionHandlerGet_ReturnsSpecifiedInstance() { // Arrange HttpMessageHandlerOptions product = CreateProductUnderTest(); IExceptionHandler expectedExceptionHandler = CreateDummyExceptionHandler(); product.ExceptionHandler = expectedExceptionHandler; // Act IExceptionHandler exceptionHandler = product.ExceptionHandler; // Assert Assert.Same(expectedExceptionHandler, exceptionHandler); }
public void BufferPolicySelectorGet_ReturnsSpecifiedInstance() { // Arrange HttpMessageHandlerOptions product = CreateProductUnderTest(); IHostBufferPolicySelector expectedBufferPolicySelector = CreateDummyBufferPolicy(); product.BufferPolicySelector = expectedBufferPolicySelector; // Act IHostBufferPolicySelector bufferPolicy = product.BufferPolicySelector; // Assert Assert.Same(expectedBufferPolicySelector, bufferPolicy); }
/// <summary>Initializes a new instance of the <see cref="HttpMessageHandlerAdapter"/> class.</summary> /// <param name="next">The next component in the pipeline.</param> /// <param name="options">The options to configure this adapter.</param> public HttpMessageHandlerAdapter(OwinMiddleware next, HttpMessageHandlerOptions options) : base(next) { if (options == null) { throw new ArgumentNullException("options"); } _messageHandler = options.MessageHandler; if (_messageHandler == null) { throw new ArgumentException(Error.Format(OwinResources.TypePropertyMustNotBeNull, typeof(HttpMessageHandlerOptions).Name, "MessageHandler"), "options"); } _messageInvoker = new HttpMessageInvoker(_messageHandler); _bufferPolicySelector = options.BufferPolicySelector; if (_bufferPolicySelector == null) { throw new ArgumentException(Error.Format(OwinResources.TypePropertyMustNotBeNull, typeof(HttpMessageHandlerOptions).Name, "BufferPolicySelector"), "options"); } _exceptionLogger = options.ExceptionLogger; if (_exceptionLogger == null) { throw new ArgumentException(Error.Format(OwinResources.TypePropertyMustNotBeNull, typeof(HttpMessageHandlerOptions).Name, "ExceptionLogger"), "options"); } _exceptionHandler = options.ExceptionHandler; if (_exceptionHandler == null) { throw new ArgumentException(Error.Format(OwinResources.TypePropertyMustNotBeNull, typeof(HttpMessageHandlerOptions).Name, "ExceptionHandler"), "options"); } _appDisposing = options.AppDisposing; if (_appDisposing.CanBeCanceled) { _appDisposing.Register(OnAppDisposing); } }
/// <summary>Initializes a new instance of the <see cref="HttpMessageHandlerAdapter"/> class.</summary> /// <param name="next">The next component in the pipeline.</param> /// <param name="options">The options to configure this adapter.</param> public HttpMessageHandlerAdapter(OwinMiddleware next, HttpMessageHandlerOptions options) : base(next) { if (options == null) { throw new ArgumentNullException("options"); } _messageHandler = options.MessageHandler; if (_messageHandler == null) { throw new ArgumentException(Error.Format(OwinResources.TypePropertyMustNotBeNull, typeof(HttpMessageHandlerOptions).Name, "MessageHandler"), "options"); } _messageInvoker = new HttpMessageInvoker(_messageHandler); _bufferPolicySelector = options.BufferPolicySelector; if (_bufferPolicySelector == null) { throw new ArgumentException(Error.Format(OwinResources.TypePropertyMustNotBeNull, typeof(HttpMessageHandlerOptions).Name, "BufferPolicySelector"), "options"); } _exceptionLogger = options.ExceptionLogger; if (_exceptionLogger == null) { throw new ArgumentException(Error.Format(OwinResources.TypePropertyMustNotBeNull, typeof(HttpMessageHandlerOptions).Name, "ExceptionLogger"), "options"); } _exceptionHandler = options.ExceptionHandler; if (_exceptionHandler == null) { throw new ArgumentException(Error.Format(OwinResources.TypePropertyMustNotBeNull, typeof(HttpMessageHandlerOptions).Name, "ExceptionHandler"), "options"); } _appDisposing = options.AppDisposing; if (_appDisposing.CanBeCanceled) { _appDisposing.Register(OnAppDisposing); } }
public void AppDisposingGet_ReturnsSpecifiedValue() { // Arrange using (CancellationTokenSource tokenSource = CreateCancellationTokenSource()) { HttpMessageHandlerOptions product = CreateProductUnderTest(); CancellationToken expectedAppDisposing = tokenSource.Token; product.AppDisposing = expectedAppDisposing; // Act CancellationToken appDisposing = product.AppDisposing; // Assert Assert.Equal(expectedAppDisposing, appDisposing); } }
public void MessageHandlerGet_ReturnsSpecifiedInstance() { // Arrange HttpMessageHandlerOptions product = CreateProductUnderTest(); using (HttpMessageHandler expectedMessageHandler = CreateDummyMessageHandler()) { product.MessageHandler = expectedMessageHandler; // Act HttpMessageHandler messageHandler = product.MessageHandler; // Assert Assert.Same(expectedMessageHandler, messageHandler); } }
public static Func<IDictionary<string, object>, Task> CreateWebApiAppFunc2(HttpConfiguration config, HttpMessageHandlerOptions options = null) { var bufferPolicy = new OwinBufferPolicySelector(); config.Services.Replace(typeof(IHostBufferPolicySelector), bufferPolicy); // Done just so App can get access to it. var app = new HttpServer(config); if (options == null) { options = new HttpMessageHandlerOptions() { MessageHandler = app, BufferPolicySelector = bufferPolicy, ExceptionLogger = new GlobalErrorLoggingService(), ExceptionHandler = new GlobalErrorHandlerService() }; } var handler = new HttpMessageHandlerAdapter(new NotFoundMiddleware(), options); return (env) => handler.Invoke(new OwinContext(env)); }
public static Func<IDictionary<string, object>, Task> CreateWebApiAppFunc(HttpConfiguration config) { var app = new HttpServer(config); var options = new HttpMessageHandlerOptions() { MessageHandler = app, BufferPolicySelector = new OwinBufferPolicySelector(), ExceptionLogger = new WebApiExceptionLogger(), ExceptionHandler = new WebApiExceptionHandler() }; var handler = new HttpMessageHandlerAdapter(null, options); return (env) => handler.Invoke(new OwinContext(env)); }
private static HttpMessageHandlerAdapter CreateProductUnderTest(OwinMiddleware next, HttpMessageHandlerOptions options) { return new HttpMessageHandlerAdapter(next, options); }
private static HttpMessageHandlerAdapter CreateProductUnderTest(HttpMessageHandlerOptions options) { return CreateProductUnderTest(next: null, options: options); }