Exemplo n.º 1
0
        public void Validate_ValidatingStringPropertyWithIncorrectExpectedValue_ValidationFails()
        {
            // Arrange
            const string         content   = "{ propertyName: 'value' }";
            JsonContentValidator validator = new JsonContentValidator(
                new JsonContentValidator.JsonStringPropertyValidator("propertyName", true, "differentValue", true, null, null));
            ResponseDataMock responseData = CreateDefaultResponseDataMock(content);

            // Act
            ValidationResult validationResult = validator.Validate(responseData, null, null);

            // Assert
            Assert.IsTrue(validationResult.HasFailures);
        }
Exemplo n.º 2
0
        public void Validate_ValidatingStringPropertyEndsWith_ValidationFails()
        {
            // Arrange
            const string         content   = "{ propertyName: 'propertyValueSuffix' }";
            JsonContentValidator validator = new JsonContentValidator(
                new JsonContentValidator.JsonStringPropertyValidator("propertyName", true, "propertyValueSuffix", true, "Value", null));
            ResponseDataMock responseData = CreateDefaultResponseDataMock(content);

            // Act
            ValidationResult validationResult = validator.Validate(responseData, null, null);

            // Assert
            Assert.IsTrue(validationResult.HasFailures);
        }
Exemplo n.º 3
0
        public void Validate_ValidatingBooleanPropertyWithIsRequiredSetToTrue_ValidationSucceeds()
        {
            // Arrange
            const string         content   = "{ propertyName: 'true' }";
            JsonContentValidator validator = new JsonContentValidator(
                new JsonContentValidator.JsonBooleanPropertyValidator("propertyName", true, false, false, null));
            ResponseDataMock responseData = CreateDefaultResponseDataMock(content);

            // Act
            ValidationResult validationResult = validator.Validate(responseData, null, null);

            // Assert
            Assert.IsFalse(validationResult.HasFailures);
        }
Exemplo n.º 4
0
        public void Validate_ValidatingBooleanPropertyWithNonBooleanActualValue_ValidationFails()
        {
            // Arrange
            const string         content   = "{ propertyName: 'value' }";
            JsonContentValidator validator = new JsonContentValidator(
                new JsonContentValidator.JsonBooleanPropertyValidator("propertyName", true, true, true, null));
            ResponseDataMock responseData = CreateDefaultResponseDataMock(content);

            // Act
            ValidationResult validationResult = validator.Validate(responseData, null, null);

            // Assert
            Assert.IsTrue(validationResult.HasFailures);
        }
Exemplo n.º 5
0
        public void Validate_ValidatingStringArrayPropertyIgnoresCase_ValidationSucceeds()
        {
            // Arrange
            const string         content   = "{ propertyName: ['value', 'value2'] }";
            JsonContentValidator validator = new JsonContentValidator(
                new JsonContentValidator.JsonArrayPropertyValidator("propertyName", true, "Value", false, null));
            ResponseDataMock responseData = CreateDefaultResponseDataMock(content);

            // Act
            ValidationResult validationResult = validator.Validate(responseData, null, null);

            // Assert
            Assert.IsFalse(validationResult.HasFailures);
        }
Exemplo n.º 6
0
        public void Validate_ValidatingIntegerPropertyWithNoExpectedValueAndNoExpectedState_ValidationSucceeds()
        {
            // Arrange
            const string         content   = "{ propertyName: '2' }";
            JsonContentValidator validator = new JsonContentValidator(
                new JsonContentValidator.JsonIntegerPropertyValidator("propertyName", true, 0, false, null));
            ResponseDataMock responseData = CreateDefaultResponseDataMock(content);

            // Act
            ValidationResult validationResult = validator.Validate(responseData, null, null);

            // Assert
            Assert.IsFalse(validationResult.HasFailures);
        }
