def alter_old_distutils_request(request: WSGIRequest): """Alter the request body for compatibility with older distutils clients Due to a bug in the Python distutils library, the request post is sent using \n as a separator instead of the \r\n that the HTTP spec demands. This breaks the Django form parser and therefore we have to write a custom parser. This bug was fixed in the Python 2.7.4 and 3.4: http://bugs.python.org/issue10510 """ # We first need to retrieve the body before accessing POST or FILES since # it can only be read once. body = request.body if request.POST or request.FILES: return new_body = BytesIO() # Split the response in the various parts based on the boundary string content_type, opts = parse_header( request.META['CONTENT_TYPE'].encode('ascii')) parts = body.split(b'\n--' + opts['boundary'] + b'\n') for part in parts: if b'\n\n' not in part: continue headers, content = part.split(b'\n\n', 1) if not headers: continue new_body.write(b'--' + opts['boundary'] + b'\r\n') new_body.write(headers.replace(b'\n', b'\r\n')) new_body.write(b'\r\n\r\n') new_body.write(content) new_body.write(b'\r\n') new_body.write(b'--' + opts['boundary'] + b'--\r\n') request._body = new_body.getvalue() request.META['CONTENT_LENGTH'] = len(request._body) # Clear out _files and _post so that the request object re-parses the body if hasattr(request, '_files'): delattr(request, '_files') if hasattr(request, '_post'): delattr(request, '_post')
def alter_old_distutils_request(request: WSGIRequest): """Alter the request body for compatibility with older distutils clients Due to a bug in the Python distutils library, the request post is sent using \n as a separator instead of the \r\n that the HTTP spec demands. This breaks the Django form parser and therefore we have to write a custom parser. This bug was fixed in the Python 2.7.4 and 3.4: http://bugs.python.org/issue10510 """ # We first need to retrieve the body before accessing POST or FILES since # it can only be read once. body = request.body if request.POST or request.FILES: return new_body = BytesIO() # Split the response in the various parts based on the boundary string content_type, opts = parse_header(request.META['CONTENT_TYPE'].encode('ascii')) parts = body.split(b'\n--' + opts['boundary'] + b'\n') for part in parts: if b'\n\n' not in part: continue headers, content = part.split(b'\n\n', 1) if not headers: continue new_body.write(b'--' + opts['boundary'] + b'\r\n') new_body.write(headers.replace(b'\n', b'\r\n')) new_body.write(b'\r\n\r\n') new_body.write(content) new_body.write(b'\r\n') new_body.write(b'--' + opts['boundary'] + b'--\r\n') request._body = new_body.getvalue() request.META['CONTENT_LENGTH'] = len(request._body) # Clear out _files and _post so that the request object re-parses the body if hasattr(request, '_files'): delattr(request, '_files') if hasattr(request, '_post'): delattr(request, '_post')
def test_integration(self): json_dict = { "entry": [{ "messaging": [{ "timestamp": 1513497365911, "postback": { "payload": 'TEST', "title": 'test' }, "recipient": { "id": "1897495093796566" }, "sender": { "id": '123' } }], "id": "1897495093796566", "time": 1513497365911 }], "object": "page" } event = DummyEvent({'title': 'test'}) chain = Chain(event, 'TEST') FacebookRequestHandler().postback_chain_container.chains = [chain] json_dump = json.dumps(json_dict).encode() request = WSGIRequest({ 'REQUEST_METHOD': 'POST', 'wsgi.input': len(json_dump) }) request._body = json_dump view = FacebookRequestHandler().handle_post(json_dict) expected = json.dumps(DummyEvent.EXPECTED_JSON, sort_keys=True, indent=2) actual = json.dumps(view.reply.to_json(), sort_keys=True, indent=2) self.assertEqual(expected, actual)
def test_integration(self): json_dict = { "entry": [ { "messaging": [ { "timestamp": 1513500364748, "message": { "text": DummyMessageEvent.EXPECTED_MESSAGE, "mid": "mid.$cAAbnMLPVyFxmlOwXzFgY6bZ7H7a4", "seq": 479412 }, "recipient": { "id": "1897495093796566" }, "sender": { "id": "1444250085633761" } } ], "id": "1897495093796566", "time": 1513500365490 } ], "object": "page" } event = DummyMessageEvent() chain = Chain(event, 'MESSAGE') FacebookRequestHandler().message_chain_container.chains = [chain] json_dump = json.dumps(json_dict).encode() request = WSGIRequest({'REQUEST_METHOD': 'POST', 'wsgi.input': len(json_dump)}) request._body = json_dump view = FacebookRequestHandler().handle_post(json_dict) expected = json.dumps(DummyEvent.EXPECTED_JSON, sort_keys=True, indent=2) actual = json.dumps(view.reply.to_json(), sort_keys=True, indent=2) self.assertEqual(expected, actual)