Exemplo n.º 1
0
    def test_with_uri_params_defined(self):
        class ResourceMethod(ResourceMethodBase):
            method = 'get'
            params = {
                'collection': {
                    'dest': 'uri'
                },
                'identity': {
                    'dest': 'uri'
                },
            }
            uri_params_order = ['collection', 'identity']

        instance = self.get_instance(ResourceMethod,
                                     path=['{collection}', '{identity}'])
        assert_that(
            instance.get_context({
                'identity': 100,
                'collection': 'users'
            }), has_entry('url', uri_join(self.base_uri, 'users', 100, '/')))

        # let's change the order
        instance = self.get_instance(ResourceMethod,
                                     path=['{identity}', '{collection}'])
        assert_that(
            instance.get_context({
                'identity': 100,
                'collection': 'users'
            }), has_entry('url', uri_join(self.base_uri, 100, 'users', '/')))
Exemplo n.º 2
0
    def test_me(self):
        api_base_path = self.CONFIG['DEFORM']['API_BASE_PATH']

        experiments = [
            {
                'kwargs': {
                    'host': 'deform.io',
                    'api_base_path': api_base_path
                },
                'expected': uri_join('https://deform.io/', api_base_path)
            },
            {
                'kwargs': {
                    'host': 'deform.io',
                    'project': 'mysquare',
                    'api_base_path': api_base_path
                },
                'expected': uri_join(
                    'https://mysquare.deform.io/',
                    api_base_path
                )
            },
            {
                'kwargs': {
                    'host': 'deform.io',
                    'port': 123,
                    'api_base_path': api_base_path
                },
                'expected': uri_join('https://deform.io:123/', api_base_path)
            },
            {
                'kwargs': {
                    'host': 'deform.io',
                    'secure': False,
                    'api_base_path': api_base_path
                },
                'expected': uri_join('http://deform.io/', api_base_path)
            },
        ]

        for exp in experiments:
            assert_that(
                get_base_uri(**exp['kwargs']),
                equal_to(exp['expected'])
            )
Exemplo n.º 3
0
    def test_dashes(self):
        experiments = [
            {
                'args': ['http://chib.me'],
                'expected': 'http://chib.me'
            },
            {
                'args': ['http://chib.me/'],
                'expected': 'http://chib.me/'
            },
            {
                'args': ['http://chib.me', 'blog'],
                'expected': 'http://chib.me/blog'
            },
            {
                'args': ['http://chib.me/', 'blog'],
                'expected': 'http://chib.me/blog'
            },
            {
                'args': ['http://chib.me/', '/blog'],
                'expected': 'http://chib.me/blog'
            },
            {
                'args': ['http://chib.me/', '/blog/'],
                'expected': 'http://chib.me/blog/'
            },
            {
                'args': ['http://chib.me', '/blog/'],
                'expected': 'http://chib.me/blog/'
            },
            {
                'args': ['http://chib.me', 'blog/'],
                'expected': 'http://chib.me/blog/'
            },
            {
                'args': ['http://chib.me', 'hello amigo', '/blog/'],
                'expected': 'http://chib.me/hello+amigo/blog/'
            },
            {
                'args': ['http://chib.me', 1, 2, 3],
                'expected': 'http://chib.me/1/2/3'
            },
            {
                'args': ['http://chib.me', '/last-slash', '/'],
                'expected': 'http://chib.me/last-slash/'
            },
        ]

        for exp in experiments:
            assert_that(uri_join(*exp['args']), equal_to(exp['expected']))
Exemplo n.º 4
0
    def test_with_uri_params_defined(self):
        class ResourceMethod(ResourceMethodBase):
            method = 'get'
            params = {
                'collection': {
                    'dest': 'uri'
                },
                'identity': {
                    'dest': 'uri'
                },
            }
            uri_params_order = ['collection', 'identity']

        instance = self.get_instance(
            ResourceMethod,
            path=['{collection}', '{identity}']
        )
        assert_that(
            instance.get_context({
                'identity': 100,
                'collection': 'users'
            }),
            has_entry('url', uri_join(self.base_uri, 'users', 100, '/'))
        )

        # let's change the order
        instance = self.get_instance(
            ResourceMethod,
            path=['{identity}', '{collection}']
        )
        assert_that(
            instance.get_context({
                'identity': 100,
                'collection': 'users'
            }),
            has_entry('url', uri_join(self.base_uri, 100, 'users', '/'))
        )
Exemplo n.º 5
0
def get_url(base_uri, path, params, definitions):
    # todo: retest me
    uri_bits = [base_uri]

    for path_item in path:
        if path_item.startswith('{'):
            path_item_var_name = path_item.strip('{}')
            value = params.get(path_item_var_name)
        else:
            value = path_item

        if value:
            if isinstance(value, list):
                uri_bits += value
            else:
                uri_bits.append(value)
    uri_bits.append('/')

    return uri_join(*uri_bits)
Exemplo n.º 6
0
def get_url(base_uri, path, params, definitions):
    # todo: retest me
    uri_bits = [base_uri]

    for path_item in path:
        if path_item.startswith('{'):
            path_item_var_name = path_item.strip('{}')
            value = params.get(path_item_var_name)
        else:
            value = path_item

        if value:
            if isinstance(value, list):
                uri_bits += value
            else:
                uri_bits.append(value)
    uri_bits.append('/')

    return uri_join(*uri_bits)
Exemplo n.º 7
0
 def get_context(self, params):
     context = super(GetFileResourceMethod, self).get_context(params)
     context['stream'] = True
     context['url'] = uri_join(context['url'], 'content/')
     return context
Exemplo n.º 8
0
 def get_context(self, params):
     context = super(GetFileResourceMethod, self).get_context(params)
     context['stream'] = True
     context['url'] = uri_join(context['url'], 'content/')
     return context
Exemplo n.º 9
0
 def test_schema_first(self):
     assert_that(
         uri_join('https://', 'chib.me', 'blog'),
         equal_to('https://chib.me/blog')
     )
Exemplo n.º 10
0
 def test_many_args(self):
     assert_that(
         uri_join('https://chib.me', 'blog', '/my', 'hello/', '/world/'),
         equal_to('https://chib.me/blog/my/hello/world/')
     )