Example #1
0
	def parse_body(self):
		type, param = parseheader(self.headers.get("Content-Type", ""))
		
		is_json = type == "application/json" or type.endswith("+json")
		is_xml = type in XML_TYPES or type.endswith("+xml")
		is_text = type in TEXT_TYPES or type.startswith("text/") or is_json or is_xml
		
		if is_text:
			try:
				self.text = self.body.decode(param.get("charset", "UTF-8"))
			except UnicodeDecodeError:
				raise HTTPError("Failed to decode HTTP body")
		
		if type == "application/x-www-form-urlencoded":
			self.form = formdecode(self.text)
			self.rawform = formdecode(self.text, False)
		
		if is_json:
			try:
				self.json = json.loads(self.text)
			except json.JSONDecodeError:
				raise HTTPError("Failed to decode JSON body")
		
		if is_xml:
			try:
				self.xml = xml.parse(self.text)
			except ValueError as e:
				raise HTTPError("Failed to decode XML body: %s" %e)
		
		if type.startswith("multipart/form-data"):
			if "boundary" not in param:
				raise HTTPError("multipart/form-data required boundary parameter")
			self.boundary = param["boundary"]
			self.files = self.parse_files(self.body)
Example #2
0
	def test_attrs(self):
		tree = xml.parse(DOCUMENT)
		assert tree.children[0].name == "key"
		assert tree.children[0].text == "hi"
		assert tree.children[1].name == "value"
		assert tree.children[1].text == "test"
		assert tree.attrs == {}
		assert tree.text == ""
		assert tree.name == "root"
Example #3
0
	def test_find(self):
		tree = xml.parse(DOCUMENT)
		assert tree.find("key") == [tree.children[0]]
		assert tree.find("root") == []
Example #4
0
	def test_len(sefl):
		tree = xml.parse(DOCUMENT)
		assert len(tree) == 2
Example #5
0
	def test_iter(self):
		tree = xml.parse(DOCUMENT)
		assert list(tree) == tree.children
Example #6
0
	def test_getitem(self):
		tree = xml.parse(DOCUMENT)
		assert tree["key"] == tree.children[0]
		with pytest.raises(KeyError):
			tree["root"]
Example #7
0
	def test_contains(self):
		tree = xml.parse(DOCUMENT)
		assert "key" in tree
		assert not "root" in tree
Example #8
0
	def test_parse(self):
		tree = xml.parse(DOCUMENT)
		assert tree.encode() == DOCUMENT