async def test_multipart_content_transfer_encoding(loop, test_client): """For issue #1168""" with multipart.MultipartWriter() as writer: writer.append(b'\x00' * 10, headers={'Content-Transfer-Encoding': 'binary'}) async def handler(request): reader = await request.multipart() assert isinstance(reader, multipart.MultipartReader) part = await reader.next() assert isinstance(part, multipart.BodyPartReader) assert part.headers['Content-Transfer-Encoding'] == 'binary' thing = await part.read() assert thing == b'\x00' * 10 resp = web.Response() resp.content_type = 'application/json' resp.body = b'' return resp app = web.Application() app.router.add_post('/', handler) client = await test_client(app) resp = await client.post('/', data=writer, headers=writer.headers) assert 200 == resp.status await resp.release()
async def test_multipart(loop, test_client): with multipart.MultipartWriter() as writer: writer.append('test') writer.append_json({'passed': True}) async def handler(request): reader = await request.multipart() assert isinstance(reader, multipart.MultipartReader) part = await reader.next() assert isinstance(part, multipart.BodyPartReader) thing = await part.text() assert thing == 'test' part = await reader.next() assert isinstance(part, multipart.BodyPartReader) assert part.headers['Content-Type'] == 'application/json' thing = await part.json() assert thing == {'passed': True} resp = web.Response() resp.content_type = 'application/json' resp.body = b'' return resp app = web.Application() app.router.add_post('/', handler) client = await test_client(app) resp = await client.post('/', data=writer, headers=writer.headers) assert 200 == resp.status await resp.release()
def function385(arg2392, arg1496): 'For issue #1168' with multipart.MultipartWriter() as var3062: var3062.append((b'\x00' * 10), headers={ 'Content-Transfer-Encoding': 'binary', }) @asyncio.coroutine def function146(arg2239): var518 = yield from arg2239.multipart() assert isinstance(var518, multipart.MultipartReader) var728 = yield from var518.next() assert isinstance(var728, multipart.BodyPartReader) assert (var728.headers['Content-Transfer-Encoding'] == 'binary') var4346 = yield from var728.read() assert (var4346 == (b'\x00' * 10)) var2520 = web.Response() var2520.content_type = 'application/json' var2520.body = b'' return var2520 var1874 = web.Application() var1874.router.add_post('/', function146) var2834 = yield from arg1496(var1874) var2808 = yield from var2834.post('/', data=var3062, headers=var3062.headers) assert (200 == var2808.status) yield from var2808.release()
def function2636(arg1060, arg1623): with multipart.MultipartWriter() as var1241: var1241.append('test') var1241.append_json({ 'passed': True, }) @asyncio.coroutine def function146(arg571): var2178 = yield from arg571.multipart() assert isinstance(var2178, multipart.MultipartReader) var2237 = yield from var2178.next() assert isinstance(var2237, multipart.BodyPartReader) var1748 = yield from var2237.text() assert (var1748 == 'test') var2237 = yield from var2178.next() assert isinstance(var2237, multipart.BodyPartReader) assert (var2237.headers['Content-Type'] == 'application/json') var1748 = yield from var2237.json() assert (var1748 == { 'passed': True, }) var3938 = web.Response() var3938.content_type = 'application/json' var3938.body = b'' return var3938 var1429 = web.Application() var1429.router.add_post('/', function146) var1114 = yield from arg1623(var1429) var2785 = yield from var1114.post('/', data=var1241, headers=var1241.headers) assert (200 == var2785.status) yield from var2785.release()
async def test_object_post(mock_create_object, cli): with multipart.MultipartWriter() as writer: writer.append('test') res = await cli.post('/objects', data=writer, headers=writer.headers) assert res.status == 200 obj = json.loads(await res.text()) assert isinstance(obj, dict) assert obj['data']['id'] is None assert mock_create_object.call_count == 1
def __init__(self, fields: Iterable[Any] = (), quote_fields: bool = True, charset: Optional[str] = None, boundary: Optional[str] = None) -> None: self._writer = multipart.MultipartWriter("form-data", boundary=boundary) self._fields = [] # type: List[Any] self._is_multipart = False self._is_processed = False self._quote_fields = quote_fields self._charset = charset if isinstance(fields, dict): fields = list(fields.items()) elif not isinstance(fields, (list, tuple)): fields = (fields, ) self.add_fields(*fields)