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)
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)
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)
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
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, {})
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)