-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
NegotiateFacts.cs
339 lines (274 loc) · 14.5 KB
/
NegotiateFacts.cs
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
using System;
using System.Threading.Tasks;
using Microsoft.AspNet.SignalR.Client.Infrastructure;
using Microsoft.AspNet.SignalR.Tests.Common.Infrastructure;
using Xunit;
namespace Microsoft.AspNet.SignalR.Tests
{
public class NegotiateFacts : HostedTest
{
[Fact]
public async Task CanConnectToEndpointWhichProducesARedirectResponse()
{
using (var host = CreateHost(HostType.Memory, TransportType.Auto))
{
host.Initialize();
using (var connection = CreateHubConnection(host, path: "/redirect", useDefaultUrl: false))
{
var hub = connection.CreateHubProxy("RedirectTestHub");
await connection.Start(host.TransportFactory());
// Verify we're connected by calling the echo method
var result = await hub.Invoke<string>("EchoReturn", "Hello, World!");
Assert.Equal("Hello, World!", result);
}
}
}
[Fact]
public async Task OriginalUrlUsedWhenClientStopsAfterRedirectResponse()
{
using (var host = CreateHost(HostType.Memory, TransportType.Auto))
{
host.Initialize();
using (var connection = CreateHubConnection(host, path: "/redirect", useDefaultUrl: false))
{
await connection.Start(host.TransportFactory());
// Verify the client redirected to the /signalr endpoint.
Assert.Equal($"{host.Url}/signalr/", ((Client.IConnection)connection).Url);
connection.Stop();
// Verify the client's URL is reset to the initial value after stoping.
Assert.Equal($"{host.Url}/redirect/", ((Client.IConnection)connection).Url);
}
}
}
[Fact]
public async Task CanConnectToEndpointWhichProducesARedirectResponseWithAQueryString()
{
using (var host = CreateHost(HostType.Memory, TransportType.Auto))
{
host.Initialize();
// "/redirect-query-string2" -> "/signalr?name1=newValue&name3=value3"
using (var connection = CreateHubConnection(host, path: "/redirect-query-string2", useDefaultUrl: false))
{
var hub = connection.CreateHubProxy("RedirectTestHub");
await connection.Start(host.TransportFactory());
Assert.Equal("newValue", await hub.Invoke<string>("GetQueryStringValue", "name1"));
Assert.Equal("value3", await hub.Invoke<string>("GetQueryStringValue", "name3"));
}
}
}
[Fact]
public async Task DoesNotMergeUserQueryStringWithRedirectQueryString()
{
using (var host = CreateHost(HostType.Memory, TransportType.Auto))
{
host.Initialize();
// This will get passed through queryString param in the (Hub)Connection ctor.
host.ExtraData["foo"] = "bar";
// "/redirect-query-string2" -> "/signalr?name1=newValue&name3=value3"
using (var connection = CreateHubConnection(host, path: "/redirect-query-string2", useDefaultUrl: false))
{
var hub = connection.CreateHubProxy("RedirectTestHub");
await connection.Start(host.TransportFactory());
// Verify query string value set via (Hub)Connection ctor is absent. In practice, the redirect should reflect the query string.
Assert.Equal(null, await hub.Invoke<string>("GetQueryStringValue", "foo"));
// Verify values set by redirect.
Assert.Equal("newValue", await hub.Invoke<string>("GetQueryStringValue", "name1"));
Assert.Equal("value3", await hub.Invoke<string>("GetQueryStringValue", "name3"));
// Verify that (Hub)Connection.QueryString only contains what was specified by the user, not the redirect.
// IConnection.QueryString contains only what was set via redirect. This is what's used by the client to actually build URLs.
Assert.Contains("foo=bar", connection.QueryString);
Assert.DoesNotContain("foo=bar", ((Client.IConnection)connection).QueryString);
Assert.DoesNotContain("name1=newValue", connection.QueryString);
Assert.Contains("name1=newValue", ((Client.IConnection)connection).QueryString);
}
}
}
[Fact]
public async Task ClearsRedirectQueryStringIfLastRedirectDoesNotIncludeAQueryString()
{
using (var host = CreateHost(HostType.Memory, TransportType.Auto))
{
host.Initialize();
// "/redirect-query-string-clear"
// -> "/redirect-query-string-clear2?clearedName=clearedValue"
// -> "/signalr"
using (var connection = CreateHubConnection(host, path: "/redirect-query-string-clear"))
{
var hub = connection.CreateHubProxy("RedirectTestHub");
await connection.Start(host.TransportFactory());
// Verify the client does not preserve a query string key-value pair only specified in an intermediate RedirectUrl.
Assert.Equal(null, await hub.Invoke<string>("GetQueryStringValue", "clearedName"));
}
}
}
[Fact]
public async Task PreservesUserQueryStringIfLastRedirectDoesNotIncludeAQueryString()
{
using (var host = CreateHost(HostType.Memory, TransportType.Auto))
{
host.Initialize();
// This will get passed through queryString param in the (Hub)Connection ctor.
host.ExtraData["foo"] = "bar";
// "/redirect-query-string-clear?foo=bar"
// -> "/redirect-query-string-clear2?clearedName=clearedValue"
// -> "/signalr?foo=bar"
using (var connection = CreateHubConnection(host, path: "/redirect-query-string-clear", useDefaultUrl: false))
{
var hub = connection.CreateHubProxy("RedirectTestHub");
await connection.Start(host.TransportFactory());
// Verify query string value set via (Hub)Connection ctor is set.
Assert.Equal("bar", await hub.Invoke<string>("GetQueryStringValue", "foo"));
// Verify the client does not preserve a query string key-value pair only specified in an intermediate RedirectUrl.
Assert.Equal(null, await hub.Invoke<string>("GetQueryStringValue", "clearedName"));
}
}
}
[Fact]
public async Task OnlyPreservesLastRedirectQueryString()
{
using (var host = CreateHost(HostType.Memory, TransportType.Auto))
{
host.Initialize();
// "/redirect-query-string"
// -> "/redirect-query-string2?name1=value1&name2=value2"
// -> "/signalr?name1=newValue&name3=value3&origName1={context.Request.Query["name1"]}"
using (var connection = CreateHubConnection(host, path: "/redirect-query-string", useDefaultUrl: false))
{
var hub = connection.CreateHubProxy("RedirectTestHub");
await connection.Start(host.TransportFactory());
// Verify the client preserves query string key-value pairs specified in the last RedirectUrl.
Assert.Equal("newValue", await hub.Invoke<string>("GetQueryStringValue", "name1"));
Assert.Equal("value3", await hub.Invoke<string>("GetQueryStringValue", "name3"));
// Verify the client used "name1=value1" from the first RedirectUrl for the next request in the redirect chain.
Assert.Equal("value1", await hub.Invoke<string>("GetQueryStringValue", "origName1"));
// Verify the client does not preserve a query string key-value pair only specified in an intermediate RedirectUrl.
Assert.Equal(null, await hub.Invoke<string>("GetQueryStringValue", "name2"));
}
}
}
[Fact]
public async Task CanConnectToEndpointWhichProducesARedirectResponseWithAnInvalidQueryString()
{
using (var host = CreateHost(HostType.Memory, TransportType.Auto))
{
host.Initialize();
// "/redirect-query-string-invalid" -> "/signalr?redirect=invalid&/?=/&"
using (var connection = CreateHubConnection(host, path: "/redirect-query-string-invalid", useDefaultUrl: false))
{
var hub = connection.CreateHubProxy("RedirectTestHub");
await connection.Start(host.TransportFactory());
Assert.Equal("invalid", await hub.Invoke<string>("GetQueryStringValue", "redirect"));
}
}
}
[Fact]
public async Task OriginalQueryStringUsedWhenClientStopsAfterRedirectResponseWithAQueryString()
{
using (var host = CreateHost(HostType.Memory, TransportType.Auto))
{
host.Initialize();
// "/redirect-query-string2" -> "/signalr?name1=newValue&name3=value3"
using (var connection = CreateHubConnection(host, path: "/redirect-query-string2", useDefaultUrl: false))
{
await connection.Start(host.TransportFactory());
// Verify the client redirected to the /signalr endpoint.
Assert.Contains("name1=newValue", ((Client.IConnection)connection).QueryString);
Assert.Contains("name3=value3", ((Client.IConnection)connection).QueryString);
connection.Stop();
// Verify the client's URL is reset to the initial value after stopping.
Assert.DoesNotContain("name1=newValue", ((Client.IConnection)connection).QueryString);
Assert.DoesNotContain("name3=value3", ((Client.IConnection)connection).QueryString);
}
}
}
[Theory]
[InlineData(TransportType.Auto)]
[InlineData(TransportType.LongPolling)]
[InlineData(TransportType.ServerSentEvents)]
[InlineData(TransportType.Websockets)]
public async Task TransportForwardsAccessTokenProvidedByRedirectResponse(TransportType transportType)
{
// HttpListener is needed to support Websockets.
using (var host = CreateHost(HostType.HttpListener, TransportType.Auto))
{
host.Initialize();
using (var connection = CreateHubConnection(host, path: "/redirect", useDefaultUrl: false))
{
var hub = connection.CreateHubProxy("RedirectTestHub");
await connection.Start(host.TransportFactory());
// Verify we're connected by calling the echo method
var result = await hub.Invoke<string>("GetAccessToken");
Assert.Equal("TestToken", result);
}
}
}
[Fact]
public async Task RedirectsAreLimitedToPreventInfiniteLooping()
{
using (var host = CreateHost(HostType.Memory, TransportType.Auto))
{
host.Initialize();
using (var connection = CreateHubConnection(host, path: "/redirect-loop", useDefaultUrl: false))
{
await Assert.ThrowsAsync<InvalidOperationException>(() => connection.Start(host.TransportFactory()).OrTimeout());
}
}
}
[Fact]
public async Task DoesNotFollowRedirectIfProtocolVersionIsLessThan20()
{
using (var host = CreateHost(HostType.Memory, TransportType.Auto))
{
host.Initialize();
using (var connection = CreateHubConnection(host, path: "/redirect-old-proto", useDefaultUrl: false))
{
// Should fail to connect.
await Assert.ThrowsAsync<TimeoutException>(() => connection.Start(host.TransportFactory()));
}
}
}
[Fact]
public async Task DoesNotFollowRedirectIfProtocolVersionIsGreaterThan21()
{
using (var host = CreateHost(HostType.Memory, TransportType.Auto))
{
host.Initialize();
using (var connection = CreateHubConnection(host, path: "/redirect-future-proto", useDefaultUrl: false))
{
// Should fail to connect.
await Assert.ThrowsAsync<InvalidOperationException>(() => connection.Start(host.TransportFactory()));
}
}
}
[Fact]
public async Task DoesFollowRedirectIfProtocolVersionIs21()
{
using (var host = CreateHost(HostType.Memory, TransportType.Auto))
{
host.Initialize();
using (var connection = CreateHubConnection(host, path: "/redirect-new-proto", useDefaultUrl: false))
{
var hub = connection.CreateHubProxy("RedirectTestHub");
await connection.Start(host.TransportFactory());
// Verify we're connected by calling the echo method
var result = await hub.Invoke<string>("EchoReturn", "Hello, World!");
Assert.Equal("Hello, World!", result);
}
}
}
[Fact]
public async Task ThrowsErrorProvidedByServerIfNegotiateResponseContainsErrorMessage()
{
using (var host = CreateHost(HostType.Memory, TransportType.Auto))
{
host.Initialize();
using (var connection = CreateHubConnection(host, path: "/negotiate-error", useDefaultUrl: false))
{
// Should fail to connect.
var ex = await Assert.ThrowsAsync<StartException>(() => connection.Start(host.TransportFactory()));
Assert.Equal("Error message received from the server: 'Server-provided negotiate error message!'.", ex.Message);
}
}
}
}
}