From c9132c149cb9fe5628c2e947434e8c58acdaa709 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jos=C3=A9=20Valim?= <jose.valim@gmail.com>
Date: Mon, 26 Apr 2010 09:04:04 +0200
Subject: Refactor tests by moving all middleware tests to the same place.

---
 railties/test/application/configuration_test.rb    | 71 ++---------------
 .../application/middleware_stack_defaults_test.rb  | 54 -------------
 railties/test/application/middleware_test.rb       | 93 ++++++++++++++++++++++
 3 files changed, 98 insertions(+), 120 deletions(-)
 delete mode 100644 railties/test/application/middleware_stack_defaults_test.rb

(limited to 'railties/test/application')

diff --git a/railties/test/application/configuration_test.rb b/railties/test/application/configuration_test.rb
index 4f1fc3c299..dfc4e2359b 100644
--- a/railties/test/application/configuration_test.rb
+++ b/railties/test/application/configuration_test.rb
@@ -109,7 +109,7 @@ module ApplicationTests
       end
     end
 
-    test "Frameworks are not preloaded by default" do
+    test "frameworks are not preloaded by default" do
       require "#{app_path}/config/environment"
 
       assert ActionController.autoload?(:RecordIdentifier)
@@ -193,71 +193,10 @@ module ApplicationTests
       assert_equal File.join(app_path, "somewhere"), Rails.public_path
     end
 
-    def make_basic_app
-      require "rails"
-      require "action_controller/railtie"
-
-      app = Class.new(Rails::Application)
-
-      yield app if block_given?
-
-      app.config.session_store :disabled
-      app.initialize!
-
-      app.routes.draw do
-        match "/" => "omg#index"
-      end
-
-      require 'rack/test'
-      extend Rack::Test::Methods
-    end
-
-    test "config.action_dispatch.x_sendfile_header defaults to ''" do
-      make_basic_app
-
-      class ::OmgController < ActionController::Base
-        def index
-          send_file __FILE__
-        end
-      end
-
-      get "/"
-      assert_equal File.read(__FILE__), last_response.body
-    end
-
-    test "config.action_dispatch.x_sendfile_header can be set" do
-      make_basic_app do |app|
-        app.config.action_dispatch.x_sendfile_header = "X-Sendfile"
-      end
-
-      class ::OmgController < ActionController::Base
-        def index
-          send_file __FILE__
-        end
-      end
-
-      get "/"
-      assert_equal File.expand_path(__FILE__), last_response.headers["X-Sendfile"]
-    end
-
-    test "config.action_dispatch.x_sendfile_header is sent to Rack::Sendfile" do
-      make_basic_app do |app|
-        app.config.action_dispatch.x_sendfile_header = 'X-Lighttpd-Send-File'
-      end
-
-      class ::OmgController < ActionController::Base
-        def index
-          send_file __FILE__
-        end
-      end
-
-      get "/"
-      assert_equal File.expand_path(__FILE__), last_response.headers["X-Lighttpd-Send-File"]
-    end
-
     test "config.secret_token is sent in env" do
       make_basic_app do |app|
         app.config.secret_token = 'ThisIsASECRET123'
+        app.config.session_store :disabled
       end
 
       class ::OmgController < ActionController::Base
@@ -287,9 +226,9 @@ module ApplicationTests
     end
 
     test "config.action_controller.perform_caching = true" do
-        make_basic_app do |app|
-          app.config.action_controller.perform_caching = true
-        end
+      make_basic_app do |app|
+        app.config.action_controller.perform_caching = true
+      end
 
       class ::OmgController < ActionController::Base
         @@count = 0
