webmock

Stub Faraday request with Webmock - help needed

北城以北 提交于 2020-06-13 09:25:08
问题 I need help stubbing a request using Faraday gem. I am making this request URL='https://secure.snd.payu.com//pl/standard/user/oauth/authorize'.freeze url_encoded = 'grant_type=client_credentials' \ + "&client_id=#{ENV['client_id'}" \ + "&client_secret=#{ENV['client_secret'}" connection = Faraday.new do |con| con.response :oj, content_type: /\bjson$/ con.adapter Faraday.default_adapter end connection.post(URL, url_encoded) which outputs #<Faraday::Response:0x00000000016ff620 @on_complete

WebMock simulate failing API (no internet, timeout ++)

北城余情 提交于 2020-01-23 04:37:15
问题 I am trying to simulate unexpected behaviour from a web api, such as not finding the server and timeouts, using webmock. What would be the best way to do this? All I can think of is to do something like this: stubbed_request = stub_request(:get, "#{host}/api/something.json"). with(:headers => {'Accept'=>'*/*', 'Content-Type'=>'application/json', 'User-Agent'=>'Ruby'}). to_return(:status => [500, "Internal Server Error"]) That should work for things like 404 etc., but how can I test timeouts ,

How can I test a Ruby command-line program that communicates with a web service?

假如想象 提交于 2019-12-22 08:20:12
问题 I am building a Ruby command-line program that communicates with a web service. I am using Cucumber and Aruba to test the program. The problem is that I need to control the data returned from the web service; the program grabs a stream of user comments, so this can change frequently as new comments are added. I tried to mock the web service using WebMock, but this didn't work, since Aruba spins the command-line program off into a separate process that is unaffected by WebMock (so it still

Using webmock to mock XMLRPC client in rspec-rails

一曲冷凌霜 提交于 2019-12-11 19:41:22
问题 I'm trying to spec a client that goes through the following series of calls via XMLRPC: post (https) myservice.auth.login (with params login, password) post (http) myservice.items.list (with sessionid from login) I'm trying to mock these so I don't have to hit a production XMLRPC service, so I did this: require 'spec_helper' describe ItemList do it "hydrates the model" do stub_request(:post, 'secure-api.myservice.com/webservices/xmlrpc/'). with(:body =~ /myservice.auth.login/m). to_return(

How to use webmock regex matcher?

两盒软妹~` 提交于 2019-12-06 16:44:38
问题 How to match a URL like: http://www.example.com/foo/:id/bar http://www.example.com/foo/1/bar http://www.example.com/foo/999/bar stub_request(:post, "www.example.com") 回答1: http://www\..*?\.com/foo/\d+/bar should work for you. 回答2: You can use %r{} instead of // for your regular expression in Ruby to avoid having to escape the forward slashes in URLs. For example: stub_request(:post, %r{\Ahttp://www.example.com/foo/\d+/bar\z}) 回答3: The second argument to stub_request must be a regular

How can I test a Ruby command-line program that communicates with a web service?

偶尔善良 提交于 2019-12-05 12:57:57
I am building a Ruby command-line program that communicates with a web service. I am using Cucumber and Aruba to test the program. The problem is that I need to control the data returned from the web service; the program grabs a stream of user comments, so this can change frequently as new comments are added. I tried to mock the web service using WebMock, but this didn't work, since Aruba spins the command-line program off into a separate process that is unaffected by WebMock (so it still communicated with the real web service). How can I test the output of this program using Cucumber? Edit:

Webmock stub request with any body and header

点点圈 提交于 2019-12-04 16:27:59
问题 How to use Webmock to stub request with any body and header? I tried to use regex WebMock.stub_request(:post, 'api.quickblox.com/').with(:body => /.*?/, :headers => /.*?/).to_return(:status => 201, :body => "", :headers => {}) in rspec but it doesn't work, it has NoMethodError: undefined method `map' for /.*?/:Regexp 回答1: Check out the Webmock docs on headers. You need to provide it a hash with further details on what you are matching. 回答2: You should be able to just use WebMock.stub_request(