def test_parse_mimetype(self): self.assertEqual(helpers.parse_mimetype(''), ('', '', '', {})) self.assertEqual(helpers.parse_mimetype('*'), ('*', '*', '', {})) self.assertEqual(helpers.parse_mimetype('application/json'), ('application', 'json', '', {})) self.assertEqual( helpers.parse_mimetype('application/json; charset=utf-8'), ('application', 'json', '', { 'charset': 'utf-8' })) self.assertEqual( helpers.parse_mimetype('''application/json; charset=utf-8;'''), ('application', 'json', '', { 'charset': 'utf-8' })) self.assertEqual( helpers.parse_mimetype('ApPlIcAtIoN/JSON;ChaRseT="UTF-8"'), ('application', 'json', '', { 'charset': 'UTF-8' })) self.assertEqual(helpers.parse_mimetype('application/rss+xml'), ('application', 'rss', 'xml', {})) self.assertEqual(helpers.parse_mimetype('text/plain;base64'), ('text', 'plain', '', { 'base64': '' }))
def test_parse_mimetype(self): self.assertEqual( helpers.parse_mimetype(''), ('', '', '', {})) self.assertEqual( helpers.parse_mimetype('*'), ('*', '*', '', {})) self.assertEqual( helpers.parse_mimetype('application/json'), ('application', 'json', '', {})) self.assertEqual( helpers.parse_mimetype('application/json; charset=utf-8'), ('application', 'json', '', {'charset': 'utf-8'})) self.assertEqual( helpers.parse_mimetype('''application/json; charset=utf-8;'''), ('application', 'json', '', {'charset': 'utf-8'})) self.assertEqual( helpers.parse_mimetype('ApPlIcAtIoN/JSON;ChaRseT="UTF-8"'), ('application', 'json', '', {'charset': 'UTF-8'})) self.assertEqual( helpers.parse_mimetype('application/rss+xml'), ('application', 'rss', 'xml', {})) self.assertEqual( helpers.parse_mimetype('text/plain;base64'), ('text', 'plain', '', {'base64': ''}))
def test_parse_mimetype(self): self.assertEqual(helpers.parse_mimetype(""), ("", "", "", {})) self.assertEqual(helpers.parse_mimetype("*"), ("*", "*", "", {})) self.assertEqual(helpers.parse_mimetype("application/json"), ("application", "json", "", {})) self.assertEqual( helpers.parse_mimetype("application/json; charset=utf-8"), ("application", "json", "", {"charset": "utf-8"}), ) self.assertEqual( helpers.parse_mimetype("""application/json; charset=utf-8;"""), ("application", "json", "", {"charset": "utf-8"}), ) self.assertEqual( helpers.parse_mimetype('ApPlIcAtIoN/JSON;ChaRseT="UTF-8"'), ("application", "json", "", {"charset": "UTF-8"}), ) self.assertEqual(helpers.parse_mimetype("application/rss+xml"), ("application", "rss", "xml", {})) self.assertEqual(helpers.parse_mimetype("text/plain;base64"), ("text", "plain", "", {"base64": ""}))
def test_parse_mimetype_6(): assert helpers.parse_mimetype('ApPlIcAtIoN/JSON;ChaRseT="UTF-8"') == ( "application", "json", "", {"charset": "UTF-8"}, )
def _try_client_response(self, headers, parsed): """ Sends a response back to the client based on Accept header Defaults to JSON """ media_msg = 'The requested media type is unsupported' mime_type = None sub_type = None try: accept_types = headers['Accept'] mime_type, sub_type, _, _ = parse_mimetype(accept_types) except KeyError: pass if mime_type == 'application' and sub_type == 'octet-stream': return web.Response( content_type='application/octet-stream', body=parsed.SerializeToString() ) if ((mime_type in ['application', '*'] or mime_type is None) and (sub_type in ['json', '*'] or sub_type is None)): return web.Response( content_type='application/json', text=MessageToJson(parsed) ) raise web.HTTPUnsupportedMediaType(reason=media_msg)
async def post(request: web.Request) -> web.Response: # language=rst """POST handler for ``/files``""" mimetype = helpers.parse_mimetype(request.headers[aiohttp.hdrs.CONTENT_TYPE]) if mimetype.type != 'multipart': raise web.HTTPBadRequest( text='multipart/* content type expected' ) reader = await request.multipart() field = await reader.next() if field.name != 'distribution': raise web.HTTPBadRequest( text='file field must be named “distribution”' ) uuid = uuid4().hex content_type = field.headers.get( aiohttp.hdrs.CONTENT_TYPE, 'application/octet-stream' ) await _put_file_to_object_store( uuid, content_type, field, filename=field.filename ) return web.Response( status=201, headers={ 'Location': _BASE_URL + uuid } )
def _try_client_response(headers, parsed): """ Used by pre-spec /state and /batches routes Should be removed when routes are updated to spec """ media_msg = 'The requested media type is unsupported' mime_type = None sub_type = None try: accept_types = headers['Accept'] mime_type, sub_type, _, _ = parse_mimetype(accept_types) except KeyError: pass if mime_type == 'application' and sub_type == 'octet-stream': return web.Response( content_type='application/octet-stream', body=parsed.SerializeToString() ) if ((mime_type in ['application', '*'] or mime_type is None) and (sub_type in ['json', '*'] or sub_type is None)): return web.Response( content_type='application/json', text=MessageToJson(parsed) ) raise web.HTTPUnsupportedMediaType(reason=media_msg)
def test_parse_mimetype_5(): assert helpers.parse_mimetype("""application/json; charset=utf-8;""") == ( "application", "json", "", {"charset": "utf-8"}, )
def _check_valid_mimetype(self, request): typ, subtype, suffix, params = parse_mimetype( request.headers.get('Content-Type')) valid_type = (typ, subtype, suffix) == ('application', 'json', '') valid_profile = (params.get('profile') == self.profile or self.profile is None) valid_version = (params.get('version') == self.version or self.version is None) if not all((valid_type, valid_profile, valid_version)): raise APIError('BadRequest', message='Invalid request MIME type')
def __init__(self, resp, *, loop=None, buffer_size=0): self._active = True self._exc = None self._queue = asyncio.Queue(maxsize=buffer_size or self.buffer_size, loop=loop) self._resp = resp ctype = resp.headers.get(CONTENT_TYPE, "").lower() *_, params = parse_mimetype(ctype) self._encoding = params.get("charset", "utf-8") # pylint: disable=E1101 asyncio.Task(self._loop(), loop=loop)
def __init__(self, resp, *, loop=None, buffer_size=buffer_size): self._active = True self._exc = None self._queue = asyncio.Queue(maxsize=buffer_size or self.buffer_size, loop=loop) self._resp = resp ctype = resp.headers.get('CONTENT-TYPE', '').lower() *_, params = parse_mimetype(ctype) self._encoding = params.get('charset', 'utf-8') asyncio.Task(self._loop(), loop=loop)
def __init__(self, resp, *, loop=None, buffer_size=0): self._active = True self._exc = None self._queue = asyncio.Queue(maxsize=buffer_size or self.buffer_size, loop=loop) self._resp = resp ctype = resp.headers.get(CONTENT_TYPE, '').lower() mimetype = parse_mimetype(ctype) self._encoding = mimetype.parameters.get('charset', 'utf-8') # pylint: disable=E1101 asyncio.Task(self._loop(), loop=loop)
def dispatch(self, headers): """Dispatches the response by the `Content-Type` header, returning suitable reader instance. :param dict headers: Response headers """ ctype = headers.get('CONTENT-TYPE', '') mtype, stype, *_ = parse_mimetype(ctype) for key in ((mtype, stype), (mtype, None), None): handler = self.dispatch_map.get(key) if handler is not None: return handler(self, headers) raise AttributeError('no handler available for content type %r', ctype)
def parse_accept(accept): parts = accept.split(',') types = [] for item in parts: if not item: continue mimetype = parse_mimetype(item) mimestring = '+'.join(filter(None, ['/'.join(filter(None, [mimetype.type, mimetype.subtype])), mimetype.suffix])) types.append(mimestring) return types
async def _iter_content_generator(response, decode_unicode): encoding = None if decode_unicode: ctype = response.headers.get('content-type', '').lower() mimetype = parse_mimetype(ctype) # Fallback to UTF-8 encoding = mimetype.parameters.get('charset', 'UTF-8') async for chunk in response.content.iter_chunked(100 * 1024): if decode_unicode: chunk = chunk.decode(encoding) # Replace CRLF newlines with LF, Python will handle # platform specific newlines if written to file. chunk = chunk.replace('\r\n', '\n') # Chunk could be ['...\r', '\n...'], strip trailing \r chunk = chunk.rstrip('\r') yield chunk
def get_encoding(self) -> str: c_type = self.headers.get("Content-Type", "").lower() mimetype = helpers.parse_mimetype(c_type) encoding = mimetype.parameters.get("charset") if encoding: try: codecs.lookup(encoding) except LookupError: encoding = None if not encoding: if mimetype.type == "application" and ( mimetype.subtype == "json" or mimetype.subtype == "rdap"): encoding = "utf-8" elif self._body is None: raise RuntimeError("Cannot guess the encoding of " "a not yet read body") else: encoding = cchardet.detect(self._body)["encoding"] if not encoding: encoding = "utf-8" return encoding
def function354(): assert (helpers.parse_mimetype('ApPlIcAtIoN/JSON;ChaRseT="UTF-8"') == ( 'application', 'json', '', { 'charset': 'UTF-8', }))
def test_parse_mimetype_2(): assert helpers.parse_mimetype("*") == ("*", "*", "", {})
def get_mimetype(self): ctype = self.headers.get(hdrs.CONTENT_TYPE, '').lower() return helpers.parse_mimetype(ctype)
def test_parse_mimetype_7(): assert (helpers.parse_mimetype('application/rss+xml') == ('application', 'rss', 'xml', {}))
def test_parse_mimetype_1(): assert helpers.parse_mimetype('') == ('', '', '', {})
def test_parse_mimetype_2(): assert helpers.parse_mimetype('*') == ('*', '*', '', {})
def test_parse_mimetype_5(): assert (helpers.parse_mimetype('''application/json; charset=utf-8;''') == ( 'application', 'json', '', { 'charset': 'utf-8' }))
def charset(r): ctype = r.headers.get('content-type', '').lower() _, _, _, params = parse_mimetype(ctype) return params.get('charset')
def test_default_subtype(self, writer): mimetype = parse_mimetype(writer.headers.get(CONTENT_TYPE)) assert 'multipart' == mimetype.type assert 'mixed' == mimetype.subtype
def function85(): assert (helpers.parse_mimetype('application/json; charset=utf-8;') == ( 'application', 'json', '', { 'charset': 'utf-8', }))
def test_parse_mimetype_7(): assert helpers.parse_mimetype("application/rss+xml") == ("application", "rss", "xml", {})
def function632(): assert (helpers.parse_mimetype('application/json') == ('application', 'json', '', {}))
def function1803(): assert (helpers.parse_mimetype('*') == ('*', '*', '', {}))
def function423(): assert (helpers.parse_mimetype('') == ('', '', '', {}))
def test_parse_mimetype_1(): assert helpers.parse_mimetype("") == ("", "", "", {})
def test_parse_mimetype_8(): assert helpers.parse_mimetype("text/plain;base64") == ("text", "plain", "", {"base64": ""})
def get_encoding(response): ctype = response.headers.get('content-type', '').lower() mimetype = parse_mimetype(ctype) # Fallback to UTF-8 return mimetype.parameters.get('charset', 'UTF-8')
def get_encoding(self): ctype = self.response.headers.get('content-type', '').lower() mtype, stype, _, params = parse_mimetype(ctype) # Fallback to UTF-8 return params.get('charset', 'UTF-8')
def test_parse_mimetype_3(): assert (helpers.parse_mimetype('application/json') == ('application', 'json', '', {}))
def test_default_subtype(self, writer) -> None: mimetype = parse_mimetype(writer.headers.get(CONTENT_TYPE)) assert 'multipart' == mimetype.type assert 'mixed' == mimetype.subtype
def test_parse_mimetype_6(): assert( helpers.parse_mimetype('ApPlIcAtIoN/JSON;ChaRseT="UTF-8"') == ('application', 'json', '', {'charset': 'UTF-8'}))
def test_parse_mimetype_8(): assert ( helpers.parse_mimetype('text/plain;base64') == ('text', 'plain', '', {'base64': ''}))
def test_parse_mimetype_6(): assert (helpers.parse_mimetype('ApPlIcAtIoN/JSON;ChaRseT="UTF-8"') == ( 'application', 'json', '', { 'charset': 'UTF-8' }))
def test_parse_mimetype(mimetype, expected): result = helpers.parse_mimetype(mimetype) assert isinstance(result, helpers.MimeType) assert result == expected
def test_parse_mimetype_8(): assert (helpers.parse_mimetype('text/plain;base64') == ('text', 'plain', '', { 'base64': '' }))
def function1524(): assert (helpers.parse_mimetype('application/rss+xml') == ('application', 'rss', 'xml', {}))
def test_parse_mimetype_3(): assert helpers.parse_mimetype("application/json") == ("application", "json", "", {})
def function356(): assert (helpers.parse_mimetype('text/plain;base64') == ('text', 'plain', '', { 'base64': '', }))
def test_parse_mimetype_5(): assert ( helpers.parse_mimetype('''application/json; charset=utf-8;''') == ('application', 'json', '', {'charset': 'utf-8'}))
def get_boundary(headers): mtype, *_, params = parse_mimetype(headers['CONTENT-TYPE']) assert mtype == 'multipart' return ('--%s' % params['boundary']).encode()
def test_parse_mimetype_7(): assert ( helpers.parse_mimetype('application/rss+xml') == ('application', 'rss', 'xml', {}))
def get_charset(headers, default=None): ctype = headers.get('CONTENT-TYPE', '') *_, params = parse_mimetype(ctype) return params.get('charset', default)
def test_parse_mimetype(mimetype, expected) -> None: result = helpers.parse_mimetype(mimetype) assert isinstance(result, helpers.MimeType) assert result == expected
def get_mimetype(self): ctype = self.headers.get(hdrs.CONTENT_TYPE, "").lower() return helpers.parse_mimetype(ctype)