def test_items(self): k = CaseInsensitiveDict({ "abc": 1, "qwerty": 2, "content-TYPE": "json" }) self.assertEqual(list(k.items()), [("abc", 1), ("qwerty", 2), ("content-TYPE", "json")])
def explain(headers: Headers) -> CaseInsensitiveDict: """ Return a brief explanation of each header present in headers if available. """ if not Header.__subclasses__(): raise LookupError( "You cannot use explain() function without properly importing the public package." ) explanations: CaseInsensitiveDict = CaseInsensitiveDict() for header in headers: if header.name in explanations: continue try: target_class = header_name_to_class(header.name, Header.__subclasses__()[0]) except TypeError: explanations[header.name] = "Unknown explanation." continue explanations[header.name] = (target_class.__doc__.replace( "\n", "").lstrip().replace(" ", " ").rstrip() if target_class.__doc__ else "Missing docstring.") return explanations
def test_insensible_key(self): k = CaseInsensitiveDict({"abc": 1, "qwerty": 2, "content-TYPE": "json"}) self.assertIn("content-type", k) self.assertIn("content_type", k) self.assertIn("ABc", k)
def test_eq(self): k = CaseInsensitiveDict({ "abc": 1, "qwerty": 2, "content-TYPE": "json" }) self.assertEqual(k, {"abc": 1, "qwerty": 2, "content-TYPE": "json"}) self.assertEqual(k, k)
def to_dict(self) -> CaseInsensitiveDict: """ Provide a CaseInsensitiveDict output of current headers. This output type has been borrowed from psf/requests. If one header appears multiple times, it would be concatenated into the same value, separated by a comma. Be aware that this repr could lead to a mistake. """ dict_headers = CaseInsensitiveDict() for header in self: header_name_no_underscore = header.name.replace("_", "-") if header_name_no_underscore not in dict_headers: dict_headers[header_name_no_underscore] = header.content continue dict_headers[header_name_no_underscore] += ", " + header.content return dict_headers
def __init__(self, name: str, content: str): """ :param name: The name of the header, should contain only ASCII characters with no spaces in it. :param content: Initial content associated with the header. """ if not is_legal_header_name(name): raise ValueError( f"'{name}' is not a valid header name. Cannot proceed with it." ) self._name: str = name self._normalized_name: str = normalize_str(self._name) self._pretty_name: str = prettify_header_name(self._name) self._content: str = content self._members: List[str] = header_content_split(self._content, ";") self._not_valued_attrs: List[str] = list() self._valued_attrs: MutableMapping[str, Union[ str, List[str]]] = CaseInsensitiveDict() for member in self._members: if member == "": continue if "=" in member: key, value = tuple(member.split("=", maxsplit=1)) # avoid confusing base64 look alike single value for (key, value) if value.count("=") == len(value) or len( value) == 0 or " " in key: self._not_valued_attrs.append(unquote(member)) continue if key not in self._valued_attrs: self._valued_attrs[key] = value else: if isinstance(self._valued_attrs[key], str): self._valued_attrs[key] = [ self._valued_attrs[key], value ] # type: ignore else: self._valued_attrs[key].append(value) # type: ignore continue self._not_valued_attrs.append(unquote(member))