Skip to content
This repository was archived by the owner on Dec 18, 2018. It is now read-only.

Commit 6d3a416

Browse files
author
Cesar Blum Silveira
committed
Test code nitpicks.
1 parent cea5fbb commit 6d3a416

File tree

6 files changed

+33
-45
lines changed

6 files changed

+33
-45
lines changed

test/Microsoft.AspNetCore.Server.KestrelTests/BadHttpRequestTests.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Server.Kestrel.Infrastructure;
56
using Xunit;
67

78
namespace Microsoft.AspNetCore.Server.KestrelTests
@@ -68,7 +69,7 @@ public class BadHttpRequestTests
6869
[InlineData("GET / HTTP/1.0\rA")]
6970
public async Task TestBadRequestLines(string request)
7071
{
71-
using (var server = new TestServer(context => { return Task.FromResult(0); }))
72+
using (var server = new TestServer(context => TaskUtilities.CompletedTask))
7273
{
7374
using (var connection = server.CreateConnection())
7475
{
@@ -85,7 +86,7 @@ public async Task TestBadRequestLines(string request)
8586
[InlineData("GET / HTTP/1.0\rA")]
8687
public async Task ServerClosesConnectionAsSoonAsBadRequestLineIsDetected(string request)
8788
{
88-
using (var server = new TestServer(context => { return Task.FromResult(0); }))
89+
using (var server = new TestServer(context => TaskUtilities.CompletedTask))
8990
{
9091
using (var connection = server.CreateConnection())
9192
{

test/Microsoft.AspNetCore.Server.KestrelTests/EngineTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using Microsoft.AspNetCore.Http;
1212
using Microsoft.AspNetCore.Http.Features;
1313
using Microsoft.AspNetCore.Server.Kestrel;
14+
using Microsoft.AspNetCore.Server.Kestrel.Infrastructure;
1415
using Xunit;
1516

1617
namespace Microsoft.AspNetCore.Server.KestrelTests
@@ -1062,7 +1063,7 @@ public async Task NoResponseSentWhenConnectionIsClosedByServerBeforeClientFinish
10621063
using (var server = new TestServer(httpContext =>
10631064
{
10641065
httpContext.Abort();
1065-
return Task.FromResult(0);
1066+
return TaskUtilities.CompletedTask;
10661067
}, testContext))
10671068
{
10681069
using (var connection = server.CreateConnection())

test/Microsoft.AspNetCore.Server.KestrelTests/HttpsConnectionFilterTests.cs

Lines changed: 22 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using Microsoft.AspNetCore.Http.Features;
1717
using Microsoft.AspNetCore.Server.Kestrel.Filter;
1818
using Microsoft.AspNetCore.Server.Kestrel.Https;
19+
using Microsoft.AspNetCore.Server.Kestrel.Infrastructure;
1920
using Microsoft.AspNetCore.Testing.xunit;
2021
using Xunit;
2122

@@ -116,13 +117,12 @@ public async Task AllowCertificateContinuesWhenNoCertificate()
116117
new NoOpConnectionFilter())
117118
);
118119

119-
RequestDelegate app = context =>
120-
{
121-
Assert.Equal(context.Features.Get<ITlsConnectionFeature>(), null);
122-
return context.Response.WriteAsync("hello world");
123-
};
124-
125-
using (var server = new TestServer(app, serviceContext, _serverAddress))
120+
using (var server = new TestServer(context =>
121+
{
122+
Assert.Equal(context.Features.Get<ITlsConnectionFeature>(), null);
123+
return context.Response.WriteAsync("hello world");
124+
},
125+
serviceContext, _serverAddress))
126126
{
127127
using (var client = new HttpClient(GetHandler()))
128128
{
@@ -153,8 +153,7 @@ public async Task UsesProvidedServerCertificate()
153153
new NoOpConnectionFilter())
154154
);
155155

156-
RequestDelegate app = context => Task.FromResult(0);
157-
using (var server = new TestServer(app, serviceContext, _serverAddress))
156+
using (var server = new TestServer(context => TaskUtilities.CompletedTask, serviceContext, _serverAddress))
158157
{
159158
using (var client = new TcpClient())
160159
{
@@ -182,16 +181,15 @@ public async Task CertificatePassedToHttpContext()
182181
new NoOpConnectionFilter())
183182
);
184183

185-
RequestDelegate app = context =>
186-
{
187-
var tlsFeature = context.Features.Get<ITlsConnectionFeature>();
188-
Assert.NotNull(tlsFeature);
189-
Assert.NotNull(tlsFeature.ClientCertificate);
190-
Assert.NotNull(context.Connection.ClientCertificate);
191-
return context.Response.WriteAsync("hello world");
192-
};
193-
194-
using (var server = new TestServer(app, serviceContext, _serverAddress))
184+
using (var server = new TestServer(context =>
185+
{
186+
var tlsFeature = context.Features.Get<ITlsConnectionFeature>();
187+
Assert.NotNull(tlsFeature);
188+
Assert.NotNull(tlsFeature.ClientCertificate);
189+
Assert.NotNull(context.Connection.ClientCertificate);
190+
return context.Response.WriteAsync("hello world");
191+
},
192+
serviceContext, _serverAddress))
195193
{
196194
using (var client = new TcpClient())
197195
{
@@ -219,9 +217,7 @@ public async Task HttpsSchemePassedToRequestFeature()
219217
new NoOpConnectionFilter())
220218
);
221219

222-
RequestDelegate app = context => context.Response.WriteAsync(context.Request.Scheme);
223-
224-
using (var server = new TestServer(app, serviceContext, _serverAddress))
220+
using (var server = new TestServer(context => context.Response.WriteAsync(context.Request.Scheme), serviceContext, _serverAddress))
225221
{
226222
using (var client = new HttpClient(GetHandler()))
227223
{
@@ -245,12 +241,7 @@ public async Task DoesNotSupportTls10()
245241
new NoOpConnectionFilter())
246242
);
247243

248-
RequestDelegate app = context =>
249-
{
250-
return context.Response.WriteAsync("hello world");
251-
};
252-
253-
using (var server = new TestServer(app, serviceContext, _serverAddress))
244+
using (var server = new TestServer(context => context.Response.WriteAsync("hello world"), serviceContext, _serverAddress))
254245
{
255246
// SslStream is used to ensure the certificate is actually passed to the server
256247
// HttpClient might not send the certificate because it is invalid or it doesn't match any
@@ -286,9 +277,7 @@ public async Task ClientCertificateValidationGetsCalledWithNotNullParameters(Cli
286277
new NoOpConnectionFilter())
287278
);
288279

289-
RequestDelegate app = context => Task.FromResult(0);
290-
291-
using (var server = new TestServer(app, serviceContext, _serverAddress))
280+
using (var server = new TestServer(context => TaskUtilities.CompletedTask, serviceContext, _serverAddress))
292281
{
293282
using (var client = new TcpClient())
294283
{
@@ -315,9 +304,7 @@ public async Task ValidationFailureRejectsConnection(ClientCertificateMode mode)
315304
new NoOpConnectionFilter())
316305
);
317306

318-
RequestDelegate app = context => Task.FromResult(0);
319-
320-
using (var server = new TestServer(app, serviceContext, _serverAddress))
307+
using (var server = new TestServer(context => TaskUtilities.CompletedTask, serviceContext, _serverAddress))
321308
{
322309
using (var client = new TcpClient())
323310
{
@@ -342,9 +329,7 @@ public async Task RejectsConnectionOnSslPolicyErrorsWhenNoValidation(ClientCerti
342329
new NoOpConnectionFilter())
343330
);
344331

345-
RequestDelegate app = context => Task.FromResult(0);
346-
347-
using (var server = new TestServer(app, serviceContext, _serverAddress))
332+
using (var server = new TestServer(context => TaskUtilities.CompletedTask, serviceContext, _serverAddress))
348333
{
349334
using (var client = new TcpClient())
350335
{

test/Microsoft.AspNetCore.Server.KestrelTests/TestHelpers/MockFrameControl.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Threading;
66
using System.Threading.Tasks;
77
using Microsoft.AspNetCore.Server.Kestrel.Http;
8+
using Microsoft.AspNetCore.Server.Kestrel.Infrastructure;
89

910
namespace Microsoft.AspNetCore.Server.KestrelTests.TestHelpers
1011
{
@@ -16,7 +17,7 @@ public void Flush()
1617

1718
public Task FlushAsync(CancellationToken cancellationToken)
1819
{
19-
return Task.FromResult(0);
20+
return TaskUtilities.CompletedTask;
2021
}
2122

2223
public void ProduceContinue()
@@ -29,7 +30,7 @@ public void Write(ArraySegment<byte> data)
2930

3031
public Task WriteAsync(ArraySegment<byte> data, CancellationToken cancellationToken)
3132
{
32-
return Task.FromResult(0);
33+
return TaskUtilities.CompletedTask;
3334
}
3435
}
3536
}

test/Microsoft.AspNetCore.Server.KestrelTests/TestHelpers/MockSocketOuptut.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public void Write(ArraySegment<byte> buffer, bool chunk = false)
2626

2727
public Task WriteAsync(ArraySegment<byte> buffer, bool chunk = false, CancellationToken cancellationToken = default(CancellationToken))
2828
{
29-
return Task.FromResult(0);
29+
return TaskUtilities.CompletedTask;
3030
}
3131
}
3232
}

test/Microsoft.AspNetCore.Server.KestrelTests/TestInput.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ void IFrameControl.Write(ArraySegment<byte> data)
7676

7777
Task IFrameControl.WriteAsync(ArraySegment<byte> data, CancellationToken cancellationToken)
7878
{
79-
return Task.FromResult(0);
79+
return TaskUtilities.CompletedTask;
8080
}
8181

8282
void IFrameControl.Flush()
@@ -85,7 +85,7 @@ void IFrameControl.Flush()
8585

8686
Task IFrameControl.FlushAsync(CancellationToken cancellationToken)
8787
{
88-
return Task.FromResult(0);
88+
return TaskUtilities.CompletedTask;
8989
}
9090

9191
public void Dispose()

0 commit comments

Comments
 (0)