44using Microsoft . AspNetCore . Builder ;
55using Microsoft . AspNetCore . Hosting ;
66using Microsoft . AspNetCore . Http . Features ;
7+ using Microsoft . Extensions . Hosting ;
78
89namespace Microsoft . AspNetCore . TestHost . Tests ;
910
@@ -19,22 +20,29 @@ public async Task ConnectAsync_ShouldSetRequestProperties(string requestUri, str
1920 string capturedHost = null ;
2021 string capturedPath = null ;
2122
22- using ( var testServer = new TestServer ( new WebHostBuilder ( )
23- . Configure ( app =>
23+ using var host = new HostBuilder ( )
24+ . ConfigureWebHost ( webHostBuilder =>
2425 {
25- app . Run ( ctx =>
26- {
27- if ( ctx . Request . Path . StartsWithSegments ( "/connect" ) )
26+ webHostBuilder
27+ . UseTestServer ( )
28+ . Configure ( app =>
2829 {
29- capturedScheme = ctx . Request . Scheme ;
30- capturedHost = ctx . Request . Host . Value ;
31- capturedPath = ctx . Request . Path ;
32- }
33- return Task . FromResult ( 0 ) ;
34- } ) ;
35- } ) ) )
36- {
37- var client = testServer . CreateWebSocketClient ( ) ;
30+ app . Run ( ctx =>
31+ {
32+ if ( ctx . Request . Path . StartsWithSegments ( "/connect" ) )
33+ {
34+ capturedScheme = ctx . Request . Scheme ;
35+ capturedHost = ctx . Request . Host . Value ;
36+ capturedPath = ctx . Request . Path ;
37+ }
38+ return Task . FromResult ( 0 ) ;
39+ } ) ;
40+ } ) ;
41+ } )
42+ . Build ( ) ;
43+
44+ var testServer = host . GetTestServer ( ) ;
45+ var client = testServer . CreateWebSocketClient ( ) ;
3846
3947 try
4048 {
@@ -46,7 +54,6 @@ await client.ConnectAsync(
4654 {
4755 // An exception will be thrown because our dummy endpoint does not implement a full Web socket server
4856 }
49- }
5057
5158 Assert . Equal ( "http" , capturedScheme ) ;
5259 Assert . Equal ( expectedHost , capturedHost ) ;
@@ -56,72 +63,84 @@ await client.ConnectAsync(
5663 [ Fact ]
5764 public async Task CanAcceptWebSocket ( )
5865 {
59- using ( var testServer = new TestServer ( new WebHostBuilder ( )
60- . Configure ( app =>
66+ using var host = new HostBuilder ( )
67+ . ConfigureWebHost ( webHostBuilder =>
6168 {
62- app . UseWebSockets ( ) ;
63- app . Run ( async ctx =>
64- {
65- if ( ctx . Request . Path . StartsWithSegments ( "/connect" ) )
69+ webHostBuilder
70+ . UseTestServer ( )
71+ . Configure ( app =>
6672 {
67- if ( ctx . WebSockets . IsWebSocketRequest )
73+ app . UseWebSockets ( ) ;
74+ app . Run ( async ctx =>
6875 {
69- using var websocket = await ctx . WebSockets . AcceptWebSocketAsync ( ) ;
70- var buffer = new byte [ 1000 ] ;
71- var res = await websocket . ReceiveAsync ( buffer , default ) ;
72- await websocket . SendAsync ( buffer . AsMemory ( 0 , res . Count ) , System . Net . WebSockets . WebSocketMessageType . Binary , true , default ) ;
73- await websocket . CloseAsync ( System . Net . WebSockets . WebSocketCloseStatus . NormalClosure , null , default ) ;
74- }
75- }
76- } ) ;
77- } ) ) )
78- {
79- var client = testServer . CreateWebSocketClient ( ) ;
76+ if ( ctx . Request . Path . StartsWithSegments ( "/connect" ) )
77+ {
78+ if ( ctx . WebSockets . IsWebSocketRequest )
79+ {
80+ using var websocket = await ctx . WebSockets . AcceptWebSocketAsync ( ) ;
81+ var buffer = new byte [ 1000 ] ;
82+ var res = await websocket . ReceiveAsync ( buffer , default ) ;
83+ await websocket . SendAsync ( buffer . AsMemory ( 0 , res . Count ) , System . Net . WebSockets . WebSocketMessageType . Binary , true , default ) ;
84+ await websocket . CloseAsync ( System . Net . WebSockets . WebSocketCloseStatus . NormalClosure , null , default ) ;
85+ }
86+ }
87+ } ) ;
88+ } ) ;
89+ } )
90+ . Build ( ) ;
8091
81- using var socket = await client . ConnectAsync (
82- uri : new Uri ( "http://localhost/connect" ) ,
83- cancellationToken : default ) ;
92+ var testServer = host . GetTestServer ( ) ;
93+ var client = testServer . CreateWebSocketClient ( ) ;
8494
85- await socket . SendAsync ( new byte [ 10 ] , System . Net . WebSockets . WebSocketMessageType . Binary , true , default ) ;
86- var res = await socket . ReceiveAsync ( new byte [ 100 ] , default ) ;
87- Assert . Equal ( 10 , res . Count ) ;
88- Assert . True ( res . EndOfMessage ) ;
95+ using var socket = await client . ConnectAsync (
96+ uri : new Uri ( "http://localhost/connect" ) ,
97+ cancellationToken : default ) ;
8998
90- await socket . CloseAsync ( System . Net . WebSockets . WebSocketCloseStatus . NormalClosure , null , default ) ;
91- }
99+ await socket . SendAsync ( new byte [ 10 ] , System . Net . WebSockets . WebSocketMessageType . Binary , true , default ) ;
100+ var res = await socket . ReceiveAsync ( new byte [ 100 ] , default ) ;
101+ Assert . Equal ( 10 , res . Count ) ;
102+ Assert . True ( res . EndOfMessage ) ;
103+
104+ await socket . CloseAsync ( System . Net . WebSockets . WebSocketCloseStatus . NormalClosure , null , default ) ;
92105 }
93106
94107 [ Fact ]
95108 public async Task VerifyWebSocketAndUpgradeFeatures ( )
96109 {
97- using ( var testServer = new TestServer ( new WebHostBuilder ( )
98- . Configure ( app =>
110+ using var host = new HostBuilder ( )
111+ . ConfigureWebHost ( webHostBuilder =>
99112 {
100- app . Run ( async c =>
101- {
102- var upgradeFeature = c . Features . Get < IHttpUpgradeFeature > ( ) ;
103- Assert . NotNull ( upgradeFeature ) ;
104- Assert . False ( upgradeFeature . IsUpgradableRequest ) ;
105- await Assert . ThrowsAsync < NotSupportedException > ( ( ) => upgradeFeature . UpgradeAsync ( ) ) ;
106-
107- var webSocketFeature = c . Features . Get < IHttpWebSocketFeature > ( ) ;
108- Assert . NotNull ( webSocketFeature ) ;
109- Assert . True ( webSocketFeature . IsWebSocketRequest ) ;
110- } ) ;
111- } ) ) )
112- {
113- var client = testServer . CreateWebSocketClient ( ) ;
113+ webHostBuilder
114+ . UseTestServer ( )
115+ . Configure ( app =>
116+ {
117+ app . Run ( async c =>
118+ {
119+ var upgradeFeature = c . Features . Get < IHttpUpgradeFeature > ( ) ;
120+ Assert . NotNull ( upgradeFeature ) ;
121+ Assert . False ( upgradeFeature . IsUpgradableRequest ) ;
122+ await Assert . ThrowsAsync < NotSupportedException > ( ( ) => upgradeFeature . UpgradeAsync ( ) ) ;
114123
115- try
116- {
117- using var socket = await client . ConnectAsync (
118- uri : new Uri ( "http://localhost/connect" ) ,
119- cancellationToken : default ) ;
120- }
121- catch
122- {
123- // An exception will be thrown because our endpoint does not accept the websocket
124- }
124+ var webSocketFeature = c . Features . Get < IHttpWebSocketFeature > ( ) ;
125+ Assert . NotNull ( webSocketFeature ) ;
126+ Assert . True ( webSocketFeature . IsWebSocketRequest ) ;
127+ } ) ;
128+ } ) ;
129+ } )
130+ . Build ( ) ;
131+
132+ var testServer = host . GetTestServer ( ) ;
133+ var client = testServer . CreateWebSocketClient ( ) ;
134+
135+ try
136+ {
137+ using var socket = await client . ConnectAsync (
138+ uri : new Uri ( "http://localhost/connect" ) ,
139+ cancellationToken : default ) ;
140+ }
141+ catch
142+ {
143+ // An exception will be thrown because our endpoint does not accept the websocket
125144 }
126145 }
127146}
0 commit comments