Skip to content

Instantly share code, notes, and snippets.

@jhubert
Last active October 9, 2018 17:25
Show Gist options
  • Save jhubert/f9ce8fc0f8147089a481af1433cdbd69 to your computer and use it in GitHub Desktop.
Save jhubert/f9ce8fc0f8147089a481af1433cdbd69 to your computer and use it in GitHub Desktop.
Investigating a strange issue with named routes and namespaced controllers
begin
require 'bundler/inline'
require 'pry'
rescue LoadError => e
$stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler'
raise e
end
gemfile(true) do
source 'https://rubygems.org'
gem 'rails', '5.2.1'
end
require 'rack/test'
require 'active_storage/engine'
require 'action_controller/railtie'
class TestApp < Rails::Application
config.root = File.dirname(__FILE__)
config.session_store :cookie_store, key: 'cookie_store_key'
secrets.secret_token = 'secret_token'
secrets.secret_key_base = 'secret_key_base'
config.eager_load = false
config.logger = Logger.new($stdout)
Rails.logger = config.logger
end
Rails.application.initialize!
TestApp.routes.draw do
get '/blob' => 'active_storage/blobs#show'
get '/test' => 'test#show'
get '/login' => 'sessions#new', as: :login
end
class ApplicationController < ActionController::Base
before_action :redirect_to_login
private
def redirect_to_login
if params[:inner_options]
redirect_to login_path({})
else
redirect_to login_path
end
end
end
class ActiveStorage::BaseController < ApplicationController
protect_from_forgery with: :exception
before_action do
ActiveStorage::Current.host = request.base_url
end
end
class SessionsController < ApplicationController
def new
render text: 'login'
end
end
class TestController < ApplicationController
def show
render text: 'test'
end
end
class ActiveStorage::BlobsController < ActiveStorage::BaseController
def show
render text: 'blob'
end
end
require 'minitest/autorun'
class TestControllerTest < ActionDispatch::IntegrationTest
test "should redirect to the login page" do
get '/test'
assert_equal 302, response.status
assert_equal "http://www.example.com/login", response.headers["location"]
end
test "should redirect to the login page with inner_options" do
get '/test?inner_options=1'
assert_equal 302, response.status
assert_equal "http://www.example.com/login", response.headers["location"]
end
end
class ActiveStorage::BlobsControllerTest < ActionDispatch::IntegrationTest
test "should redirect to the login page" do
get '/blob'
assert_equal 302, response.status
assert_equal "http://www.example.com/login", response.headers["location"]
end
test "should redirect to the login page with inner_options" do
get '/blob?inner_options=1'
assert_equal 302, response.status
assert_equal "http://www.example.com/login", response.headers["location"]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment