Skip to content

Instantly share code, notes, and snippets.

@kurtsson
Last active August 29, 2015 14:06
Show Gist options
  • Save kurtsson/75a17499fb2d90687438 to your computer and use it in GitHub Desktop.
Save kurtsson/75a17499fb2d90687438 to your computer and use it in GitHub Desktop.
Serve a static build folder using Rack when running Capybara
require 'rack_directory_index.rb'
$documentRoot = File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'build'))
Capybara.app = Rack::Builder.new do |builder|
puts "Creating static rack server serving #{$documentRoot}"
use Rack::DirectoryIndex
run Rack::Directory.new($documentRoot)
end
Capybara.configure do |config|
config.run_server = true
end
module Rack
class DirectoryIndex
def initialize(app)
@app = app
end
def call(env)
index_path = ::File.join($documentRoot, Rack::Request.new(env).path.split('/'), 'index.html')
if ::File.exists?(index_path)
return [200, {"Content-Type" => "text/html"}, [::File.read(index_path)]]
else
@app.call(env)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment