Пример #1
0
 def setUp(self):
     self.validator = Validator('test.yaml', host='127.0.0.1', port=80)
     self.validator.spec = YamlValidatorSpec([
         ValidatorSpecRule(
             'http://example.com',
             status_code=200,
             headers={'x-test': 'foobar'}
         )])
Пример #2
0
    def test_spec_request_host(self, mock):
        '''Compare spec rules'''
        mock_open(mock, read_data=self.fixtures[0])
        validator = Validator.load('rtd.json', host='127.0.0.1', port=8000)
        rule = list(validator.spec.get_rules())[0]

        # No host/port
        req = rule.get_request()
        assert req.url == 'http://example.com'
        assert req.method == 'get'
        assert not 'Host' in req.headers
        # Host, no port
        req = rule.get_request(host='0.0.0.0')
        assert req.url == 'http://0.0.0.0'
        assert req.method == 'get'
        assert 'Host' in req.headers
        assert req.headers['Host'] == 'example.com'
        # Host and port
        req = rule.get_request(host='0.0.0.0', port=8000)
        assert req.url == 'http://0.0.0.0:8000'
        assert req.method == 'get'
        assert 'Host' in req.headers
        assert req.headers['Host'] == 'example.com'
Пример #3
0
    def test_spec_request_host(self, mock):
        '''Compare spec rules'''
        mock_open(mock, read_data=self.fixtures[0])
        validator = Validator.load('rtd.json', host='127.0.0.1', port=8000)
        rule = list(validator.spec.get_rules())[0]

        # No host/port
        req = rule.get_request()
        assert req.url == 'http://example.com'
        assert req.method == 'get'
        assert not 'Host' in req.headers
        # Host, no port
        req = rule.get_request(host='0.0.0.0')
        assert req.url == 'http://0.0.0.0'
        assert req.method == 'get'
        assert 'Host' in req.headers
        assert req.headers['Host'] == 'example.com'
        # Host and port
        req = rule.get_request(host='0.0.0.0', port=8000)
        assert req.url == 'http://0.0.0.0:8000'
        assert req.method == 'get'
        assert 'Host' in req.headers
        assert req.headers['Host'] == 'example.com'
Пример #4
0
class TestValidatorValidation(TestCase):

    def setUp(self):
        self.validator = Validator('test.yaml', host='127.0.0.1', port=80)
        self.validator.spec = YamlValidatorSpec([
            ValidatorSpecRule(
                'http://example.com',
                status_code=200,
                headers={'x-test': 'foobar'}
            )])

    def assertRuleMatches(self, result, passing=True, error=None):
        if passing:
            self.assertIsInstance(result, ValidationPass)
        else:
            self.assertIsInstance(result, ValidationFail)
        if error is not None:
            self.assertRegexpMatches(str(result.error), error)

    @patch('validatehttp.validate.Session.send')
    def test_response_match(self, mock):
        '''Vanilla response with headers matches rule'''
        mock.return_value = Response()
        mock.return_value.status_code = 200
        mock.return_value.headers = {'x-test': 'foobar'}
        for result in self.validator.validate():
            self.assertRuleMatches(result, passing=True)

    @patch('validatehttp.validate.Session.send')
    def test_response_status_mismatch(self, mock):
        '''Response status code doesn't match'''
        mock.return_value = Response()
        mock.return_value.status_code = 400
        mock.return_value.headers = {'x-test': 'foobar'}
        for result in self.validator.validate():
            self.assertRuleMatches(result, passing=False,
                                   error=r'mismatch: status_code')

    @patch('validatehttp.validate.Session.send')
    def test_response_header_mismatch(self, mock):
        '''Response header is missing or doesn't match'''
        mock.return_value = Response()
        mock.return_value.status_code = 200
        mock.return_value.headers = {'x-nonexistant': 'foobar'}
        for result in self.validator.validate():
            self.assertRuleMatches(result, passing=False,
                                   error=r'header mismatch: x-test')
        mock.return_value.headers = {'x-test': 'do not match'}
        for result in self.validator.validate():
            self.assertRuleMatches(result, passing=False,
                                   error=r'header mismatch: x-test')

    @patch('validatehttp.validate.Session.send')
    def test_connection_error(self, mock):
        '''Test connection error throws a validation error'''
        def _raise(*args, **kwargs):
            raise ConnectionError('Connection error')
        mock.side_effect = _raise
        results = list(self.validator.validate())
        self.assertRuleMatches(results[0], passing=False, error=r'Connection')

    @patch('validatehttp.validate.Session.send')
    def test_ssl_error(self, mock):
        '''Test ssl error response'''
        def _raise(*args, **kwargs):
            raise SSLError('SSL error')
        mock.side_effect = _raise
        results = list(self.validator.validate())
        self.assertRuleMatches(results[0], passing=False, error=r'SSL')
Пример #5
0
 def test_load_yaml(self, mock):
     '''Pass loading YAML file'''
     mock_open(mock, read_data=("foo:\n"
                                "  status_code: 200\n"))
     validator = Validator.load('test.yaml')
     self.assertEqual(len(list(validator.spec.get_rules())), 1)
