|
| 1 | +// Copyright 2020 New Relic, Inc. All rights reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +#if !NETFRAMEWORK |
| 5 | +using System; |
| 6 | +using System.Net; |
| 7 | +using System.Net.Http; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using NewRelic.Agent.Configuration; |
| 10 | +using NewRelic.Agent.Core.DataTransport.Client.Interfaces; |
| 11 | +using NUnit.Framework; |
| 12 | +using Telerik.JustMock; |
| 13 | +using Telerik.JustMock.Helpers; |
| 14 | + |
| 15 | +namespace NewRelic.Agent.Core.DataTransport.Client |
| 16 | +{ |
| 17 | + internal class NRHttpClientFactoryTests |
| 18 | + { |
| 19 | + private IConfiguration _mockConfiguration; |
| 20 | + private IWebProxy _mockProxy; |
| 21 | + private IHttpClientFactory _httpClientFactory; |
| 22 | + |
| 23 | + [SetUp] |
| 24 | + public void SetUp() |
| 25 | + { |
| 26 | + _mockConfiguration = Mock.Create<IConfiguration>(); |
| 27 | + Mock.Arrange(() => _mockConfiguration.AgentLicenseKey).Returns("12345"); |
| 28 | + Mock.Arrange(() => _mockConfiguration.AgentRunId).Returns("123"); |
| 29 | + Mock.Arrange(() => _mockConfiguration.CollectorMaxPayloadSizeInBytes).Returns(int.MaxValue); |
| 30 | + |
| 31 | + _mockProxy = Mock.Create<IWebProxy>(); |
| 32 | + |
| 33 | + _httpClientFactory = new NRHttpClientFactory(); |
| 34 | + } |
| 35 | + |
| 36 | + [Test] |
| 37 | + public void CreateClient_NotNull() |
| 38 | + { |
| 39 | + var client = _httpClientFactory.CreateClient(null, _mockConfiguration); |
| 40 | + |
| 41 | + Assert.That(client, Is.Not.Null); |
| 42 | + } |
| 43 | + |
| 44 | + [Test] |
| 45 | + public void CreateClient_NoProxy_ReturnsSameClient() |
| 46 | + { |
| 47 | + var clientA = _httpClientFactory.CreateClient(null, _mockConfiguration); |
| 48 | + var clientB = _httpClientFactory.CreateClient(null, _mockConfiguration); |
| 49 | + |
| 50 | + Assert.That(clientA == clientB); |
| 51 | + } |
| 52 | + |
| 53 | + [Test] |
| 54 | + public void CreateClient_Proxy_ReturnsSameClient() |
| 55 | + { |
| 56 | + var clientA = _httpClientFactory.CreateClient(_mockProxy, _mockConfiguration); |
| 57 | + var clientB = _httpClientFactory.CreateClient(_mockProxy, _mockConfiguration); |
| 58 | + |
| 59 | + Assert.That(clientA == clientB); |
| 60 | + } |
| 61 | + |
| 62 | + [Test] |
| 63 | + public void CreateClient_NoProxyToProxy_ReturnsNewClient() |
| 64 | + { |
| 65 | + var clientA = _httpClientFactory.CreateClient(null, _mockConfiguration); |
| 66 | + var clientB = _httpClientFactory.CreateClient(_mockProxy, _mockConfiguration); |
| 67 | + |
| 68 | + Assert.That(clientA != clientB); |
| 69 | + } |
| 70 | + |
| 71 | + [Test] |
| 72 | + public void CreateClient_ProxyToNoProxy_ReturnsNewClient() |
| 73 | + { |
| 74 | + var clientA = _httpClientFactory.CreateClient(_mockProxy, _mockConfiguration); |
| 75 | + var clientB = _httpClientFactory.CreateClient(null, _mockConfiguration); |
| 76 | + |
| 77 | + Assert.That(clientA != clientB); |
| 78 | + } |
| 79 | + } |
| 80 | +} |
| 81 | +#endif |
0 commit comments