bblimke / webmock

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

Not able to stub request #51

Closed nickhammond closed 13 years ago

nickhammond commented 13 years ago

I'm integrating with the Chargify API which requires that everything go over https. I've added in a stub in a before block in my env.rb along with disable_net_connect! and reset_webmock as follows:

http://gist.github.com/627763

I know that the stubs shouldn't go in there but I just want to get the mocking working and then I'll wrap them up in a module and load in the module. I stubbed this request directly from the output of webmock when running my tests so I'm not sure where the discrepancy is.

I messed around with the gem a little bit and I can see that it is actually stubbing them but whenever I would print out the URL that WebMock was using it always had the port 443 in the URL. Not sure if that is what is throwing it off or what it is.

The https request occurs upon an account creation so my steps only pertain to an account being created and not a specific web request such as "I request a map from google maps" so I can't wrap it into a step or scenario.

bblimke commented 13 years ago

Maybe it's a problem with xml string formatting? Have you tried stubbing request using a hash instead of xml i.e stub_http_request(:post, "https://ABCDEFGHI:X@testaccount.chargify.com/subscriptions.xml"). with(:body => {:subscription => {:credit-card-attributes => ...}}) ?

Port 443 is the default one for https so webmock will match even if it's not specified directly.

Could you send a complete failing test with a stubbed request and request invocation? This will allow me to debug the issue.

nickhammond commented 13 years ago

I'll get something back to you this week about it or let you know if it was me just overlooking something which is highly possible.

nickhammond commented 13 years ago

require 'webmock/rspec' require 'active_support'

Spec::Runner.configure do |config| config.include WebMock::API end

describe "webmock" do it "should stub the https request" do xml_body = "<?xml version='1.0' encoding='UTF-8'?>

2015 H 05 1 Nick H nick@nickhammond.com Nick garage
"
hash_body = Hash.from_xml(xml_body)

url = "https://abc:X@test-account.chargify.com/subscriptions.xml" 

# stubbed both the xml version and the hash version
stub_http_request(:post, url).with(:body => hash_body)
stub_http_request(:post, url).with(:body => xml_body)

res = Net::HTTP.post_form(URI.parse(url), hash_body)
res = Net::HTTP.post_form(URI.parse(url), xml_body)

Webmock.should have_requested(:post, url).with(:body => hash_body)
Webmock.should have_requested(:post, url).with(:body => xml_body)

end

end

Fails on the initial post_form request since it doesn't see it as stubbed yet.

1) WebMock::NetConnectNotAllowedError in 'webmock should stub the request' Real HTTP connections are disabled. Unregistered request: POST http://abc:X@test-account.chargify.com:443/subscriptions.xml with body 'subscription=credit_card_attributesexpiration_year2015full_number1last_nameHexpiration_month05first_nameNickcustomer_attributeslast_nameHfirst_nameNickemailnick%40nickhammond.comproducthandlegarage' with headers {'Accept'=>'/_', 'Content-Type'=>'application/x-www-form-urlencoded'} ./test-webmock.rb:36:

nickhammond commented 13 years ago

hmm. If I change the url to "http" instead of "https" then my tests pass. Looking into it further..

nickhammond commented 13 years ago

Got it passing with the single spec, just not working with the chargify gem. Going to close this since everything seems to be working as expected on webmock's side. Thanks.

bblimke commented 13 years ago

Hi.

Sorry for the late reply but I was away. Have you made it working? Can I help in anyway?

Bartosz

nickhammond commented 13 years ago

I got it working, I had to specify it as a hash instead of the raw XML like I had for the body.

Is there a way to do regexes in the response? For instance, to just match a number within a field (\d+)?

bblimke commented 13 years ago

I'm not sure what you mean by doing regexes in the response. Can you show an example of what you want to achieve?