Skip to content

Commit 09f18b5

Browse files
committed
Replaced use of deprecated webhostbuilder in introspection tests
1 parent 2ef6e9f commit 09f18b5

File tree

3 files changed

+91
-74
lines changed

3 files changed

+91
-74
lines changed

introspection/test/AspNetCore.Authentication.OAuth2Introspection.Tests/Configuration.cs

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,50 +9,57 @@ namespace Duende.AspNetCore.Authentication.OAuth2Introspection;
99
public class Configuration
1010
{
1111
[Fact]
12-
public void Empty_Options()
12+
public async Task Empty_Options()
1313
{
14-
var act = () => PipelineFactory.CreateClient(options => { })
15-
.GetAsync("http://test").GetAwaiter().GetResult();
14+
var client = await PipelineFactory.CreateClient(_ => { });
1615

17-
act.ShouldThrow<InvalidOperationException>().Message.ShouldBe("You must either set Authority or IntrospectionEndpoint");
16+
var act = async () => await client.GetAsync("http://test");
17+
18+
var result = await act.ShouldThrowAsync<InvalidOperationException>();
19+
result.Message.ShouldBe("You must either set Authority or IntrospectionEndpoint");
1820
}
1921

2022
[Fact]
21-
public void Endpoint_But_No_Authority()
23+
public async Task Endpoint_But_No_Authority()
2224
{
23-
var act = () => PipelineFactory.CreateClient(options =>
25+
var client = await PipelineFactory.CreateClient(options =>
2426
{
2527
options.IntrospectionEndpoint = "http://endpoint";
2628
options.ClientId = "scope";
2729

28-
}).GetAsync("http://test").GetAwaiter().GetResult();
30+
});
31+
32+
var act = async () => await client.GetAsync("http://test");
2933

30-
act.ShouldNotThrow();
34+
await act.ShouldNotThrowAsync();
3135
}
3236

3337
[Fact]
34-
public void No_ClientName_But_Introspection_Handler()
38+
public async Task No_ClientName_But_Introspection_Handler()
3539
{
3640
var handler = new IntrospectionEndpointHandler(IntrospectionEndpointHandler.Behavior.Active);
37-
38-
var act = () => PipelineFactory.CreateClient(options =>
41+
var client = await PipelineFactory.CreateClient(options =>
3942
{
4043
options.IntrospectionEndpoint = "http://endpoint";
41-
}, handler).GetAsync("http://test").GetAwaiter().GetResult();
44+
}, handler);
45+
46+
var act = async () => await client.GetAsync("http://test");
4247

43-
act.ShouldNotThrow();
48+
await act.ShouldNotThrowAsync();
4449
}
4550

4651
[Fact]
47-
public void Authority_No_Network_Delay_Load()
52+
public async Task Authority_No_Network_Delay_Load()
4853
{
49-
var act = () => PipelineFactory.CreateClient(options =>
54+
var client = await PipelineFactory.CreateClient(options =>
5055
{
5156
options.Authority = "http://localhost:6666";
5257
options.ClientId = "scope";
53-
}).GetAsync("http://test").GetAwaiter().GetResult();
58+
});
59+
60+
var act = async () => await client.GetAsync("http://test");
5461

55-
act.ShouldNotThrow();
62+
await act.ShouldNotThrowAsync();
5663
}
5764

5865
[Fact]
@@ -61,7 +68,7 @@ public async Task Authority_Get_Introspection_Endpoint()
6168
OAuth2IntrospectionOptions ops = null!;
6269
var handler = new IntrospectionEndpointHandler(IntrospectionEndpointHandler.Behavior.Active);
6370

