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.



56
57
58
59
60
61
62
63
64
# File 'lib/rack_session_manipulation/capybara.rb', line 56

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.



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

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.



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

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

#session_manipulation_configvoid



37
38
39
# File 'lib/rack_session_manipulation/capybara.rb', line 37

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.



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

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