integration.rb 16.8 KB
Newer Older
1 2
require 'stringio'
require 'uri'
3
require 'active_support/core_ext/kernel/singleton_class'
4
require 'active_support/core_ext/object/try'
J
Joshua Peek 已提交
5
require 'rack/test'
6 7

module ActionDispatch
D
David Heinemeier Hansson 已提交
8
  module Integration #:nodoc:
9 10 11 12 13 14 15 16 17 18
    module RequestHelpers
      # Performs a GET request with the given parameters.
      #
      # - +path+: The URI (as a String) on which you want to perform a GET
      #   request.
      # - +parameters+: The HTTP parameters that you want to pass. This may
      #   be +nil+,
      #   a Hash, or a String that is appropriately encoded
      #   (<tt>application/x-www-form-urlencoded</tt> or
      #   <tt>multipart/form-data</tt>).
19
      # - +headers_or_env+: Additional headers to pass, as a Hash. The headers will be
20
      #   merged into the Rack env hash.
21
      #
R
Robin Dupret 已提交
22
      # This method returns a Response object, which one can use to
23
      # inspect the details of the response. Furthermore, if this method was
24
      # called from an ActionDispatch::IntegrationTest object, then that
25 26 27
      # object's <tt>@response</tt> instance variable will point to the same
      # response object.
      #
28 29
      # You can also perform POST, PATCH, PUT, DELETE, and HEAD requests with
      # +#post+, +#patch+, +#put+, +#delete+, and +#head+.
30 31
      def get(path, parameters = nil, headers_or_env = nil)
        process :get, path, parameters, headers_or_env
32 33
      end

34
      # Performs a POST request with the given parameters. See +#get+ for more
35
      # details.
36 37
      def post(path, parameters = nil, headers_or_env = nil)
        process :post, path, parameters, headers_or_env
38 39
      end

40 41
      # Performs a PATCH request with the given parameters. See +#get+ for more
      # details.
42 43
      def patch(path, parameters = nil, headers_or_env = nil)
        process :patch, path, parameters, headers_or_env
44 45
      end

46
      # Performs a PUT request with the given parameters. See +#get+ for more
47
      # details.
48 49
      def put(path, parameters = nil, headers_or_env = nil)
        process :put, path, parameters, headers_or_env
50 51
      end

52
      # Performs a DELETE request with the given parameters. See +#get+ for
53
      # more details.
54 55
      def delete(path, parameters = nil, headers_or_env = nil)
        process :delete, path, parameters, headers_or_env
56 57
      end

58
      # Performs a HEAD request with the given parameters. See +#get+ for more
59
      # details.
60 61
      def head(path, parameters = nil, headers_or_env = nil)
        process :head, path, parameters, headers_or_env
62 63 64 65 66
      end

      # Performs an XMLHttpRequest request with the given parameters, mirroring
      # a request from the Prototype library.
      #
67 68
      # The request_method is +:get+, +:post+, +:patch+, +:put+, +:delete+ or
      # +:head+; the parameters are +nil+, a hash, or a url-encoded or multipart
69
      # string; the headers are a hash.
