Test code nitpicks.

This commit is contained in:
Cesar Blum Silveira 2016-05-26 20:34:46 -07:00
parent cea5fbbafa
commit 6d3a416f0e
6 changed files with 33 additions and 45 deletions

View File

@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Server.Kestrel.Infrastructure;
using Xunit; using Xunit;
namespace Microsoft.AspNetCore.Server.KestrelTests namespace Microsoft.AspNetCore.Server.KestrelTests
@ -68,7 +69,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
[InlineData("GET / HTTP/1.0\rA")] [InlineData("GET / HTTP/1.0\rA")]
public async Task TestBadRequestLines(string request) public async Task TestBadRequestLines(string request)
{ {
using (var server = new TestServer(context => { return Task.FromResult(0); })) using (var server = new TestServer(context => TaskUtilities.CompletedTask))
{ {
using (var connection = server.CreateConnection()) using (var connection = server.CreateConnection())
{ {
@ -85,7 +86,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
[InlineData("GET / HTTP/1.0\rA")] [InlineData("GET / HTTP/1.0\rA")]
public async Task ServerClosesConnectionAsSoonAsBadRequestLineIsDetected(string request) public async Task ServerClosesConnectionAsSoonAsBadRequestLineIsDetected(string request)
{ {
using (var server = new TestServer(context => { return Task.FromResult(0); })) using (var server = new TestServer(context => TaskUtilities.CompletedTask))
{ {
using (var connection = server.CreateConnection()) using (var connection = server.CreateConnection())
{ {

View File

@ -11,6 +11,7 @@ using System.Threading.Tasks;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features; using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Server.Kestrel; using Microsoft.AspNetCore.Server.Kestrel;
using Microsoft.AspNetCore.Server.Kestrel.Infrastructure;
using Xunit; using Xunit;
namespace Microsoft.AspNetCore.Server.KestrelTests namespace Microsoft.AspNetCore.Server.KestrelTests
@ -1062,7 +1063,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
using (var server = new TestServer(httpContext => using (var server = new TestServer(httpContext =>
{ {
httpContext.Abort(); httpContext.Abort();
return Task.FromResult(0); return TaskUtilities.CompletedTask;
}, testContext)) }, testContext))
{ {
using (var connection = server.CreateConnection()) using (var connection = server.CreateConnection())

View File

@ -16,6 +16,7 @@ using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features; using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Server.Kestrel.Filter; using Microsoft.AspNetCore.Server.Kestrel.Filter;
using Microsoft.AspNetCore.Server.Kestrel.Https; using Microsoft.AspNetCore.Server.Kestrel.Https;
using Microsoft.AspNetCore.Server.Kestrel.Infrastructure;
using Microsoft.AspNetCore.Testing.xunit; using Microsoft.AspNetCore.Testing.xunit;
using Xunit; using Xunit;
@ -116,13 +117,12 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
new NoOpConnectionFilter()) new NoOpConnectionFilter())
); );
RequestDelegate app = context => using (var server = new TestServer(context =>
{ {
Assert.Equal(context.Features.Get<ITlsConnectionFeature>(), null); Assert.Equal(context.Features.Get<ITlsConnectionFeature>(), null);
return context.Response.WriteAsync("hello world"); return context.Response.WriteAsync("hello world");
}; },
serviceContext, _serverAddress))
using (var server = new TestServer(app, serviceContext, _serverAddress))
{ {
using (var client = new HttpClient(GetHandler())) using (var client = new HttpClient(GetHandler()))
{ {
@ -153,8 +153,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
new NoOpConnectionFilter()) new NoOpConnectionFilter())
); );
RequestDelegate app = context => Task.FromResult(0); using (var server = new TestServer(context => TaskUtilities.CompletedTask, serviceContext, _serverAddress))
using (var server = new TestServer(app, serviceContext, _serverAddress))
{ {
using (var client = new TcpClient()) using (var client = new TcpClient())
{ {
@ -182,16 +181,15 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
new NoOpConnectionFilter()) new NoOpConnectionFilter())
); );
RequestDelegate app = context => using (var server = new TestServer(context =>
{ {
var tlsFeature = context.Features.Get<ITlsConnectionFeature>(); var tlsFeature = context.Features.Get<ITlsConnectionFeature>();
Assert.NotNull(tlsFeature); Assert.NotNull(tlsFeature);
Assert.NotNull(tlsFeature.ClientCertificate); Assert.NotNull(tlsFeature.ClientCertificate);
Assert.NotNull(context.Connection.ClientCertificate); Assert.NotNull(context.Connection.ClientCertificate);
return context.Response.WriteAsync("hello world"); return context.Response.WriteAsync("hello world");
}; },
serviceContext, _serverAddress))
using (var server = new TestServer(app, serviceContext, _serverAddress))
{ {
using (var client = new TcpClient()) using (var client = new TcpClient())
{ {
@ -219,9 +217,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
new NoOpConnectionFilter()) new NoOpConnectionFilter())
); );
RequestDelegate app = context => context.Response.WriteAsync(context.Request.Scheme); using (var server = new TestServer(context => context.Response.WriteAsync(context.Request.Scheme), serviceContext, _serverAddress))
using (var server = new TestServer(app, serviceContext, _serverAddress))
{ {
using (var client = new HttpClient(GetHandler())) using (var client = new HttpClient(GetHandler()))
{ {
@ -245,12 +241,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
new NoOpConnectionFilter()) new NoOpConnectionFilter())
); );
RequestDelegate app = context => using (var server = new TestServer(context => context.Response.WriteAsync("hello world"), serviceContext, _serverAddress))
{
return context.Response.WriteAsync("hello world");
};
using (var server = new TestServer(app, serviceContext, _serverAddress))
{ {
// SslStream is used to ensure the certificate is actually passed to the server // SslStream is used to ensure the certificate is actually passed to the server
// HttpClient might not send the certificate because it is invalid or it doesn't match any // HttpClient might not send the certificate because it is invalid or it doesn't match any
@ -286,9 +277,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
new NoOpConnectionFilter()) new NoOpConnectionFilter())
); );
RequestDelegate app = context => Task.FromResult(0); using (var server = new TestServer(context => TaskUtilities.CompletedTask, serviceContext, _serverAddress))
using (var server = new TestServer(app, serviceContext, _serverAddress))
{ {
using (var client = new TcpClient()) using (var client = new TcpClient())
{ {
@ -315,9 +304,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
new NoOpConnectionFilter()) new NoOpConnectionFilter())
); );
RequestDelegate app = context => Task.FromResult(0); using (var server = new TestServer(context => TaskUtilities.CompletedTask, serviceContext, _serverAddress))
using (var server = new TestServer(app, serviceContext, _serverAddress))
{ {
using (var client = new TcpClient()) using (var client = new TcpClient())
{ {
@ -342,9 +329,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
new NoOpConnectionFilter()) new NoOpConnectionFilter())
); );
RequestDelegate app = context => Task.FromResult(0); using (var server = new TestServer(context => TaskUtilities.CompletedTask, serviceContext, _serverAddress))
using (var server = new TestServer(app, serviceContext, _serverAddress))
{ {
using (var client = new TcpClient()) using (var client = new TcpClient())
{ {

View File

@ -5,6 +5,7 @@ using System;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Server.Kestrel.Http; using Microsoft.AspNetCore.Server.Kestrel.Http;
using Microsoft.AspNetCore.Server.Kestrel.Infrastructure;
namespace Microsoft.AspNetCore.Server.KestrelTests.TestHelpers namespace Microsoft.AspNetCore.Server.KestrelTests.TestHelpers
{ {
@ -16,7 +17,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests.TestHelpers
public Task FlushAsync(CancellationToken cancellationToken) public Task FlushAsync(CancellationToken cancellationToken)
{ {
return Task.FromResult(0); return TaskUtilities.CompletedTask;
} }
public void ProduceContinue() public void ProduceContinue()
@ -29,7 +30,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests.TestHelpers
public Task WriteAsync(ArraySegment<byte> data, CancellationToken cancellationToken) public Task WriteAsync(ArraySegment<byte> data, CancellationToken cancellationToken)
{ {
return Task.FromResult(0); return TaskUtilities.CompletedTask;
} }
} }
} }

View File

@ -26,7 +26,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests.TestHelpers
public Task WriteAsync(ArraySegment<byte> buffer, bool chunk = false, CancellationToken cancellationToken = default(CancellationToken)) public Task WriteAsync(ArraySegment<byte> buffer, bool chunk = false, CancellationToken cancellationToken = default(CancellationToken))
{ {
return Task.FromResult(0); return TaskUtilities.CompletedTask;
} }
} }
} }

View File

@ -76,7 +76,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
Task IFrameControl.WriteAsync(ArraySegment<byte> data, CancellationToken cancellationToken) Task IFrameControl.WriteAsync(ArraySegment<byte> data, CancellationToken cancellationToken)
{ {
return Task.FromResult(0); return TaskUtilities.CompletedTask;
} }
void IFrameControl.Flush() void IFrameControl.Flush()
@ -85,7 +85,7 @@ namespace Microsoft.AspNetCore.Server.KestrelTests
Task IFrameControl.FlushAsync(CancellationToken cancellationToken) Task IFrameControl.FlushAsync(CancellationToken cancellationToken)
{ {
return Task.FromResult(0); return TaskUtilities.CompletedTask;
} }
public void Dispose() public void Dispose()