我的问题是我正在创建一个包装器来处理与外部服务器(GeoNetwork)的连接并处理它的响应。
虽然我的搜索控制器中有我的请求,但它工作正常。然后我尝试为包装器遵循这个解决方案:
http://code.tutsplus.com/articles/writing-an-api-wrapper-in-ruby-with-tdd--net-23875
但是现在,我在 webmock 上有一个错误,我不知道为什么。
../lib/wrapper/api.rb
module Wrapper
class Api
class << self
attr_accessor :base_uri
end
include HTTP
@base_uri = 'http://boldo.caiena.net:8080/geonetwork/srv/eng/'
builder_for_summary = Nokogiri::XML::Builder.new do |xml|
xml['csw'].GetRecords('xmlns:csw' => 'http://www.opengis.net/cat/csw/2.0.2',
'service' => 'CSW',
'version' => '2.0.2',
'resultType' => 'results',
# metadata records start at position 1
'startPosition' => '1',
'maxRecords' => '10') do
xml['csw'].Query('typeNames' => 'gmd:MD_Metadata') {
xml['csw'].Constraint('version' => '1.1.0'){
xml.Filter('xmlns' => 'http://www.opengis.net/ogc',
'xmlns:gml' => 'http://www.opengis.net/gml'){
xml.PropertyIsLike('wildCard' => '',
'singleChar' => '_'){
xml.PropertyName 'any'
xml.Literal '' #params[:search_field]
}
}
}
}
end
end
response = HTTP.post("#{@base_uri}csw", body: builder_for_summary.to_xml)
.with_headers(content_type: "application/xml")
.response
end
end
../spec/lib/api_spec.rb
require 'spec_helper'
describe Wrapper::Api do
it 'should work' do
expect('Yay!').to be_an_instance_of String
end
describe 'default attributes' do
it 'should include http methods' do
expect(Wrapper::Api).to include HTTP
end
it 'should have the base url set to the GeoNetwork API endpoint' do
expect(Wrapper::Api.base_uri).to eq('http://boldo.caiena.net:8080/geonetwork/srv/eng/')
end
end
describe 'request' do
it 'makes a request to GeoNetwork' do
WebMock.should have_requested(:post, "#{Wrapper::Api.base_uri}csw")
end
end
end
../spec/spec_helper.rb
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require_relative '../config/environment'
require_relative '../lib/wrapper'
require 'rspec/rails'
require 'rspec/autorun'
require 'webmock/rspec'
WebMock.disable_net_connect!(allow_localhost: true)
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
# Checks for pending migrations before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)
RSpec.configure do |config|
config.before(:each) do
support_path = 'spec/support/fixtures/'
url = 'http://boldo.caiena.net:8080/geonetwork/srv/eng/'
canned_request = File.read "#{support_path}request_all_metadata.xml"
canned_response = File.read "#{support_path}all_metadata_results.xml"
stub_request(:post, "#{url}csw")
.with(header:'application/xml')
.to_return(body: canned_response)
end
# ## Mock Framework
#
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
#
# config.mock_with :mocha
# config.mock_with :flexmock
# config.mock_with :rr
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
# config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.fixture_path = "#{::Rails.root}/spec/fixtures"
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = true
# If true, the base class of anonymous controllers will be inferred
# automatically. This will be the default behavior in future versions of
# rspec-rails.
config.infer_base_class_for_anonymous_controllers = false
# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
# --seed 1234
config.order = "random"
# Enabling FactoryGirl methods with ease
config.include FactoryGirl::Syntax::Methods
config.include Capybara::DSL
end
我得到的错误是:
/Users/thiagoalves/.rvm/gems/ruby-2.1.0@copernico/gems/webmock-1.17.4/lib/webmock/http_lib_adapters/http_gem_adapter.rb:125:in `halt': Real HTTP connections are disabled. Unregistered request: POST http://boldo.caiena.net:8080/geonetwork/srv/eng/csw with body '<?xml version="1.0"?> (WebMock::NetConnectNotAllowedError)
<csw:GetRecords xmlns:csw="http://www.opengis.net/cat/csw/2.0.2" service="CSW" version="2.0.2" resultType="results" startPosition="1" maxRecords="10">
<csw:Query typeNames="gmd:MD_Metadata">
<csw:Constraint version="1.1.0">
<Filter xmlns="http://www.opengis.net/ogc" xmlns:gml="http://www.opengis.net/gml">
<PropertyIsLike wildCard="" singleChar="_">
<PropertyName>any</PropertyName>
<Literal/>
</PropertyIsLike>
</Filter>
</csw:Constraint>
</csw:Query>
</csw:GetRecords>
' with headers {'Host'=>'boldo.caiena.net', 'User-Agent'=>'RubyHTTPGem/0.5.0'}
You can stub this request with the following snippet:
stub_request(:post, "http://boldo.caiena.net:8080/geonetwork/srv/eng/csw").
with(:body => "<?xml version=\"1.0\"?>\n<csw:GetRecords xmlns:csw=\"http://www.opengis.net/cat/csw/2.0.2\" service=\"CSW\" version=\"2.0.2\" resultType=\"results\" startPosition=\"1\" maxRecords=\"10\">\n <csw:Query typeNames=\"gmd:MD_Metadata\">\n <csw:Constraint version=\"1.1.0\">\n <Filter xmlns=\"http://www.opengis.net/ogc\" xmlns:gml=\"http://www.opengis.net/gml\">\n <PropertyIsLike wildCard=\"\" singleChar=\"_\">\n <PropertyName>any</PropertyName>\n <Literal/>\n </PropertyIsLike>\n </Filter>\n </csw:Constraint>\n </csw:Query>\n</csw:GetRecords>\n",
:headers => {'Host'=>'boldo.caiena.net', 'User-Agent'=>'RubyHTTPGem/0.5.0'}).
to_return(:status => 200, :body => "", :headers => {})
============================================================
from /Users/thiagoalves/.rvm/gems/ruby-2.1.0@copernico/gems/webmock-1.17.4/lib/webmock/http_lib_adapters/http_gem_adapter.rb:76:in `exec'
from /Users/thiagoalves/.rvm/gems/ruby-2.1.0@copernico/gems/webmock-1.17.4/lib/webmock/http_lib_adapters/http_gem_adapter.rb:146:in `perform'
from /Users/thiagoalves/.rvm/gems/ruby-2.1.0@copernico/gems/http-0.5.0/lib/http/client.rb:58:in `request'
from /Users/thiagoalves/.rvm/gems/ruby-2.1.0@copernico/gems/http-0.5.0/lib/http/chainable.rb:50:in `request'
from /Users/thiagoalves/.rvm/gems/ruby-2.1.0@copernico/gems/http-0.5.0/lib/http/chainable.rb:15:in `post'
from /Users/thiagoalves/Workspace/copernico-ide/copernico/lib/wrapper/api.rb:34:in `<class:Api>'
from /Users/thiagoalves/Workspace/copernico-ide/copernico/lib/wrapper/api.rb:2:in `<module:Wrapper>'
from /Users/thiagoalves/Workspace/copernico-ide/copernico/lib/wrapper/api.rb:1:in `<top (required)>'
from /Users/thiagoalves/.rvm/gems/ruby-2.1.0@copernico/gems/activesupport-4.0.3/lib/active_support/dependencies.rb:229:in `require'
from /Users/thiagoalves/.rvm/gems/ruby-2.1.0@copernico/gems/activesupport-4.0.3/lib/active_support/dependencies.rb:229:in `block in require'
from /Users/thiagoalves/.rvm/gems/ruby-2.1.0@copernico/gems/activesupport-4.0.3/lib/active_support/dependencies.rb:214:in `load_dependency'
from /Users/thiagoalves/.rvm/gems/ruby-2.1.0@copernico/gems/activesupport-4.0.3/lib/active_support/dependencies.rb:229:in `require'
from /Users/thiagoalves/Workspace/copernico-ide/copernico/lib/wrapper.rb:4:in `block in <top (required)>'
from /Users/thiagoalves/Workspace/copernico-ide/copernico/lib/wrapper.rb:3:in `each'
from /Users/thiagoalves/Workspace/copernico-ide/copernico/lib/wrapper.rb:3:in `<top (required)>'
from /Users/thiagoalves/Workspace/copernico-ide/copernico/spec/spec_helper.rb:5:in `require_relative'
from /Users/thiagoalves/Workspace/copernico-ide/copernico/spec/spec_helper.rb:5:in `<top (required)>'
from /Users/thiagoalves/Workspace/copernico-ide/copernico/spec/controllers/search_controller_spec.rb:1:in `require'
from /Users/thiagoalves/Workspace/copernico-ide/copernico/spec/controllers/search_controller_spec.rb:1:in `<top (required)>'
from /Users/thiagoalves/.rvm/gems/ruby-2.1.0@copernico/gems/rspec-core-2.14.8/lib/rspec/core/configuration.rb:896:in `load'
from /Users/thiagoalves/.rvm/gems/ruby-2.1.0@copernico/gems/rspec-core-2.14.8/lib/rspec/core/configuration.rb:896:in `block in load_spec_files'
from /Users/thiagoalves/.rvm/gems/ruby-2.1.0@copernico/gems/rspec-core-2.14.8/lib/rspec/core/configuration.rb:896:in `each'
from /Users/thiagoalves/.rvm/gems/ruby-2.1.0@copernico/gems/rspec-core-2.14.8/lib/rspec/core/configuration.rb:896:in `load_spec_files'
from /Users/thiagoalves/.rvm/gems/ruby-2.1.0@copernico/gems/rspec-core-2.14.8/lib/rspec/core/command_line.rb:22:in `run'
from /Users/thiagoalves/.rvm/gems/ruby-2.1.0@copernico/gems/rspec-core-2.14.8/lib/rspec/core/runner.rb:80:in `run'
from /Users/thiagoalves/.rvm/gems/ruby-2.1.0@copernico/gems/rspec-core-2.14.8/lib/rspec/core/runner.rb:17:in `block in autorun'