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

Already on GitHub? Sign in to your account

Not able to stub request #51

Closed
nickhammond opened this Issue Oct 15, 2010 · 8 comments

Comments

Projects
None yet
2 participants

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.

Owner

bblimke commented Oct 16, 2010

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.

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.

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 = "


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.

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.comproduct_handlegarage' with headers {'Accept'=>'/', 'Content-Type'=>'application/x-www-form-urlencoded'}
./test-webmock.rb:36:

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

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.

Owner

bblimke commented Oct 25, 2010

Hi.

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

Bartosz

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+)?

Owner

bblimke commented Oct 27, 2010

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

This issue was closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment