integration.rb 17.0 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
require 'minitest'
7 8

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

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

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

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

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

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

      # Performs an XMLHttpRequest request with the given parameters, mirroring
      # a request from the Prototype library.
      #
68 69
      # The request_method is +:get+, +:post+, +:patch+, +:put+, +:delete+ or
      # +:head+; the parameters are +nil+, a hash, or a url-encoded or multipart
70
      # string; the headers are a hash.
71 72 73 74 75
      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)
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
      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.
92 93
      def request_via_redirect(http_method, path, parameters = nil, headers_or_env = nil)
        process(http_method, path, parameters, headers_or_env)
94 95 96 97 98 99
        follow_redirect! while redirect?
        status
      end

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

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

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

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

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

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

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

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

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

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

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

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

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

      # 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

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

181 182
      include ActionDispatch::Routing::UrlFor

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

        # 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
190 191
        if app.respond_to?(:routes)
          singleton_class.class_eval do
192
            include app.routes.url_helpers
193
            include app.routes.mounted_helpers
194
          end
195 196
        end

197 198 199
        reset!
      end

200 201 202 203
      def url_options
        @url_options ||= default_url_options.dup.tap do |url_options|
          url_options.reverse_merge!(controller.url_options) if controller

204
          if @app.respond_to?(:routes)
205 206 207 208 209
            url_options.reverse_merge!(@app.routes.default_url_options)
          end

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

212 213 214 215 216 217 218 219
      # 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
220
        @_mock_session = nil
221
        @request_count = 0
222
        @url_options = nil
223

224
        self.host        = DEFAULT_HOST
225
        self.remote_addr = "127.0.0.1"
226 227 228
        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 已提交
229

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

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

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

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

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

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

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

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

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

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

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

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

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

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

308
          return response.status
309
        end
310 311 312 313

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

316
    module Runner
317 318
      include ActionDispatch::Assertions

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

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

329 330 331 332
      def remove! # :nodoc:
        @integration_session = nil
      end

333
      %w(get post patch put head delete cookies assigns
334
         xml_http_request xhr get_via_redirect post_via_redirect).each do |method|
335
        define_method(method) do |*args|
336
          reset! unless integration_session
337 338 339 340 341 342 343

          # reset the html_document variable, except for cookies/assigns calls
          unless method == 'cookies' || method == 'assigns'
            @html_document = nil
            reset_template_assertion
          end

344
          integration_session.__send__(method, *args).tap do
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
            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 已提交
360
      def open_session
361 362
        dup.tap do |session|
          yield session if block_given?
363 364 365 366 367 368
        end
      end

      # Copy the instance variables from the current session instance into the
      # test instance.
      def copy_session_variables! #:nodoc:
369
        return unless integration_session
370
        %w(controller response request).each do |var|
371
          instance_variable_set("@#{var}", @integration_session.__send__(var))
372 373 374
        end
      end

375 376 377 378
      def default_url_options
        reset! unless integration_session
        integration_session.default_url_options
      end
379

380
      def default_url_options=(options)
381
        reset! unless integration_session
382
        integration_session.default_url_options = options
383 384
      end

385
      def respond_to?(method, include_private = false)
386
        integration_session.respond_to?(method, include_private) || super
387 388
      end

389 390
      # Delegate unhandled messages to the current session instance.
      def method_missing(sym, *args, &block)
391 392 393
        reset! unless integration_session
        if integration_session.respond_to?(sym)
          integration_session.__send__(sym, *args, &block).tap do
394 395 396 397
            copy_session_variables!
          end
        else
          super
398 399
        end
      end
400 401 402 403 404

      private
        def integration_session
          @integration_session ||= nil
        end
405
    end
406 407
  end

408
  # An integration test spans multiple controllers and actions,
409 410 411 412
  # 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.
  #
413
  # At its simplest, you simply extend <tt>IntegrationTest</tt> and write your tests
414 415
  # using the get/post methods:
  #
416
  #   require "test_helper"
417
  #
418
  #   class ExampleTest < ActionDispatch::IntegrationTest
419 420 421 422 423 424 425 426
  #     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 已提交
427 428
  #       post "/login", username: people(:jamis).username,
  #         password: people(:jamis).password
429 430 431 432 433 434 435 436 437
  #       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
438
  # reference any named routes you happen to have defined.
439
  #
440
  #   require "test_helper"
441
  #
442
  #   class AdvancedTest < ActionDispatch::IntegrationTest
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
  #     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 已提交
461
  #           get(room_url(id: room.id))
462 463 464 465 466
  #           assert(...)
  #           ...
  #         end
  #
  #         def speak(room, message)
A
AvnerCohen 已提交
467
  #           xml_http_request "/say/#{room.id}", message: message
468 469 470 471 472 473 474 475 476
  #           assert(...)
  #           ...
  #         end
  #       end
  #
  #       def login(who)
  #         open_session do |sess|
  #           sess.extend(CustomAssertions)
  #           who = people(who)
A
AvnerCohen 已提交
477 478
  #           sess.post "/login", username: who.username,
  #             password: who.password
479 480 481 482
  #           assert(...)
  #         end
  #       end
  #   end
483
  class IntegrationTest < ActiveSupport::TestCase
484
    include Integration::Runner
485
    include ActionController::TemplateAssertions
486
    include ActionDispatch::Routing::UrlFor
487 488 489 490

    @@app = nil

    def self.app
491
      @@app || ActionDispatch.test_app
492 493 494 495 496 497 498 499 500
    end

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

    def app
      super || self.class.app
    end
501 502 503 504 505

    def url_options
      reset! unless integration_session
      integration_session.url_options
    end
506 507

    def document_root_element
508
      html_document
509
    end
510
  end
511
end