thoughtbot / capybara-webkit

A Capybara driver for headless WebKit to test JavaScript web apps
https://thoughtbot.com/open-source
MIT License
1.97k stars 428 forks source link

Follow 308 redirects #1067

Closed DannyBen closed 6 years ago

DannyBen commented 6 years ago

Not sure if this is a capybara-webkit issue or a capybara issue, or someone elses...

It seems like capybara does not follow 308 redirects.

Related code snippets:

# Gemfile
source "https://rubygems.org"

gem 'cucumber', require: false
gem 'capybara', '~> 2.4.0'
gem 'capybara-webkit', '~> 1.3.1'
gem 'headless'
gem 'rspec-expectations'
# features/redirects.feature
Feature: Redirects

Scenario: 200
   When I visit "https://httpstat.us/200"
   Then I expect the status to be "200"
    And I should be on "/200"

Scenario: 301
   When I visit "https://httpstat.us/301"
   Then I should be on "/"

Scenario: 302
   When I visit "https://httpstat.us/302"
   Then I should be on "/"

# ---- This one fails ----
Scenario: 308
   When I visit "https://httpstat.us/308"
   Then I should be on "/"
# features/step_definitions/defs.rb
When(/^I (?:go to|visit) "([^"]*)"$/) do |path|
  visit path
end

Then(/^I expect the status to be "(.*?)"$/) do |status|
  expect(page.status_code.to_s).to eq(status)
end

Then(/^I should be on "(.*?)"$/) do |path|
  expect(current_path).to eq path
end

Failing with:

  Scenario: 308
    When I visit "https://httpstat.us/308"
    Then I should be on "/"

      expected: "/"
           got: "/308"
jferris commented 6 years ago

The redirect handling is part of QtWebKit, which we rely on for most network processing.

We do a little processing to track redirects, but we only look at the Location header as far as I can tell. This is likely a feature that isn't supported by the version of QtWebKit you're using. Unfortunately, it may not be supported by any version of QtWebKit, because QtWebKit development was largely abandoned years ago.

DannyBen commented 6 years ago

I see... is there a workaround for us peasants who still use it?... :) Perhaps I should catch 308 after calls to visit and follow redirect myself? Would that be feasible?

jferris commented 6 years ago

Yes, you could look at driver.status and driver.response_headers to verify that the page did what you expected.

DannyBen commented 6 years ago

Done. This seems to work.

When(/^I (?:go to|visit) "([^"]*)"$/) do |path|
  visit path
  if page.status_code == 308 and page.response_headers['Location']
    visit page.response_headers['Location']
  end
end

Thanks.

twalpole commented 6 years ago

@DannyBen Quick question about this. What version of Qt did you build capybara-webkit with? because my tests show that this should work with Qt 5.5

twalpole commented 6 years ago

Actually I take that back -- works for 307 not for 308

DannyBen commented 6 years ago

Actually I take that back -- works for 307 not for 308

You gave me hope.... then took it away... :) Thanks for checking.

twalpole commented 6 years ago

@DannyBen Sorry about that -- 308 wasn't added to the HTTP spec until 2015 so no release version of QtWebkit supports it.