93 lines
3.2 KiB
C#
93 lines
3.2 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;
|
|
using System.Net.Sockets;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Server.Kestrel.Core;
|
|
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal;
|
|
using Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv;
|
|
using Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv.Internal;
|
|
using Microsoft.AspNetCore.Testing;
|
|
|
|
namespace Microsoft.AspNetCore.Server.Kestrel.FunctionalTests
|
|
{
|
|
/// <summary>
|
|
/// Summary description for TestServer
|
|
/// </summary>
|
|
public class TestServer : IDisposable
|
|
{
|
|
private LibuvTransport _transport;
|
|
private ListenOptions _listenOptions;
|
|
|
|
public TestServer(RequestDelegate app)
|
|
: this(app, new TestServiceContext())
|
|
{
|
|
}
|
|
|
|
public TestServer(RequestDelegate app, TestServiceContext context)
|
|
: this(app, context, httpContextFactory: null)
|
|
{
|
|
}
|
|
|
|
public TestServer(RequestDelegate app, TestServiceContext context, IHttpContextFactory httpContextFactory)
|
|
: this(app, context, new ListenOptions(new IPEndPoint(IPAddress.Loopback, 0)), httpContextFactory)
|
|
{
|
|
}
|
|
|
|
public TestServer(RequestDelegate app, TestServiceContext context, ListenOptions listenOptions)
|
|
: this(app, context, listenOptions, null)
|
|
{
|
|
}
|
|
|
|
public TestServer(RequestDelegate app, TestServiceContext context, ListenOptions listenOptions, IHttpContextFactory httpContextFactory)
|
|
{
|
|
_listenOptions = listenOptions;
|
|
|
|
Context = context;
|
|
TransportContext = new LibuvTransportContext()
|
|
{
|
|
AppLifetime = new LifetimeNotImplemented(),
|
|
ConnectionHandler = new ConnectionHandler<HttpContext>(listenOptions, Context, new DummyApplication(app, httpContextFactory)),
|
|
Log = new LibuvTrace(new TestApplicationErrorLogger()),
|
|
Options = new LibuvTransportOptions()
|
|
{
|
|
ThreadCount = 1,
|
|
ShutdownTimeout = TimeSpan.FromSeconds(5)
|
|
}
|
|
};
|
|
|
|
try
|
|
{
|
|
_transport = new LibuvTransport(TransportContext, _listenOptions);
|
|
_transport.BindAsync().Wait();
|
|
}
|
|
catch
|
|
{
|
|
_transport.UnbindAsync().Wait();
|
|
_transport.StopAsync().Wait();
|
|
throw;
|
|
}
|
|
}
|
|
|
|
public IPEndPoint EndPoint => _listenOptions.IPEndPoint;
|
|
public int Port => _listenOptions.IPEndPoint.Port;
|
|
public AddressFamily AddressFamily => _listenOptions.IPEndPoint.AddressFamily;
|
|
|
|
public TestServiceContext Context { get; }
|
|
public LibuvTransportContext TransportContext { get; }
|
|
|
|
public TestConnection CreateConnection()
|
|
{
|
|
return new TestConnection(Port, AddressFamily);
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
_transport.UnbindAsync().Wait();
|
|
_transport.StopAsync().Wait();
|
|
}
|
|
}
|
|
}
|