Use sinatra for test apps

This commit is contained in:
Joe Ferris 2012-07-08 16:09:40 -07:00
parent 265a7da652
commit 327ae85e86
4 changed files with 519 additions and 599 deletions

View File

@ -6,18 +6,13 @@ describe Capybara::Webkit::Driver, "rendering an image" do
include AppRunner
let(:driver) do
driver_for_app do |env|
body = <<-HTML
<html>
<body>
<h1>Hello World</h1>
</body>
</html>
HTML
[200,
{ 'Content-Type' => 'text/html', 'Content-Length' => body.length.to_s },
[body]]
end
driver_for_html(<<-HTML)
<html>
<body>
<h1>Hello World</h1>
</body>
</html>
HTML
end
before(:each) do

File diff suppressed because it is too large Load Diff

View File

@ -25,6 +25,10 @@ require 'capybara/webkit'
connection = Capybara::Webkit::Connection.new(:socket_class => TCPSocket, :stdout => nil)
$webkit_browser = Capybara::Webkit::Browser.new(connection)
if ENV['DEBUG']
$webkit_browser.enable_logging
end
RSpec.configure do |config|
config.before { $webkit_browser.reset! }
end

View File

@ -1,6 +1,8 @@
# Boots a single Capybara::Server for a Rack application that delegates to another, singleton Rack
# application that can be configured for each spec. Also configures Capybara to use that server.
require 'sinatra/base'
module AppRunner
class << self
attr_accessor :app, :app_host
@ -19,13 +21,28 @@ module AppRunner
end
end
def run_application(&app)
def run_application(app)
AppRunner.app = app
end
def driver_for_app(&app)
run_application(&app)
Capybara::Webkit::Driver.new(app, :browser => $webkit_browser)
def driver_for_app(&body)
app = Class.new(ExampleApp, &body)
run_application app
build_driver
end
def driver_for_html(html)
app = lambda do |env|
[200, { 'Content-Type' => 'text/html', 'Content-Length' => html.size.to_s }, [html]]
end
run_application app
build_driver
end
private
def build_driver
Capybara::Webkit::Driver.new(AppRunner.app, :browser => $webkit_browser)
end
def self.included(example_group)
@ -46,4 +63,29 @@ module AppRunner
end
end
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
AppRunner.boot