存根法拉第请求与网络模拟 - 需要帮助



>我需要帮助使用法拉第宝石存根请求。我提出这个要求

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)

哪些输出

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

我像这样刺痛这个请求

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"}')

我收到此错误。

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'
})

我不知道为什么它抱怨标题。从我在上面的输出中看到的,法拉第不会发送 Webmock 指向的标头。你有什么想法如何解决这个问题吗?

是有必要存根请求,还是只需要模拟响应?当我编写 Faraday 客户端规范时,我通常会做这样的事情:

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

相关内容

  • 没有找到相关文章

最新更新