Exemplo n.º 7
0
        public void Validate_ValidatingStringPropertyWithEmptyJsonAndIsRequiredSetToTrue_ValidationFails()
        {
            // Arrange
            const string         content   = "{ }";
            JsonContentValidator validator = new JsonContentValidator(
                new JsonContentValidator.JsonStringPropertyValidator("propertyName", true, "value", false, null, null));
            ResponseDataMock responseData = CreateDefaultResponseDataMock(content);

            // Act
            ValidationResult validationResult = validator.Validate(responseData, null, null);

            // Assert
            Assert.IsTrue(validationResult.HasFailures);
        }
Exemplo n.º 8
0
        public void Validate_AbsoluteUrlPropertyWithNoValue_NotRequiredValidating_ValidationSucceeds()
        {
            // Arrange
            const string         content   = "{ }";
            JsonContentValidator validator = new JsonContentValidator(
                new JsonContentValidator.JsonAbsoluteUrlPropertyValidator("propertyName", false, false, null));
            ResponseDataMock responseData = CreateDefaultResponseDataMock(content);

            // Act
            ValidationResult validationResult = validator.Validate(responseData, null, null);

            // Assert
            Assert.IsFalse(validationResult.HasFailures);
        }
Exemplo n.º 9
0
        public void Validate_ValidatingAbsoluteUrlPropertyWithCorrectValue_RequiresAccessToken_ValidationSucceeds()
        {
            // Arrange
            const string         content   = "{ propertyName: 'https://contoso.com/?access_token=abc123' }";
            JsonContentValidator validator = new JsonContentValidator(
                new JsonContentValidator.JsonAbsoluteUrlPropertyValidator("propertyName", false, true, null));
            ResponseDataMock responseData = CreateDefaultResponseDataMock(content);

            // Act
            ValidationResult validationResult = validator.Validate(responseData, null, null);

            // Assert
            Assert.IsFalse(validationResult.HasFailures);
        }
Exemplo n.º 10
0
        public void Validate_ValidatingAbsoluteUrlPropertyWithIncorrectValue_ValidationFails()
        {
            // Arrange
            const string         content   = "{ propertyName: 'bing.com' }";
            JsonContentValidator validator = new JsonContentValidator(
                new JsonContentValidator.JsonAbsoluteUrlPropertyValidator("propertyName", false, false, null));
            ResponseDataMock responseData = CreateDefaultResponseDataMock(content);

            // Act
            ValidationResult validationResult = validator.Validate(responseData, null, null);

            // Assert
            Assert.IsTrue(validationResult.HasFailures);
        }
        public void Validate_NonZeroContentAndUnequalContentLength_Fails()
        {
            MemoryStream  responseStream = new MemoryStream(Encoding.UTF8.GetBytes("my content"));
            IResponseData response       = new ResponseDataMock
            {
                ResponseStream = responseStream,
                Headers        = new CaseInsensitiveDictionary(1)
                {
                    { "Content-Length", "1" }
                }
            };
            ValidationResult result = new ContentLengthValidator().Validate(response, null, null);

            Assert.IsTrue(result.HasFailures);
        }
        public void Validate_ZeroContentAndZeroContentLength_Succeeds()
        {
            MemoryStream  responseStream = new MemoryStream(Encoding.UTF8.GetBytes(""));
            IResponseData response       = new ResponseDataMock
            {
                ResponseStream = responseStream,
                Headers        = new CaseInsensitiveDictionary(1)
                {
                    { "Content-Length", "0" }
                }
            };
            ValidationResult result = new ContentLengthValidator().Validate(response, null, null);

            Assert.IsFalse(result.HasFailures);
        }
        public void Validate_CaseInsensitiveHeaderNameMatching_Succeeds()
        {
            MemoryStream  responseStream = new MemoryStream(Encoding.UTF8.GetBytes("my content"));
            IResponseData response       = new ResponseDataMock
            {
                ResponseStream = responseStream,
                Headers        = new CaseInsensitiveDictionary(1)
                {
                    { "content-length", responseStream.Length.ToString() }
                }
            };
            ValidationResult result = new ContentLengthValidator().Validate(response, null, null);

            Assert.IsFalse(result.HasFailures);
        }
        public void Validate_ResponseStreamIsNull_ValidationFails()
        {
            // Arrange
            JsonContentValidator validator    = new JsonContentValidator();
            ResponseDataMock     responseData = new ResponseDataMock
            {
                ResponseStream = null
            };

            // Act
            ValidationResult validationResult = validator.Validate(responseData, null, null);

            // Assert
            Assert.IsTrue(validationResult.HasFailures);
        }
