def test_alternatives(self): builder = RegexContainerBuilder( (r'(\d+)/(\d+)/\d+/', r'^(.+)/.*?([0-9A-Fa-f]{2})(?=[0-9A-Fa-f]{6})', r'^(.*)/')) gen = builder.alternatives('cloud-images/img-01234567') names = [x for x in gen] self.assertListEqual(['cloud-images01', 'cloud-images'], names)
def test_concatenated_digits(self): builder = RegexContainerBuilder(r'(\d+)/(\d+)/(\d+)') self.assertEqual(builder( "previews/images/591/384/697/normal/idirlgfdh.jpg?1502312379"), "591384697") self.assertEqual(builder( "previews/images/59/38/69/normal/idirlgfdh.jpg?1502312379"), "593869") self.assertRaises( ValueError, builder, "previews/images/normal/idirlgfdh.jpg?1502312379")
def test_several_regex(self): builder = RegexContainerBuilder((r'(\d+)/(\d+)/\d+/original', r'(\d+)/(\d+)/(\d+)/normal')) self.assertEqual(builder( "previews/images/591/384/697/original/idirlgfdh.jpg?1502312379"), "591384") self.assertEqual(builder( "previews/images/591/384/697/normal/idirlgfdh.jpg?1502312379"), "591384697") self.assertRaises( ValueError, builder, "previews/images/591/384/697/medium/idirlgfdh.jpg?1502312379")
def test_wildcard_fallback(self): builder = RegexContainerBuilder( (r'(\d+)/(\d+)/\d+/', r'^([^/]+/)', r'^(.{1,10})')) self.assertEqual( builder( "previews/images/591/384/697/original/idirlgfdh.jpg?1502312379" ), "591384") self.assertEqual( builder("previews/images/normal/idirlgfdh.jpg?1502312379"), "previews/") self.assertEqual( builder("91d44c2637e476a3862a5ded7fd55ef053364916.png"), "91d44c2637") self.assertEqual(builder("91d44"), "91d44")
def test_digit_block(self): builder = RegexContainerBuilder(r'(\d+)') self.assertEqual(builder("abc/123/def"), "123") self.assertEqual(builder("abc123def"), "123") self.assertRaises(NoMatchFound, builder, "abcdef")
def test_prefix_container(self): builder = RegexContainerBuilder((r'^([^/]*/[^/]*/).*', )) self.assertEqual( builder( "previews/images/591/384/697/original/idirlgfdh.jpg?1502312379" ), "previews/images/")
def __init__(self, app, acct, patterns, failsafe=False, **kwargs): super(RegexContainerMiddleware, self).__init__( app, acct, **kwargs) self.con_builder = RegexContainerBuilder(patterns) self.failsafe = failsafe