def test_put_retry(self): eq_(g.bucket.mock_responses, []) xml = "<?xml etc... ?>" g.bucket.add_resp("/foo.txt", g.H("application/xml"), xml, status="500 Internal Server Error") g.bucket.add_resp("/foo.txt", g.H("text/plain"), "OK!") g.bucket.put("foo.txt", "hello") eq_(len(g.bucket.mock_requests), 2) for req in g.bucket.mock_requests: eq_(req.get_method(), "PUT") eq_(req.get_selector(), "/foo.txt") eq_(g.bucket.mock_responses, [])
def test_copy_metadata(self): g.bucket.add_resp("/bar", g.H("application/xml"), "<ok />") g.bucket.copy("foo/bar", "bar", acl="public") req = g.bucket.mock_requests[-1] eq_(req.get_method(), "PUT") eq_(req.headers["X-amz-copy-source"], "foo/bar") eq_(req.headers["X-amz-metadata-directive"], "COPY")
def test_put(self): g.bucket.add_resp("/foo.txt", g.H("application/xml"), "OK!") g.bucket["foo.txt"] = "hello" hdrs = set(v.lower() for v in g.bucket.mock_requests[-1].headers) assert "content-length" in hdrs assert "content-type" in hdrs assert "content-md5" in hdrs assert "authorization" in hdrs
def test_delete_other_error(self): g.bucket.add_resp("/foo.txt", g.H("application/xml"), "<wat />", status="403 What's Up?") try: g.bucket.delete("foo.txt") except simples3.S3Error, e: eq_(e.extra.get("key"), "foo.txt") eq_(e.code, 403)
def test_get(self): dt = datetime.datetime(1990, 1, 31, 12, 34, 56) headers = g.H("text/plain", ("date", rfc822_fmtdate(dt)), ("x-amz-meta-foo", "bar")) g.bucket.add_resp("/foo.txt", headers, "ohi") fp = g.bucket["foo.txt"] eq_(fp.s3_info["mimetype"], "text/plain") eq_(fp.s3_info["metadata"], {"foo": "bar"}) eq_(fp.s3_info["date"], dt) eq_(fp.read().decode("ascii"), "ohi")
def _put_file_contents(self, key, contents): g.streaming_bucket.add_resp("/%s" % key, g.H("application/xml"), "OK!") try: fp = io.StringIO() fp.write(contents) g.streaming_bucket.put_file(key, fp, size=len(contents)) finally: fp.close() self._verify_headers(contents, g.streaming_bucket.mock_requests[-1].headers)
def test_empty_listing(self): xml = """ <?xml version="1.0" encoding="UTF-8"?> <ListBucketResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/"> <Name>bucket</Name> <Prefix></Prefix> <Marker></Marker> <MaxKeys>1000</MaxKeys> <IsTruncated>false</IsTruncated> </ListBucketResult> """.lstrip() g.bucket.add_resp("/", g.H("application/xml"), xml) eq_([], list(g.bucket.listdir()))
def test_listdir(self): xml = """ <?xml version="1.0" encoding="UTF-8"?> <ListBucketResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/"> <Name>bucket</Name> <Prefix></Prefix> <Marker></Marker> <MaxKeys>1000</MaxKeys> <IsTruncated>false</IsTruncated> <Contents> <Key>my-image.jpg</Key> <LastModified>2009-10-12T17:50:30.000Z</LastModified> <ETag>"fba9dede5f27731c9771645a39863328"</ETag> <Size>434234</Size> <StorageClass>STANDARD</StorageClass> <Owner> <ID>0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef</ID> <DisplayName>johndoe</DisplayName> </Owner> </Contents> <Contents> <Key>my-third-image.jpg</Key> <LastModified>2009-10-12T17:50:30.000Z</LastModified> <ETag>"1b2cf535f27731c974343645a3985328"</ETag> <Size>64994</Size> <StorageClass>STANDARD</StorageClass> <Owner> <ID>0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef</ID> <DisplayName>johndoe</DisplayName> </Owner> </Contents> </ListBucketResult> """.lstrip() g.bucket.add_resp("/", g.H("application/xml"), xml) reftups = (('my-image.jpg', datetime.datetime(2009, 10, 12, 17, 50, 30), '"fba9dede5f27731c9771645a39863328"', 434234), ('my-third-image.jpg', datetime.datetime(2009, 10, 12, 17, 50, 30), '"1b2cf535f27731c974343645a3985328"', 64994)) next_reftup = iter(reftups).next for tup in g.bucket.listdir(): eq_(len(tup), 4) eq_(tup, next_reftup()) key, mtype, etag, size = tup
class InfoTests(S3BucketTestCase): headers = g.H("text/plain", ("x-amz-meta-foo", "bar"), ("last-modified", "Mon, 06 Sep 2010 19:34:18 GMT"), ("content-length", "1234")) def test_info(self): g.bucket.add_resp("/foo.txt", self.headers, "") info = g.bucket.info("foo.txt") eq_(info["mimetype"], "text/plain") eq_(info["metadata"], {"foo": "bar"}) def test_mapping(self): g.bucket.add_resp("/foo.txt", self.headers, "") assert "foo.txt" in g.bucket def test_mapping_not(self): g.bucket.add_resp("/foobar.txt", self.headers, "", status="404 Blah") assert "foobar.txt" not in g.bucket
def test_get_not_found(self): xml = ('<?xml version="1.0" encoding="UTF-8"?>\n' '<Error><Code>NoSuchKey</Code>' '<Message>The specified key does not exist.</Message>' '<Key>foo.txt</Key>' '<RequestId>abcdef</RequestId>' '<HostId>abcdef</HostId>' '</Error>') g.bucket.add_resp("/foo.txt", g.H("application/xml"), xml, status="404 Not Found") try: g.bucket.get("foo.txt") except simples3.KeyNotFound, e: eq_(e.key, "foo.txt") eq_( str(e), "The specified key does not exist. (code=404, " "key='foo.txt', filename='http://johnsmith.s3." "amazonaws.com/foo.txt')")
def test_copy_replace_metadata(self): g.bucket.add_resp("/bar", g.H("application/xml"), "<ok />") g.bucket.copy("foo/bar", "bar", metadata={}, acl="public") req = g.bucket.mock_requests[-1] eq_(req.headers["X-amz-metadata-directive"], "REPLACE")
def test_delete_not_found(self): g.bucket.add_resp("/foo.txt", g.H("application/xml"), "<notfound />", status="404 Not Found") assert not g.bucket.delete("foo.txt")
def test_bucket_delete(self): g.bucket.add_resp("/", g.H("application/xml"), "<ok />") g.bucket.delete_bucket() req = g.bucket.mock_requests[-1] eq_(req.get_method(), "DELETE")
def test_put_s3file(self): g.bucket.add_resp("/foo.txt", g.H("application/xml"), "OK!") g.bucket["foo.txt"] = simples3.S3File("hello") data = g.bucket.mock_requests[-1].get_data() eq_(data.decode("ascii"), "hello")
def test_bucket_put(self): g.bucket.add_resp("/", g.H("application/xml"), "<ok />") g.bucket.put_bucket(acl="private") req = g.bucket.mock_requests[-1] eq_(req.get_method(), "PUT") eq_(req.headers["X-amz-acl"], "private")
def test_bucket_put_conf(self): g.bucket.add_resp("/", g.H("application/xml"), "<ok />") g.bucket.put_bucket("<etc>etc</etc>", acl="public") req = g.bucket.mock_requests[-1] eq_(req.get_method(), "PUT") eq_(req.headers["X-amz-acl"], "public")
def test_delete(self): g.bucket.add_resp("/foo.txt", g.H("application/xml"), "<ok />") assert g.bucket.delete("foo.txt") req = g.bucket.mock_requests[-1] eq_(req.get_method(), "DELETE")
def test_delete_multi_object(self): expected = ('<?xml version="1.0" encoding="UTF-8"?>\n' '<DeleteResult xmlns="http://s3.amazonaws.com/doc/2006-' '03-01/"></DeleteResult>') g.bucket.add_resp("/?delete", g.H("application/xml"), expected) assert g.bucket.delete("foo.txt", "bar.txt", "baz.txt")
def _put_contents(self, key, contents): g.bucket.add_resp("/%s" % key, g.H("application/xml"), "OK!") g.bucket[key] = contents self._verify_headers(contents, g.bucket.mock_requests[-1].headers)