Exemple #1
0
    def finish_parsing(self):
        content_type = self.headers.get("Content-Type", "")
        fields = content_type.split(";")
        type = fields[0].strip()

        param = {}
        for field in fields[1:]:
            field = field.strip()
            if not "=" in field:
                raise HTTPError("Malformed directive in Content-Type header")

            key, value = field.split("=", 1)
            param[key] = value

        if type in TEXT_TYPES:
            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.plainform = formdecode(self.text, False)

        if type in JSON_TYPES:
            try:
                self.json = json.loads(self.text)
            except json.JSONDecodeError:
                raise HTTPError("Failed to decode JSON body")

        if type in XML_TYPES:
            try:
                self.xml = xml.parse(self.text)
            except ValueError as e:
                raise HTTPError("Failed to decode XML body: %s" % e)
 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"
 def test_find(self):
     tree = xml.parse(DOCUMENT)
     assert tree.find("key") == [tree.children[0]]
     assert tree.find("root") == []
 def test_len(sefl):
     tree = xml.parse(DOCUMENT)
     assert len(tree) == 2
 def test_iter(self):
     tree = xml.parse(DOCUMENT)
     assert list(tree) == tree.children
 def test_getitem(self):
     tree = xml.parse(DOCUMENT)
     assert tree["key"] == tree.children[0]
     with pytest.raises(KeyError):
         tree["root"]
 def test_contains(self):
     tree = xml.parse(DOCUMENT)
     assert "key" in tree
     assert not "root" in tree
 def test_parse(self):
     tree = xml.parse(DOCUMENT)
     assert tree.encode() == DOCUMENT