1
1
// Copyright (c) Martin Costello, 2021. All rights reserved.
2
2
// Licensed under the Apache 2.0 license. See the LICENSE file in the project root for full license information.
3
3
4
- using System . IO ;
5
4
using System . Net . Http ;
6
- using System . Reflection ;
7
5
using System . Security . Cryptography . X509Certificates ;
8
- using AspNet . Security . OAuth . GitHub ;
9
- using JustEat . HttpClientInterception ;
10
- using MartinCostello . Logging . XUnit ;
11
6
using Microsoft . AspNetCore . Hosting ;
12
7
using Microsoft . AspNetCore . Hosting . Server ;
13
8
using Microsoft . AspNetCore . Hosting . Server . Features ;
14
- using Microsoft . AspNetCore . Mvc . Testing ;
15
- using Microsoft . Extensions . Configuration ;
16
9
using Microsoft . Extensions . DependencyInjection ;
17
10
using Microsoft . Extensions . Hosting ;
18
- using Microsoft . Extensions . Http ;
19
- using Microsoft . Extensions . Logging ;
20
- using Microsoft . Extensions . Options ;
21
11
22
12
namespace TodoApp
23
13
{
24
- public sealed class HttpServerFixture : WebApplicationFactory < Startup > , IAsyncLifetime , ITestOutputHelperAccessor
14
+ public sealed class HttpServerFixture : TodoAppFixture , IAsyncLifetime
25
15
{
26
16
private IHost ? _host ;
27
17
private bool _disposed ;
28
18
29
- public HttpServerFixture ( )
30
- : base ( )
31
- {
32
- ClientOptions . AllowAutoRedirect = false ;
33
- ClientOptions . BaseAddress = new Uri ( "https://localhost" ) ;
34
- Interceptor = new HttpClientInterceptorOptions ( ) . ThrowsOnMissingRegistration ( ) ;
35
- }
36
-
37
- public HttpClientInterceptorOptions Interceptor { get ; }
38
-
39
- public ITestOutputHelper ? OutputHelper { get ; set ; }
40
-
41
19
public string ServerAddress => ClientOptions . BaseAddress . ToString ( ) ;
42
20
43
- public override IServiceProvider ? Services => _host ? . Services ;
44
-
45
- public void ClearOutputHelper ( )
46
- => OutputHelper = null ;
47
-
48
- public void SetOutputHelper ( ITestOutputHelper value )
49
- => OutputHelper = value ;
21
+ public override IServiceProvider Services => _host ? . Services ! ;
50
22
51
23
async Task IAsyncLifetime . InitializeAsync ( )
52
24
=> await EnsureHttpServerAsync ( ) ;
@@ -92,49 +64,13 @@ protected override void ConfigureWebHost(IWebHostBuilder builder)
92
64
{
93
65
base . ConfigureWebHost ( builder ) ;
94
66
95
- builder . ConfigureAppConfiguration ( ( builder ) =>
96
- {
97
- string dataDirectory = Path . Combine ( Path . GetTempPath ( ) , Guid . NewGuid ( ) . ToString ( ) ) ;
98
-
99
- if ( ! Directory . Exists ( dataDirectory ) )
100
- {
101
- Directory . CreateDirectory ( dataDirectory ) ;
102
- }
103
-
104
- var config = new [ ]
105
- {
106
- KeyValuePair . Create ( "DataDirectory" , dataDirectory ) ,
107
- } ;
108
-
109
- string ? directory = Path . GetDirectoryName ( typeof ( HttpServerFixture ) . Assembly . Location ) ;
110
- string fullPath = Path . Combine ( directory ?? "." , "testsettings.json" ) ;
111
-
112
- builder . Sources . Clear ( ) ;
113
-
114
- builder . AddJsonFile ( fullPath )
115
- . AddInMemoryCollection ( config ) ;
116
- } ) ;
117
-
118
67
builder . ConfigureKestrel (
119
- ( serverOptions ) => serverOptions . ConfigureHttpsDefaults (
120
- ( httpsOptions ) => httpsOptions . ServerCertificate = new X509Certificate2 ( "localhost-dev.pfx" , "Pa55w0rd!" ) ) ) ;
121
-
122
- builder . ConfigureLogging ( ( loggingBuilder ) => loggingBuilder . ClearProviders ( ) . AddXUnit ( this ) )
123
- . UseContentRoot ( GetApplicationContentRootPath ( ) ) ;
124
-
125
- builder . ConfigureServices ( ( services ) =>
126
- {
127
- services . AddSingleton < IHttpMessageHandlerBuilderFilter , HttpRequestInterceptionFilter > (
128
- ( _ ) => new HttpRequestInterceptionFilter ( Interceptor ) ) ;
129
-
130
- services . AddSingleton < IPostConfigureOptions < GitHubAuthenticationOptions > , RemoteAuthorizationEventsFilter > ( ) ;
131
- } ) ;
68
+ serverOptions => serverOptions . ConfigureHttpsDefaults (
69
+ httpsOptions => httpsOptions . ServerCertificate = new X509Certificate2 ( "localhost-dev.pfx" , "Pa55w0rd!" ) ) ) ;
132
70
133
71
// Configure the server address for the server to
134
72
// listen on for HTTPS requests on a dynamic port.
135
73
builder . UseUrls ( "https://127.0.0.1:0" ) ;
136
-
137
- Interceptor . RegisterBundle ( "oauth-http-bundle.json" ) ;
138
74
}
139
75
140
76
protected override void Dispose ( bool disposing )
@@ -154,38 +90,28 @@ protected override void Dispose(bool disposing)
154
90
155
91
private async Task EnsureHttpServerAsync ( )
156
92
{
157
- if ( _host == null )
93
+ if ( _host is null )
158
94
{
159
- await CreateHttpServer ( ) ;
95
+ await CreateHttpServerAsync ( ) ;
160
96
}
161
97
}
162
98
163
- private async Task CreateHttpServer ( )
99
+ private async Task CreateHttpServerAsync ( )
164
100
{
165
- var builder = CreateHostBuilder ( ) . ConfigureWebHost ( ConfigureWebHost ) ;
166
-
167
- _host = builder . Build ( ) ;
101
+ _host = CreateHostBuilder ( ) !
102
+ . ConfigureWebHost ( ConfigureWebHost )
103
+ . Build ( ) ;
168
104
169
105
// Force creation of the Kestrel server and start it
170
106
var hostedService = _host . Services . GetService < IHostedService > ( ) ;
171
107
await hostedService ! . StartAsync ( default ) ;
172
108
173
109
var server = _host . Services . GetRequiredService < IServer > ( ) ;
110
+ var addresses = server . Features . Get < IServerAddressesFeature > ( ) ;
174
111
175
- ClientOptions . BaseAddress = server . Features . Get < IServerAddressesFeature > ( ) ! . Addresses
112
+ ClientOptions . BaseAddress = addresses ! . Addresses
176
113
. Select ( ( p ) => new Uri ( p ) )
177
- . First ( ) ;
178
- }
179
-
180
- private string GetApplicationContentRootPath ( )
181
- {
182
- var attribute = GetTestAssemblies ( )
183
- . SelectMany ( ( p ) => p . GetCustomAttributes < WebApplicationFactoryContentRootAttribute > ( ) )
184
- . Where ( ( p ) => string . Equals ( p . Key , "TodoApp" , StringComparison . OrdinalIgnoreCase ) )
185
- . OrderBy ( ( p ) => p . Priority )
186
- . First ( ) ;
187
-
188
- return attribute . ContentRootPath ;
114
+ . Last ( ) ;
189
115
}
190
116
}
191
117
}
0 commit comments