public async Task <IHttpActionResult> PostAsync([FromBody] string ipOrUrl) { if (string.IsNullOrWhiteSpace(ipOrUrl)) { return(BadRequest("IP or URL should be provided.")); } try { if (locationValidator.IsValidIpAddress(ipOrUrl)) { CreateGeolocationDetailsWithIpReturnModel newItemWithIp = await detailsManager.CreateWithIpAsync(ipOrUrl); return(Created($"api/geolocation/{newItemWithIp.IP}", newItemWithIp)); } else if (locationValidator.IsValidUrl(ipOrUrl)) { CreateGeolocationDetailsWithUrlReturnModel newItemWithUrl = await detailsManager.CreateWithUrlAsync(ipOrUrl); return(Created($"api/geolocation/{newItemWithUrl.URL}", newItemWithUrl)); } return(BadRequest("Invalid IP or URL provided.")); } catch (Exception) { return(InternalServerError()); } }
public void Can_Create_Geolocation_Details_With_URL() { // Arrange GeolocationDetails sampleDetails = new GeolocationDetails() { URL = "URL" }; CreateGeolocationDetailsWithUrlReturnModel sampleControllerModel = new CreateGeolocationDetailsWithUrlReturnModel(sampleDetails); Mock <IGeolocationDetailsManager> mockManger = new Mock <IGeolocationDetailsManager>(); mockManger.Setup(x => x.CreateWithUrlAsync(It.IsAny <string>())).Returns(Task.FromResult(sampleControllerModel)); Mock <ILocationValidator> mockLocationValidator = new Mock <ILocationValidator>(); mockLocationValidator.Setup(x => x.IsValidUrl(It.IsAny <string>())).Returns(true); var controller = new GeolocationController(mockManger.Object, mockLocationValidator.Object); // Act var actionResult = controller.PostAsync("test").Result as CreatedNegotiatedContentResult <CreateGeolocationDetailsWithUrlReturnModel>; // Assert Assert.IsNotNull(actionResult); Assert.AreEqual("URL", actionResult.Content.URL); }