def test_regex(): match_type = request.values.get('match_type', 'findall') regex = request.values.get('regex', '') test_string = request.values.get('test_string', '') try: flags = int(request.values.get('flags', '0')) except TypeError: flags = 0 try: service = RegexService(regex, match_type, flags) except ValueError as e: fmt = 'Invalid value for {}: "{}"' if len(e.args) > 2: fmt += ". Acceptable values are {}" args = [", ".join(a) if type(a) is tuple else a for a in e.args] return api_error(fmt.format(*args)) except InvalidRegexError: return api_error('Invalid regular expression: %s' % regex) try: result = service.test(test_string) except UnprocessibleRegexError: return api_error('This regular expression is unprocessible', status=422) return jsonify(result_type=match_type, result=result)
def test_match(self): svc = RegexService(r'\d+', 'match', 0) result = svc.test("1984") self.assertIsNotNone(result) self.assertEqual(result['group'], "1984") result = svc.test("x1984") self.assertIsNone(result)
def test_findall(self): svc = RegexService(r'\d+', 'findall', 0) result = svc.test("1984 2013 2020") self.assertIsNotNone(result) self.assertEqual(result, ['1984', '2013', '2020']) result = svc.test("testing") self.assertIsNone(result)
def test_findall(self): svc = RegexService(r"\d+", "findall", 0) result = svc.test("1984 2013 2020") self.assertIsNotNone(result) self.assertEqual(result, ["1984", "2013", "2020"]) result = svc.test("testing") self.assertIsNone(result)
def test_match(self): svc = RegexService(r"\d+", "match", 0) result = svc.test("1984") self.assertIsNotNone(result) self.assertEqual(result["group"], "1984") result = svc.test("x1984") self.assertIsNone(result)
def test_invalidArgs(self): self.assertRaises(ValueError, lambda: RegexService(None, 'match', 2)) self.assertRaises(ValueError, lambda: RegexService(r'\d+', None, 2)) self.assertRaises(ValueError, lambda: RegexService(r'\d+', 'findall', -1)) self.assertRaises(ValueError, lambda: RegexService(r'\d+', 'invalid', 2)) self.assertRaises(InvalidRegexError, lambda: RegexService('\\d+[', 'match', 2))
def test_catastrophicBacktrace(self): input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + \ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" def timeout_cb(signum, frame): raise TimeoutException("Timeout!") old_handler = signal.signal(signal.SIGALRM, timeout_cb) signal.alarm(5) try: svc = RegexService(r'(a?a)+b', 'match', 0) with self.assertRaises(UnprocessibleRegex): svc.test(input) except TimeoutException, e: self.fail("Response took more than 5 seconds to execute")
def test_catastrophicBacktrace(self): input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + \ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" def timeout_cb(signum, frame): raise TimeoutException("Timeout!") old_handler = signal.signal(signal.SIGALRM, timeout_cb) signal.alarm(5) try: svc = RegexService(r'(a?a)+b', 'match', 0) with self.assertRaises(UnprocessibleRegexError): svc.test(input) except TimeoutException as e: self.fail("Response took more than 5 seconds to execute") finally: signal.alarm(0) signal.signal(signal.SIGALRM, old_handler)
def test_catastrophicBacktrace(self): input = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + \ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" svc = RegexService(r'(a?a)+b', 'match', 0) with self.assertRaises(UnprocessibleRegex): svc.test(input)
def test_findall2(self): svc = RegexService(r'[\w\']+', 'findall', 0) result = svc.test('Hey, you - what are you doing here!?') self.assertIsNotNone(result) self.assertEqual(result, ['Hey', 'you', 'what', 'are', 'you', 'doing', 'here'])
def test_immutability(self): svc = RegexService(r'\d+', 'match', 0) with self.assertRaises(TypeError): svc.match_type = 'findall'
def test_initialize(self): svc = RegexService(r'\d+', 'match', re.I)
def test_immutability(self): svc = RegexService(r"\d+", "match", 0) with self.assertRaises(TypeError): svc.match_type = "findall"
def test_initialize(self): svc = RegexService(r'\d+', 'match', int(re.I)) self.assertIsNotNone(svc)