Module: RackSessionManipulation::Capybara

Defined in:
lib/rack_session_manipulation/capybara.rb

Overview

This module exposes the session state helpers to Capybara, allowing feature tests to quickly access the session mechanisms.

Instance Method Summary collapse

Instance Method Details

#driver_method_fallback(method, path, dat = {}) ⇒ void (protected)

Capybara drivers are only required to implement the #get method. Where appropriate we’ll use the correct HTTP method, but when unavailable we’ll use the fallback mechanism.

There may be a subtle bug in this fallback mechanism when the driver doesn’t support ‘POST’ requests. If large data payloads are used. Normal HTTP request parameters within the URL itself are limited to 1024 characters.

Parameters:

  • method (Symbol)

    A valid HTTP method name (all lowercase).

  • path (String)

    The path to request in the driver’s rack app.

  • dat (Hash) (defaults to: {})

    An optional hash of data that will be sent along as part of the query parameters.



50
51
52
53
54
55
56
57
58
# File 'lib/rack_session_manipulation/capybara.rb', line 50

def driver_method_fallback(method, path, dat = {})
  if driver.respond_to?(method)
    driver.send(method, path, dat)
    return
  end

  data.merge!('_method' => method.to_s.upcase)
  driver.respond_to?(:post) ? driver.post(path, dat) : driver.get(path, dat)
end

#reset_sessionvoid

Provides a mechanism to completely reset the server’s session to a fresh blank slate.



8
9
10
# File 'lib/rack_session_manipulation/capybara.rb', line 8

def reset_session
  driver_method_fallback(:delete, RackSessionManipulation.config.path, data)
end

#sessionHash

Retrieve a hash containing the entire state of the current session.

Returns:

  • (Hash)

    Hash of the session



15
16
17
18
# File 'lib/rack_session_manipulation/capybara.rb', line 15

def session
  driver.get(RackSessionManipulation.config.path)
  RackSessionManipulation.decode(driver.response.body)
end

#session=(hash) ⇒ void

Updates the state of the current session. An important thing to note about this mechanism is that it does not set the session to perfectly match the state of the provided hash. The provided hash is effectively merged with the current state of the session.

This also does not support marshalling objects into the session state. This limitation is intentional as objects stored in sessions tend to be a detriment to both performance and security.

Parameters:

  • hash (Hash)

    Data to be set / updated within the current session.



30
31
32
33
# File 'lib/rack_session_manipulation/capybara.rb', line 30

def session=(hash)
  data = { 'session_data' => RackSessionManipulation.encode(hash) }
  driver_method_fallback(:put, RackSessionManipulation.config.path, data)
end