Example #1
0
    def test_payload_required(self):
        assert not defined(self.BaseAction.PAYLOAD_REQUIRED)

        test_values = (
            ('GET', False),
            ('POST', True),
            ('PUT', True),
            ('PATCH', True),
            ('DELETE', False),
        )

        for method, expected_value in test_values:

            class Action(HttpAction):
                METHOD = method

            assert Action.PAYLOAD_REQUIRED is expected_value

        class BaseAction(HttpAction):
            PAYLOAD_REQUIRED = True

        class Action(BaseAction):
            METHOD = 'GET'

        assert Action.PAYLOAD_REQUIRED is True
Example #2
0
    def build_headers(self):
        """
        Returns headers if HEADERS_PARAMS constant
        is defined for the action
        """

        headers_params = self.action.HEADERS_PARAMS
        if defined(headers_params):
            return self.filter_raw_kwargs(include=headers_params)
Example #3
0
    def filter_raw_kwargs(self, include=UNDEFINED, exclude=UNDEFINED):
        check = None

        if defined(include):

            def check(key):
                return key in include
        elif defined(exclude):

            def check(key):
                return key not in exclude

        if check:
            return {
                key: value
                for key, value in self.raw_kwargs.items() if check(key)
            }

        return self.raw_kwargs
Example #4
0
    def test_trailing_slash(self):
        assert not defined(self.BaseAction.USE_TRAILING_SLASH)

        class TrailingSlashAction(self.BaseAction):
            USE_TRAILING_SLASH = True

        class AnotherAction(TrailingSlashAction):
            URL_COMPONENTS = ('test', )

        assert TrailingSlashAction.URL_PATH_TEMPLATE == 'http://example.com/' \
                                                        'api/v1/'
        assert AnotherAction.URL_PATH_TEMPLATE == 'http://example.com/' \
                                                  'api/v1/test/'
Example #5
0
    def build_params(self):
        """Returns query string params"""

        url_query_params = self.action.URL_QUERY_PARAMS
        if defined(url_query_params):
            return self.filter_raw_kwargs(include=url_query_params)

        if self.action.PAYLOAD_REQUIRED:
            if 'data' in self.raw_kwargs:
                return self.filter_raw_kwargs(
                    exclude={'data'}.union(self.action.DEFINED_PARAMS or ()))
            if defined(self.action.PAYLOAD_PARAMS):
                return self.filter_raw_kwargs(
                    exclude=self.action.DEFINED_PARAMS)

            # We should not return query string params if payload is
            # required but the 'data' is not in kwargs and
            # URL_QUERY_PARAMS and PAYLOAD_PARAMS were not defined,
            # because all the kwargs that are not in DEFINED_PARAMS
            # will be used to build the payload by default.

        else:
            return self.filter_raw_kwargs(exclude=self.action.DEFINED_PARAMS)
Example #6
0
 def test_defined(self, value):
     assert defined(value)
Example #7
0
 def test_not_defined(self):
     assert not defined(UNDEFINED)