Skip to content
Myr

Move nested function up to module level and rename. Add more tests for function.

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by dbairaktaris1 · committed by dbairaktaris1 · parent 2dc51c8 · Browse files · 2 files+67 added−16 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+67 added−16 removed

  • tests:+36 added−1 removed
  • other:+31 added−15 removed

2 files+67 added−16 removed

modified:requests/utils.py+31 added-15 removed
444 unmodified lines
445 pragma_re.findall(content) +446 xml_re.findall(content))447448449def _parse_content_type_header(header):450 """Returns content type and parameters from given header451452 :param header: string453 :return: tuple containing content type and dictionary of454 parameters455 """456 if not header:457 return None458 # append delimiter on end to ensure at least two elements when split by ';'459 header += ';'460 # split content type's main value from params461 tokens = header.split(';', 1)462 content_type_index = 0463 params_index = 1464465 content_type = tokens[content_type_index].strip()466 params = tokens[params_index]467 params_dict = dict()468469 for param in params.split(';'):470 if param and not param.isspace():471 param = param.strip()472 key, value = param, True473 if '=' in param:474 param_tokens = [x.strip('\'" ') for x in param.split('=', 1)]475 key, value = param_tokens[0], param_tokens[1]476 params_dict[key] = value477 return content_type, params_dict478479def get_encoding_from_headers(headers):480 """Returns encodings from given HTTP Header Dict.481482 :param headers: dictionary to extract encoding from.483 :rtype: str484 """455 def parse_header(content_type):456 #Inner function to parse header457 #append delimiter on end to ensure atleast two elements when split by ';'458 content_type_and_params_delimiter = ';'459 content_type += content_type_and_params_delimiter485461 tokens = content_type.split(content_type_and_params_delimiter)462 content_type_index = 0463 params_index = 1464465 content_type = tokens[content_type_index]466 params = tokens[params_index]467 params_dict = dict(param.split('=') for param in params.split())468 return content_type,params_dict469486 content_type = headers.get('content-type')487488 if not content_type:489 return None490475 content_type, params = parse_header(content_type)491 content_type, params = _parse_content_type_header(content_type)492493 if 'charset' in params:494 return params['charset'].strip("'\"")495
482 unmodified lines
modified:tests/test_utils.py+36 added-1 removed
11 unmodified lines
12from requests.cookies import RequestsCookieJar13from requests.structures import CaseInsensitiveDict14from requests.utils import (15 address_in_network, dotted_netmask, extract_zipped_paths,16 get_auth_from_url, get_encoding_from_headers,16 get_auth_from_url, _parse_content_type_header, get_encoding_from_headers,17 get_encodings_from_content, get_environ_proxies,18 guess_filename, guess_json_utf, is_ipv4_address,19 is_valid_cidr, iter_slices, parse_dict_header,20 parse_header_links, prepend_scheme_if_needed,
451 unmodified lines
472473@pytest.mark.parametrize(474 'value, expected', (475 (476 None,477 None478 ),479(480 '',481 None482 ),483 (484 'application/xml',485 ('application/xml', dict())486 ),487 (488 'application/json ; charset=utf-8',489 ('application/json', {'charset': 'utf-8'})490 ),491 (492 'text/plain',493 ('text/plain', dict())494 ),495 (496 'multipart/form-data; boundary = something ; \'boundary2=something_else\' ; no_equals ',497 ('multipart/form-data', {'boundary': 'something', 'boundary2': 'something_else', 'no_equals': True})498 ),499 (500 'application/json ;; ; ',501 ('application/json', dict())502 )503 ))504def test__parse_content_type_header(value, expected):505 assert _parse_content_type_header(value) == expected506507508@pytest.mark.parametrize(509 'value, expected', (510 (511 CaseInsensitiveDict(),512 None513 ),514 (
238 unmodified lines