Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stubbing :head_bucket breaks bucket.exists? #1289

Closed
paul opened this issue Sep 14, 2016 · 5 comments
Closed

Stubbing :head_bucket breaks bucket.exists? #1289

paul opened this issue Sep 14, 2016 · 5 comments
Labels
guidance Question that needs advice or information.

Comments

@paul
Copy link

paul commented Sep 14, 2016

When I stub requests to :head_bucket with "NotFound", it causes bucket.exists? to break.

With a stubbed client, I get an exception:

require "aws-sdk"
require "logger"

logger = Logger.new(STDOUT)

bucket_name = "some-bucket-name-that-doesnt-exist"

stub_client = Aws::S3::Client.new(stub_responses: true, logger: logger)
stub_client.stub_responses(:head_bucket, "NotFound")

bucket = Aws::S3::Bucket.new(bucket_name, client: stub_client)

bucket.exists?
# [Aws::S3::Client 400 0.000593 0 retries] \
#   head_bucket(bucket:"some-bucket-name-that-doesnt-exist") \
#   Aws::S3::Errors::NotFound stubbed-response-error-message

# Aws::S3::Errors::NotFound: stubbed-response-error-message
#   from /Users/rando/.gem/ruby/2.3.1/gems/aws-sdk-resources-2.6.0/lib/aws-sdk-resources/resource.rb:134:in `rescue in exists?'
#   from /Users/rando/.gem/ruby/2.3.1/gems/aws-sdk-resources-2.6.0/lib/aws-sdk-resources/resource.rb:131:in `exists?'

When I try with a non-stubbed client, I get the same Aws::S3::Errors::NotFound error, but it returns false, rather than letting the exception bubble up:

client = Aws::S3::Client.new(logger: logger)
bucket = Aws::S3::Bucket.new(bucket_name, client: client)

bucket.exists?
# [Aws::S3::Client 404 0.607778 0 retries] \
#   head_bucket(bucket:"some-bucket-name-that-doesnt-exist") \
#   Aws::S3::Errors::NotFound
# => false

The difference appears to be that the stubbed response has the stubbed-response-error-message message, perhaps that's confusing the WaitOperations's rescue?

@paul
Copy link
Author

paul commented Sep 14, 2016

Looking closer, it appears the stubbed response is a 400, while the actual one is a 404. Do I need to set the status code to 404 myself in the stub? The docs aren't clear on how to stub both a response error object and a status code. I would also expect the "NotFound" response string to set the 404 for me.

@awood45
Copy link
Member

awood45 commented Sep 14, 2016

Identical to issue #1058 - your instinct is correct on this one.

@awood45
Copy link
Member

awood45 commented Sep 14, 2016

You can stub the status code as well, and doing that will give you the outcome you're looking for.

@paul
Copy link
Author

paul commented Sep 14, 2016

Got it, thanks. Sorry for the dupe, I tried searching the issues, not far enough back I guess. I had also tried just :head_bucket, {status_code: 404} and got an error, didn't realize all the keys were required. Thanks for your help.

@awood45
Copy link
Member

awood45 commented Sep 14, 2016

No problem, sorry for the confusion!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
guidance Question that needs advice or information.
Projects
None yet
Development

No branches or pull requests

3 participants