aspnetcore/src/Microsoft.AspNet.TestHost/TestServer.cs

122 lines
4.0 KiB
C#

// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNet.Hosting;
using Microsoft.AspNet.Hosting.Server;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Http.Features;
using Context = Microsoft.AspNet.Hosting.Internal.HostingApplication.Context;
namespace Microsoft.AspNet.TestHost
{
public class TestServer : IServer
{
private const string DefaultEnvironmentName = "Development";
private const string ServerName = nameof(TestServer);
private IWebApplication _appInstance;
private bool _disposed = false;
private IHttpApplication<Context> _application;
public TestServer(IWebApplicationBuilder builder)
{
if (!builder.Settings.ContainsKey(WebApplicationDefaults.CaptureStartupErrorsKey))
{
builder.UseCaptureStartupErrors(false);
}
var application = builder.UseServer(this).Build();
application.Start();
_appInstance = application;
}
public Uri BaseAddress { get; set; } = new Uri("http://localhost/");
public IWebApplication Application
{
get
{
return _appInstance;
}
}
IFeatureCollection IServer.Features { get; }
public HttpMessageHandler CreateHandler()
{
var pathBase = BaseAddress == null ? PathString.Empty : PathString.FromUriComponent(BaseAddress);
return new ClientHandler(pathBase, _application);
}
public HttpClient CreateClient()
{
return new HttpClient(CreateHandler()) { BaseAddress = BaseAddress };
}
public WebSocketClient CreateWebSocketClient()
{
var pathBase = BaseAddress == null ? PathString.Empty : PathString.FromUriComponent(BaseAddress);
return new WebSocketClient(pathBase, _application);
}
/// <summary>
/// Begins constructing a request message for submission.
/// </summary>
/// <param name="path"></param>
/// <returns><see cref="RequestBuilder"/> to use in constructing additional request details.</returns>
public RequestBuilder CreateRequest(string path)
{
return new RequestBuilder(this, path);
}
public void Dispose()
{
if (!_disposed)
{
_disposed = true;
_appInstance.Dispose();
}
}
void IServer.Start<TContext>(IHttpApplication<TContext> application)
{
_application = new ApplicationWrapper<Context>((IHttpApplication<Context>)application, () =>
{
if (_disposed)
{
throw new ObjectDisposedException(GetType().FullName);
}
});
}
private class ApplicationWrapper<TContext> : IHttpApplication<TContext>
{
private readonly IHttpApplication<TContext> _application;
private readonly Action _preProcessRequestAsync;
public ApplicationWrapper(IHttpApplication<TContext> application, Action preProcessRequestAsync)
{
_application = application;
_preProcessRequestAsync = preProcessRequestAsync;
}
public TContext CreateContext(IFeatureCollection contextFeatures)
{
return _application.CreateContext(contextFeatures);
}
public void DisposeContext(TContext context, Exception exception)
{
_application.DisposeContext(context, exception);
}
public Task ProcessRequestAsync(TContext context)
{
_preProcessRequestAsync();
return _application.ProcessRequestAsync(context);
}
}
}
}