5
5
from twisted.internet.defer import succeed
7
7
from txaws.credentials import AWSCredentials
8
from txaws.service import AWSServiceEndpoint
8
9
from txaws.storage.client import S3, S3Request
9
from txaws.tests import TXAWSTestCase
10
from txaws.testing.base import TXAWSTestCase
10
11
from txaws.util import calculate_md5
21
class RequestTests(TXAWSTestCase):
23
creds = AWSCredentials(access_key='0PN5J17HBGZHT7JJ3X82',
24
secret_key='uV3F3YluFJax1cknvbcGwgjvx4QpvB+leU8dUj2o')
22
class RequestTestCase(TXAWSTestCase):
24
creds = AWSCredentials(access_key='fookeyid', secret_key='barsecretkey')
25
endpoint = AWSServiceEndpoint("https://s3.amazonaws.com/")
27
def test_get_uri_with_endpoint(self):
28
endpoint = AWSServiceEndpoint("http://localhost/")
29
request = S3Request('PUT', endpoint=endpoint)
30
self.assertEquals(request.endpoint.get_uri(), "http://localhost/")
31
self.assertEquals(request.get_uri(), "http://localhost/")
33
def test_get_uri_with_endpoint_bucket_and_object(self):
34
endpoint = AWSServiceEndpoint("http://localhost/")
35
request = S3Request('PUT', bucket="mybucket", object_name="myobject",
39
"http://localhost/mybucket/myobject")
41
def test_get_uri_with_no_endpoint(self):
42
request = S3Request('PUT')
43
self.assertEquals(request.endpoint, None)
44
self.assertEquals(request.get_uri(), "http:///")
46
def test_get_path_with_bucket_and_object(self):
47
request = S3Request('PUT', bucket="mybucket", object_name="myobject")
48
self.assertEquals(request.get_path(), "/mybucket/myobject")
50
def test_get_path_with_no_bucket_or_object(self):
51
request = S3Request('PUT')
52
self.assertEquals(request.get_path(), "/")
26
54
def test_objectRequest(self):
31
59
DIGEST = 'zhdB6gwvocWv/ourYUWMxA=='
33
61
request = S3Request('PUT', 'somebucket', 'object/name/here', DATA,
34
content_type='text/plain', metadata={'foo': 'bar'})
62
content_type='text/plain', metadata={'foo': 'bar'},
63
creds=self.creds, endpoint=self.endpoint)
64
request.get_signature = lambda headers: "TESTINGSIG="
35
65
self.assertEqual(request.verb, 'PUT')
38
68
'https://s3.amazonaws.com/somebucket/object/name/here')
39
69
headers = request.get_headers()
40
70
self.assertNotEqual(headers.pop('Date'), '')
41
self.assertEqual(headers,
42
{'Content-Type': 'text/plain',
43
'Content-Length': len(DATA),
44
'Content-MD5': DIGEST,
45
'x-amz-meta-foo': 'bar'})
73
'Authorization': 'AWS fookeyid:TESTINGSIG=',
74
'Content-Type': 'text/plain',
75
'Content-Length': len(DATA),
76
'Content-MD5': DIGEST,
77
'x-amz-meta-foo': 'bar'})
46
78
self.assertEqual(request.data, 'objectData')
48
80
def test_bucketRequest(self):
52
84
DIGEST = '1B2M2Y8AsgTpgAmY7PhCfg=='
54
request = S3Request('GET', 'somebucket')
86
request = S3Request('GET', 'somebucket', creds=self.creds,
87
endpoint=self.endpoint)
88
request.get_signature = lambda headers: "TESTINGSIG="
55
89
self.assertEqual(request.verb, 'GET')
57
91
request.get_uri(), 'https://s3.amazonaws.com/somebucket')
58
92
headers = request.get_headers()
59
93
self.assertNotEqual(headers.pop('Date'), '')
60
self.assertEqual(headers,
62
'Content-MD5': DIGEST})
96
'Authorization': 'AWS fookeyid:TESTINGSIG=',
98
'Content-MD5': DIGEST})
63
99
self.assertEqual(request.data, '')
65
101
def test_submit(self):
67
103
Submitting the request should invoke getPage correctly.
69
request = StubbedS3Request('GET', 'somebucket')
105
request = StubbedS3Request('GET', 'somebucket', creds=self.creds,
106
endpoint=self.endpoint)
71
108
def _postCheck(result):
72
109
self.assertEqual(result, '')
80
117
return request.submit().addCallback(_postCheck)
82
119
def test_authenticationTestCases(self):
83
req = S3Request('GET', creds=self.creds)
84
req.date = 'Wed, 28 Mar 2007 01:29:59 +0000'
120
request = S3Request('GET', creds=self.creds, endpoint=self.endpoint)
121
request.get_signature = lambda headers: "TESTINGSIG="
122
request.date = 'Wed, 28 Mar 2007 01:29:59 +0000'
86
headers = req.get_headers()
124
headers = request.get_headers()
88
126
headers['Authorization'],
89
'AWS 0PN5J17HBGZHT7JJ3X82:jF7L3z/FTV47vagZzhKupJ9oNig=')
127
'AWS fookeyid:TESTINGSIG=')
92
130
class InertRequest(S3Request):
153
191
TXAWSTestCase.setUp(self)
154
192
self.creds = AWSCredentials(
155
193
access_key='accessKey', secret_key='secretKey')
156
self.s3 = TestableS3(creds=self.creds)
194
self.endpoint = AWSServiceEndpoint()
195
self.s3 = TestableS3(creds=self.creds, endpoint=self.endpoint)
158
197
def test_make_request(self):
160
Test that make_request passes in the service credentials.
199
Test that make_request passes in the credentials object.
162
201
marker = object()
164
203
def _cb(*a, **kw):
165
204
self.assertEqual(kw['creds'], self.creds)
205
self.assertEqual(kw['endpoint'], self.endpoint)
168
208
self.s3.request_factory = _cb