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 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'
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')
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)
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)
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)
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)
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"})] )
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")
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)
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)