require 'abstract_unit'
require 'controller/fake_controllers'
require 'rails/engine'
class SessionTest < ActiveSupport::TestCase
  StubApp = lambda { |env|
    [200, {"Content-Type" => "text/html", "Content-Length" => "13"}, ["Hello, World!"]]
  }
  def setup
    @session = ActionDispatch::Integration::Session.new(StubApp)
  end
  def test_https_bang_works_and_sets_truth_by_default
    assert !@session.https?
    @session.https!
    assert @session.https?
    @session.https! false
    assert !@session.https?
  end
  def test_host!
    assert_not_equal "glu.ttono.us", @session.host
    @session.host! "rubyonrails.com"
    assert_equal "rubyonrails.com", @session.host
  end
  def test_follow_redirect_raises_when_no_redirect
    @session.stubs(:redirect?).returns(false)
    assert_raise(RuntimeError) { @session.follow_redirect! }
  end
  def test_request_via_redirect_uses_given_method
    path = "/somepath"; args = {:id => '1'}; headers = {"X-Test-Header" => "testvalue"}
    @session.expects(:process).with(:put, path, params: args, headers: headers)
    @session.stubs(:redirect?).returns(false)
    @session.request_via_redirect(:put, path, params: args, headers: headers)
  end
  def test_deprecated_request_via_redirect_uses_given_method
    path = "/somepath"; args = { id: '1' }; headers = { "X-Test-Header" => "testvalue" }
    @session.expects(:process).with(:put, path, params: args, headers: headers)
    @session.stubs(:redirect?).returns(false)
    assert_deprecated { @session.request_via_redirect(:put, path, args, headers) }
  end
  def test_request_via_redirect_follows_redirects
    path = "/somepath"; args = {:id => '1'}; headers = {"X-Test-Header" => "testvalue"}
    @session.stubs(:redirect?).returns(true, true, false)
    @session.expects(:follow_redirect!).times(2)
    @session.request_via_redirect(:get, path, params: args, headers: headers)
  end
  def test_request_via_redirect_returns_status
    path = "/somepath"; args = {:id => '1'}; headers = {"X-Test-Header" => "testvalue"}
    @session.stubs(:redirect?).returns(false)
    @session.stubs(:status).returns(200)
    assert_equal 200, @session.request_via_redirect(:get, path, params: args, headers: headers)
  end
  def test_deprecated_get_via_redirect
    path = "/somepath"; args = { id: '1' }; headers = { "X-Test-Header" => "testvalue" }
    @session.expects(:request_via_redirect).with(:get, path, args, headers)
    assert_deprecated do
      @session.get_via_redirect(path, args, headers)
    end
  end
  def test_deprecated_post_via_redirect
    path = "/somepath"; args = { id: '1' }; headers = { "X-Test-Header" => "testvalue" }
    @session.expects(:request_via_redirect).with(:post, path, args, headers)
    assert_deprecated do
      @session.post_via_redirect(path, args, headers)
    end
  end
  def test_deprecated_patch_via_redirect
    path = "/somepath"; args = { id: '1' }; headers = { "X-Test-Header" => "testvalue" }
    @session.expects(:request_via_redirect).with(:patch, path, args, headers)
    assert_deprecated do
      @session.patch_via_redirect(path, args, headers)
    end
  end
  def test_deprecated_put_via_redirect
    path = "/somepath"; args = { id: '1' }; headers = { "X-Test-Header" => "testvalue" }
    @session.expects(:request_via_redirect).with(:put, path, args, headers)
    assert_deprecated do
      @session.put_via_redirect(path, args, headers)
    end
  end
  def test_deprecated_delete_via_redirect
    path = "/somepath"; args = { id: '1' }; headers = { "X-Test-Header" => "testvalue" }
    @session.expects(:request_via_redirect).with(:delete, path, args, headers)
    assert_deprecated do
      @session.delete_via_redirect(path, args, headers)
    end
  end
  def test_get
    path = "/index"; params = "blah"; headers = { location: 'blah' }
    @session.expects(:process).with(:get, path, params: params, headers: headers)
    @session.get(path, params: params, headers: headers)
  end
  def test_get_with_env_and_headers
    path = "/index"; params = "blah"; headers = { location: 'blah' }; env = { 'HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest' }
    @session.expects(:process).with(:get, path, params: params, headers: headers, env: env)
    @session.get(path, params: params, headers: headers, env: env)
  end
  def test_deprecated_get
    path = "/index"; params = "blah"; headers = { location: 'blah' }
    @session.expects(:process).with(:get, path, params: params, headers: headers)
    assert_deprecated {
      @session.get(path, params, headers)
    }
  end
  def test_post
    path = "/index"; params = "blah"; headers = { location: 'blah' }
    @session.expects(:process).with(:post, path, params: params, headers: headers)
    assert_deprecated {
      @session.post(path, params, headers)
    }
  end
  def test_deprecated_post
    path = "/index"; params = "blah"; headers = { location: 'blah' }
    @session.expects(:process).with(:post, path, params: params, headers: headers)
    @session.post(path, params: params, headers: headers)
  end
  def test_patch
    path = "/index"; params = "blah"; headers = { location: 'blah' }
    @session.expects(:process).with(:patch, path, params: params, headers: headers)
    @session.patch(path, params: params, headers: headers)
  end
  def test_deprecated_patch
    path = "/index"; params = "blah"; headers = { location: 'blah' }
    @session.expects(:process).with(:patch, path, params: params, headers: headers)
    assert_deprecated {
      @session.patch(path, params, headers)
    }
  end
  def test_put
    path = "/index"; params = "blah"; headers = { location: 'blah' }
    @session.expects(:process).with(:put, path, params: params, headers: headers)
    @session.put(path, params: params, headers: headers)
  end
  def test_deprecated_put
    path = "/index"; params = "blah"; headers = { location: 'blah' }
    @session.expects(:process).with(:put, path, params: params, headers: headers)
    assert_deprecated {
      @session.put(path, params, headers)
    }
  end
  def test_delete
    path = "/index"; params = "blah"; headers = { location: 'blah' }
    @session.expects(:process).with(:delete, path, params: params, headers: headers)
    assert_deprecated {
      @session.delete(path,params,headers)
    }
  end
  def test_deprecated_delete
    path = "/index"; params = "blah"; headers = { location: 'blah' }
    @session.expects(:process).with(:delete, path, params: params, headers: headers)
    @session.delete(path, params: params, headers: headers)
  end
  def test_head
    path = "/index"; params = "blah"; headers = { location: 'blah' }
    @session.expects(:process).with(:head, path, params: params, headers: headers)
    @session.head(path, params: params, headers: headers)
  end
  def deprecated_test_head
    path = "/index"; params = "blah"; headers = { location: 'blah' }
    @session.expects(:process).with(:head, path, params: params, headers: headers)
    assert_deprecated {
      @session.head(path, params, headers)
    }
  end
  def test_xml_http_request_get
    path = "/index"; params = "blah"; headers = { location: 'blah' }
    @session.expects(:process).with(:get, path, params: params, headers: headers, xhr: true)
    @session.get(path, params: params, headers: headers, xhr: true)
  end
  def test_deprecated_xml_http_request_get
    path = "/index"; params = "blah"; headers = { location: 'blah' }
    @session.expects(:process).with(:get, path, params: params, headers: headers, xhr: true)
    @session.get(path, params: params, headers: headers, xhr: true)
  end
  def test_deprecated_args_xml_http_request_get
    path = "/index"; params = "blah"; headers = { location: 'blah' }
    @session.expects(:process).with(:get, path, params: params, headers: headers, xhr: true)
    assert_deprecated(/xml_http_request/) {
      @session.xml_http_request(:get, path, params, headers)
    }
  end
  def test_xml_http_request_post
    path = "/index"; params = "blah"; headers = { location: 'blah' }
    @session.expects(:process).with(:post, path, params: params, headers: headers, xhr: true)
    @session.post(path, params: params, headers: headers, xhr: true)
  end
  def test_deprecated_xml_http_request_post
    path = "/index"; params = "blah"; headers = { location: 'blah' }
    @session.expects(:process).with(:post, path, params: params, headers: headers, xhr: true)
    @session.post(path, params: params, headers: headers, xhr: true)
  end
  def test_deprecated_args_xml_http_request_post
    path = "/index"; params = "blah"; headers = { location: 'blah' }
    @session.expects(:process).with(:post, path, params: params, headers: headers, xhr: true)
    assert_deprecated(/xml_http_request/) { @session.xml_http_request(:post,path,params,headers) }
  end
  def test_xml_http_request_patch
    path = "/index"; params = "blah"; headers = { location: 'blah' }
    @session.expects(:process).with(:patch, path, params: params, headers: headers, xhr: true)
    @session.patch(path, params: params, headers: headers, xhr: true)
  end
  def test_deprecated_xml_http_request_patch
    path = "/index"; params = "blah"; headers = { location: 'blah' }
    @session.expects(:process).with(:patch, path, params: params, headers: headers, xhr: true)
    @session.patch(path, params: params, headers: headers, xhr: true)
  end
  def test_deprecated_args_xml_http_request_patch
    path = "/index"; params = "blah"; headers = { location: 'blah' }
    @session.expects(:process).with(:patch, path, params: params, headers: headers, xhr: true)
    assert_deprecated(/xml_http_request/) { @session.xml_http_request(:patch,path,params,headers) }
  end
  def test_xml_http_request_put
    path = "/index"; params = "blah"; headers = { location: 'blah' }
    @session.expects(:process).with(:put, path, params: params, headers: headers, xhr: true)
    @session.put(path, params: params, headers: headers, xhr: true)
  end
  def test_deprecated_xml_http_request_put
    path = "/index"; params = "blah"; headers = { location: 'blah' }
    @session.expects(:process).with(:put, path, params: params, headers: headers, xhr: true)
    @session.put(path, params: params, headers: headers, xhr: true)
  end
  def test_deprecated_args_xml_http_request_put
    path = "/index"; params = "blah"; headers = { location: 'blah' }
    @session.expects(:process).with(:put, path, params: params, headers: headers, xhr: true)
    assert_deprecated(/xml_http_request/) { @session.xml_http_request(:put, path, params, headers) }
  end
  def test_xml_http_request_delete
    path = "/index"; params = "blah"; headers = { location: 'blah' }
    @session.expects(:process).with(:delete, path, params: params, headers: headers, xhr: true)
    @session.delete(path, params: params, headers: headers, xhr: true)
  end
  def test_deprecated_xml_http_request_delete
    path = "/index"; params = "blah"; headers = { location: 'blah' }
    @session.expects(:process).with(:delete, path, params: params, headers: headers, xhr: true)
    assert_deprecated { @session.xml_http_request(:delete, path, params: params, headers: headers) }
  end
  def test_deprecated_args_xml_http_request_delete
    path = "/index"; params = "blah"; headers = { location: 'blah' }
    @session.expects(:process).with(:delete, path, params: params, headers: headers, xhr: true)
    assert_deprecated(/xml_http_request/) { @session.xml_http_request(:delete, path, params, headers) }
  end
  def test_xml_http_request_head
    path = "/index"; params = "blah"; headers = { location: 'blah' }
    @session.expects(:process).with(:head, path, params: params, headers: headers, xhr: true)
    @session.head(path, params: params, headers: headers, xhr: true)
  end
  def test_deprecated_xml_http_request_head
    path = "/index"; params = "blah"; headers = { location: 'blah' }
    @session.expects(:process).with(:head, path, params: params, headers: headers, xhr: true)
    assert_deprecated(/xml_http_request/) { @session.xml_http_request(:head, path, params: params, headers: headers) }
  end
  def test_deprecated_args_xml_http_request_head
    path = "/index"; params = "blah"; headers = { location: 'blah' }
    @session.expects(:process).with(:head, path, params: params, headers: headers, xhr: true)
    assert_deprecated { @session.xml_http_request(:head, path, params, headers) }
  end
