|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 3 | + |
1 | 4 | using System;
|
| 5 | +using System.Collections.Generic; |
2 | 6 | using System.Net;
|
3 | 7 | using System.Net.Http;
|
| 8 | +using System.Net.WebSockets; |
| 9 | +using System.Reflection; |
| 10 | +using System.Security.Cryptography.X509Certificates; |
4 | 11 | using System.Threading.Tasks;
|
5 | 12 | using Microsoft.AspNetCore.Connections;
|
| 13 | +using Microsoft.AspNetCore.Http.Connections; |
6 | 14 | using Microsoft.AspNetCore.Http.Connections.Client;
|
7 | 15 | using Microsoft.AspNetCore.SignalR.Protocol;
|
8 | 16 | using Microsoft.Extensions.Logging.Abstractions;
|
@@ -64,5 +72,49 @@ public async Task OptionsUrlMustMatchEndPointIfSet()
|
64 | 72 | var ex = await Assert.ThrowsAsync<InvalidOperationException>(async () => await factory.ConnectAsync(new HttpEndPoint(url2)));
|
65 | 73 | Assert.Equal("If HttpConnectionOptions.Url was set, it must match the HttpEndPoint.Url passed to ConnectAsync.", ex.Message);
|
66 | 74 | }
|
| 75 | + |
| 76 | + [Fact] |
| 77 | + public void ShallowCopyHttpConnectionOptionsCopiesAllPublicProperties() |
| 78 | + { |
| 79 | + Func<HttpMessageHandler, HttpMessageHandler> handlerFactory = handler => handler; |
| 80 | + Func<Task<string>> tokenProvider = () => Task.FromResult(""); |
| 81 | + Action<ClientWebSocketOptions> webSocketConfig = options => { }; |
| 82 | + |
| 83 | + var testValues = new Dictionary<string, object> |
| 84 | + { |
| 85 | + { $"{nameof(HttpConnectionOptions.HttpMessageHandlerFactory)}", handlerFactory }, |
| 86 | + { $"{nameof(HttpConnectionOptions.Headers)}", new Dictionary<string, string>() }, |
| 87 | + { $"{nameof(HttpConnectionOptions.ClientCertificates)}", new X509CertificateCollection() }, |
| 88 | + { $"{nameof(HttpConnectionOptions.Cookies)}", new CookieContainer() }, |
| 89 | + { $"{nameof(HttpConnectionOptions.Url)}", new Uri("https://example.com") }, |
| 90 | + { $"{nameof(HttpConnectionOptions.Transports)}", HttpTransportType.ServerSentEvents }, |
| 91 | + { $"{nameof(HttpConnectionOptions.SkipNegotiation)}", true }, |
| 92 | + { $"{nameof(HttpConnectionOptions.AccessTokenProvider)}", tokenProvider }, |
| 93 | + { $"{nameof(HttpConnectionOptions.CloseTimeout)}", TimeSpan.FromDays(1) }, |
| 94 | + { $"{nameof(HttpConnectionOptions.Credentials)}", Mock.Of<ICredentials>() }, |
| 95 | + { $"{nameof(HttpConnectionOptions.Proxy)}", Mock.Of<IWebProxy>() }, |
| 96 | + { $"{nameof(HttpConnectionOptions.UseDefaultCredentials)}", true }, |
| 97 | + { $"{nameof(HttpConnectionOptions.WebSocketConfiguration)}", webSocketConfig }, |
| 98 | + }; |
| 99 | + |
| 100 | + var options = new HttpConnectionOptions(); |
| 101 | + var properties = typeof(HttpConnectionOptions) |
| 102 | + .GetProperties(BindingFlags.Public | BindingFlags.Instance); |
| 103 | + |
| 104 | + foreach (var property in properties) |
| 105 | + { |
| 106 | + property.SetValue(options, testValues[property.Name]); |
| 107 | + } |
| 108 | + |
| 109 | + var shallowCopiedOptions = HttpConnectionFactory.ShallowCopyHttpConnectionOptions(options); |
| 110 | + |
| 111 | + foreach (var property in properties) |
| 112 | + { |
| 113 | + Assert.Equal(testValues[property.Name], property.GetValue(shallowCopiedOptions)); |
| 114 | + testValues.Remove(property.Name); |
| 115 | + } |
| 116 | + |
| 117 | + Assert.Empty(testValues); |
| 118 | + } |
67 | 119 | }
|
68 | 120 | }
|
0 commit comments