Created
June 30, 2014 08:28
-
-
Save bagilevi/8227dc70cf2d0718dcb5 to your computer and use it in GitHub Desktop.
Capybara - following local redirects only
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 CapybaraExtras | |
| def browser | |
| Capybara.current_session.driver.browser | |
| end | |
| def response | |
| @response | |
| end | |
| def redirect_url | |
| browser.last_response.headers["Location"] | |
| end | |
| module FollowLocalRedirectsOnly | |
| def self.included(base) | |
| base.instance_eval do | |
| include CapybaraExtras | |
| before(:each) do | |
| browser.extend(FollowLocalRedirectsOnly::BrowserPatch) | |
| end | |
| end | |
| end | |
| module BrowserPatch | |
| def process_and_follow_redirects(method, path, attributes = {}, env = {}) | |
| process(method, path, attributes, env) | |
| if driver.follow_redirects? | |
| redirected_within_site = nil | |
| driver.redirect_limit.times do | |
| if last_response.redirect? | |
| redirected_within_site = last_response['Location'].start_with?('http://www.example.com/') | |
| if ! redirected_within_site | |
| break # don't redirect to external sites | |
| end | |
| process(:get, last_response["Location"], {}, env) | |
| end | |
| end | |
| if last_response.redirect? && redirected_within_site | |
| raise Capybara::InfiniteRedirectError, "redirected more than #{driver.redirect_limit} times, check for infinite redirects." | |
| end | |
| end | |
| end | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment