我使用Capybara和WebMock以及代理(Sinatra)来测试远程应用程序。 我没有存根请求,而是使用WebMock断言请求。我已将代理分配给capybara.app并添加到Chrome驱动程序中,以便将请求转发给代理。
我的问题是,有时候我有待处理的请求会引发以下错误:
Failure/Error: raise "Requests did not finish in 60 seconds"
我想知道如何禁用此错误? 另外,如何更改60的硬编码超时(无论如何都会阻止测试的继续)
Timeout.timeout(60) { sleep(0.01) while @middleware.pending_requests? }
capybara.rb:
require 'capybara/rspec'
require 'capybara'
require 'capybara/dsl'
require_relative 'sinatra_proxy'
require 'selenium/webdriver'
require 'selenium/webdriver/remote/http/curb' if !isWindows
Capybara.server_port = 9980
Capybara.register_driver :selenium_chrome do |app|
http_client = isWindows ? nil : Selenium::WebDriver::Remote::Http::Curb.new
options = {
http_client: http_client,
browser: :chrome,
switches: [
"--proxy-server=0.0.0.0:9980",
"--disable-web-security",
'--user-agent="Chrome under Selenium for Capybara"',
"--start-maximized",
'--no-sandbox',
]
}
Capybara::Selenium::Driver.new app, options
end
Capybara.default_driver = :selenium_chrome
Capybara.app = SinatraProxy.new
Capybara.server_host = '0.0.0.0'
Capybara.default_max_wait_time = 8
Sinatra代理:
require "sinatra"
require 'net/http'
require 'json'
file = File.read 'config.json'
config_json = JSON.parse(file)
HOST = 'remote_app'
PORT = '80'
HEADERS_NOT_TO_PROXY = %w(transfer-encoding)
class SinatraProxy < Sinatra::Base
# configure :development do
# register Sinatra::Reloader
# end
def request_headers
request.env.select {|k,v| k.start_with? 'HTTP_'}
.collect {|pair| [pair[0].sub(/^HTTP_/, ''), pair[1]]}
.to_h # Ruby 2.1
.merge('CONTENT-TYPE' => request.env['CONTENT_TYPE'] || 'application/json')
end
proxy = lambda do
# puts "REQUEST HEADERS #{request_headers}"
uri = URI.parse(request.url)
http = Net::HTTP.new(HOST, PORT)
response = http.send_request(
request.request_method.upcase,
uri.request_uri,
request.body.read,
request_headers)
response_headers = {}
response.to_hash.each{|k,v| response_headers[k]=v.join unless HEADERS_NOT_TO_PROXY.include?(k) }
status response.code
headers response_headers
headers 'Access-Control-Allow-Origin' => '*'
# puts "RESPONSE HEADERS #{response_headers}, BODY: #{response.body}"
body response.body
end
get '/*', &proxy
post '/*', &proxy
patch '/*', &proxy
put '/*', &proxy
delete '/*', &proxy
options "*" do
headers 'Access-Control-Allow-Origin' => '*',
'Access-Control-Allow-Methods' => 'HEAD,GET,PUT,DELETE,OPTIONS'
halt 200
end
end
答案 0 :(得分:0)
您无法在重置期间更改等待的超时,但是您不应该为您的代理应用设置Capybara.app
。 Capybara.app
适用于您的AUT(正在测试的应用程序),其中任何在被告知退出后挂起超过60秒的请求肯定是错误的。由于您的代理不是您的AUT,只需单独运行它并告诉Capybara不要运行服务器/应用程序。
Capybara.run_server = false
Thread.new do
Rack::Handler::Puma.run(SinatraProxy.new, Host: '0.0.0.0', Port: 9980) # You might need/want other options here or to use thin/webrick/etc
end