-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfiguration.cs
More file actions
128 lines (104 loc) · 4.22 KB
/
Configuration.cs
File metadata and controls
128 lines (104 loc) · 4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright (c) Duende Software. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
using Duende.IdentityModel.Client;
using FluentAssertions;
using IdentityModel.AspNetCore.OAuth2Introspection;
using System;
using System.Threading.Tasks;
using Tests.Util;
using Xunit;
namespace Tests
{
public class Configuration
{
[Fact]
public void Empty_Options()
{
Action act = () => PipelineFactory.CreateClient(options => { })
.GetAsync("http://test").GetAwaiter().GetResult();
act.Should().Throw<InvalidOperationException>()
.WithMessage("You must either set Authority or IntrospectionEndpoint");
}
[Fact]
public void No_Token_Retriever()
{
Action act = () => PipelineFactory.CreateClient(options =>
{
options.Authority = "http://foo";
options.ClientId = "scope";
options.TokenRetriever = null;
}).GetAsync("http://test").GetAwaiter().GetResult();
act.Should().Throw<ArgumentException>()
.Where(e => e.Message.StartsWith("TokenRetriever must be set"));
}
[Fact]
public void Endpoint_But_No_Authority()
{
Action act = () => PipelineFactory.CreateClient(options =>
{
options.IntrospectionEndpoint = "http://endpoint";
options.ClientId = "scope";
}).GetAsync("http://test").GetAwaiter().GetResult();
act.Should().NotThrow();
}
[Fact]
public void Caching_With_Caching_Service()
{
Action act = () => PipelineFactory.CreateClient(options =>
{
options.IntrospectionEndpoint = "http://endpoint";
options.ClientId = "scope";
options.EnableCaching = true;
}, addCaching: true).GetAsync("http://test").GetAwaiter().GetResult();
act.Should().NotThrow();
}
[Fact]
public void Caching_Without_Caching_Service()
{
Action act = () => PipelineFactory.CreateClient(options =>
{
options.IntrospectionEndpoint = "http://endpoint";
options.ClientId = "scope";
options.EnableCaching = true;
}).GetAsync("http://test").GetAwaiter().GetResult();
act.Should().Throw<ArgumentException>()
.Where(e => e.Message.StartsWith("Caching is enabled, but no IDistributedCache is found in the services collection"));
}
[Fact]
public void No_ClientName_But_Introspection_Handler()
{
var handler = new IntrospectionEndpointHandler(IntrospectionEndpointHandler.Behavior.Active);
Action act = () => PipelineFactory.CreateClient(options =>
{
options.IntrospectionEndpoint = "http://endpoint";
}, handler).GetAsync("http://test").GetAwaiter().GetResult();
act.Should().NotThrow();
}
[Fact]
public void Authority_No_Network_Delay_Load()
{
Action act = () => PipelineFactory.CreateClient(options =>
{
options.Authority = "http://localhost:6666";
options.ClientId = "scope";
}).GetAsync("http://test").GetAwaiter().GetResult();
act.Should().NotThrow();
}
[Fact]
public async Task Authority_Get_Introspection_Endpoint()
{
OAuth2IntrospectionOptions ops = null;
var handler = new IntrospectionEndpointHandler(IntrospectionEndpointHandler.Behavior.Active);
var client = PipelineFactory.CreateClient(options =>
{
options.Authority = "https://authority.com/";
options.ClientId = "scope";
options.DiscoveryPolicy.RequireKeySet = false;
ops = options;
}, handler);
client.SetBearerToken("token");
await client.GetAsync("http://server/api");
ops.IntrospectionEndpoint.Should().Be("https://authority.com/introspection_endpoint");
}
}
}