Exemplo n.º 15
0
        public void Validate_ResponseCodeDifferentThanExpected_ValidationFails()
        {
            // Arrange
            const int             expectedResponseCode = 200;
            const int             actualResponseCode   = 300;
            ResponseCodeValidator validator            = new ResponseCodeValidator(expectedResponseCode);
            ResponseDataMock      responseData         = new ResponseDataMock {
                StatusCode = actualResponseCode
            };

            // Act
            ValidationResult validationResult = validator.Validate(responseData, null, null);

            // Assert
            Assert.IsTrue(validationResult.HasFailures);
        }
Exemplo n.º 16
0
        public void Validate_OneValidatorValidation_Fails()
        {
            //Arrange
            const int        actualResponseCode   = 200;
            int              expectedResponseCode = 400;
            IValidator       validator            = new ResponseCodeValidator(expectedResponseCode);
            IValidator       orValidator          = new OrValidator(new IValidator[] { validator });
            ResponseDataMock responseData         = new ResponseDataMock {
                StatusCode = actualResponseCode
            };

            //Test
            ValidationResult result = orValidator.Validate(responseData, null, null);

            // Assert
            Assert.IsTrue(result.HasFailures);
        }
Exemplo n.º 17
0
        public void Validate_ResponseCodeDifferentThanExpected_OneErrorReturned()
        {
            // Arrange
            const int             expectedResponseCode = 200;
            const int             actualResponseCode   = 300;
            ResponseCodeValidator validator            = new ResponseCodeValidator(expectedResponseCode);
            ResponseDataMock      responseData         = new ResponseDataMock {
                StatusCode = actualResponseCode
            };

            // Act
            ValidationResult validationResult = validator.Validate(responseData, null, null);

            // Assert
            Assert.IsNotNull(validationResult.Errors);
            Assert.AreEqual(1, validationResult.Errors.Count());
        }
