mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
Rewrite Metal tests
This commit is contained in:
86
railties/test/application/metal_test.rb
Normal file
86
railties/test/application/metal_test.rb
Normal file
@@ -0,0 +1,86 @@
|
||||
require 'isolation/abstract_unit'
|
||||
|
||||
module ApplicationTests
|
||||
class MetalTest < Test::Unit::TestCase
|
||||
include ActiveSupport::Testing::Isolation
|
||||
|
||||
def setup
|
||||
build_app
|
||||
boot_rails
|
||||
|
||||
require 'rack/test'
|
||||
extend Rack::Test::Methods
|
||||
end
|
||||
|
||||
def app
|
||||
@app ||= begin
|
||||
require "#{app_path}/config/environment"
|
||||
Rails.application
|
||||
end
|
||||
end
|
||||
|
||||
test "single metal endpoint" do
|
||||
app_file 'app/metal/foo_metal.rb', <<-RUBY
|
||||
class FooMetal
|
||||
def self.call(env)
|
||||
[200, { "Content-Type" => "text/html"}, ["FooMetal"]]
|
||||
end
|
||||
end
|
||||
RUBY
|
||||
|
||||
get "/"
|
||||
assert_equal 200, last_response.status
|
||||
assert_equal "FooMetal", last_response.body
|
||||
end
|
||||
|
||||
test "multiple metal endpoints" do
|
||||
app_file 'app/metal/metal_a.rb', <<-RUBY
|
||||
class MetalA
|
||||
def self.call(env)
|
||||
[404, { "Content-Type" => "text/html"}, ["Metal A"]]
|
||||
end
|
||||
end
|
||||
RUBY
|
||||
|
||||
app_file 'app/metal/metal_b.rb', <<-RUBY
|
||||
class MetalB
|
||||
def self.call(env)
|
||||
[200, { "Content-Type" => "text/html"}, ["Metal B"]]
|
||||
end
|
||||
end
|
||||
RUBY
|
||||
|
||||
get "/"
|
||||
assert_equal 200, last_response.status
|
||||
assert_equal "Metal B", last_response.body
|
||||
end
|
||||
|
||||
test "pass through to application" do
|
||||
app_file 'app/metal/foo_metal.rb', <<-RUBY
|
||||
class FooMetal
|
||||
def self.call(env)
|
||||
[404, { "Content-Type" => "text/html"}, ["Not Found"]]
|
||||
end
|
||||
end
|
||||
RUBY
|
||||
|
||||
controller :foo, <<-RUBY
|
||||
class FooController < ActionController::Base
|
||||
def index
|
||||
render :text => "foo"
|
||||
end
|
||||
end
|
||||
RUBY
|
||||
|
||||
app_file 'config/routes.rb', <<-RUBY
|
||||
AppTemplate::Application.routes.draw do |map|
|
||||
match ':controller(/:action)'
|
||||
end
|
||||
RUBY
|
||||
|
||||
get "/foo"
|
||||
assert_equal 200, last_response.status
|
||||
assert_equal "foo", last_response.body
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,5 +0,0 @@
|
||||
class MetalA
|
||||
def self.call(env)
|
||||
[404, { "Content-Type" => "text/html"}, ["Metal A"]]
|
||||
end
|
||||
end
|
||||
@@ -1,5 +0,0 @@
|
||||
class MetalB
|
||||
def self.call(env)
|
||||
[200, { "Content-Type" => "text/html"}, ["Metal B"]]
|
||||
end
|
||||
end
|
||||
@@ -1,5 +0,0 @@
|
||||
class LegacyRoutes < Rails::Rack::Metal
|
||||
def self.call(env)
|
||||
[301, { "Location" => "http://example.com"}, []]
|
||||
end
|
||||
end
|
||||
@@ -1,5 +0,0 @@
|
||||
class FooMetal < Rails::Rack::Metal
|
||||
def self.call(env)
|
||||
[200, { "Content-Type" => "text/html"}, ["Hi"]]
|
||||
end
|
||||
end
|
||||
@@ -1,7 +0,0 @@
|
||||
module Folder
|
||||
class MetalA < Rails::Rack::Metal
|
||||
def self.call(env)
|
||||
[200, { "Content-Type" => "text/html"}, ["Hi"]]
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,7 +0,0 @@
|
||||
module Folder
|
||||
class MetalB < Rails::Rack::Metal
|
||||
def self.call(env)
|
||||
[200, { "Content-Type" => "text/html"}, ["Hi"]]
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,101 +0,0 @@
|
||||
require 'abstract_unit'
|
||||
|
||||
class MetalTest < Test::Unit::TestCase
|
||||
def test_metals_should_return_list_of_found_metal_apps
|
||||
use_appdir("singlemetal") do
|
||||
assert_equal(["FooMetal"], found_metals_as_string_array)
|
||||
end
|
||||
end
|
||||
|
||||
def test_metals_should_respect_class_name_conventions
|
||||
use_appdir("pluralmetal") do
|
||||
assert_equal(["LegacyRoutes"], found_metals_as_string_array)
|
||||
end
|
||||
end
|
||||
|
||||
def test_metals_should_return_alphabetical_list_of_found_metal_apps
|
||||
use_appdir("multiplemetals") do
|
||||
assert_equal(["MetalA", "MetalB"], found_metals_as_string_array)
|
||||
end
|
||||
end
|
||||
|
||||
def test_metals_load_order_should_be_overriden_by_requested_metals
|
||||
use_appdir("multiplemetals") do
|
||||
Rails::Rack::Metal.requested_metals = ["MetalB", "MetalA"]
|
||||
assert_equal(["MetalB", "MetalA"], found_metals_as_string_array)
|
||||
end
|
||||
end
|
||||
|
||||
def test_metals_not_listed_should_not_load
|
||||
use_appdir("multiplemetals") do
|
||||
Rails::Rack::Metal.requested_metals = ["MetalB"]
|
||||
assert_equal(["MetalB"], found_metals_as_string_array)
|
||||
end
|
||||
end
|
||||
|
||||
def test_metal_finding_should_work_with_subfolders
|
||||
use_appdir("subfolders") do
|
||||
assert_equal(["Folder::MetalA", "Folder::MetalB"], found_metals_as_string_array)
|
||||
end
|
||||
end
|
||||
|
||||
def test_metal_finding_with_requested_metals_should_work_with_subfolders
|
||||
use_appdir("subfolders") do
|
||||
Rails::Rack::Metal.requested_metals = ["Folder::MetalB"]
|
||||
assert_equal(["Folder::MetalB"], found_metals_as_string_array)
|
||||
end
|
||||
end
|
||||
|
||||
def test_metal_finding_should_work_with_multiple_metal_paths_in_185_and_below
|
||||
use_appdir("singlemetal") do
|
||||
engine_metal_path = "#{File.dirname(__FILE__)}/fixtures/plugins/engines/engine/app/metal"
|
||||
Rails::Rack::Metal.metal_paths << engine_metal_path
|
||||
$LOAD_PATH << engine_metal_path
|
||||
assert_equal(["FooMetal", "EngineMetal"], found_metals_as_string_array)
|
||||
end
|
||||
end
|
||||
|
||||
def test_metal_default_pass_through_on_404
|
||||
use_appdir("multiplemetals") do
|
||||
result = Rails::Rack::Metal.new(app).call({})
|
||||
assert_equal 200, result.first
|
||||
assert_equal ["Metal B"], result.last
|
||||
end
|
||||
end
|
||||
|
||||
def test_metal_pass_through_on_417
|
||||
use_appdir("multiplemetals") do
|
||||
Rails::Rack::Metal.pass_through_on = 417
|
||||
result = Rails::Rack::Metal.new(app).call({})
|
||||
assert_equal 404, result.first
|
||||
assert_equal ["Metal A"], result.last
|
||||
end
|
||||
end
|
||||
|
||||
def test_metal_pass_through_on_404_and_200
|
||||
use_appdir("multiplemetals") do
|
||||
Rails::Rack::Metal.pass_through_on = [404, 200]
|
||||
result = Rails::Rack::Metal.new(app).call({})
|
||||
assert_equal 402, result.first
|
||||
assert_equal ["End of the Line"], result.last
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def app
|
||||
lambda{|env|[402,{},["End of the Line"]]}
|
||||
end
|
||||
|
||||
def use_appdir(root)
|
||||
dir = "#{File.dirname(__FILE__)}/fixtures/metal/#{root}"
|
||||
Rails::Rack::Metal.metal_paths = ["#{dir}/app/metal"]
|
||||
Rails::Rack::Metal.requested_metals = nil
|
||||
$LOAD_PATH << "#{dir}/app/metal"
|
||||
yield
|
||||
end
|
||||
|
||||
def found_metals_as_string_array
|
||||
Rails::Rack::Metal.metals.map { |m| m.to_s }
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user