Forked from mcmire/bypass_broken_images_middleware.rb
Created
September 18, 2018 16:34
-
-
Save supairish/1d62e625a673bd9e0af27009e9f6c122 to your computer and use it in GitHub Desktop.
Ignore requests for broken images in Capybara tests
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
# Instructions | |
# ------------ | |
# | |
# * Save this as app/middlewares/bypass_broken_images_middleware.rb | |
# * Add the following inside of the Rails.application.configure block | |
# in config/environments/test.rb: | |
# | |
# config.middleware.insert_before( | |
# ActionDispatch::DebugExceptions, | |
# BypassBrokenImagesMiddleware, | |
# ) | |
class BypassBrokenImagesMiddleware | |
def initialize(app) | |
@app = app | |
end | |
def call(env) | |
@app.(env) | |
rescue => error | |
if unknown_path?(error) && request_for_image?(env) | |
# nothing to see here, move along | |
[404, {}, ""] | |
else | |
raise error | |
end | |
end | |
private | |
def unknown_path?(error) | |
error.is_a?(ActionController::RoutingError) | |
end | |
def request_for_image?(env) | |
env["PATH_INFO"] =~ %r{\A/(assets|images)/} || | |
env["PATH_INFO"] =~ /\.(jpg|jpeg|png)\Z/ | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment