Skip to content

Instantly share code, notes, and snippets.

@shime
Last active October 24, 2016 04:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shime/6129865 to your computer and use it in GitHub Desktop.
Save shime/6129865 to your computer and use it in GitHub Desktop.
describes redirection issue from stackoverflow.com/a/9627796/726020

Run it with:

curl -fsSL https://gist.github.com/shime/6129865/raw/try_it.sh | bash -e
cd /tmp
rails new buggy_redirects
cd buggy_redirects
cat > /tmp/buggy_redirects/config/routes.rb <<EOF
BuggyRedirects::Application.routes.draw do
namespace :api, defaults: {format: :json} do
namespace :v1 do
resources :users
end
namespace :v2 do
resources :users
end
match 'v:api/*path', :to => redirect("/api/v2/%{path}")
match '*path', :to => redirect("/api/v2/%{path}")
end
end
EOF
cat > /tmp/buggy_redirects/test_request.rb <<EOF
require_relative "./config/environment"
app = ActionDispatch::Integration::Session.new(Rails.application)
puts "GET /api/users\n"
app.get "/api/users", :format => :json
if app.response.body == '<html><body>You are being <a href="http://www.example.com/api/v2/users">redirected</a>.</body></html>'
puts
puts "Yup, I'm definitely getting the HTML redirect:"
end
puts
puts "status: #{app.response.status}"
puts "body: #{app.response.body}"
puts
EOF
ruby /tmp/buggy_redirects/test_request.rb
echo "the app is located in /tmp/buggy_redirects"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment