1
1
// Copyright (c) .NET Foundation. All rights reserved.
2
2
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3
3
4
- using System ;
5
4
using System . Threading . Tasks ;
6
- using http2cat ;
7
- using Microsoft . AspNetCore . Hosting ;
5
+ using Microsoft . AspNetCore . Http2Cat ;
8
6
using Microsoft . AspNetCore . Server . Kestrel . Core . Internal . Http2 ;
9
7
using Microsoft . AspNetCore . Testing ;
10
8
using Microsoft . Extensions . Hosting ;
@@ -27,62 +25,53 @@ public async Task ConnectionClose_NoOSSupport_NoGoAway()
27
25
return Task . FromResult ( 0 ) ;
28
26
} ) ;
29
27
30
- using var host = new HostBuilder ( )
31
- . ConfigureServices ( services =>
28
+ await new HostBuilder ( )
29
+ . UseHttp2Cat ( address , async h2Connection =>
32
30
{
33
- services . UseHttp2Cat ( options =>
34
- {
35
- options . Url = address ;
36
- options . Scenaro = async ( http2Utilities , logger ) =>
37
- {
38
- await http2Utilities . InitializeConnectionAsync ( ) ;
31
+ await h2Connection . InitializeConnectionAsync ( ) ;
39
32
40
- logger . LogInformation ( "Initialized http2 connection. Starting stream 1." ) ;
33
+ h2Connection . Logger . LogInformation ( "Initialized http2 connection. Starting stream 1." ) ;
41
34
42
- await http2Utilities . StartStreamAsync ( 1 , Http2Utilities . _browserRequestHeaders , endStream : true ) ;
35
+ await h2Connection . StartStreamAsync ( 1 , Http2Utilities . BrowserRequestHeaders , endStream : true ) ;
43
36
44
- var headersFrame = await http2Utilities . ReceiveFrameAsync ( ) ;
37
+ var headersFrame = await h2Connection . ReceiveFrameAsync ( ) ;
45
38
46
- Assert . Equal ( Http2FrameType . HEADERS , headersFrame . Type ) ;
47
- Assert . True ( ( headersFrame . Flags & ( byte ) Http2HeadersFrameFlags . END_HEADERS ) != 0 ) ;
48
- Assert . True ( ( headersFrame . Flags & ( byte ) Http2HeadersFrameFlags . END_STREAM ) != 0 ) ;
39
+ Assert . Equal ( Http2FrameType . HEADERS , headersFrame . Type ) ;
40
+ Assert . True ( ( headersFrame . Flags & ( byte ) Http2HeadersFrameFlags . END_HEADERS ) != 0 ) ;
41
+ Assert . True ( ( headersFrame . Flags & ( byte ) Http2HeadersFrameFlags . END_STREAM ) != 0 ) ;
49
42
50
- logger . LogInformation ( "Received headers in a single frame." ) ;
43
+ h2Connection . Logger . LogInformation ( "Received headers in a single frame." ) ;
51
44
52
- var decodedHeaders = http2Utilities . DecodeHeaders ( headersFrame ) ;
45
+ var decodedHeaders = h2Connection . DecodeHeaders ( headersFrame ) ;
53
46
54
- // HTTP/2 filters out the connection header
55
- Assert . False ( decodedHeaders . ContainsKey ( HeaderNames . Connection ) ) ;
56
- Assert . Equal ( "200" , decodedHeaders [ HeaderNames . Status ] ) ;
47
+ // HTTP/2 filters out the connection header
48
+ Assert . False ( decodedHeaders . ContainsKey ( HeaderNames . Connection ) ) ;
49
+ Assert . Equal ( "200" , decodedHeaders [ HeaderNames . Status ] ) ;
57
50
58
- // Send and receive a second request to ensure there is no GoAway frame on the wire yet.
51
+ // Send and receive a second request to ensure there is no GoAway frame on the wire yet.
59
52
60
- await http2Utilities . StartStreamAsync ( 3 , Http2Utilities . _browserRequestHeaders , endStream : true ) ;
53
+ await h2Connection . StartStreamAsync ( 3 , Http2Utilities . BrowserRequestHeaders , endStream : true ) ;
61
54
62
- headersFrame = await http2Utilities . ReceiveFrameAsync ( ) ;
55
+ headersFrame = await h2Connection . ReceiveFrameAsync ( ) ;
63
56
64
- Assert . Equal ( Http2FrameType . HEADERS , headersFrame . Type ) ;
65
- Assert . True ( ( headersFrame . Flags & ( byte ) Http2HeadersFrameFlags . END_HEADERS ) != 0 ) ;
66
- Assert . True ( ( headersFrame . Flags & ( byte ) Http2HeadersFrameFlags . END_STREAM ) != 0 ) ;
57
+ Assert . Equal ( Http2FrameType . HEADERS , headersFrame . Type ) ;
58
+ Assert . True ( ( headersFrame . Flags & ( byte ) Http2HeadersFrameFlags . END_HEADERS ) != 0 ) ;
59
+ Assert . True ( ( headersFrame . Flags & ( byte ) Http2HeadersFrameFlags . END_STREAM ) != 0 ) ;
67
60
68
- logger . LogInformation ( "Received headers in a single frame." ) ;
61
+ h2Connection . Logger . LogInformation ( "Received headers in a single frame." ) ;
69
62
70
- http2Utilities . ResetHeaders ( ) ;
71
- decodedHeaders = http2Utilities . DecodeHeaders ( headersFrame ) ;
63
+ h2Connection . ResetHeaders ( ) ;
64
+ decodedHeaders = h2Connection . DecodeHeaders ( headersFrame ) ;
72
65
73
- // HTTP/2 filters out the connection header
74
- Assert . False ( decodedHeaders . ContainsKey ( HeaderNames . Connection ) ) ;
75
- Assert . Equal ( "200" , decodedHeaders [ HeaderNames . Status ] ) ;
66
+ // HTTP/2 filters out the connection header
67
+ Assert . False ( decodedHeaders . ContainsKey ( HeaderNames . Connection ) ) ;
68
+ Assert . Equal ( "200" , decodedHeaders [ HeaderNames . Status ] ) ;
76
69
77
- await http2Utilities . StopConnectionAsync ( expectedLastStreamId : 1 , ignoreNonGoAwayFrames : false ) ;
70
+ await h2Connection . StopConnectionAsync ( expectedLastStreamId : 1 , ignoreNonGoAwayFrames : false ) ;
78
71
79
- logger . LogInformation ( "Connection stopped." ) ;
80
- } ;
81
- } ) ;
72
+ h2Connection . Logger . LogInformation ( "Connection stopped." ) ;
82
73
} )
83
- . Build ( ) ;
84
-
85
- await host . RunHttp2CatAsync ( ) ;
74
+ . Build ( ) . RunAsync ( ) ;
86
75
}
87
76
88
77
[ ConditionalFact ]
@@ -95,64 +84,55 @@ public async Task ConnectionClose_OSSupport_SendsGoAway()
95
84
return Task . FromResult ( 0 ) ;
96
85
} ) ;
97
86
98
- using var host = new HostBuilder ( )
99
- . ConfigureServices ( services =>
87
+ await new HostBuilder ( )
88
+ . UseHttp2Cat ( address , async h2Connection =>
100
89
{
101
- services . UseHttp2Cat ( options =>
102
- {
103
- options . Url = address ;
104
- options . Scenaro = async ( http2Utilities , logger ) =>
105
- {
106
- await http2Utilities . InitializeConnectionAsync ( ) ;
107
-
108
- logger . LogInformation ( "Initialized http2 connection. Starting stream 1." ) ;
109
-
110
- await http2Utilities . StartStreamAsync ( 1 , Http2Utilities . _browserRequestHeaders , endStream : true ) ;
111
-
112
- var goAwayFrame = await http2Utilities . ReceiveFrameAsync ( ) ;
113
- http2Utilities . VerifyGoAway ( goAwayFrame , int . MaxValue , Http2ErrorCode . NO_ERROR ) ;
114
-
115
- var headersFrame = await http2Utilities . ReceiveFrameAsync ( ) ;
116
-
117
- Assert . Equal ( Http2FrameType . HEADERS , headersFrame . Type ) ;
118
- Assert . Equal ( Http2HeadersFrameFlags . END_HEADERS , headersFrame . HeadersFlags ) ;
119
-
120
- logger . LogInformation ( "Received headers in a single frame." ) ;
121
-
122
- var decodedHeaders = http2Utilities . DecodeHeaders ( headersFrame ) ;
123
-
124
- // HTTP/2 filters out the connection header
125
- Assert . False ( decodedHeaders . ContainsKey ( HeaderNames . Connection ) ) ;
126
- Assert . Equal ( "200" , decodedHeaders [ HeaderNames . Status ] ) ;
127
-
128
- var dataFrame = await http2Utilities . ReceiveFrameAsync ( ) ;
129
- Assert . Equal ( Http2FrameType . DATA , dataFrame . Type ) ;
130
- Assert . Equal ( Http2DataFrameFlags . END_STREAM , dataFrame . DataFlags ) ;
131
- Assert . Equal ( 0 , dataFrame . PayloadLength ) ;
132
-
133
- // TODO: Why doesn't HttpSys send a final GoAway or close the connection?
134
- // https://tools.ietf.org/html/rfc7540#section-6.8
135
- // A server that is attempting to gracefully shut down a
136
- // connection SHOULD send an initial GOAWAY frame with the last stream
137
- // identifier set to 2^31-1 and a NO_ERROR code. This signals to the
138
- // client that a shutdown is imminent and that initiating further
139
- // requests is prohibited. After allowing time for any in-flight stream
140
- // creation (at least one round-trip time), the server can send another
141
- // GOAWAY frame with an updated last stream identifier. This ensures
142
- // that a connection can be cleanly shut down without losing requests.
143
- //
144
- // await http2Utilities.StopConnectionAsync(expectedLastStreamId: 1, ignoreNonGoAwayFrames: false);
145
- // or
146
- // await http2Utilities.SendGoAwayAsync();
147
- // await http2Utilities.WaitForConnectionStopAsync(expectedLastStreamId: 1, ignoreNonGoAwayFrames: false);
148
-
149
- logger . LogInformation ( "Connection stopped." ) ;
150
- } ;
151
- } ) ;
152
- } )
153
- . Build ( ) ;
90
+ await h2Connection . InitializeConnectionAsync ( ) ;
91
+
92
+ h2Connection . Logger . LogInformation ( "Initialized http2 connection. Starting stream 1." ) ;
93
+
94
+ await h2Connection . StartStreamAsync ( 1 , Http2Utilities . BrowserRequestHeaders , endStream : true ) ;
95
+
96
+ var goAwayFrame = await h2Connection . ReceiveFrameAsync ( ) ;
97
+ h2Connection . VerifyGoAway ( goAwayFrame , int . MaxValue , Http2ErrorCode . NO_ERROR ) ;
98
+
99
+ var headersFrame = await h2Connection . ReceiveFrameAsync ( ) ;
100
+
101
+ Assert . Equal ( Http2FrameType . HEADERS , headersFrame . Type ) ;
102
+ Assert . Equal ( Http2HeadersFrameFlags . END_HEADERS , headersFrame . HeadersFlags ) ;
154
103
155
- await host . RunHttp2CatAsync ( ) ;
104
+ h2Connection . Logger . LogInformation ( "Received headers in a single frame." ) ;
105
+
106
+ var decodedHeaders = h2Connection . DecodeHeaders ( headersFrame ) ;
107
+
108
+ // HTTP/2 filters out the connection header
109
+ Assert . False ( decodedHeaders . ContainsKey ( HeaderNames . Connection ) ) ;
110
+ Assert . Equal ( "200" , decodedHeaders [ HeaderNames . Status ] ) ;
111
+
112
+ var dataFrame = await h2Connection . ReceiveFrameAsync ( ) ;
113
+ Assert . Equal ( Http2FrameType . DATA , dataFrame . Type ) ;
114
+ Assert . Equal ( Http2DataFrameFlags . END_STREAM , dataFrame . DataFlags ) ;
115
+ Assert . Equal ( 0 , dataFrame . PayloadLength ) ;
116
+
117
+ // TODO: Why doesn't HttpSys send a final GoAway or close the connection?
118
+ // https://tools.ietf.org/html/rfc7540#section-6.8
119
+ // A server that is attempting to gracefully shut down a
120
+ // connection SHOULD send an initial GOAWAY frame with the last stream
121
+ // identifier set to 2^31-1 and a NO_ERROR code. This signals to the
122
+ // client that a shutdown is imminent and that initiating further
123
+ // requests is prohibited. After allowing time for any in-flight stream
124
+ // creation (at least one round-trip time), the server can send another
125
+ // GOAWAY frame with an updated last stream identifier. This ensures
126
+ // that a connection can be cleanly shut down without losing requests.
127
+ //
128
+ // await h2Connection.StopConnectionAsync(expectedLastStreamId: 1, ignoreNonGoAwayFrames: false);
129
+ // or
130
+ // await h2Connection.SendGoAwayAsync();
131
+ // await h2Connection.WaitForConnectionStopAsync(expectedLastStreamId: 1, ignoreNonGoAwayFrames: false);
132
+
133
+ h2Connection . Logger . LogInformation ( "Connection stopped." ) ;
134
+ } )
135
+ . Build ( ) . RunAsync ( ) ;
156
136
}
157
137
}
158
138
}
0 commit comments