'Tests basic read/write to keys.'

def test_read_write(self):

    bucket = self._MakeBucket()
    bucket_name = bucket.name
    bucket = self._GetConnection().get_bucket(bucket_name)
    key_name = 'foobar'
    k = bucket.new_key(key_name)
    s1 = 'This    is    a    test    of    file    upload    and    download'
    k.set_contents_from_string(s1)
    tmpdir = self._MakeTempDir()
    fpath = os.path.join(tmpdir, key_name)
    fp = open(fpath, 'wb')
    k.get_contents_to_file(fp)
    fp.close()
    fp = open(fpath)
    self.assertEqual(s1, fp.read())
    fp.close()
    url = self._conn.generate_url(900, 'GET', bucket=bucket.name, key=key_name)
    f = urllib.urlopen(url)
    self.assertEqual(s1, f.read())
    f.close()
    sfp = StringIO.StringIO('foo')
    k.set_contents_from_file(sfp)
    self.assertEqual(k.get_contents_as_string(), 'foo')
    sfp2 = StringIO.StringIO('foo2')
    k.set_contents_from_file(sfp2)
    self.assertEqual(k.get_contents_as_string(), 'foo2')
