Пример #1
0
    def __call__(self, environ, start_response):
        content_type = get_content_type(environ.get('CONTENT_TYPE'))
        if content_type == 'application/json' and 'wsgi.input' in environ:
            body = environ['wsgi.input'].read()
            if body:
                arguments = []
                body = to_string(body)
                try:
                    body_json = loads(body)
                    for key, values in dict(body_json).items():
                        values = maybe_list(values)
                        for value in values:
                            if value is None:
                                arguments.append('%s=' % key)
                            else:
                                arguments.append('%s=%s' % (key, quote(dump_query_value(value))))
                    body = '&'.join(arguments)
                except (ValueError, UnicodeEncodeError):
                    headers = [('Content-type', 'application/json')]
                    error = HTTPInvalidJSONPayload()
                    start_response(error.status, headers)
                    return format_error_response_to_json(error)

            environ_add_POST(environ, body or '')

        return self.application(environ, start_response)
Пример #2
0
 def tearDown(self):
     for k, v in self._prop_cache.items():
         self._set_request_property(k, v)
     self.env['request'].environ.pop('HTTP_X_REQUESTED_WITH', None)
     environ_add_POST(self.env['request'].environ, {})
     if self.__setup_db__:
         TestWithDbAndData.tearDown(self)
Пример #3
0
 def tearDown(self):
     for k, v in self._prop_cache.items():
         self._set_request_property(k, v)
     self.env['request'].environ.pop('HTTP_X_REQUESTED_WITH', None)
     environ_add_POST(self.env['request'].environ, {})
     if self.__setup_db__:
         TestWithDbAndData.tearDown(self)
Пример #4
0
def _set_request_property(req, k, v):
    if k == 'is_xhr':
        req.environ['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest' if v else ''
    elif k == 'params':
        environ_add_POST(req.environ, v)
    elif k == 'matched_route':
        setattr(req, k, Route(v))
    else:  # pragma: no cover
        setattr(req, k, v)
Пример #5
0
 def _set_request_property(self, k, v):
     if k == 'is_xhr':
         self.env['request'].environ['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest'\
             if v else ''
     elif k == 'params':
         environ_add_POST(self.env['request'].environ, v)
     else:  # pragma: no cover
         try:
             setattr(self.env['request'], k, v)
         except:
             print(k)
             raise
Пример #6
0
 def _set_request_property(self, k, v):
     if k == 'is_xhr':
         self.env['request'].environ['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest'\
             if v else ''
     elif k == 'params':
         environ_add_POST(self.env['request'].environ, v)
     else:  # pragma: no cover
         try:
             setattr(self.env['request'], k, v)
         except:
             print(k)
             raise
Пример #7
0
def request(env, **props):
    _cache = {}
    for k, v in props.items():
        _cache[k] = getattr(env['request'], k, None)
        _set_request_property(env['request'], k, v)

    yield env['request']

    for k, v in _cache.items():
        _set_request_property(env['request'], k, v)
    env['request'].environ.pop('HTTP_X_REQUESTED_WITH', None)
    environ_add_POST(env['request'].environ, {})
Пример #8
0
    def __call__(self, environ, start_response):
        content_type = get_content_type(environ.get('CONTENT_TYPE'))
        if content_type == 'application/json' and 'wsgi.input' in environ:
            body = environ['wsgi.input'].read()
            if body:
                arguments = []
                try:
                    body_json = loads(body)
                    for key, values in dict(body_json).items():
                        values = maybe_list(values)
                        value = ','.join(
                            '' if v is None else dump_query_value(v)
                            for v in values)
                        arguments.append('%s=%s' % (force_string(key), value))
                    body = '&'.join(arguments)
                except (ValueError, UnicodeEncodeError):
                    headers = [('Content-type', 'application/json')]
                    error = HTTPInvalidJSONPayload()
                    start_response(error.status, headers)
                    return format_error_to_json(error)

            environ_add_POST(environ, body or '')

        return self.application(environ, start_response)