2012-07-08 16:38:19 -04:00
|
|
|
# Boots a single Capybara::Server for a Rack application that delegates to another, singleton Rack
|
2012-11-16 23:44:03 -05:00
|
|
|
# application that can be configured for each spec.
|
2012-07-08 16:38:19 -04:00
|
|
|
|
2012-07-08 19:09:40 -04:00
|
|
|
require 'sinatra/base'
|
|
|
|
|
2012-07-08 16:38:19 -04:00
|
|
|
module AppRunner
|
|
|
|
class << self
|
2015-04-10 17:00:32 -04:00
|
|
|
attr_accessor :app, :app_host, :configuration
|
2012-07-08 16:38:19 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.boot
|
|
|
|
app_container = lambda { |env| AppRunner.app.call(env) }
|
|
|
|
server = Capybara::Server.new(app_container)
|
|
|
|
server.boot
|
|
|
|
self.app_host = "http://127.0.0.1:#{server.port}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.reset
|
|
|
|
self.app = lambda do |env|
|
|
|
|
[200, { 'Content-Type' => 'html', 'Content-Length' => 0 }, []]
|
|
|
|
end
|
2015-04-10 17:00:32 -04:00
|
|
|
|
|
|
|
self.configuration = Capybara::Webkit::Configuration.new
|
2012-07-08 16:38:19 -04:00
|
|
|
end
|
|
|
|
|
2012-07-08 19:09:40 -04:00
|
|
|
def run_application(app)
|
2012-07-08 16:38:19 -04:00
|
|
|
AppRunner.app = app
|
|
|
|
end
|
|
|
|
|
2015-04-10 17:00:32 -04:00
|
|
|
def configure
|
|
|
|
yield AppRunner.configuration
|
|
|
|
end
|
|
|
|
|
2015-06-05 13:29:26 -04:00
|
|
|
def driver_for_app(*driver_args, &body)
|
2012-07-08 19:09:40 -04:00
|
|
|
app = Class.new(ExampleApp, &body)
|
|
|
|
run_application app
|
2015-06-05 13:29:26 -04:00
|
|
|
build_driver(*driver_args)
|
2012-07-08 19:09:40 -04:00
|
|
|
end
|
|
|
|
|
2014-11-14 14:55:14 -05:00
|
|
|
def driver_for_html(html, *driver_args)
|
2012-07-09 20:34:09 -04:00
|
|
|
run_application_for_html html
|
2014-11-14 14:55:14 -05:00
|
|
|
build_driver(*driver_args)
|
2012-07-08 19:09:40 -04:00
|
|
|
end
|
|
|
|
|
2013-11-09 13:42:27 -05:00
|
|
|
def session_for_app(&body)
|
|
|
|
app = Class.new(ExampleApp, &body)
|
|
|
|
run_application app
|
|
|
|
Capybara::Session.new(:reusable_webkit, AppRunner.app)
|
|
|
|
end
|
|
|
|
|
2012-07-09 20:34:09 -04:00
|
|
|
def run_application_for_html(html)
|
|
|
|
run_application lambda { |env|
|
|
|
|
[200, { 'Content-Type' => 'text/html', 'Content-Length' => html.size.to_s }, [html]]
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2012-07-08 19:09:40 -04:00
|
|
|
private
|
|
|
|
|
2015-04-10 17:00:32 -04:00
|
|
|
def build_driver(overrides = {})
|
|
|
|
options = AppRunner.configuration.
|
|
|
|
to_hash.
|
|
|
|
merge(browser: $webkit_browser).
|
|
|
|
merge(overrides)
|
|
|
|
Capybara::Webkit::Driver.new(AppRunner.app, options)
|
2012-07-08 16:38:19 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.included(example_group)
|
|
|
|
example_group.class_eval do
|
|
|
|
before { AppRunner.reset }
|
2012-11-28 21:06:04 -05:00
|
|
|
after { $webkit_browser.reset! }
|
2012-07-08 16:38:19 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-07-08 19:09:40 -04:00
|
|
|
class ExampleApp < Sinatra::Base
|
|
|
|
# Sinatra fixes invalid responses that would break QWebPage, so this middleware breaks them again
|
|
|
|
# for testing purposes.
|
|
|
|
class ResponseInvalidator
|
|
|
|
def initialize(app)
|
|
|
|
@app = app
|
|
|
|
end
|
|
|
|
|
|
|
|
def call(env)
|
|
|
|
response = @app.call(env)
|
|
|
|
if response.to_a[1]['X-Response-Invalid']
|
|
|
|
[404, {}, []]
|
|
|
|
else
|
|
|
|
response
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
use ResponseInvalidator
|
|
|
|
|
|
|
|
def invalid_response
|
|
|
|
[200, { 'X-Response-Invalid' => 'TRUE' }, []]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-07-08 16:38:19 -04:00
|
|
|
AppRunner.boot
|