[英]How to register a request stub using 80 port with webmock?
I am using webmock to stub and set expectations on HTTP requests in Ruby. 我正在使用webmock对Ruby中的HTTP请求进行存根并设置期望。 Also I am using
rspec
. 我也在用
rspec
。
Here is my spec/feature/***.rb
's code: 这是我的
spec/feature/***.rb
的代码:
require 'webmock/rspec'
require 'net/http'
require 'uri'
# ...
it 'Request third-part API' do
stub_request(:get, "http://third_part_api_path/api/param=value")
Net::HTTP.get("http://third_part_api_path", "/api/param=100")
expect(WebMock).to have_requested(:get, "http://third_part_api_path/api/param=100")
end
But after I run my rspec test code, the error was: 但是在我运行rspec测试代码后,错误是:
Failure/Error: Net::HTTP.get(@uri, "/api/param=100”)
WebMock::NetConnectNotAllowedError:
Real HTTP connections are disabled. Unregistered request: GET http://http//third_part_api_path:80/api/param=100 with headers {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}
You can stub this request with the following snippet:
stub_request(:get, "http://http//third_part_api_path:80/api/param=100”).
with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}).
to_return(:status => 200, :body => "", :headers => {})
registered request stubs:
stub_request(:get, "http://http//third_part_api_path/api/param=100”)
============================================================
you don't need the protocol: http://
part of the URL for the Net::HTTP.get
- just the bare URL 您不需要协议:
Net::HTTP.get
URL的http://
部分-只需裸URL
eg 例如
Net::HTTP.get("third_part_api_path", "/api/param=100")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.