Exemplo n.º 18
0
        public void Validate_ResponseCodeDifferentThanExpected_ErrorMessageContainsExpectedResponseCode()
        {
            // Arrange
            const int             expectedResponseCode = 200;
            const int             actualResponseCode   = 300;
            ResponseCodeValidator validator            = new ResponseCodeValidator(expectedResponseCode);
            ResponseDataMock      responseData         = new ResponseDataMock {
                StatusCode = actualResponseCode
            };

            // Act
            ValidationResult validationResult = validator.Validate(responseData, null, null);

            // Assert
            string error = validationResult.Errors.First();

            Assert.IsTrue(error.Contains(expectedResponseCode.ToString(CultureInfo.InvariantCulture)));
        }
        public void Validate_ValidatingIntegerPropertyWithCorrectExpectedStateValue_ValidationSucceeds()
        {
            // Arrange
            const string content = "{ propertyName: '2' }";
            Dictionary <string, string> savedState = new Dictionary <string, string>()
            {
                { "StateKey", "2" }
            };
            JsonContentValidator validator = new JsonContentValidator(
                new JsonContentValidator.JsonIntegerPropertyValidator("propertyName", true, 1, true, "StateKey"));
            ResponseDataMock responseData = CreateDefaultResponseDataMock(content);

            // Act
            ValidationResult validationResult = validator.Validate(responseData, null, savedState);

            // Assert
            Assert.IsFalse(validationResult.HasFailures);
        }
        public void Validate_ValidatingStringArrayPropertyWithExpectedValueNotPresentAndIncorrectExpectedStateValue_ValidationFails()
        {
            // Arrange
            const string content = "{ propertyName: ['value', 'value2'] }";
            Dictionary <string, string> savedState = new Dictionary <string, string>()
            {
                { "StateKey", "incorrect value" }
            };
            JsonContentValidator validator = new JsonContentValidator(
                new JsonContentValidator.JsonArrayPropertyValidator("propertyName", true, "incorrect value", true, "StateKey"));
            ResponseDataMock responseData = CreateDefaultResponseDataMock(content);

            // Act
            ValidationResult validationResult = validator.Validate(responseData, null, savedState);

            // Assert
            Assert.IsTrue(validationResult.HasFailures);
        }
        public void Validate_ValidatingBooleanPropertyWithNoExpectedValueAndNonBooleanExpectedStateValue_ValidationFails()
        {
            // Arrange
            const string content = "{ propertyName: '2' }";
            Dictionary <string, string> savedState = new Dictionary <string, string>()
            {
                { "StateKey", "non boolean" }
            };
            JsonContentValidator validator = new JsonContentValidator(
                new JsonContentValidator.JsonBooleanPropertyValidator("propertyName", true, false, false, "StateKey"));
            ResponseDataMock responseData = CreateDefaultResponseDataMock(content);

            // Act
            ValidationResult validationResult = validator.Validate(responseData, null, savedState);

            // Assert
            Assert.IsTrue(validationResult.HasFailures);
        }
        public void Validate_HeaderKeyDoesntExist_IsRequiredIsFalse_ValidationSucceeds()
        {
            // Arrange
            const string            headerKey     = "key";
            const string            expectedValue = "value";
            const bool              shouldMatch   = true;
            ResponseHeaderValidator validator     = new ResponseHeaderValidator(headerKey, expectedValue, null, false, shouldMatch);

            ResponseDataMock responseData = new ResponseDataMock
            {
                Headers = new CaseInsensitiveDictionary()
            };

            // Act
            ValidationResult validationResult = validator.Validate(responseData, null, null);

            // Assert
            Assert.IsFalse(validationResult.HasFailures);
        }
        public void Validate_Null_Content_String_Validation_Fails()
        {
            // Arrange
            const string         content   = "";
            JsonContentValidator validator = new JsonContentValidator(
                new JsonContentValidator.JsonStringPropertyValidator("propertyName", true, "value", true, null, null));
            ResponseDataMock responseData = new ResponseDataMock
            {
                ResponseStream      = new MemoryStream(Encoding.UTF8.GetBytes(content)),
                ResponseContentText = null,
                IsTextResponse      = true
            };

            // Act
            ValidationResult validationResult = validator.Validate(responseData, null, null);

            // Assert
            Assert.IsTrue(validationResult.HasFailures);
        }
Exemplo n.º 24
0
        public void Validate_MultipleValidatorsAllInvalid_Fails()
        {
            //Arrange
            const int actualResponseCode = 200;

            IValidator[] validators = new ResponseCodeValidator[] {
                new ResponseCodeValidator(400),
                new ResponseCodeValidator(401)
            };
            IValidator       orValidator  = new OrValidator(validators);
            ResponseDataMock responseData = new ResponseDataMock {
                StatusCode = actualResponseCode
            };

            //Test
            ValidationResult result = orValidator.Validate(responseData, null, null);

            // Assert
            Assert.IsTrue(result.HasFailures);
        }
Exemplo n.º 25
0
        public void Validate_ResponseStreamNotAsExpected_ValidationFails()
        {
            // Arrange
            const string             fileId          = "fileId";
            MemoryStream             responseStream  = new MemoryStream(Encoding.UTF8.GetBytes("my content"));
            MemoryStream             expectedStream  = new MemoryStream();
            ResponseContentValidator validator       = new ResponseContentValidator(fileId);
            ResourceManagerMock      resourceManager = new ResourceManagerMock {
                { fileId, expectedStream }
            };
            ResponseDataMock responseData = new ResponseDataMock {
                ResponseStream = responseStream
            };

            // Act
            ValidationResult validationResult = validator.Validate(responseData, resourceManager, null);

            // Assert
            Assert.IsTrue(validationResult.HasFailures);
        }