diff --git a/railties/test/application/middleware_stack_defaults_test.rb b/railties/test/application/middleware_stack_defaults_test.rb
deleted file mode 100644
index f31ca01fbf..0000000000
--- a/railties/test/application/middleware_stack_defaults_test.rb
+++ /dev/null
@@ -1,54 +0,0 @@
-require 'isolation/abstract_unit'
-
-class MiddlewareStackDefaultsTest < Test::Unit::TestCase
-  include ActiveSupport::Testing::Isolation
-
-  def setup
-    boot_rails
-    require "rails"
-    require "action_controller/railtie"
-
-    Object.const_set(:MyApplication, Class.new(Rails::Application))
-    MyApplication.class_eval do
-      config.secret_token = "3b7cd727ee24e8444053437c36cc66c4"
-      config.session_store :cookie_store, :key => "_myapp_session"
-    end
-  end
-
-  def remote_ip(env = {})
-    remote_ip = nil
-    env = Rack::MockRequest.env_for("/").merge(env).merge('action_dispatch.show_exceptions' => false)
-
-    endpoint = Proc.new do |e|
-      remote_ip = ActionDispatch::Request.new(e).remote_ip
-      [200, {}, ["Hello"]]
-    end
-
-    out = MyApplication.middleware.build(endpoint).call(env)
-    remote_ip
-  end
-
-  test "remote_ip works" do
-    assert_equal "1.1.1.1", remote_ip("REMOTE_ADDR" => "1.1.1.1")
-  end
-
-  test "checks IP spoofing by default" do
-    assert_raises(ActionDispatch::RemoteIp::IpSpoofAttackError) do
-      remote_ip("HTTP_X_FORWARDED_FOR" => "1.1.1.1", "HTTP_CLIENT_IP" => "1.1.1.2")
-    end
-  end
-
-  test "can disable IP spoofing check" do
-    MyApplication.config.action_dispatch.ip_spoofing_check = false
-
-    assert_nothing_raised(ActionDispatch::RemoteIp::IpSpoofAttackError) do
-      assert_equal "1.1.1.2", remote_ip("HTTP_X_FORWARDED_FOR" => "1.1.1.1", "HTTP_CLIENT_IP" => "1.1.1.2")
-    end
-  end
-
-  test "the user can set trusted proxies" do
-    MyApplication.config.action_dispatch.trusted_proxies = /^4\.2\.42\.42$/
-
-    assert_equal "1.1.1.1", remote_ip("REMOTE_ADDR" => "4.2.42.42,1.1.1.1")
-  end
-end
diff --git a/railties/test/application/middleware_test.rb b/railties/test/application/middleware_test.rb
index 7f72881d55..27374dcb28 100644
--- a/railties/test/application/middleware_test.rb
+++ b/railties/test/application/middleware_test.rb
@@ -10,6 +10,10 @@ module ApplicationTests
       FileUtils.rm_rf "#{app_path}/config/environments"
     end
 
+    def app
+      @app ||= Rails.application
+    end
+
     test "default middleware stack" do
       boot!
 
@@ -83,7 +87,83 @@ module ApplicationTests
       assert middleware.include?("ActionDispatch::Cascade")
     end
 
+    # x_sendfile_header middleware
+    test "config.action_dispatch.x_sendfile_header defaults to ''" do
+      make_basic_app
+
+      class ::OmgController < ActionController::Base
+        def index
+          send_file __FILE__
+        end
+      end
+
+      get "/"
+      assert_equal File.read(__FILE__), last_response.body
+    end
+
+    test "config.action_dispatch.x_sendfile_header can be set" do
+      make_basic_app do |app|
+        app.config.action_dispatch.x_sendfile_header = "X-Sendfile"
+      end
+
+      class ::OmgController < ActionController::Base
+        def index
+          send_file __FILE__
+        end
+      end
+
+      get "/"
+      assert_equal File.expand_path(__FILE__), last_response.headers["X-Sendfile"]
+    end
+
+    test "config.action_dispatch.x_sendfile_header is sent to Rack::Sendfile" do
+      make_basic_app do |app|
+        app.config.action_dispatch.x_sendfile_header = 'X-Lighttpd-Send-File'
+      end
+
+      class ::OmgController < ActionController::Base
+        def index
+          send_file __FILE__
+        end
+      end
+
+      get "/"
+      assert_equal File.expand_path(__FILE__), last_response.headers["X-Lighttpd-Send-File"]
+    end
+
+    # remote_ip tests
+    test "remote_ip works" do
+      make_basic_app
+      assert_equal "1.1.1.1", remote_ip("REMOTE_ADDR" => "1.1.1.1")
+    end
+
+    test "checks IP spoofing by default" do
+      make_basic_app
+      assert_raises(ActionDispatch::RemoteIp::IpSpoofAttackError) do
+        remote_ip("HTTP_X_FORWARDED_FOR" => "1.1.1.1", "HTTP_CLIENT_IP" => "1.1.1.2")
+      end
+    end
+
+    test "can disable IP spoofing check" do
+      make_basic_app do |app|
+        app.config.action_dispatch.ip_spoofing_check = false
+      end
+
+      assert_nothing_raised(ActionDispatch::RemoteIp::IpSpoofAttackError) do
+        assert_equal "1.1.1.2", remote_ip("HTTP_X_FORWARDED_FOR" => "1.1.1.1", "HTTP_CLIENT_IP" => "1.1.1.2")
+      end
+    end
+
+    test "the user can set trusted proxies" do
+      make_basic_app do |app|
+        app.config.action_dispatch.trusted_proxies = /^4\.2\.42\.42$/
+      end
+
+      assert_equal "1.1.1.1", remote_ip("REMOTE_ADDR" => "4.2.42.42,1.1.1.1")
+    end
+
     private
+
       def boot!
         require "#{app_path}/config/environment"
       end
@@ -91,5 +171,18 @@ module ApplicationTests
       def middleware
         AppTemplate::Application.middleware.active.map(&:klass).map(&:name)
       end
+
+      def remote_ip(env = {})
+        remote_ip = nil
+        env = Rack::MockRequest.env_for("/").merge(env).merge('action_dispatch.show_exceptions' => false)
+
+        endpoint = Proc.new do |e|
+          remote_ip = ActionDispatch::Request.new(e).remote_ip
+          [200, {}, ["Hello"]]
+        end
+
+        Rails.application.middleware.build(endpoint).call(env)
+        remote_ip
+      end
   end
 end
-- 
cgit v1.2.3