64-
var client = PipelineFactory.CreateClient(options =>
71+
var client = await PipelineFactory.CreateClient(options =>
6572
{
6673
options.Authority = "https://authority.com/";
6774
options.ClientId = "scope";

introspection/test/AspNetCore.Authentication.OAuth2Introspection.Tests/Introspection.cs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public async Task Unauthorized_Client()
3939
{
4040
var handler = new IntrospectionEndpointHandler(IntrospectionEndpointHandler.Behavior.Unauthorized);
4141

42-
var client = PipelineFactory.CreateClient(o => _options(o), handler);
42+
var client = await PipelineFactory.CreateClient(o => _options(o), handler);
4343
client.SetBearerToken("sometoken");
4444

4545
var result = await client.GetAsync("http://test");
@@ -51,7 +51,7 @@ public async Task ActiveToken()
5151
{
5252
var handler = new IntrospectionEndpointHandler(IntrospectionEndpointHandler.Behavior.Active);
5353

54-
var client = PipelineFactory.CreateClient(_options, handler);
54+
var client = await PipelineFactory.CreateClient(_options, handler);
5555
client.SetBearerToken("sometoken");
5656

5757
var result = await client.GetAsync("http://test");
@@ -76,7 +76,7 @@ public async Task TwoConcurrentCalls_FirstIntrospectDoesNotThrow_SecondShouldNot
7676

7777
var requestCount = 0;
7878

79-
var messageHandler = PipelineFactory.CreateHandler(o =>
79+
var messageHandler = await PipelineFactory.CreateHandler(o =>
8080
{
8181
_options(o);
8282

@@ -129,7 +129,7 @@ public async Task ActiveToken_WithTwoConcurrentCalls_FirstCancelled_SecondShould
129129
var waitForTheSecondRequestToStart = new ManualResetEvent(initialState: false);
130130
var handler = new IntrospectionEndpointHandler(IntrospectionEndpointHandler.Behavior.Active);
131131

132-
var messageHandler = PipelineFactory.CreateHandler(o =>
132+
var messageHandler = await PipelineFactory.CreateHandler(o =>
133133
{
134134
_options(o);
135135

@@ -173,7 +173,7 @@ public async Task ActiveToken_With_ClientAssertion(int ttl, string assertion1, s
173173
var handler = new IntrospectionEndpointHandler(IntrospectionEndpointHandler.Behavior.Active);
174174
var count = 0;
175175

176-
var client = PipelineFactory.CreateClient(o =>
176+
var client = await PipelineFactory.CreateClient(o =>
177177
{
178178
_options(o);
179179
o.ClientSecret = null;
@@ -221,7 +221,7 @@ public async Task Active_token_with_inline_event_events_should_be_called()
221221
bool? validatedCalled = null;
222222
bool? failureCalled = null;
223223

224-
var client = PipelineFactory.CreateClient(o =>
224+
var client = await PipelineFactory.CreateClient(o =>
225225
{
226226
_options(o);
227227

@@ -256,7 +256,7 @@ public async Task ActiveToken_With_Caching_Ttl_Longer_Than_Duration()
256256
{
257257
var introspectionCalls = 0;
258258
var handler = new IntrospectionEndpointHandler(IntrospectionEndpointHandler.Behavior.Active, TimeSpan.FromHours(1));
259-
var client = PipelineFactory.CreateClient(o =>
259+
var client = await PipelineFactory.CreateClient(o =>
260260
{
261261
_options(o);
262262

@@ -284,7 +284,7 @@ public async Task ActiveToken_With_Caching_Ttl_Shorter_Than_Duration()
284284
{
285285
var handler = new IntrospectionEndpointHandler(IntrospectionEndpointHandler.Behavior.Active, TimeSpan.FromMinutes(5));
286286

287-
var client = PipelineFactory.CreateClient(o =>
287+
var client = await PipelineFactory.CreateClient(o =>
288288
{
289289
_options(o);
290290

@@ -305,7 +305,7 @@ public async Task InactiveToken()
305305
{
306306
var handler = new IntrospectionEndpointHandler(IntrospectionEndpointHandler.Behavior.Inactive);
307307

308-
var client = PipelineFactory.CreateClient(o => _options(o), handler);
308+
var client = await PipelineFactory.CreateClient(o => _options(o), handler);
309309
client.SetBearerToken("sometoken");
310310

311311
var result = await client.GetAsync("http://test");
@@ -319,7 +319,7 @@ public async Task InActive_token_with_inline_event_events_should_be_called()
319319
bool? validatedCalled = null;
320320
bool? failureCalled = null;
321321

322-
var client = PipelineFactory.CreateClient(o =>
322+
var client = await PipelineFactory.CreateClient(o =>
323323
{
324324
_options(o);
325325

@@ -355,7 +355,7 @@ public async Task ActiveToken_With_SavedToken()
355355
var expectedToken = "expected_token";
356356
var handler = new IntrospectionEndpointHandler(IntrospectionEndpointHandler.Behavior.Active);
357357

358-
var client = PipelineFactory.CreateClient(o =>
358+
var client = await PipelineFactory.CreateClient(o =>
359359
{
360360
_options(o);
361361

@@ -380,7 +380,7 @@ public async Task ActiveToken_With_SavedToken_And_Caching()
380380
var expectedToken = "expected_token";
381381
var handler = new IntrospectionEndpointHandler(IntrospectionEndpointHandler.Behavior.Active, TimeSpan.FromHours(1));
382382

383-
var server = PipelineFactory.CreateServer(o =>
383+
var server = await PipelineFactory.CreateServer(o =>
384384
{
385385
_options(o);
386386

@@ -411,7 +411,7 @@ public async Task ActiveToken_With_SavedToken_And_Caching_With_Cache_Key_Prefix(
411411
var cacheKeyPrefix = "KeyPrefix";
412412
var handler = new IntrospectionEndpointHandler(IntrospectionEndpointHandler.Behavior.Active, TimeSpan.FromHours(1));
413413

414-
var server = PipelineFactory.CreateServer(o =>
414+
var server = await PipelineFactory.CreateServer(o =>
415415
{
416416
_options(o);
417417

@@ -442,7 +442,7 @@ public async Task Repeated_active_token_with_caching_enabled_should_hit_cache()
442442
var expectedToken = "expected_token";
443443
var handler = new IntrospectionEndpointHandler(IntrospectionEndpointHandler.Behavior.Active, TimeSpan.FromHours(1));
444444

445-
var server = PipelineFactory.CreateServer(o =>
445+
var server = await PipelineFactory.CreateServer(o =>
446446
{
447447
_options(o);
448448

@@ -469,7 +469,7 @@ public async Task Repeated_inactive_token_with_caching_enabled_should_hit_cache(
469469
var expectedToken = "expected_token";
470470
var handler = new IntrospectionEndpointHandler(IntrospectionEndpointHandler.Behavior.Inactive);
471471

472-
var server = PipelineFactory.CreateServer(o =>
472+
var server = await PipelineFactory.CreateServer(o =>
473473
{
474474
_options(o);
475475

@@ -496,7 +496,7 @@ public async Task ActiveToken_With_Discovery_Unavailable_On_First_Request()
496496
{
497497
var handler = new IntrospectionEndpointHandler(IntrospectionEndpointHandler.Behavior.Active);
498498

499-
var client = PipelineFactory.CreateClient(o => _options(o), handler);
499+
var client = await PipelineFactory.CreateClient(o => _options(o), handler);
500500
client.SetBearerToken("sometoken");
501501

502502
handler.IsDiscoveryFailureTest = true;
@@ -512,7 +512,7 @@ public async Task ActiveToken_RequestSending_AdditionalParameter_with_inline_eve
512512
{
513513
var handler = new IntrospectionEndpointHandler(IntrospectionEndpointHandler.Behavior.Active);
514514

515-
var client = PipelineFactory.CreateClient(o =>
515+
var client = await PipelineFactory.CreateClient(o =>
516516
{
517517
_options(o);
518518

@@ -538,7 +538,7 @@ public async Task ActiveToken_expires_while_cached()
538538
var introspectionRequestsMade = 0;
539539
var handler = new IntrospectionEndpointHandler(IntrospectionEndpointHandler.Behavior.Active, TimeSpan.FromMilliseconds(250));
540540

541-
var client = PipelineFactory.CreateClient(o =>
541+
var client = await PipelineFactory.CreateClient(o =>
542542
{
543543
_options(o);
544544

@@ -570,7 +570,7 @@ public async Task ActiveToken_can_override_cache_settings_to_prevent_caching()
570570
var token = "sometoken";
571571
var handler = new IntrospectionEndpointHandler(IntrospectionEndpointHandler.Behavior.Active, TimeSpan.FromMinutes(5));
572572

573-
var server = PipelineFactory.CreateServer(o =>
573+
var server = await PipelineFactory.CreateServer(o =>
574574
{
575575
_options(o);
576576

introspection/test/AspNetCore.Authentication.OAuth2Introspection.Tests/Util/PipelineFactory.cs

Lines changed: 47 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,60 +9,70 @@
99
using Microsoft.AspNetCore.Http;
1010
using Microsoft.AspNetCore.TestHost;
1111
using Microsoft.Extensions.DependencyInjection;
12+
using Microsoft.Extensions.Hosting;
1213

1314
namespace Duende.AspNetCore.Authentication.OAuth2Introspection.Util;
1415

1516
internal static class PipelineFactory
1617
{
17-
public static TestServer CreateServer(
18+
public static async Task<TestServer> CreateServer(
1819
Action<OAuth2IntrospectionOptions> options,
19-
DelegatingHandler? backChannelHandler = null) => new(new WebHostBuilder()
20-
.ConfigureServices(services =>
21-
{
22-
services
23-
.AddAuthentication(OAuth2IntrospectionDefaults.AuthenticationScheme)
24-
.AddOAuth2Introspection(options);
25-
26-
if (backChannelHandler != null)
20+
DelegatingHandler? backChannelHandler = null)
21+
{
22+
var host = new HostBuilder()
23+
.ConfigureWebHost(webHostBuilder => webHostBuilder
24+
.UseTestServer()
25+
.ConfigureServices(services =>
2726
{
28-
services.AddHttpClient(OAuth2IntrospectionDefaults.BackChannelHttpClientName)
29-
.AddHttpMessageHandler(() => backChannelHandler);
30-
}
31-
})
32-
.Configure(app =>
33-
{
34-
app.UseAuthentication();
27+
services
28+
.AddAuthentication(OAuth2IntrospectionDefaults.AuthenticationScheme)
29+
.AddOAuth2Introspection(options);
3530

36-
app.Run(async context =>
31+
if (backChannelHandler != null)
32+
{
33+
services.AddHttpClient(OAuth2IntrospectionDefaults.BackChannelHttpClientName)
34+
.AddHttpMessageHandler(() => backChannelHandler);
35+
}
36+
})
37+
.Configure(app =>
3738
{
38-
var user = context.User;
39+
app.UseAuthentication();
3940

40-
if (user.Identity!.IsAuthenticated)
41+
app.Run(async context =>
4142
{
42-
var token = await context.GetTokenAsync("access_token");
43-
var responseObject = new Dictionary<string, string>
43+
var user = context.User;
44+
45+
if (user.Identity!.IsAuthenticated)
4446
{
45-
{"token", token! }
46-
};
47+
var token = await context.GetTokenAsync("access_token");
48+
var responseObject = new Dictionary<string, string>
49+
{
50+
{ "token", token! }
51+
};
4752

48-
var json = JsonSerializer.Serialize(responseObject);
53+
var json = JsonSerializer.Serialize(responseObject);
4954

50-
context.Response.StatusCode = 200;
51-
await context.Response.WriteAsync(json, Encoding.UTF8);
52-
}
53-
else
54-
{
55-
context.Response.StatusCode = 401;
56-
}
57-
});
58-
}));
55+
context.Response.StatusCode = 200;
56+
await context.Response.WriteAsync(json, Encoding.UTF8);
57+
}
58+
else
59+
{
60+
context.Response.StatusCode = 401;
61+
}
62+
});
63+
})).Build();
64+
65+
await host.StartAsync();
66+
67+
return host.GetTestServer();
68+
}
5969

60-
public static HttpClient CreateClient(
70+
public static async Task<HttpClient> CreateClient(
6171
Action<OAuth2IntrospectionOptions> options,
6272
DelegatingHandler? handler = null)
63-
=> CreateServer(options, handler).CreateClient();
73+
=> (await CreateServer(options, handler)).CreateClient();
6474

65-
public static HttpMessageHandler CreateHandler(
75+
public static async Task<HttpMessageHandler> CreateHandler(
6676
Action<OAuth2IntrospectionOptions> options,
67-
DelegatingHandler? handler = null) => CreateServer(options, handler).CreateHandler();
77+
DelegatingHandler? handler = null) => (await CreateServer(options, handler)).CreateHandler();
6878
}

0 commit comments

Comments
 (0)