aws / aws-sdk-ruby

The official AWS SDK for Ruby.
https://aws.amazon.com/sdk-for-ruby/
Apache License 2.0
3.55k stars 1.22k forks source link

Can't send queue message on local environment. #777

Closed sunao-uehara closed 9 years ago

sunao-uehara commented 9 years ago

Hi There,

I am using ver 2.0.38 for SQS with elasticMQ to simulate SQS on my localhost development. but I have a trouble using APIs because of localhost domain.

Aws.config(
    :access_key_id => 'XXX',
    :secret_access_key => 'XXX'
)
client = Aws::SQS::Client.new(
          region: 'XXX',
          endpoint: 'http://localhost'
)

client.create_queue(queue_name: "my_queue_1")

queue_url = client.get_queue_url(queue_name: "my_queue_1")[:queue_url]

client.send_message(
    queue_url: queue_url,
    message_body:"Test Message"
)
aws-sdk-core-2.0.38/lib/aws-sdk-core/plugins/sqs_queue_urls.rb:26:in `update_region': invalid queue url `http://localhost/queue/my_queue_1

So I ended up adding monkey-patch like below to allow "localhost" by overriding SQSQueueUrls class, but do you guys have any plan fixing it or allowing localhost or non-dot-separated host? I know it is nothing to do with real aws environment, besides ElasticMQ is not aws supported lib, but it is convenient for developer to working on localhost.

module Aws
  module Plugins
    # @api private
    class SQSQueueUrls < Seahorse::Client::Plugin
      class Handler < Seahorse::Client::Handler
        def update_region(context, url)
          if region = url.to_s.split('.')[1]
            context.config = context.config.dup
            context.config.region = region
            context.config.sigv4_region = region
          elsif url.include?('localhost')
            return
          else
            raise ArgumentError, "invalid queue url `#{url}'"
          end
        end
      end
    end
  end
end

Thanks

trevorrowe commented 9 years ago

I suspect you can remove your monkey-patch and simply remove the SQSQueueUrls plugin from the SDK while under test.

# in your spec/test helper
Aws::SQS::Client.remove_plugin(Aws::Plugins::SQSQueueUrls)

This will prevent the client from redirecting the request to the given queue URL. Let me know if this does not work.

sunao-uehara commented 9 years ago

Thanks trevorrowe,

It works perfectly fine. I still need to check condition of env to include that code, but I think it is fine for now.