Last active
August 29, 2015 14:06
-
-
Save kurtsson/75a17499fb2d90687438 to your computer and use it in GitHub Desktop.
Serve a static build folder using Rack when running Capybara
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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