70 71 72 73 74
      def xml_http_request(request_method, path, parameters = nil, headers_or_env = nil)
        headers_or_env ||= {}
        headers_or_env['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest'
        headers_or_env['HTTP_ACCEPT'] ||= [Mime::JS, Mime::HTML, Mime::XML, 'text/xml', Mime::ALL].join(', ')
        process(request_method, path, parameters, headers_or_env)
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
      end
      alias xhr :xml_http_request

      # Follow a single redirect response. If the last response was not a
      # redirect, an exception will be raised. Otherwise, the redirect is
      # performed on the location header.
      def follow_redirect!
        raise "not a redirect! #{status} #{status_message}" unless redirect?
        get(response.location)
        status
      end

      # Performs a request using the specified method, following any subsequent
      # redirect. Note that the redirects are followed until the response is
      # not a redirect--this means you may run into an infinite loop if your
      # redirect loops back to itself.
91 92
      def request_via_redirect(http_method, path, parameters = nil, headers_or_env = nil)
        process(http_method, path, parameters, headers_or_env)
93 94 95 96 97 98
        follow_redirect! while redirect?
        status
      end

      # Performs a GET request, following any subsequent redirect.
      # See +request_via_redirect+ for more information.
99 100
      def get_via_redirect(path, parameters = nil, headers_or_env = nil)
        request_via_redirect(:get, path, parameters, headers_or_env)
101 102 103 104
      end

      # Performs a POST request, following any subsequent redirect.
      # See +request_via_redirect+ for more information.
105 106
      def post_via_redirect(path, parameters = nil, headers_or_env = nil)
        request_via_redirect(:post, path, parameters, headers_or_env)
107 108
      end

109 110
      # Performs a PATCH request, following any subsequent redirect.
      # See +request_via_redirect+ for more information.
111 112
      def patch_via_redirect(path, parameters = nil, headers_or_env = nil)
        request_via_redirect(:patch, path, parameters, headers_or_env)
113 114
      end

115 116
      # Performs a PUT request, following any subsequent redirect.
      # See +request_via_redirect+ for more information.
117 118
      def put_via_redirect(path, parameters = nil, headers_or_env = nil)
        request_via_redirect(:put, path, parameters, headers_or_env)
119 120 121 122
      end

      # Performs a DELETE request, following any subsequent redirect.
      # See +request_via_redirect+ for more information.
123 124
      def delete_via_redirect(path, parameters = nil, headers_or_env = nil)
        request_via_redirect(:delete, path, parameters, headers_or_env)
125 126 127
      end
    end

128 129
    # An instance of this class represents a set of requests and responses
    # performed sequentially by a test process. Because you can instantiate
130 131 132
    # multiple sessions and run them side-by-side, you can also mimic (to some
    # limited extent) multiple simultaneous users interacting with your system.
    #
133 134 135
    # Typically, you will instantiate a new session using
    # IntegrationTest#open_session, rather than instantiating
    # Integration::Session directly.
136
    class Session
137 138
      DEFAULT_HOST = "www.example.com"

139
      include Minitest::Assertions
J
Joshua Peek 已提交
140
      include TestProcess, RequestHelpers, Assertions
141

142 143 144
      %w( status status_message headers body redirect? ).each do |method|
        delegate method, :to => :response, :allow_nil => true
      end
145

146 147 148
      %w( path ).each do |method|
        delegate method, :to => :request, :allow_nil => true
      end
149 150

      # The hostname used in the last request.
151 152 153 154
      def host
        @host || DEFAULT_HOST
      end
      attr_writer :host
155 156 157

      # The remote_addr used in the last request.
      attr_accessor :remote_addr
158

159 160 161
      # The Accept header to send.
      attr_accessor :accept

162 163
      # A map of the cookies returned by the last response, and which will be
      # sent with the next request.
164
      def cookies
165
        _mock_session.cookie_jar
166
      end
167 168 169 170 171 172 173 174 175 176

      # A reference to the controller instance used by the last request.
      attr_reader :controller

      # A reference to the request instance used by the last request.
      attr_reader :request

      # A reference to the response instance used by the last request.
      attr_reader :response

177 178 179
      # A running counter of the number of requests processed.
      attr_accessor :request_count

180 181
      include ActionDispatch::Routing::UrlFor

P
Pratik Naik 已提交
182
      # Create and initialize a new Session instance.
183
      def initialize(app)
J
José Valim 已提交
184
        super()
185
        @app = app
186 187 188

        # If the app is a Rails app, make url_helpers available on the session
        # This makes app.url_for and app.foo_path available in the console
189 190
        if app.respond_to?(:routes)
          singleton_class.class_eval do
191
            include app.routes.url_helpers
192
            include app.routes.mounted_helpers
193
          end
194 195
        end

196 197 198
        reset!
      end

199 200 201 202 203 204 205 206 207 208
      def url_options
        @url_options ||= default_url_options.dup.tap do |url_options|
          url_options.reverse_merge!(controller.url_options) if controller

          if @app.respond_to?(:routes) && @app.routes.respond_to?(:default_url_options)
            url_options.reverse_merge!(@app.routes.default_url_options)
          end

          url_options.reverse_merge!(:host => host, :protocol => https? ? "https" : "http")
        end
209 210
      end

211 212 213 214 215 216 217 218
      # Resets the instance. This can be used to reset the state information
      # in an existing session instance, so it can be used from a clean-slate
      # condition.
      #
      #   session.reset!
      def reset!
        @https = false
        @controller = @request = @response = nil
219
        @_mock_session = nil
220
        @request_count = 0
221
        @url_options = nil
222

223
        self.host        = DEFAULT_HOST
224
        self.remote_addr = "127.0.0.1"
225 226 227
        self.accept      = "text/xml,application/xml,application/xhtml+xml," +
                           "text/html;q=0.9,text/plain;q=0.8,image/png," +
                           "*/*;q=0.5"
J
Jamis Buck 已提交
228

229
        unless defined? @named_routes_configured
J
Jamis Buck 已提交
230 231 232 233
          # the helpers are made protected by default--we make them public for
          # easier access during testing and troubleshooting.
          @named_routes_configured = true
        end
234 235 236 237 238 239
      end

      # Specify whether or not the session should mimic a secure HTTPS request.
      #
      #   session.https!
      #   session.https!(false)
240
      def https!(flag = true)
241
        @https = flag
242 243
      end

244
      # Returns +true+ if the session is mimicking a secure HTTPS request.
245 246 247 248 249 250 251 252 253 254
      #
      #   if session.https?
      #     ...
      #   end
      def https?
        @https
      end

      # Set the host name to use in the next request.
      #
255
      #   session.host! "www.example.com"
A
Aaron Patterson 已提交
256
      alias :host! :host=
257 258

      private
259 260 261
        def _mock_session
          @_mock_session ||= Rack::MockSession.new(@app, host)
        end
262

263
        # Performs the actual request.
264
        def process(method, path, parameters = nil, headers_or_env = nil)
265 266 267
          if path =~ %r{://}
            location = URI.parse(path)
            https! URI::HTTPS === location if location.scheme
268
            host! "#{location.host}:#{location.port}" if location.host
269 270
            path = location.query ? "#{location.path}?#{location.query}" : location.path
          end
271

272
          hostname, port = host.split(':')
273

274
          env = {
275
            :method => method,
276
            :params => parameters,
277

278
            "SERVER_NAME"     => hostname,
279
            "SERVER_PORT"     => port || (https? ? "443" : "80"),
280 281 282
            "HTTPS"           => https? ? "on" : "off",
            "rack.url_scheme" => https? ? "https" : "http",

283
            "REQUEST_URI"    => path,
284
            "HTTP_HOST"      => host,
285
            "REMOTE_ADDR"    => remote_addr,
286
            "CONTENT_TYPE"   => "application/x-www-form-urlencoded",
287
            "HTTP_ACCEPT"    => accept
288
          }
289 290
          # this modifies the passed env directly
          Http::Headers.new(env).merge!(headers_or_env || {})
291

292
          session = Rack::Test::Session.new(_mock_session)
293

294 295
          # NOTE: rack-test v0.5 doesn't build a default uri correctly
          # Make sure requested path is always a full uri
296
          session.request(build_full_uri(path, env), env)
297

298
          @request_count += 1
299
          @request  = ActionDispatch::Request.new(session.last_request.env)
300
          response = _mock_session.last_response
301
          @response = ActionDispatch::TestResponse.new(response.status, response.headers, response.body)
302
          @html_document = nil
303
          @url_options = nil
304

305 306
          @controller = session.last_request.env['action_controller.instance']

307
          return response.status
308
        end
309 310 311 312

        def build_full_uri(path, env)
          "#{env['rack.url_scheme']}://#{env['SERVER_NAME']}:#{env['SERVER_PORT']}#{path}"
        end
313 314
    end

315
    module Runner
316 317
      include ActionDispatch::Assertions

318
      def app
319
        @app ||= nil
320 321
      end

322 323 324
      # Reset the current session. This is useful for testing multiple sessions
      # in a single test case.
      def reset!
325
        @integration_session = Integration::Session.new(app)
326 327
      end

328
      %w(get post patch put head delete cookies assigns
329
         xml_http_request xhr get_via_redirect post_via_redirect).each do |method|
330
        define_method(method) do |*args|
331
          reset! unless integration_session
332
          # reset the html_document variable, but only for new get/post calls
333
          @html_document = nil unless method == 'cookies' || method == 'assigns'
334
          integration_session.__send__(method, *args).tap do
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
            copy_session_variables!
          end
        end
      end

      # Open a new session instance. If a block is given, the new session is
      # yielded to the block before being returned.
      #
      #   session = open_session do |sess|
      #     sess.extend(CustomAssertions)
      #   end
      #
      # By default, a single session is automatically created for you, but you
      # can use this method to open multiple sessions that ought to be tested
      # simultaneously.
G
Guo Xiang Tan 已提交
350
      def open_session
351 352
        dup.tap do |session|
          yield session if block_given?
353 354 355 356 357 358
        end
      end

      # Copy the instance variables from the current session instance into the
      # test instance.
      def copy_session_variables! #:nodoc:
359
        return unless integration_session
360
        %w(controller response request).each do |var|
361
          instance_variable_set("@#{var}", @integration_session.__send__(var))
362 363 364
        end
      end

365 366 367 368
      def default_url_options
        reset! unless integration_session
        integration_session.default_url_options
      end
369

370
      def default_url_options=(options)
371
        reset! unless integration_session
372
        integration_session.default_url_options = options
373 374
      end

375
      def respond_to?(method, include_private = false)
376
        integration_session.respond_to?(method, include_private) || super
377 378
      end

379 380
      # Delegate unhandled messages to the current session instance.
      def method_missing(sym, *args, &block)
381 382 383
        reset! unless integration_session
        if integration_session.respond_to?(sym)
          integration_session.__send__(sym, *args, &block).tap do
384 385 386 387
            copy_session_variables!
          end
        else
          super
388 389
        end
      end
390 391 392 393 394

      private
        def integration_session
          @integration_session ||= nil
        end
395
    end
396 397
  end

398
  # An integration test spans multiple controllers and actions,
399 400 401 402
  # tying them all together to ensure they work together as expected. It tests
  # more completely than either unit or functional tests do, exercising the
  # entire stack, from the dispatcher to the database.
  #
403
  # At its simplest, you simply extend <tt>IntegrationTest</tt> and write your tests
404 405
  # using the get/post methods:
  #
406
  #   require "test_helper"
407
  #
408
  #   class ExampleTest < ActionDispatch::IntegrationTest
409 410 411 412 413 414 415 416
  #     fixtures :people
  #
  #     def test_login
  #       # get the login page
  #       get "/login"
  #       assert_equal 200, status
  #
  #       # post the login and follow through to the home page
A
AvnerCohen 已提交
417 418
  #       post "/login", username: people(:jamis).username,
  #         password: people(:jamis).password
419 420 421 422 423 424 425 426 427
  #       follow_redirect!
  #       assert_equal 200, status
  #       assert_equal "/home", path
  #     end
  #   end
  #
  # However, you can also have multiple session instances open per test, and
  # even extend those instances with assertions and methods to create a very
  # powerful testing DSL that is specific for your application. You can even
428
  # reference any named routes you happen to have defined.
429
  #
430
  #   require "test_helper"
431
  #
432
  #   class AdvancedTest < ActionDispatch::IntegrationTest
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
  #     fixtures :people, :rooms
  #
  #     def test_login_and_speak
  #       jamis, david = login(:jamis), login(:david)
  #       room = rooms(:office)
  #
  #       jamis.enter(room)
  #       jamis.speak(room, "anybody home?")
  #
  #       david.enter(room)
  #       david.speak(room, "hello!")
  #     end
  #
  #     private
  #
  #       module CustomAssertions
  #         def enter(room)
  #           # reference a named route, for maximum internal consistency!
A
AvnerCohen 已提交
451
  #           get(room_url(id: room.id))
452 453 454 455 456
  #           assert(...)
  #           ...
  #         end
  #
  #         def speak(room, message)
A
AvnerCohen 已提交
457
  #           xml_http_request "/say/#{room.id}", message: message
458 459 460 461 462 463 464 465 466
  #           assert(...)
  #           ...
  #         end
  #       end
  #
  #       def login(who)
  #         open_session do |sess|
  #           sess.extend(CustomAssertions)
  #           who = people(who)
A
AvnerCohen 已提交
467 468
  #           sess.post "/login", username: who.username,
  #             password: who.password
469 470 471 472
  #           assert(...)
  #         end
  #       end
  #   end
473
  class IntegrationTest < ActiveSupport::TestCase
474
    include Integration::Runner
475
    include ActionController::TemplateAssertions
476
    include ActionDispatch::Routing::UrlFor
477 478 479 480

    @@app = nil

    def self.app
481
      @@app || ActionDispatch.test_app
482 483 484 485 486 487 488 489 490
    end

    def self.app=(app)
      @@app = app
    end

    def app
      super || self.class.app
    end
491 492 493 494 495

    def url_options
      reset! unless integration_session
      integration_session.url_options
    end
496
  end
497
end