forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpRequestMessageTest.cs
More file actions
496 lines (419 loc) · 19.1 KB
/
HttpRequestMessageTest.cs
File metadata and controls
496 lines (419 loc) · 19.1 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
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Generic;
using System.IO;
using System.Net.Http.Headers;
using System.Net.Http;
using System.Net;
using System.Runtime.InteropServices.JavaScript.Tests;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;
namespace System.Runtime.InteropServices.JavaScript.Http.Tests
{
public class HttpRequestMessageTest
{
private readonly Version _expectedRequestMessageVersion = HttpVersion.Version11;
private HttpRequestOptionsKey<bool> EnableStreamingResponse = new HttpRequestOptionsKey<bool>("WebAssemblyEnableStreamingResponse");
#nullable enable
private HttpRequestOptionsKey<IDictionary<string, object?>> FetchOptions = new HttpRequestOptionsKey<IDictionary<string, object?>>("WebAssemblyFetchOptions");
#nullable disable
[Fact]
public void Ctor_Default_CorrectDefaults()
{
var rm = new HttpRequestMessage();
Assert.Equal(HttpMethod.Get, rm.Method);
Assert.Null(rm.Content);
Assert.Null(rm.RequestUri);
}
[Fact]
public void Ctor_RelativeStringUri_CorrectValues()
{
var rm = new HttpRequestMessage(HttpMethod.Post, "/relative");
Assert.Equal(HttpMethod.Post, rm.Method);
Assert.Equal(_expectedRequestMessageVersion, rm.Version);
Assert.Null(rm.Content);
Assert.Equal(new Uri("/relative", UriKind.Relative), rm.RequestUri);
}
[Theory]
[InlineData("http://host/absolute/")]
[InlineData("blob:http://host/absolute/")]
public void Ctor_AbsoluteStringUri_CorrectValues(string uri)
{
var rm = new HttpRequestMessage(HttpMethod.Post, uri);
Assert.Equal(HttpMethod.Post, rm.Method);
Assert.Equal(_expectedRequestMessageVersion, rm.Version);
Assert.Null(rm.Content);
Assert.Equal(new Uri(uri), rm.RequestUri);
}
[Fact]
public void Ctor_NullStringUri_Accepted()
{
var rm = new HttpRequestMessage(HttpMethod.Put, (string)null);
Assert.Null(rm.RequestUri);
Assert.Equal(HttpMethod.Put, rm.Method);
Assert.Equal(_expectedRequestMessageVersion, rm.Version);
Assert.Null(rm.Content);
}
[Fact]
public void Ctor_RelativeUri_CorrectValues()
{
var uri = new Uri("/relative", UriKind.Relative);
var rm = new HttpRequestMessage(HttpMethod.Post, uri);
Assert.Equal(HttpMethod.Post, rm.Method);
Assert.Equal(_expectedRequestMessageVersion, rm.Version);
Assert.Null(rm.Content);
Assert.Equal(uri, rm.RequestUri);
}
[Theory]
[InlineData("http://host/absolute/")]
[InlineData("blob:http://host/absolute/")]
public void Ctor_AbsoluteUri_CorrectValues(string uriData)
{
var uri = new Uri(uriData);
var rm = new HttpRequestMessage(HttpMethod.Post, uri);
Assert.Equal(HttpMethod.Post, rm.Method);
Assert.Equal(_expectedRequestMessageVersion, rm.Version);
Assert.Null(rm.Content);
Assert.Equal(uri, rm.RequestUri);
}
[Fact]
public void Ctor_NullUri_Accepted()
{
var rm = new HttpRequestMessage(HttpMethod.Put, (Uri)null);
Assert.Null(rm.RequestUri);
Assert.Equal(HttpMethod.Put, rm.Method);
Assert.Equal(_expectedRequestMessageVersion, rm.Version);
Assert.Null(rm.Content);
}
[Theory]
[InlineData("http://example.com")]
[InlineData("blob:http://example.com")]
public void Ctor_NullMethod_ThrowsArgumentNullException(string uriData)
{
Assert.Throws<ArgumentNullException>(() => new HttpRequestMessage(null, uriData));
}
[Fact]
public void Ctor_NonHttpUri_ThrowsArgumentException()
{
AssertExtensions.Throws<ArgumentException>("requestUri", () => new HttpRequestMessage(HttpMethod.Put, "ftp://example.com"));
}
[Theory]
[InlineData("http://example.com")]
[InlineData("blob:http://example.com")]
public void Dispose_DisposeObject_ContentGetsDisposedAndSettersWillThrowButGettersStillWork(string uriData)
{
var rm = new HttpRequestMessage(HttpMethod.Get, uriData);
var content = new MockContent();
rm.Content = content;
Assert.False(content.IsDisposed);
rm.Dispose();
rm.Dispose(); // Multiple calls don't throw.
Assert.True(content.IsDisposed);
Assert.Throws<ObjectDisposedException>(() => { rm.Method = HttpMethod.Put; });
Assert.Throws<ObjectDisposedException>(() => { rm.RequestUri = null; });
Assert.Throws<ObjectDisposedException>(() => { rm.Version = new Version(1, 0); });
Assert.Throws<ObjectDisposedException>(() => { rm.Content = null; });
// Property getters should still work after disposing.
Assert.Equal(HttpMethod.Get, rm.Method);
Assert.Equal(new Uri(uriData), rm.RequestUri);
Assert.Equal(_expectedRequestMessageVersion, rm.Version);
Assert.Equal(content, rm.Content);
}
[Theory]
[InlineData("https://example.com")]
[InlineData("blob:https://example.com")]
public void Properties_SetOptionsAndGetTheirValue_MatchingValues(string uriData)
{
var rm = new HttpRequestMessage();
var content = new MockContent();
var uri = new Uri(uriData);
var version = new Version(1, 0);
var method = new HttpMethod("custom");
rm.Content = content;
rm.Method = method;
rm.RequestUri = uri;
rm.Version = version;
Assert.Equal(content, rm.Content);
Assert.Equal(uri, rm.RequestUri);
Assert.Equal(method, rm.Method);
Assert.Equal(version, rm.Version);
Assert.NotNull(rm.Headers);
Assert.NotNull(rm.Options);
}
#nullable enable
[Theory]
[InlineData("https://example.com")]
[InlineData("blob:https://example.com")]
public void Properties_SetOptionsAndGetTheirValue_Set_FetchOptions(string uriData)
{
var rm = new HttpRequestMessage();
var content = new MockContent();
var uri = new Uri(uriData);
var version = new Version(1, 0);
var method = new HttpMethod("custom");
rm.Content = content;
rm.Method = method;
rm.RequestUri = uri;
rm.Version = version;
var fetchme = new Dictionary<string, object?>();
fetchme.Add("hic", null);
fetchme.Add("sunt", 4444);
fetchme.Add("dracones", new List<string>());
rm.Options.Set(FetchOptions, fetchme);
Assert.Equal(content, rm.Content);
Assert.Equal(uri, rm.RequestUri);
Assert.Equal(method, rm.Method);
Assert.Equal(version, rm.Version);
Assert.NotNull(rm.Headers);
Assert.NotNull(rm.Options);
rm.Options.TryGetValue(FetchOptions, out IDictionary<string, object?>? fetchOptionsValue);
Assert.NotNull(fetchOptionsValue);
if (fetchOptionsValue != null)
{
foreach (var item in fetchOptionsValue)
{
Assert.True(fetchme.ContainsKey(item.Key));
}
}
}
#nullable disable
#nullable enable
[Theory]
[InlineData("https://example.com")]
[InlineData("blob:https://example.com")]
public void Properties_SetOptionsAndGetTheirValue_NotSet_FetchOptions(string uriData)
{
var rm = new HttpRequestMessage();
var content = new MockContent();
var uri = new Uri(uriData);
var version = new Version(1, 0);
var method = new HttpMethod("custom");
rm.Content = content;
rm.Method = method;
rm.RequestUri = uri;
rm.Version = version;
Assert.Equal(content, rm.Content);
Assert.Equal(uri, rm.RequestUri);
Assert.Equal(method, rm.Method);
Assert.Equal(version, rm.Version);
Assert.NotNull(rm.Headers);
Assert.NotNull(rm.Options);
rm.Options.TryGetValue(FetchOptions, out IDictionary<string, object?>? fetchOptionsValue);
Assert.Null(fetchOptionsValue);
}
#nullable disable
[Theory]
[InlineData("https://example.com")]
[InlineData("blob:https://example.com")]
public void Properties_SetOptionsAndGetTheirValue_Set_EnableStreamingResponse(string uriData)
{
var rm = new HttpRequestMessage();
var content = new MockContent();
var uri = new Uri(uriData);
var version = new Version(1, 0);
var method = new HttpMethod("custom");
rm.Content = content;
rm.Method = method;
rm.RequestUri = uri;
rm.Version = version;
rm.Options.Set(EnableStreamingResponse, true);
Assert.Equal(content, rm.Content);
Assert.Equal(uri, rm.RequestUri);
Assert.Equal(method, rm.Method);
Assert.Equal(version, rm.Version);
Assert.NotNull(rm.Headers);
Assert.NotNull(rm.Options);
rm.Options.TryGetValue(EnableStreamingResponse, out bool streamingEnabledValue);
Assert.True(streamingEnabledValue);
}
[Theory]
[InlineData("https://example.com")]
[InlineData("blob:https://example.com")]
public void Properties_SetOptionsAndGetTheirValue_NotSet_EnableStreamingResponse(string uriData)
{
var rm = new HttpRequestMessage();
var content = new MockContent();
var uri = new Uri(uriData);
var version = new Version(1, 0);
var method = new HttpMethod("custom");
rm.Content = content;
rm.Method = method;
rm.RequestUri = uri;
rm.Version = version;
Assert.Equal(content, rm.Content);
Assert.Equal(uri, rm.RequestUri);
Assert.Equal(method, rm.Method);
Assert.Equal(version, rm.Version);
Assert.NotNull(rm.Headers);
Assert.NotNull(rm.Options);
rm.Options.TryGetValue(EnableStreamingResponse, out bool streamingEnabledValue);
Assert.False(streamingEnabledValue);
}
[Fact]
public void RequestUri_SetNonHttpUri_ThrowsArgumentException()
{
var rm = new HttpRequestMessage();
AssertExtensions.Throws<ArgumentException>("value", () => { rm.RequestUri = new Uri("ftp://example.com"); });
}
[Fact]
public void Version_SetToNull_ThrowsArgumentNullException()
{
var rm = new HttpRequestMessage();
Assert.Throws<ArgumentNullException>(() => { rm.Version = null; });
}
[Fact]
public void Method_SetToNull_ThrowsArgumentNullException()
{
var rm = new HttpRequestMessage();
Assert.Throws<ArgumentNullException>(() => { rm.Method = null; });
}
[Fact]
public void ToString_DefaultInstance_DumpAllFields()
{
var rm = new HttpRequestMessage();
string expected =
"Method: GET, RequestUri: '<null>', Version: " +
_expectedRequestMessageVersion.ToString(2) +
$", Content: <null>, Headers:{Environment.NewLine}{{{Environment.NewLine}}}";
Assert.Equal(expected, rm.ToString());
}
[Theory]
[InlineData("http://a.com/")]
[InlineData("blob:http://a.com/")]
public void ToString_NonDefaultInstanceWithNoCustomHeaders_DumpAllFields(string uriData)
{
var rm = new HttpRequestMessage();
rm.Method = HttpMethod.Put;
rm.RequestUri = new Uri(uriData);
rm.Version = new Version(1, 0);
rm.Content = new StringContent("content");
// Note that there is no Content-Length header: The reason is that the value for Content-Length header
// doesn't get set by StringContent..ctor, but only if someone actually accesses the ContentLength property.
Assert.Equal(
$"Method: PUT, RequestUri: '{uriData}', Version: 1.0, Content: " + typeof(StringContent).ToString() + ", Headers:" + Environment.NewLine +
$"{{{Environment.NewLine}" +
" Content-Type: text/plain; charset=utf-8" + Environment.NewLine +
"}", rm.ToString());
}
[Theory]
[InlineData("http://a.com/")]
[InlineData("blob:http://a.com/")]
public void ToString_NonDefaultInstanceWithCustomHeaders_DumpAllFields(string uriData)
{
var rm = new HttpRequestMessage();
rm.Method = HttpMethod.Put;
rm.RequestUri = new Uri(uriData);
rm.Version = new Version(1, 0);
rm.Content = new StringContent("content");
rm.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("text/plain", 0.2));
rm.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("text/xml", 0.1));
rm.Headers.Add("Custom-Request-Header", "value1");
rm.Content.Headers.Add("Custom-Content-Header", "value2");
Assert.Equal(
$"Method: PUT, RequestUri: '{uriData}', Version: 1.0, Content: " + typeof(StringContent).ToString() + ", Headers:" + Environment.NewLine +
"{" + Environment.NewLine +
" Accept: text/plain; q=0.2" + Environment.NewLine +
" Accept: text/xml; q=0.1" + Environment.NewLine +
" Custom-Request-Header: value1" + Environment.NewLine +
" Content-Type: text/plain; charset=utf-8" + Environment.NewLine +
" Custom-Content-Header: value2" + Environment.NewLine +
"}", rm.ToString());
}
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsBrowserDomSupported))]
public async Task BlobUri_Marshal_CorrectValues_Browser()
{
Runtime.InvokeJS(@"
function typedArrayToURL(typedArray, mimeType) {
return URL.createObjectURL(new Blob([typedArray.buffer], {type: mimeType}))
}
const bytes = new Uint8Array(59);
for(let i = 0; i < 59; i++) {
bytes[i] = 32 + i;
}
const url = typedArrayToURL(bytes, 'text/plain');
// Calls method with string that will be marshaled as valid URI
App.call_test_method (""InvokeString"", [ url ]);
");
var client = new HttpClient ();
Assert.StartsWith ("blob:", HelperMarshal._stringResource);
HttpRequestMessage rm = new HttpRequestMessage(HttpMethod.Get, new Uri (HelperMarshal._stringResource));
HttpResponseMessage resp = await client.SendAsync (rm);
Assert.NotNull (resp.Content);
string content = await resp.Content.ReadAsStringAsync();
Assert.Equal (59, content.Length);
}
[Fact]
public void BlobStringUri_Marshal_CorrectValues()
{
Runtime.InvokeJS(@"
function typedArrayToURL(typedArray, mimeType) {
// URL.createObjectURL does not work outside of browser but since this was actual
// test code from https://developer.mozilla.org/en-US/docs/Web/API/Blob
// left it in to show what this should do if the test code were to actually run
//return URL.createObjectURL(new Blob([typedArray.buffer], {type: mimeType}))
return 'blob:https://mdn.mozillademos.org/ca45b575-6348-4d3e-908a-3dbf3d146ea7';
}
const bytes = new Uint8Array(59);
for(let i = 0; i < 59; i++) {
bytes[i] = 32 + i;
}
const url = typedArrayToURL(bytes, 'text/plain');
// Calls method with string that will be converted to a valid Uri
// within the method
App.call_test_method (""SetBlobUrl"", [ url ]);
");
var rm = new HttpRequestMessage(HttpMethod.Post, HelperMarshal._blobURL);
Assert.Equal(HttpMethod.Post, rm.Method);
Assert.Equal(_expectedRequestMessageVersion, rm.Version);
Assert.Null(rm.Content);
Assert.Equal(new Uri("blob:https://mdn.mozillademos.org/ca45b575-6348-4d3e-908a-3dbf3d146ea7"), rm.RequestUri);
}
[Fact]
public void BlobUri_Marshal_CorrectValues()
{
Runtime.InvokeJS(@"
function typedArrayToURL(typedArray, mimeType) {
// URL.createObjectURL does not work outside of browser but since this was actual
// test code from https://developer.mozilla.org/en-US/docs/Web/API/Blob
// left it in to show what this should do if the test code were to actually run
//return URL.createObjectURL(new Blob([typedArray.buffer], {type: mimeType}))
return 'blob:https://mdn.mozillademos.org/ca45b575-6348-4d3e-908a-3dbf3d146ea7';
}
const bytes = new Uint8Array(59);
for(let i = 0; i < 59; i++) {
bytes[i] = 32 + i;
}
const url = typedArrayToURL(bytes, 'text/plain');
// Calls method with string that will be marshaled as valid URI
App.call_test_method (""SetBlobAsUri"", [ url ]);
");
var rm = new HttpRequestMessage(HttpMethod.Post, HelperMarshal._blobURI);
Assert.Equal(HttpMethod.Post, rm.Method);
Assert.Equal(_expectedRequestMessageVersion, rm.Version);
Assert.Null(rm.Content);
Assert.Equal(new Uri("blob:https://mdn.mozillademos.org/ca45b575-6348-4d3e-908a-3dbf3d146ea7"), rm.RequestUri);
}
#region Helper methods
private class MockContent : HttpContent
{
public bool IsDisposed { get; private set; }
protected override bool TryComputeLength(out long length)
{
throw new NotImplementedException();
}
#nullable enable
protected override Task SerializeToStreamAsync(Stream stream, TransportContext? context)
{
#nullable disable
throw new NotImplementedException();
}
protected override void Dispose(bool disposing)
{
IsDisposed = true;
base.Dispose(disposing);
}
}
#endregion
}
}