Exemplo n.º 26
0
        public void Validate_BodyAsExpected_ValidationSucceeds()
        {
            // Arrange
            const string             fileId          = "fileId";
            const string             content         = "my content";
            MemoryStream             responseStream  = new MemoryStream(Encoding.UTF8.GetBytes(content));
            MemoryStream             expectedStream  = new MemoryStream(Encoding.UTF8.GetBytes(content));
            ResponseContentValidator validator       = new ResponseContentValidator(null, content);
            ResourceManagerMock      resourceManager = new ResourceManagerMock {
                { fileId, expectedStream }
            };
            ResponseDataMock responseData = new ResponseDataMock {
                ResponseStream = responseStream
            };

            // Act
            ValidationResult validationResult = validator.Validate(responseData, resourceManager, null);

            // Assert
            Assert.IsFalse(validationResult.HasFailures);
        }
Exemplo n.º 27
0
        public void Validate_ResponseStreamNotAsExpected_OneErrorReturned()
        {
            // Arrange
            const string             fileId          = "fileId";
            MemoryStream             responseStream  = new MemoryStream(Encoding.UTF8.GetBytes("my content"));
            MemoryStream             expectedStream  = new MemoryStream();
            ResponseContentValidator validator       = new ResponseContentValidator(fileId);
            ResourceManagerMock      resourceManager = new ResourceManagerMock {
                { fileId, expectedStream }
            };
            ResponseDataMock responseData = new ResponseDataMock {
                ResponseStream = responseStream
            };

            // Act
            ValidationResult validationResult = validator.Validate(responseData, resourceManager, null);

            // Assert
            Assert.IsNotNull(validationResult.Errors);
            Assert.AreEqual(1, validationResult.Errors.Count());
        }
        public void Validate_HeaderAndExpectedValueAreEmpty_ShouldMatchAndIsRequiredAreTrue_ValidationSucceeds()
        {
            // Arrange
            const string            headerKey   = "key";
            const string            actualValue = "";
            const bool              shouldMatch = true;
            const bool              isRequired  = true;
            ResponseHeaderValidator validator   = new ResponseHeaderValidator(headerKey, "", null, isRequired, shouldMatch);

            ResponseDataMock responseData = new ResponseDataMock
            {
                Headers = new CaseInsensitiveDictionary()
                {
                    { headerKey, actualValue }
                }
            };

            // Act
            ValidationResult validationResult = validator.Validate(responseData, null, null);

            // Assert
            Assert.IsFalse(validationResult.HasFailures);
        }
        public void Validate_HeaderValueSameAsExpectedValue_ShouldMatchIsFalse_ValidationFails()
        {
            // Arrange
            const string            headerKey     = "key";
            const string            expectedValue = "value";
            const string            actualValue   = expectedValue;
            const bool              shouldMatch   = false;
            ResponseHeaderValidator validator     = new ResponseHeaderValidator(headerKey, expectedValue, null, false, shouldMatch);

            ResponseDataMock responseData = new ResponseDataMock
            {
                Headers = new CaseInsensitiveDictionary()
                {
                    { headerKey, actualValue }
                }
            };

            // Act
            ValidationResult validationResult = validator.Validate(responseData, null, null);

            // Assert
            Assert.IsTrue(validationResult.HasFailures);
        }
        public void Validate_HeaderValueSameAsExpectedValue_ShouldMatchIsFalse_OneErrorReturned()
        {
            // Arrange
            const string            headerKey     = "key";
            const string            expectedValue = "value";
            const string            actualValue   = expectedValue;
            const bool              shouldMatch   = false;
            ResponseHeaderValidator validator     = new ResponseHeaderValidator(headerKey, expectedValue, null, false, shouldMatch);

            ResponseDataMock responseData = new ResponseDataMock
            {
                Headers = new CaseInsensitiveDictionary()
                {
                    { headerKey, actualValue }
                }
            };

            // Act
            ValidationResult validationResult = validator.Validate(responseData, null, null);

            // Assert
            Assert.IsNotNull(validationResult.Errors);
            Assert.AreEqual(1, validationResult.Errors.Count());
        }