bblimke / webmock

Library for stubbing and setting expectations on HTTP requests in Ruby.
MIT License
3.97k stars 555 forks source link

Call to Amazon Product Advertising API results in error due to unregistered stub #459

Closed sbambey closed 9 years ago

sbambey commented 9 years ago

I am stubbing a request to the Amazon Product advertising exactly as is recommended in the error message I am receiving:

Failure/Error: service_response = service.search
     WebMock::NetConnectNotAllowedError:
       Real HTTP connections are disabled. Unregistered request: GET http://webservices.amazon.com/onca/xml?AWSAccessKeyId=AKIAIWO3OEDYYTGZ5HOQ&AssociateTag=readinglist09-20&IdType=ISBN&ItemId=9780313364587&Operation=ItemLookup&ResponseGroup=ItemAttributes&SearchIndex=Books&Service=AWSECommerceService&Signature=kPXl6Vdrk0J%2BCkX1vEHZiyLNJJNOHoKCJUnzVhd5gkI=&SignatureMethod=HmacSHA256&SignatureVersion=2&Timestamp=2015-03-08T01:21:47Z&Version=2011-08-01 with headers {'Host'=>'webservices.amazon.com', 'User-Agent'=>'Jeff/1.2.0 (Language=Ruby; Simons-MacBook-Pro-2.local)'}

       You can stub this request with the following snippet:

       stub_request(:get, "http://webservices.amazon.com/onca/xml?AWSAccessKeyId=AKIAIWO3OEDYYTGZ5HOQ&AssociateTag=readinglist09-20&IdType=ISBN&ItemId=9780313364587&Operation=ItemLookup&ResponseGroup=ItemAttributes&SearchIndex=Books&Service=AWSECommerceService&Signature=kPXl6Vdrk0J%2BCkX1vEHZiyLNJJNOHoKCJUnzVhd5gkI=&SignatureMethod=HmacSHA256&SignatureVersion=2&Timestamp=2015-03-08T01:21:47Z&Version=2011-08-01").
         with(:headers => {'Host'=>'webservices.amazon.com', 'User-Agent'=>'Jeff/1.2.0 (Language=Ruby; Simons-MacBook-Pro-2.local)'}).
         to_return(:status => 200, :body => "", :headers => {})

       registered request stubs:

       stub_request(:get, "http://webservices.amazon.com/onca/xml?AWSAccessKeyId=AKIAIWO3OEDYYTGZ5HOQ&AssociateTag=readinglist09-20&IdType=ISBN&ItemId=9780313364587&Operation=ItemLookup&ResponseGroup=ItemAttributes&SearchIndex=Books&Service=AWSECommerceService&Signature=am8BWpCI8de53jAYR2%2B6rGEr/UxwY/0fJz%2BDRVyNo%2Bc=&SignatureMethod=HmacSHA256&SignatureVersion=2&Timestamp=2015-03-08T01:16:12Z&Version=2011-08-01").
         with(:headers => {'Host'=>'webservices.amazon.com', 'User-Agent'=>'Jeff/1.2.0 (Language=Ruby; Simons-MacBook-Pro-2.local)'})

My code can be seen here:

require 'rails_helper'

RSpec.describe AmazonBooksService do
  let(:service) { AmazonBooksService.new({isbns: ["9780313364587"]}) }

  subject { service }

  it "searches google books and returns proper item" do
    api_response = File.new "#{Rails.root}/spec/services/amazon_books_canned_response.xml"
    stub_request(:get, "http://webservices.amazon.com/onca/xml?AWSAccessKeyId=AKIAIWO3OEDYYTGZ5HOQ&AssociateTag=readinglist09-20&IdType=ISBN&ItemId=9780313364587&Operation=ItemLookup&ResponseGroup=ItemAttributes&SearchIndex=Books&Service=AWSECommerceService&Signature=am8BWpCI8de53jAYR2%2B6rGEr/UxwY/0fJz%2BDRVyNo%2Bc=&SignatureMethod=HmacSHA256&SignatureVersion=2&Timestamp=2015-03-08T01:16:12Z&Version=2011-08-01").
         with(:headers => {'Host'=>'webservices.amazon.com', 'User-Agent'=>'Jeff/1.2.0 (Language=Ruby; Simons-MacBook-Pro-2.local)'}).
         to_return(:status => 200, :body => "", :headers => {})
    service_response = service.search
    expect(service_response["ItemAttributes"]["ISBN"]).to_not be_nil
  end
end

Note the missing to_return in the registered stub. I can't seem to figure out what I am doing wrong.

bblimke commented 9 years ago

@sbambey you are making signed requests to AWS so each request has a unique signature and timestamp.

I suggest you use partial query matching and only focus on these attributes that don't change: https://github.com/bblimke/webmock#matching-partial-query-params-using-hash

sbambey commented 9 years ago

That solved it for me. Thanks so much!