6
6
using System . Linq ;
7
7
using System . Threading . Tasks ;
8
8
using Microsoft . AspNetCore . Builder ;
9
+ using Microsoft . AspNetCore . Builder . Internal ;
10
+ using Microsoft . AspNetCore . Http ;
11
+ using Microsoft . AspNetCore . Http . Internal ;
9
12
using Microsoft . Extensions . DependencyInjection ;
10
13
using Xunit ;
11
14
12
15
namespace Microsoft . AspNetCore . Owin
13
16
{
17
+ using AddMiddleware = Action < Func <
18
+ Func < IDictionary < string , object > , Task > ,
19
+ Func < IDictionary < string , object > , Task >
20
+ > > ;
14
21
using AppFunc = Func < IDictionary < string , object > , Task > ;
15
22
using CreateMiddleware = Func <
16
23
Func < IDictionary < string , object > , Task > ,
17
24
Func < IDictionary < string , object > , Task >
18
25
> ;
19
- using AddMiddleware = Action < Func <
20
- Func < IDictionary < string , object > , Task > ,
21
- Func < IDictionary < string , object > , Task >
22
- > > ;
23
26
24
27
public class OwinExtensionTests
25
28
{
26
29
static AppFunc notFound = env => new Task ( ( ) => { env [ "owin.ResponseStatusCode" ] = 404 ; } ) ;
27
30
28
31
[ Fact ]
29
- public void OwinConfigureServiceProviderAddsServices ( )
32
+ public async Task OwinConfigureServiceProviderAddsServices ( )
30
33
{
31
34
var list = new List < CreateMiddleware > ( ) ;
32
35
AddMiddleware build = list . Add ;
@@ -36,24 +39,30 @@ public void OwinConfigureServiceProviderAddsServices()
36
39
var builder = build . UseBuilder ( applicationBuilder =>
37
40
{
38
41
serviceProvider = applicationBuilder . ApplicationServices ;
39
- applicationBuilder . Run ( async context =>
42
+ applicationBuilder . Run ( context =>
40
43
{
41
44
fakeService = context . RequestServices . GetService < FakeService > ( ) ;
45
+ return Task . FromResult ( 0 ) ;
42
46
} ) ;
43
- } , new ServiceCollection ( ) . AddSingleton ( new FakeService ( ) ) . BuildServiceProvider ( ) ) ;
47
+ } ,
48
+ new ServiceCollection ( ) . AddSingleton ( new FakeService ( ) ) . BuildServiceProvider ( ) ) ;
44
49
45
50
list . Reverse ( ) ;
46
- list . Aggregate ( notFound , ( next , middleware ) => middleware ( next ) ) . Invoke ( new Dictionary < string , object > ( ) ) ;
51
+ await list
52
+ . Aggregate ( notFound , ( next , middleware ) => middleware ( next ) )
53
+ . Invoke ( new Dictionary < string , object > ( ) ) ;
47
54
48
- Assert . NotNull ( fakeService ) ;
55
+ Assert . NotNull ( serviceProvider ) ;
49
56
Assert . NotNull ( serviceProvider . GetService < FakeService > ( ) ) ;
57
+ Assert . NotNull ( fakeService ) ;
50
58
}
51
59
52
60
[ Fact ]
53
- public void OwinDefaultNoServices ( )
61
+ public async Task OwinDefaultNoServices ( )
54
62
{
55
63
var list = new List < CreateMiddleware > ( ) ;
56
64
AddMiddleware build = list . Add ;
65
+ IServiceProvider expectedServiceProvider = new ServiceCollection ( ) . BuildServiceProvider ( ) ;
57
66
IServiceProvider serviceProvider = null ;
58
67
FakeService fakeService = null ;
59
68
bool builderExecuted = false ;
@@ -63,25 +72,94 @@ public void OwinDefaultNoServices()
63
72
{
64
73
builderExecuted = true ;
65
74
serviceProvider = applicationBuilder . ApplicationServices ;
66
- applicationBuilder . Run ( async context =>
75
+ applicationBuilder . Run ( context =>
67
76
{
68
77
applicationExecuted = true ;
69
78
fakeService = context . RequestServices . GetService < FakeService > ( ) ;
79
+ return Task . FromResult ( 0 ) ;
70
80
} ) ;
71
- } ) ;
81
+ } ,
82
+ expectedServiceProvider ) ;
72
83
73
84
list . Reverse ( ) ;
74
- list . Aggregate ( notFound , ( next , middleware ) => middleware ( next ) ) . Invoke ( new Dictionary < string , object > ( ) ) ;
85
+ await list
86
+ . Aggregate ( notFound , ( next , middleware ) => middleware ( next ) )
87
+ . Invoke ( new Dictionary < string , object > ( ) ) ;
75
88
76
89
Assert . True ( builderExecuted ) ;
90
+ Assert . Equal ( expectedServiceProvider , serviceProvider ) ;
91
+ Assert . True ( applicationExecuted ) ;
77
92
Assert . Null ( fakeService ) ;
93
+ }
94
+
95
+ [ Fact ]
96
+ public async Task OwinDefaultNullServiceProvider ( )
97
+ {
98
+ var list = new List < CreateMiddleware > ( ) ;
99
+ AddMiddleware build = list . Add ;
100
+ IServiceProvider serviceProvider = null ;
101
+ FakeService fakeService = null ;
102
+ bool builderExecuted = false ;
103
+ bool applicationExecuted = false ;
104
+
105
+ var builder = build . UseBuilder ( applicationBuilder =>
106
+ {
107
+ builderExecuted = true ;
108
+ serviceProvider = applicationBuilder . ApplicationServices ;
109
+ applicationBuilder . Run ( context =>
110
+ {
111
+ applicationExecuted = true ;
112
+ fakeService = context . RequestServices . GetService < FakeService > ( ) ;
113
+ return Task . FromResult ( 0 ) ;
114
+ } ) ;
115
+ } ) ;
116
+
117
+ list . Reverse ( ) ;
118
+ await list
119
+ . Aggregate ( notFound , ( next , middleware ) => middleware ( next ) )
120
+ . Invoke ( new Dictionary < string , object > ( ) ) ;
121
+
122
+ Assert . True ( builderExecuted ) ;
123
+ Assert . NotNull ( serviceProvider ) ;
78
124
Assert . True ( applicationExecuted ) ;
79
- Assert . Null ( serviceProvider ) ;
125
+ Assert . Null ( fakeService ) ;
80
126
}
81
127
82
- private class FakeService
128
+ [ Fact ]
129
+ public async Task UseOwin ( )
83
130
{
131
+ var serviceProvider = new ServiceCollection ( ) . BuildServiceProvider ( ) ;
132
+ var builder = new ApplicationBuilder ( serviceProvider ) ;
133
+ IDictionary < string , object > environment = null ;
134
+ var context = new DefaultHttpContext ( ) ;
135
+
136
+ builder . UseOwin ( addToPipeline =>
137
+ {
138
+ addToPipeline ( next =>
139
+ {
140
+ Assert . NotNull ( next ) ;
141
+ return async env =>
142
+ {
143
+ environment = env ;
144
+ await next ( env ) ;
145
+ } ;
146
+ } ) ;
147
+ } ) ;
148
+ await builder . Build ( ) . Invoke ( context ) ;
149
+
150
+ // Dictionary contains context but does not contain "websocket.Accept" or "websocket.AcceptAlt" keys.
151
+ Assert . NotNull ( environment ) ;
152
+ var value = Assert . Single (
153
+ environment ,
154
+ kvp => string . Equals ( typeof ( HttpContext ) . FullName , kvp . Key , StringComparison . Ordinal ) )
155
+ . Value ;
156
+ Assert . Equal ( context , value ) ;
157
+ Assert . False ( environment . ContainsKey ( "websocket.Accept" ) ) ;
158
+ Assert . False ( environment . ContainsKey ( "websocket.AcceptAlt" ) ) ;
159
+ }
84
160
161
+ private class FakeService
162
+ {
85
163
}
86
164
}
87
165
}
0 commit comments