Skip to content

Instantly share code, notes, and snippets.

@mjtko
Forked from goosetav/howto.md
Created November 2, 2012 18:56
Show Gist options
  • Save mjtko/4003597 to your computer and use it in GitHub Desktop.
Save mjtko/4003597 to your computer and use it in GitHub Desktop.
prevent ttf fonts while running poltergeist

RE: teampoltergeist/poltergeist#44

Use Rack::SimpleEndpoint to prevent Poltergeist/PhantomJS from crashing on TTF fonts.

Create config/initializers/middleware-deny-ttf-to-phantomjs-under-test.rb:

if Rails.env.test?
  require 'rack/contrib/simple_endpoint'
  Rails.application.config.middleware.insert_after Rack::Runtime, Rack::SimpleEndpoint, /\.ttf$/ do |req, res|
    ua = req.env['HTTP_USER_AGENT']
    if ua =~ /Intel Mac OS X.*PhantomJS/
      res.status = 403
      "Denying #{req.fullpath} to #{ua}"
    else
      :pass
    end
  end
end

and add to your Gemfile (in the :test group ideally)

  group :test do
    # used for Rack::SimpleEndpoint in config/environments/test.rb (for
    # PhantomJS crash workaround)
    gem 'rack-contrib'
  end

NOTE: This only works if assets are being served through rails while running tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment