def test_simple_attr_add(self): content_type = Header("Content-Type", 'text/html; charset="utf-8"') self.assertNotIn("format", content_type) content_type.format = "flowed" self.assertIn("format", content_type) self.assertEqual("flowed", content_type.format) self.assertEqual('text/html; charset="utf-8"; format="flowed"', content_type)
def test_remove_exclusively_valued_attrs(self): content_type = Header("Content-Type", 'text/html; charset="utf-8"; charset') with self.subTest("Trying to subtract 'charset' attrs ONLY."): del content_type.charset self.assertEqual("text/html; charset", str(content_type))
def test_attribute_not_in(self): self.assertNotIn( "format", Header( "Content-Type", 'multipart/alternative; boundary="_000_455DADE4FB733C4C8F62EB4CEB36D8DE05037EA94Fswexch1sesaml_"; charset=utf-8', ), ) self.assertNotIn( "secret", Header( "Content-Type", 'multipart/alternative; boundary="_000_455DADE4FB733C4C8F62EB4CEB36D8DE05037EA94Fswexch1sesaml_"; charset=utf-8', ), )
def test_remove_exclusively_none_attrs(self): content_type = Header("Content-Type", 'text/html; charset="utf-8"; text/html=Ahah') with self.subTest("Trying to subtract 'text/html' member ONLY."): content_type -= "text/html" self.assertEqual('charset="utf-8"; text/html="Ahah"', str(content_type))
def test_attribute_access_exist(self): self.assertIn( "charset", Header( "Content-Type", 'multipart/alternative; boundary="_000_455DADE4FB733C4C8F62EB4CEB36D8DE05037EA94Fswexch1sesaml_"; charset=utf-8', ), ) self.assertIn( "boundary", Header( "Content-Type", 'multipart/alternative; boundary="_000_455DADE4FB733C4C8F62EB4CEB36D8DE05037EA94Fswexch1sesaml_"; charset=utf-8', ), )
def test_invalid_eq(self): header = Header( "Message-ID", "<455DADE4FB733C4C8F62EB4CEB36D8DE05037EA94F@johndoe>") with self.assertRaises(NotImplementedError): k = header == 1
def test_access_attribute(self): self.assertEqual( Header( "Content-Type", 'multipart/alternative; boundary="_000_455DADE4FB733C4C8F62EB4CEB36D8DE05037EA94Fswexch1sesaml_"; charset=utf-8', ).charset, "utf-8", ) self.assertEqual( Header( "Content-Type", 'multipart/alternative; boundary="_000_455DADE4FB733C4C8F62EB4CEB36D8DE05037EA94Fswexch1sesaml_"; charset=utf-8', )["charset"], "utf-8", )
def test_replicate_raw_from_objects(self): headers = ( Header("Accept-Ch", "DPR") + Header("Accept-Ch-Lifetime", "2592000") + AltSvc("quic", ":443", max_age=2592000, versions=["46", "43"]) + AltSvc("h3-Q050", ":443", max_age=2592000) + AltSvc("h3-Q049", ":443", max_age=2592000) + AltSvc("h3-Q048", ":443", max_age=2592000) + AltSvc("h3-Q046", ":443", max_age=2592000) + AltSvc("h3-Q043", ":443", max_age=2592000) + CacheControl("private") + CacheControl(max_age=0) + ContentEncoding("br") + ContentLength(64032) + ContentType("text/html", charset="utf-8") + Date("Mon, 16 Mar 2020 21:27:31 GMT") + Expires("-1") + Header( "P3P", 'CP="This is not a P3P policy! See g.co/p3phelp for more info."' ) + Server("gws") + SetCookie( "1P_JAR", "2020-03-16-21", expires="Wed, 15-Apr-2020 21:27:31 GMT", path="/", domain=".google.fr", samesite="none", is_secure=True, is_httponly=False, ) + SetCookie( "NID", "200=IGpBMMA3G7tki0niFFATFQ2BnsNceVP6XBtwOutoyw97AJ4_YFT5l1oLfLeX22xeI_STiP4omAB4rmMP3Sxgyo287ldQGwdZSdPOOZ_Md3roDOMAOtXEQ_hFbUvo0VPjS2gL1y00_6kQwpVxCghI2Ozrx-A4Xks3ZIXRj11RsWs", expires="Tue, 15-Sep-2020 21:27:31 GMT", path="/", domain=".google.fr", samesite="none", is_secure=True, ) + SetCookie( "CONSENT", "WP.284b10", expires="Fri, 01-Jan-2038 00:00:00 GMT", path="/", domain=".google.fr", is_httponly=False, ) + Header("Status", "200") + StrictTransportSecurity(max_age=31536000) + XFrameOptions("SAMEORIGIN") + XXssProtection(False)) self.assertEqual(parse_it(RAW_HEADERS), headers)
def test_complex_second_attr_removal(self): content_type = Header( "Content-Type", 'text/html; format=flowed; charset="utf-8"; format=flowed; format="origin";', ) del content_type.format self.assertEqual('text/html; charset="utf-8"', str(content_type))
def test_subtract_adjective(self): content_type = Header("Content-Type", 'text/html; charset="utf-8"') self.assertIn("text/html", content_type) content_type -= "text/html" self.assertNotIn("text/html", content_type) self.assertEqual('charset="utf-8"', str(content_type))
def test_isub_adjective_error(self): content_type = Header("Content-Type", 'text/html; charset="utf-8"') self.assertNotIn("text/xml", content_type) with self.assertRaises(ValueError): content_type = content_type - "text/xml" with self.assertRaises(TypeError): content_type = content_type - 1
def test_headers(): cmline = "--header 'Content-Type: application/json' --header 'Host: quan.hoabinh.vn' http://103.92.28.225" args = parsed_args(cmline) debug(args.header) assert args.header == [ 'Content-Type: application/json', 'Host: quan.hoabinh.vn' ] debug(args._headers) assert args._headers == Headers(Header('Host', 'quan.hoabinh.vn')) assert args._request_json
def test_iadd_adjective(self): content_type = Header("Content-Type", 'charset="utf-8"') self.assertNotIn("text/html", content_type) content_type = content_type + "text/html" self.assertIn("text/html", content_type) self.assertEqual('charset="utf-8"; text/html', str(content_type))
def test_simple_attr_removal(self): content_type = Header("Content-Type", 'text/html; charset="utf-8"') self.assertIn("charset", content_type) self.assertEqual("utf-8", content_type.charset) del content_type.charset self.assertNotIn("charset", content_type) self.assertEqual(str(content_type), "text/html")
def test_attrs_access_case_insensitive(self): content_type = Header("Content-Type", 'text/html; charset="utf-8"') with self.subTest("Verify that attrs can be accessed no matter case"): self.assertEqual("utf-8", content_type.charset) self.assertEqual("utf-8", content_type.charseT) self.assertEqual("utf-8", content_type.CHARSET) with self.subTest("Using del on attr using case insensitive key"): del content_type.CHARSET self.assertNotIn("charset", content_type)
def test_simple_eq(self): self.assertEqual( Header("Message-ID", "<455DADE4FB733C4C8F62EB4CEB36D8DE05037EA94F@johndoe>"). content, "<455DADE4FB733C4C8F62EB4CEB36D8DE05037EA94F@johndoe>", ) self.assertEqual( Header("Message-ID", "<455DADE4FB733C4C8F62EB4CEB36D8DE05037EA94F@johndoe>"), "<455DADE4FB733C4C8F62EB4CEB36D8DE05037EA94F@johndoe>", ) self.assertNotEqual( Header("Message-ID", "<455DADE4FB733C4C8F62EB4CEB36D8DE05037EA94F@johndoe>"), Header( "Message-ID-Dummy", "<455DADE4FB733C4C8F62EB4CEB36D8DE05037EA94F@johndoe>", ), ) self.assertEqual( Header("Message-ID", "<455DADE4FB733C4C8F62EB4CEB36D8DE05037EA94F@johndoe>"), Header("Message-ID", "<455DADE4FB733C4C8F62EB4CEB36D8DE05037EA94F@johndoe>"), )
def test_add(self): headers = parse_it( """X-My-Testing: 1\nX-My-Second-Test: 1\nX-My-Second-Test: Precisly\nReceived: outpost\nReceived: outpost""" ) self.assertEqual(5, len(headers)) headers_ = headers + Header("content-type", "application/json") self.assertEqual(6, len(headers_)) self.assertIn("content-type", headers_) self.assertNotIn("content-type", headers)
def test_single_header_iterator(self): header = Header( "Content-Type", 'multipart/alternative; boundary="_000_455DADE4FB733C4C8F62EB4CEB36D8DE05037EA94Fswexch1sesaml_"; charset=utf-8', ) self.assertEqual( { "multipart/alternative": None, "boundary": "_000_455DADE4FB733C4C8F62EB4CEB36D8DE05037EA94Fswexch1sesaml_", "charset": "utf-8", }, dict(header), )
def test_contain_space_delimiter(self): authorization = Header("Authorization", "Bearer mysupersecrettoken") self.assertIn("Bearer", authorization) self.assertIn("beaRer", authorization) self.assertNotIn("beare", authorization) self.assertFalse(authorization == "Bearer") self.assertTrue(authorization == "bearer mysupersecrettoken") self.assertFalse(authorization == "basic mysupersecrettoken")
def test_complex_attr_removal(self): content_type = Header( "Content-Type", 'text/html; charset="utf-8"; format=flowed; format="origin";', ) del content_type.format self.assertEqual('text/html; charset="utf-8"', str(content_type)) with self.assertRaises(AttributeError): del content_type.format del content_type["charset"] self.assertEqual("text/html", str(content_type)) with self.assertRaises(KeyError): del content_type["charset"]
def process_args(self): u = yarl.URL(self.url) # Clean fragment, if exist url = str(u.with_fragment(None).with_query(None)) # Strip leading "http://" to be short self._url = url[7:] if u.scheme == 'http' else url self._params = deque(u.query.items()) self._data = deque() for dstring in self.data: result = parse_post_data(dstring) self._data.extend(result.data) self._errors.extend(result.errors) for dstring in self.data_raw: result = parse_post_data(dstring, ignore_at=True) self._data.extend(result.data) self._errors.extend(result.errors) for dstring in self.form: result = parse_post_data(dstring) self._data.extend(result.data) self._errors.extend(result.errors) for h in self.header: headers = parse_it(h) if not headers: continue if HH.CONTENT_TYPE in headers: hx = get_polymorphic(headers, ContentType) if hx.get_mime() == HH.CONTENT_TYPE_VALUES.json: self._request_json = True continue elif HH.AUTHORIZATION in headers and headers.authorization.content.startswith( 'Basic '): hx = get_polymorphic(headers, BasicAuthorization) self._auth = hx continue # kiss-header doesn't prevent duplicate, so we have to check ourselve # Please note the behavior of kiss-headers: The "Accept-Encoding: gzip, deflate" # will be parsed to two Header objects, to get all the "value" side, we have to # convert the parse result to dict. name = headers[0].pretty_name if self._headers.has(name): del self._headers[name] value = headers.to_dict()[name] self._headers += Header(name, value)
def test_isub(self): headers = parse_it( """X-My-Testing: 1\nX-My-Second-Test: 1\nX-My-Second-Test: Precisly\nReceived: outpost\nReceived: outpost""" ) self.assertEqual(5, len(headers)) headers -= "X-My-Testing" self.assertEqual(4, len(headers)) self.assertNotIn("X-My-Testing", headers) headers -= "Received" self.assertEqual(2, len(headers)) self.assertNotIn("Received", headers) headers -= Header("X-My-Second-Test", "Precisly") self.assertEqual(1, len(headers)) self.assertIn("X-My-Second-Test", headers)
def test_illegal_delitem_operation(self): content_type = Header("Content-Type", 'text/html; charset="utf-8"') with self.subTest("Forbid to remove non-valued attr using delitem"): with self.assertRaises(KeyError): del content_type["text/html"]