1
0
Fork 0
mirror of https://github.com/thoughtbot/capybara-webkit synced 2023-03-27 23:22:28 -04:00

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 include AppRunner
let(:driver) do let(:driver) do
driver_for_app do |env| driver_for_html(<<-HTML)
body = <<-HTML <html>
<html> <body>
<body> <h1>Hello World</h1>
<h1>Hello World</h1> </body>
</body> </html>
</html> HTML
HTML
[200,
{ 'Content-Type' => 'text/html', 'Content-Length' => body.length.to_s },
[body]]
end
end end
before(:each) do 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) connection = Capybara::Webkit::Connection.new(:socket_class => TCPSocket, :stdout => nil)
$webkit_browser = Capybara::Webkit::Browser.new(connection) $webkit_browser = Capybara::Webkit::Browser.new(connection)
if ENV['DEBUG']
$webkit_browser.enable_logging
end
RSpec.configure do |config| RSpec.configure do |config|
config.before { $webkit_browser.reset! } config.before { $webkit_browser.reset! }
end end

View file

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