public void LegalIdentifiersPassValidation(string identifier) { var parameter = "someIdentity"; var errorCode = "someError"; var errorDescription = "error description"; var requestBody = "REQUEST BODY CONTENT"; var mockLogger = new Mock <ILogger>(); var mockDependencyScope = new Mock <IDependencyScope>(); var httpConfiguration = new HttpConfiguration(); var routeData = new HttpRouteData(new HttpRoute()); var request = new HttpRequestMessage(); var values = new Dictionary <string, object> { { parameter, identifier } }; var identityValidator = new IdentifierRouteConstraint(errorCode, errorDescription); mockDependencyScope.Setup(scope => scope.GetService(It.Is <Type>(type => type == typeof(ILogger)))) .Returns(mockLogger.Object); mockLogger.Setup(logger => logger.ForContext(It.IsAny <string>(), It.IsAny <object>(), It.IsAny <bool>())) .Returns(mockLogger.Object); request.Content = new StringContent(requestBody); request.SetConfiguration(httpConfiguration); request.SetRouteData(routeData); request.Properties.Add(HttpPropertyKeys.DependencyScope, mockDependencyScope.Object); Action actionUnderTest = () => identityValidator.Match(request, Mock.Of <IHttpRoute>(), parameter, values, HttpRouteDirection.UriResolution); actionUnderTest.ShouldNotThrow("because the constraint was not violated"); }
public void WhenConstructedWithGenericTypeShort_ItShouldUseShortRouteConstraint(object routeValue, bool expectedResult) { // Arrange var routeConstraint = new IdentifierRouteConstraint <short>(); var routeValues = RouteValueDictionary.FromArray(new[] { new KeyValuePair <string, object>("id", routeValue), }); // Act var result = routeConstraint.Match(null, null, "id", routeValues, RouteDirection.IncomingRequest); // Assert Assert.Equal(expectedResult, result); }
public void ValidationIsNotPerformedWhenParameterIsNotPresent() { var parameter = "someIdentity"; var errorCode = "someError"; var errorDescription = "error description"; var httpConfiguration = new HttpConfiguration(); var routeData = new HttpRouteData(new HttpRoute()); var request = new HttpRequestMessage(); var values = new Dictionary <string, object> { { "Not My Parameter", "Not My Value" } }; var identityValidator = new IdentifierRouteConstraint(errorCode, errorDescription); request.SetConfiguration(httpConfiguration); request.SetRouteData(routeData); Action actionUnderTest = () => identityValidator.Match(request, Mock.Of <IHttpRoute>(), parameter, values, HttpRouteDirection.UriResolution); actionUnderTest.ShouldNotThrow("because the parameter to check the constraint against was not present"); }
public async Task IdentifiersWithBadCharactersFailValidationAndAreLogged(string identifier) { var parameter = "someIdentity"; var errorCode = "someError"; var errorDescription = "error description"; var requestBody = "REQUEST BODY CONTENT"; var mockLogger = new Mock <ILogger>(); var mockDependencyScope = new Mock <IDependencyScope>(); var httpConfiguration = new HttpConfiguration(); var routeData = new HttpRouteData(new HttpRoute()); var request = new HttpRequestMessage(); var values = new Dictionary <string, object> { { parameter, identifier } }; var identityValidator = new IdentifierRouteConstraint(errorCode, errorDescription); mockDependencyScope.Setup(scope => scope.GetService(It.Is <Type>(type => type == typeof(ILogger)))) .Returns(mockLogger.Object); mockLogger.Setup(logger => logger.ForContext(It.IsAny <string>(), It.IsAny <object>(), It.IsAny <bool>())) .Returns(mockLogger.Object); request.Content = new StringContent(requestBody); request.SetConfiguration(httpConfiguration); request.SetRouteData(routeData); request.Properties.Add(HttpPropertyKeys.DependencyScope, mockDependencyScope.Object); var response = default(HttpResponseMessage); Action actionUnderTest = () => { try { identityValidator.Match(request, Mock.Of <IHttpRoute>(), parameter, values, HttpRouteDirection.UriResolution); } catch (HttpResponseException ex) { response = ex.Response; throw; } }; actionUnderTest.ShouldThrow <HttpResponseException>("because the constraint was violated"); response.Should().NotBeNull("because the failed validation should set a response"); response.StatusCode.Should().Be(HttpStatusCode.BadRequest, "because a failed validation should be considered a BadRequest"); response.Content.Should().NotBeNull("because there should have been an error set returned for context"); var responseContent = await response.Content.ReadAsAsync <ErrorSet>(); responseContent.Should().NotBeNull("because the content body should be an error set"); responseContent.Errors.Should().NotBeNull("because the error set should contain errors"); var errors = responseContent.Errors.ToList(); errors.Count.Should().Be(1, "because a single error should be present"); errors[0].Code.Should().Be(errorCode, "because the provided error code should be used for the failure response"); errors[0].Description.Should().Be(errorDescription, "because the provided error description should be used for the failure response"); mockLogger.Verify(logger => logger.Information(It.IsAny <string>(), It.Is <HttpStatusCode>(code => code == HttpStatusCode.BadRequest), It.Is <string>(parameterName => parameterName == parameter), It.Is <Uri>(uri => uri == request.RequestUri), It.Is <ErrorSet>(set => set == responseContent)), Times.Once, "The route parameter validation failure should have been logged"); }