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.



59
60
61
62
63
64
65
66
67
# File 'lib/rack_session_manipulation/capybara.rb', line 59

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

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

#sessionHash

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

Returns:

  • (Hash)

    Hash of the session



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

def session
  driver.get(session_manipulation_config.path)
  session_manipulation_config.encoder.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.



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

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

#session_manipulation_configRackSessionManipulation::Config

Expose an instance of the middleware’s configuration object to Capybara to allow configuration alongside the DSL.



40
41
42
# File 'lib/rack_session_manipulation/capybara.rb', line 40

def session_manipulation_config
  @rsm_config ||= Config.new
end

#session_resetvoid

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



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

def session_reset
  driver_method_fallback(:delete, session_manipulation_config.path, {})
end