end
class IntegrationTestTest < ActiveSupport::TestCase
  def setup
    @test = ::ActionDispatch::IntegrationTest.new(:app)
    @test.class.stubs(:fixture_table_names).returns([])
    @session = @test.open_session
  end
  def test_opens_new_session
    session1 = @test.open_session { |sess| }
    session2 = @test.open_session # implicit session
    assert_respond_to session1, :assert_template, "open_session makes assert_template available"
    assert_respond_to session2, :assert_template, "open_session makes assert_template available"
    assert !session1.equal?(session2)
  end
  # RSpec mixes Matchers (which has a #method_missing) into
  # IntegrationTest's superclass.  Make sure IntegrationTest does not
  # try to delegate these methods to the session object.
  def test_does_not_prevent_method_missing_passing_up_to_ancestors
    mixin = Module.new do
      def method_missing(name, *args)
        name.to_s == 'foo' ? 'pass' : super
      end
    end
    @test.class.superclass.__send__(:include, mixin)
    begin
      assert_equal 'pass', @test.foo
    ensure
      # leave other tests as unaffected as possible
      mixin.__send__(:remove_method, :method_missing)
    end
  end
end
# Tests that integration tests don't call Controller test methods for processing.
# Integration tests have their own setup and teardown.
class IntegrationTestUsesCorrectClass < ActionDispatch::IntegrationTest
  def self.fixture_table_names
    []
  end
  def test_integration_methods_called
    reset!
    @integration_session.stubs(:generic_url_rewriter)
    @integration_session.stubs(:process)
    %w( get post head patch put delete ).each do |verb|
      assert_nothing_raised("'#{verb}' should use integration test methods") { __send__(verb, '/') }
    end
  end
end
class IntegrationProcessTest < ActionDispatch::IntegrationTest
  class IntegrationController < ActionController::Base
    def get
      respond_to do |format|
        format.html { render :text => "OK", :status => 200 }
        format.js { render :text => "JS OK", :status => 200 }
        format.xml { render :xml => "