def test_abstract_extractor_readableconfig(self): """ Test human-readable extractor config string output """ config = 'key.val' extractor = validators.parse_extractor('jsonpath_mini', config) expected_string = 'Extractor Type: jsonpath_mini, Query: "key.val", Templated?: False' self.assertEqual(expected_string, extractor.get_readable_config()) # Check empty context & args uses okay context = Context() self.assertEqual(expected_string, extractor.get_readable_config(context=context)) context.bind_variable('foo', 'bar') self.assertEqual(expected_string, extractor.get_readable_config(context=context)) extractor.args = dict() self.assertEqual(expected_string, extractor.get_readable_config(context=context)) # Check args output is handled correctly extractor.args = {'caseSensitive': True} self.assertEqual(expected_string + ", Args: " + str(extractor.args), extractor.get_readable_config(context=context)) # Check template handling is okay config = {'template': 'key.$templated'} context.bind_variable('templated', 'val') extractor = validators.parse_extractor('jsonpath_mini', config) expected_string = 'Extractor Type: jsonpath_mini, Query: "key.val", Templated?: True' self.assertEqual(expected_string, extractor.get_readable_config(context=context))
def test_raw_body_extractor(self): query = '' extractor = validators.parse_extractor('raw_body', None) extractor = validators.parse_extractor('raw_body', query) self.assertTrue(isinstance(extractor, validators.RawBodyExtractor)) self.assertTrue(extractor._is_body_extractor) self.assertFalse(extractor._is_header_extractor) bod = u'j1j21io312j3' val = extractor.extract(body=bod, headers='') self.assertEqual(bod, val) bod = b'j1j21io312j3' val = extractor.extract(body=bod, headers='') self.assertEqual(bod, val)
def test_parse_extractor(self): """ Test parsing an extractor using the registry """ config = 'key.val' myjson = '{"key": {"val": 3}}' extractor = validators.parse_extractor('jsonpath_mini', config) self.assertTrue(isinstance(extractor, validators.AbstractExtractor)) self.assertEqual(3, extractor.extract(body=myjson))
def extract_binds(self, bind_dict): bind_dict = Parser.flatten_dictionaries(bind_dict) for variable_name, extractor in bind_dict.items(): if not isinstance(extractor, dict) or len(extractor) == 0: raise BindError("Extractors must be defined as maps of extractorType:{configs} with 1 entry") if len(extractor) > 1: raise BindError("Cannot define multiple extractors for given variable name") for extractor_type, extractor_config in extractor.items(): self.__extract_binds_dict[variable_name] = parse_extractor(extractor_type, extractor_config)
def test_parse_header_extractor(self): query = 'content-type' extractor = validators.parse_extractor('header', query) self.assertTrue(isinstance(extractor, validators.HeaderExtractor)) self.assertTrue(extractor._is_header_extractor) self.assertFalse(extractor._is_body_extractor)