Пример #6
0
 def test_load_json(self, mock):
     '''Pass loading JSON file'''
     mock_open(mock, read_data='{"/foo": {"status_code": 200}}')
     validator = Validator.load('test.json')
     self.assertEqual(len(list(validator.spec.get_rules())), 1)
Пример #7
0
 def test_bad_spec(self, mock):
     '''Valid data, but bad spec'''
     mock_open(mock, read_data='foo:')
     validator = Validator.load('rtd.yaml', host='127.0.0.1', port=8000)
     self.assertEqual(len(list(validator.spec.get_rules())), 0)
Пример #8
0
 def test_load_json_from_file(self, mock):
     '''Load json from mocked file'''
     mock_open(mock, read_data=self.fixtures[0])
     validator = Validator.load('rtd.json', host='127.0.0.1', port=8000)
     self.assertEqual(len(list(validator.spec.get_rules())), 1)
Пример #9
0
 def setUp(self):
     self.validator = Validator("test.yaml", host="127.0.0.1", port=80)
     self.validator.spec = YamlValidatorSpec(
         [ValidatorSpecRule("http://example.com", status_code=200, headers={"x-test": "foobar"})]
     )
Пример #10
0
class TestValidatorValidation(TestCase):
    def setUp(self):
        self.validator = Validator("test.yaml", host="127.0.0.1", port=80)
        self.validator.spec = YamlValidatorSpec(
            [ValidatorSpecRule("http://example.com", status_code=200, headers={"x-test": "foobar"})]
        )

    def assertRuleMatches(self, result, passing=True, error=None):
        if passing:
            self.assertIsInstance(result, ValidationPass)
        else:
            self.assertIsInstance(result, ValidationFail)
        if error is not None:
            self.assertRegexpMatches(str(result.error), error)

    @patch("validatehttp.validate.Session.send")
    def test_response_match(self, mock):
        """Vanilla response with headers matches rule"""
        mock.return_value = Response()
        mock.return_value.status_code = 200
        mock.return_value.headers = {"x-test": "foobar"}
        for result in self.validator.validate():
            self.assertRuleMatches(result, passing=True)

    @patch("validatehttp.validate.Session.send")
    def test_response_status_mismatch(self, mock):
        """Response status code doesn't match"""
        mock.return_value = Response()
        mock.return_value.status_code = 400
        mock.return_value.headers = {"x-test": "foobar"}
        for result in self.validator.validate():
            self.assertRuleMatches(result, passing=False, error=r"status_code mismatch")

    @patch("validatehttp.validate.Session.send")
    def test_response_header_mismatch(self, mock):
        """Response header is missing or doesn't match"""
        mock.return_value = Response()
        mock.return_value.status_code = 200
        mock.return_value.headers = {"x-nonexistant": "foobar"}
        for result in self.validator.validate():
            self.assertRuleMatches(result, passing=False, error=r"header x-test mismatch")
        mock.return_value.headers = {"x-test": "do not match"}
        for result in self.validator.validate():
            self.assertRuleMatches(result, passing=False, error=r"header x-test mismatch")

    @patch("validatehttp.validate.Session.send")
    def test_connection_error(self, mock):
        """Test connection error throws a validation error"""

        def _raise(*args, **kwargs):
            raise ConnectionError("Connection error")

        mock.side_effect = _raise
        results = list(self.validator.validate())
        self.assertRuleMatches(results[0], passing=False, error=r"Connection")

    @patch("validatehttp.validate.Session.send")
    def test_ssl_error(self, mock):
        """Test ssl error response"""

        def _raise(*args, **kwargs):
            raise SSLError("SSL error")

        mock.side_effect = _raise
        results = list(self.validator.validate())
        self.assertRuleMatches(results[0], passing=False, error=r"SSL")
Пример #11
0
 def test_load_yaml(self, mock):
     """Pass loading YAML file"""
     mock_open(mock, read_data=("foo:\n" "  status_code: 200\n"))
     validator = Validator.load("test.yaml")
     self.assertEqual(len(list(validator.spec.get_rules())), 1)
Пример #12
0
 def test_load_json(self, mock):
     """Pass loading JSON file"""
     mock_open(mock, read_data='{"/foo": {"status_code": 200}}')
     validator = Validator.load("test.json")
     self.assertEqual(len(list(validator.spec.get_rules())), 1)
Пример #13
0
 def test_bad_spec(self, mock):
     '''Valid data, but bad spec'''
     mock_open(mock, read_data='foo:')
     validator = Validator.load('rtd.yaml', host='127.0.0.1', port=8000)
     self.assertEqual(len(list(validator.spec.get_rules())), 0)
Пример #14
0
 def test_load_json_from_file(self, mock):
     '''Load json from mocked file'''
     mock_open(mock, read_data=self.fixtures[0])
     validator = Validator.load('rtd.json', host='127.0.0.1', port=8000)
     self.assertEqual(len(list(validator.spec.get_rules())), 1)