I want to let one of my controllers make an http request from within my controller, to "get" and parse an xml, that I can then display in my view. Sounds like an easy task..
我想让我的一个控制器从我的控制器中发出一个http请求,“获取”并解析一个xml,然后我可以在我的视图中显示。听起来很容易......
require 'net/http'
class Profile::EventTypesController < ProfileController
def index
url = URI.parse('instagram.com')
req = Net::HTTP.new(url, 80)
res = req.get('/my_account')
xml = res.body
@feed = Feedjira::Feed.parse xml
...
...
end
end
As soon as I try it out it wont work, showing the following exception:
一旦我尝试它就不会工作,显示以下异常:
WebMock::NetConnectNotAllowedError at /de
Real HTTP connections are disabled. Unregistered request: GET http://instagram.com/my_account with headers {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Ruby'}
stub_request(:get, "http://instagram.com/partyguerilla").
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: {})
Is there any way to enable "REAL HTTP connections" from within my controller? When I google, it is all about mocking stubs and doing this from within tests.. Did someone experience the same before?
有没有办法在我的控制器中启用“REAL HTTP连接”?当我谷歌时,它是关于嘲笑存根并在测试中做到这一点。有人经历过同样的事情吗?
thx
谢谢
1 个解决方案
#1
1
Check your Gemfile for gem webmock
. It should be included only within the test group, not development.
检查您的Gemfile以获取gem webmock。它应该只包含在测试组中,而不是开发中。
group :test do
gem 'webmock'
end
the same applies to rspec-rails
gem. See this issue
这同样适用于rspec-rails gem。看到这个问题
#1
1
Check your Gemfile for gem webmock
. It should be included only within the test group, not development.
检查您的Gemfile以获取gem webmock。它应该只包含在测试组中,而不是开发中。
group :test do
gem 'webmock'
end
the same applies to rspec-rails
gem. See this issue
这同样适用于rspec-rails gem。看到这个问题