Sha256: 0645f24af73230a8ccc795d265d6f48a312d86dba31982b9e1749b662d4fb2bb

Contents?: true

Size: 1.99 KB

Versions: 4

Compression:

Stored size: 1.99 KB

Contents

# Boots a single Capybara::Server for a Rack application that delegates to another, singleton Rack
# application that can be configured for each spec.

require 'sinatra/base'

module AppRunner
  class << self
    attr_accessor :app, :app_host
  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
  end

  def run_application(app)
    AppRunner.app = app
  end

  def driver_for_app(&body)
    app = Class.new(ExampleApp, &body)
    run_application app
    build_driver
  end

  def driver_for_html(html, *driver_args)
    run_application_for_html html
    build_driver(*driver_args)
  end

  def session_for_app(&body)
    app = Class.new(ExampleApp, &body)
    run_application app
    Capybara::Session.new(:reusable_webkit, AppRunner.app)
  end

  def run_application_for_html(html)
    run_application lambda { |env|
      [200, { 'Content-Type' => 'text/html', 'Content-Length' => html.size.to_s }, [html]]
    }
  end

  private

  def build_driver(browser = $webkit_browser)
    Capybara::Webkit::Driver.new(AppRunner.app, :browser => browser)
  end

  def self.included(example_group)
    example_group.class_eval do
      before { AppRunner.reset }
      after { $webkit_browser.reset! }
    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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
capybara-webkit-1.5.1 spec/support/app_runner.rb
capybara-webkit-1.5.0 spec/support/app_runner.rb
capybara-webkit-1.4.1 spec/support/app_runner.rb
capybara-webkit-1.4.0 spec/support/app_runner.rb