工作下面的代碼工作webmock 1.20.4但不能與2.0.1stub_request與承載授權頭不是webmock 2
stub_request(:get, "http://www.myapi.com/my-endpoint")
.with(headers: {'Authorization' => "Bearer fake_oauth_token"})
.to_return(:body => mock_response)
這是我磕碰的代碼。
def get_stuff(oauth_token)
faraday = Faraday.new(:url => "http://www.myapi.com/my-endpoint", :ssl => {verify: false})
response = faraday.get do |req|
req.options[:timeout] = 10
req.headers['Authorization'] = "Bearer #{oauth_token}"
end
if response.status == 200
response.body
else
{error: "failed"}.to_json
end
end
使用assert_requested :get, "http://www.myapi.com/my-endpoint", :headers => {'Authorization' => "Bearer fake_oauth_token"}, :times => 1
和刪除stub_request我從斷言下面的輸出頭。
Failure/Error: assert_requested :get, "#{Conf.graphql[:host]}?query=#{graphql_user_details_query}", :headers => headers, :times => 1
The request GET http://www.myapi.com/my-endpoint with headers {'Authorization'=>'Bearer fake_oauth_token'} was expected to execute 1 time but it executed 0 times
The following requests were made:
GET http://www.myapi.com/my-endpoint with headers {'Accept-Encoding'=>'gzip, compressed', 'Authorization'=>'Basic QmVhcmVyIGZha2Vfb2F1dGhfdG9rZW4=', 'User-Agent'=>'Faraday v0.9.2'} was made 1 time
有沒有辦法讓stub_request代碼和webmock 2一起工作?