Add If-Match, If-None-Match and If-Unmodified-Since to S3 GET/HEAD (#3021)

fixes #2705
This commit is contained in:
Arcadiy Ivanov 2020-09-11 05:17:39 -04:00 committed by GitHub
commit c2d1ce2c14
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 167 additions and 7 deletions

View file

@ -2335,6 +2335,64 @@ def test_boto3_get_object_if_modified_since():
e.response["Error"].should.equal({"Code": "304", "Message": "Not Modified"})
@mock_s3
def test_boto3_get_object_if_unmodified_since():
s3 = boto3.client("s3", region_name=DEFAULT_REGION_NAME)
bucket_name = "blah"
s3.create_bucket(Bucket=bucket_name)
key = "hello.txt"
s3.put_object(Bucket=bucket_name, Key=key, Body="test")
with assert_raises(botocore.exceptions.ClientError) as err:
s3.get_object(
Bucket=bucket_name,
Key=key,
IfUnmodifiedSince=datetime.datetime.utcnow() - datetime.timedelta(hours=1),
)
e = err.exception
e.response["Error"]["Code"].should.equal("PreconditionFailed")
e.response["Error"]["Condition"].should.equal("If-Unmodified-Since")
@mock_s3
def test_boto3_get_object_if_match():
s3 = boto3.client("s3", region_name=DEFAULT_REGION_NAME)
bucket_name = "blah"
s3.create_bucket(Bucket=bucket_name)
key = "hello.txt"
s3.put_object(Bucket=bucket_name, Key=key, Body="test")
with assert_raises(botocore.exceptions.ClientError) as err:
s3.get_object(
Bucket=bucket_name, Key=key, IfMatch='"hello"',
)
e = err.exception
e.response["Error"]["Code"].should.equal("PreconditionFailed")
e.response["Error"]["Condition"].should.equal("If-Match")
@mock_s3
def test_boto3_get_object_if_none_match():
s3 = boto3.client("s3", region_name=DEFAULT_REGION_NAME)
bucket_name = "blah"
s3.create_bucket(Bucket=bucket_name)
key = "hello.txt"
etag = s3.put_object(Bucket=bucket_name, Key=key, Body="test")["ETag"]
with assert_raises(botocore.exceptions.ClientError) as err:
s3.get_object(
Bucket=bucket_name, Key=key, IfNoneMatch=etag,
)
e = err.exception
e.response["Error"].should.equal({"Code": "304", "Message": "Not Modified"})
@mock_s3
def test_boto3_head_object_if_modified_since():
s3 = boto3.client("s3", region_name=DEFAULT_REGION_NAME)
@ -2355,6 +2413,62 @@ def test_boto3_head_object_if_modified_since():
e.response["Error"].should.equal({"Code": "304", "Message": "Not Modified"})
@mock_s3
def test_boto3_head_object_if_unmodified_since():
s3 = boto3.client("s3", region_name=DEFAULT_REGION_NAME)
bucket_name = "blah"
s3.create_bucket(Bucket=bucket_name)
key = "hello.txt"
s3.put_object(Bucket=bucket_name, Key=key, Body="test")
with assert_raises(botocore.exceptions.ClientError) as err:
s3.head_object(
Bucket=bucket_name,
Key=key,
IfUnmodifiedSince=datetime.datetime.utcnow() - datetime.timedelta(hours=1),
)
e = err.exception
e.response["Error"].should.equal({"Code": "412", "Message": "Precondition Failed"})
@mock_s3
def test_boto3_head_object_if_match():
s3 = boto3.client("s3", region_name=DEFAULT_REGION_NAME)
bucket_name = "blah"
s3.create_bucket(Bucket=bucket_name)
key = "hello.txt"
s3.put_object(Bucket=bucket_name, Key=key, Body="test")
with assert_raises(botocore.exceptions.ClientError) as err:
s3.head_object(
Bucket=bucket_name, Key=key, IfMatch='"hello"',
)
e = err.exception
e.response["Error"].should.equal({"Code": "412", "Message": "Precondition Failed"})
@mock_s3
def test_boto3_head_object_if_none_match():
s3 = boto3.client("s3", region_name=DEFAULT_REGION_NAME)
bucket_name = "blah"
s3.create_bucket(Bucket=bucket_name)
key = "hello.txt"
etag = s3.put_object(Bucket=bucket_name, Key=key, Body="test")["ETag"]
with assert_raises(botocore.exceptions.ClientError) as err:
s3.head_object(
Bucket=bucket_name, Key=key, IfNoneMatch=etag,
)
e = err.exception
e.response["Error"].should.equal({"Code": "304", "Message": "Not Modified"})
@mock_s3
@reduced_min_part_size
def test_boto3_multipart_etag():