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_callbacks=[], @env=#<Faraday::Env @method=:post @body={"access_token"=>"00a4e007-220b-4119-aae8-3cb93bb36066", "token_type"=>"bearer", "expires_in"=>43199, "grant_type"=>"client_credentials"} @url=#<URI::HTTPS https://secure.snd.payu.com/pl/standard/user/oauth/authorize> @request=#<Faraday::RequestOptions (empty)> @request_headers={"User-Agent"=>"Faraday v0.17.1"} @ssl=#<Faraday::SSLOptions verify=true> @response=#<Faraday::Response:0x00000000016ff620 ...> @response_headers={"set-cookie"=>"cookieFingerprint=70a4a8d1-7b05-4cb9-9d5c-5ad12e966586; Expires=Fri, 25-Dec-2020 09:31:02 GMT; Path=/; ; HttpOnly, payu_persistent=mobile_agent-false#; Expires=Sun, 20-Dec-2020 09:31:02 GMT; Path=/; ; HttpOnly", "correlation-id"=>"0A4DC804-62FD_AC11000F-0050_5E047DD5_8A0178-0015", "cache-control"=>"no-store, no-cache, no-store, must-revalidate", "pragma"=>"no-cache, no-cache", "content-type"=>"application/json;charset=UTF-8", "transfer-encoding"=>"chunked", "date"=>"Thu, 26 Dec 2019 09:31:01 GMT", "server"=>"Apache", "x-content-type-options"=>"nosniff", "x-frame-options"=>"SAMEORIGIN", "x-xss-protection"=>"1; mode=block", "expires"=>"0", "connection"=>"close"} @status=200 @reason_phrase="OK">>
 => #<Faraday::Response:0x00000000016ff620 @on_complete_callbacks=[], @env=#<Faraday::Env @method=:post @body={"access_token"=>"00a4e007-220b-4119-aae8-3cb93bb36066", "token_type"=>"bearer", "expires_in"=>43199, "grant_type"=>"client_credentials"} @url=#<URI::HTTPS https://secure.snd.payu.com/pl/standard/user/oauth/authorize> @request=#<Faraday::RequestOptions (empty)> @request_headers={"User-Agent"=>"Faraday v0.17.1"} @ssl=#<Faraday::SSLOptions verify=true> @response=#<Faraday::Response:0x00000000016ff620 ...> @response_headers={"set-cookie"=>"cookieFingerprint=70a4a8d1-7b05-4cb9-9d5c-5ad12e966586; Expires=Fri, 25-Dec-2020 09:31:02 GMT; Path=/; ; HttpOnly, payu_persistent=mobile_agent-false#; Expires=Sun, 20-Dec-2020 09:31:02 GMT; Path=/; ; HttpOnly", "correlation-id"=>"0A4DC804-62FD_AC11000F-0050_5E047DD5_8A0178-0015", "cache-control"=>"no-store, no-cache, no-store, must-revalidate", "pragma"=>"no-cache, no-cache", "content-type"=>"application/json;charset=UTF-8", "transfer-encoding"=>"chunked", "date"=>"Thu, 26 Dec 2019 09:31:01 GMT", "server"=>"Apache", "x-content-type-options"=>"nosniff", "x-frame-options"=>"SAMEORIGIN", "x-xss-protection"=>"1; mode=block", "expires"=>"0", "connection"=>"close"} @status=200 @reason_phrase="OK">> 

I am stubbing this request like this

stub_request(:post, 'https://secure.payu.com/pl/standard/user/oauth/authorize')
  .with(
         body: 'grant_type=client_credentials&client_id=10&client_secret=absdef',
         headers: { 'Content-Type' => 'application/x-www-form-urlencoded' }
       )
  .to_return(status: 200, body: '{"status":"SUCCESS"}')

and I am getting this error.

WebMock::NetConnectNotAllowedError:
       Real HTTP connections are disabled. Unregistered request: POST https://secure.payu.com/pl/standard/user/oauth/authorize with body 'grant_type=client_credentials&client_id=12345&client_secret=secret_client' with headers {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Faraday v0.17.1'}

       You can stub this request with the following snippet:

       stub_request(:post, "https://secure.payu.com/pl/standard/user/oauth/authorize").
         with(
           body: "grant_type=client_credentials&client_id=12345&client_secret=secret_client",
           headers: {
          'Accept'=>'*/*',
          'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
          'User-Agent'=>'Faraday v0.17.1'
           }).
         to_return(status: 200, body: "", headers: {})

       registered request stubs:

       stub_request(:post, "https://secure.payu.com/pl/standard/user/oauth/authorize").
         with(
           body: "grant_type=client_credentials&client_id=10&client_secret=absdef",
           headers: {
          'Content-Type'=>'application/x-www-form-urlencoded'
           })

I don't know why it's complaining about the headers. From what I see in the output above, Faraday does not send the headers Webmock is poining at. Would you have any ideas how to solve this?


回答1:


Is it necessary to stub the request, or do you just need to mock the response? When I write Faraday client specs, I'll typically do something like this:

let(:client) {
  instance_double(Faraday::Connection, post: faraday_response)
}
let(:faraday_response) { instance_double(Faraday::Response, body: body) }
let(:body)             {
  {
    'some_key' => 'some_value'
  }
}

before do
  allow(Faraday).to receive(:new) { client } 
  allow(client).to receive(:post) { faraday_response }
end

it 'redirects to the place' do
  request
  expect(response).to redirect_to(some_path)
end

it 'changes the stuff' do
  expect { request }.to change { 
    something.reload.attribute 
  }.from(thing1).to(thing2)
end



来源:https://stackoverflow.com/questions/59486571/stub-faraday-request-with-webmock-help-needed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!