1
# Copyright (c) 2009 Canonical Ltd <duncan.mcgreggor@canonical.com>
2
# Licenced under the txaws licence available at /LICENSE in the txaws source.
4
from twisted.trial.unittest import TestCase
6
from txaws.exception import AWSError
7
from txaws.exception import AWSResponseParseError
8
from txaws.util import XML
11
REQUEST_ID = "0ef9fc37-6230-4d81-b2e6-1b36277d4247"
14
class AWSErrorTestCase(TestCase):
16
def test_creation(self):
17
error = AWSError("<dummy1 />", 500, "Server Error", "<dummy2 />")
18
self.assertEquals(error.status, 500)
19
self.assertEquals(error.response, "<dummy2 />")
20
self.assertEquals(error.original, "<dummy1 />")
21
self.assertEquals(error.errors, [])
22
self.assertEquals(error.request_id, "")
24
def test_node_to_dict(self):
25
xml = "<parent><child1>text1</child1><child2>text2</child2></parent>"
26
error = AWSError("<dummy />", 400)
27
data = error._node_to_dict(XML(xml))
28
self.assertEquals(data, {"child1": "text1", "child2": "text2"})
30
def test_set_request_id(self):
31
xml = "<a><b /><RequestID>%s</RequestID></a>" % REQUEST_ID
32
error = AWSError("<dummy />", 400)
33
error._set_request_id(XML(xml))
34
self.assertEquals(error.request_id, REQUEST_ID)
36
def test_set_host_id(self):
37
host_id = "ASD@#FDG$E%FG"
38
xml = "<a><b /><HostID>%s</HostID></a>" % host_id
39
error = AWSError("<dummy />", 400)
40
error._set_host_id(XML(xml))
41
self.assertEquals(error.host_id, host_id)
43
def test_set_empty_errors(self):
44
xml = "<a><Errors /><b /></a>"
45
error = AWSError("<dummy />", 500)
46
error._set_500_error(XML(xml))
47
self.assertEquals(error.errors, [])
49
def test_set_empty_error(self):
50
xml = "<a><Errors><Error /><Error /></Errors><b /></a>"
51
error = AWSError("<dummy />", 500)
52
error._set_500_error(XML(xml))
53
self.assertEquals(error.errors, [])
55
def test_parse_without_xml(self):
57
error = AWSError(xml, 400)
59
self.assertEquals(error.original, xml)
61
def test_parse_with_xml(self):
64
error = AWSError(xml1, 400)
66
self.assertEquals(error.original, xml2)
68
def test_parse_html(self):
69
xml = "<html><body>a page</body></html>"
70
self.assertRaises(AWSResponseParseError, AWSError, xml, 400)
72
def test_empty_xml(self):
73
self.assertRaises(ValueError, AWSError, "", 400)
75
def test_no_request_id(self):
76
errors = "<Errors><Error><Code /><Message /></Error></Errors>"
77
xml = "<Response>%s<RequestID /></Response>" % errors
78
error = AWSError(xml, 400)
79
self.assertEquals(error.request_id, "")
81
def test_no_request_id_node(self):
82
errors = "<Errors><Error><Code /><Message /></Error></Errors>"
83
xml = "<Response>%s</Response>" % errors
84
error = AWSError(xml, 400)
85
self.assertEquals(error.request_id, "")
87
def test_no_errors_node(self):
88
xml = "<Response><RequestID /></Response>"
89
error = AWSError(xml, 400)
90
self.assertEquals(error.errors, [])
92
def test_no_error_node(self):
93
xml = "<Response><Errors /><RequestID /></Response>"
94
error = AWSError(xml, 400)
95
self.assertEquals(error.errors, [])
97
def test_no_error_code_node(self):
98
errors = "<Errors><Error><Message /></Error></Errors>"
99
xml = "<Response>%s<RequestID /></Response>" % errors
100
error = AWSError(xml, 400)
101
self.assertEquals(error.errors, [])
103
def test_no_error_message_node(self):
104
errors = "<Errors><Error><Code /></Error></Errors>"
105
xml = "<Response>%s<RequestID /></Response>" % errors
106
error = AWSError(xml, 400)
107
self.assertEquals(error.errors, [])
109
def test_set_500_error(self):
110
xml = "<Error><Code>500</Code><Message>Oops</Message></Error>"
111
error = AWSError("<dummy />", 500)
112
error._set_500_error(XML(xml))
113
self.assertEquals(error.errors[0]["Code"], "500")
114
self.assertEquals(error.errors[0]["Message"], "Oops")