public void HasIpAddressChanged_ShouldThrowException_WhenHttpRequestFailsToGetData(string oldIpAddress, string newIpAddress) { // Arrange // IHttpClientFactory var httpClientFactory = new Mock <IHttpClientFactory>(); var mockHttpMessageHandler = new Mock <HttpMessageHandler>(); mockHttpMessageHandler.Protected() .Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>()) .ReturnsAsync(new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = new StringContent(newIpAddress), }) .Verifiable(); var client = new HttpClient(mockHttpMessageHandler.Object); var expectedUrl = new Uri(IpAddressChangeDetector.IpifyUri); client.BaseAddress = expectedUrl; httpClientFactory.Setup(_ => _.CreateClient(It.IsAny <string>())).Returns(client); // FileSystem var mockFileSystem = new MockFileSystem(); var mockInputFile = new MockFileData(oldIpAddress); mockFileSystem.AddFile(IpAddressChangeDetector.PreviousIpAddressFile, mockInputFile); // Act and Assert var sut = new IpAddressChangeDetector(httpClientFactory.Object, mockFileSystem, Mock.Of <ILogger>()); Assert.That(async() => { await sut.HasIpAddressChanged(); }, Throws.Exception.TypeOf <InvalidDataException>()); }
public async Task HasIpAddressChanged_ShouldDetectIsChanged(string oldIpAddress, string newIpAddress) { // Arrange // IHttpClientFactory var httpClientFactory = new Mock <IHttpClientFactory>(); var mockHttpMessageHandler = new Mock <HttpMessageHandler>(); mockHttpMessageHandler.Protected() .Setup <Task <HttpResponseMessage> >("SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>()) .ReturnsAsync(new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = new StringContent(newIpAddress), }) .Verifiable(); var client = new HttpClient(mockHttpMessageHandler.Object); var expectedUrl = new Uri(IpAddressChangeDetector.IpifyUri); client.BaseAddress = expectedUrl; httpClientFactory.Setup(_ => _.CreateClient(It.IsAny <string>())).Returns(client); // FileSystem var mockFileSystem = new MockFileSystem(); var mockInputFile = new MockFileData(oldIpAddress); mockFileSystem.AddFile(IpAddressChangeDetector.PreviousIpAddressFile, mockInputFile); // Act var sut = new IpAddressChangeDetector(httpClientFactory.Object, mockFileSystem, Mock.Of <ILogger>()); var result = await sut.HasIpAddressChanged(); // Assert Assert.AreEqual(string.Compare(oldIpAddress, newIpAddress) != 0, result.IpAddressHasChanged); Assert.AreEqual(oldIpAddress, result.OldIpAddress); Assert.AreEqual(newIpAddress, result.NewIpAddress); }
public void HasIpAddressChanged_ShouldThrowException_WhenHttpRequestThrowException() { // Arrange // IHttpClientFactory var httpClientFactory = new Mock <IHttpClientFactory>(); var client = new HttpClient(new ThrowingHandler()); var expectedUrl = new Uri(IpAddressChangeDetector.IpifyUri); client.BaseAddress = expectedUrl; httpClientFactory.Setup(_ => _.CreateClient(It.IsAny <string>())).Returns(client); // FileSystem var mockFileSystem = new MockFileSystem(); var mockInputFile = new MockFileData(""); mockFileSystem.AddFile(IpAddressChangeDetector.PreviousIpAddressFile, mockInputFile); // Act and Assert var sut = new IpAddressChangeDetector(httpClientFactory.Object, mockFileSystem, Mock.Of <ILogger>()); Assert.That(async() => { await sut.HasIpAddressChanged(); }, Throws.Exception.TypeOf <Exception>()); }