Skip to content

Commit

Permalink
Add ActionDispatch::Request::Session#store method.
Browse files Browse the repository at this point in the history
Rack specification states that a hash-like object
stored in environment with `rack.session` key
MUST implement `store/2` method.

Without the alias, this test fails with the following:

```
Exception while processing request: Rack::Lint::LintError: session #<ActionDispatch::Request::Session:0x3570 not yet loaded> must respond to store and []=
    /home/zzak/.rbenv/versions/3.3.6/lib/ruby/gems/3.3.0/gems/rack-3.1.8/lib/rack/lint.rb:206:in `check_environment'
    /home/zzak/.rbenv/versions/3.3.6/lib/ruby/gems/3.3.0/gems/rack-3.1.8/lib/rack/lint.rb:63:in `response'
    /home/zzak/.rbenv/versions/3.3.6/lib/ruby/gems/3.3.0/gems/rack-3.1.8/lib/rack/lint.rb:41:in `call'
    lib/action_dispatch/middleware/cookies.rb:706:in `call'
    /home/zzak/.rbenv/versions/3.3.6/lib/ruby/gems/3.3.0/gems/rack-session-2.0.0/lib/rack/session/abstract/id.rb:272:in `context'
    /home/zzak/.rbenv/versions/3.3.6/lib/ruby/gems/3.3.0/gems/rack-session-2.0.0/lib/rack/session/abstract/id.rb:266:in `call'
    /home/zzak/.rbenv/versions/3.3.6/lib/ruby/gems/3.3.0/gems/rack-3.1.8/lib/rack/head.rb:15:in `call'
    /home/zzak/.rbenv/versions/3.3.6/lib/ruby/gems/3.3.0/gems/rack-3.1.8/lib/rack/method_override.rb:28:in `call'
    lib/action_dispatch/middleware/cookies.rb:706:in `call'
    lib/action_dispatch/middleware/callbacks.rb:31:in `block in call'
    /home/zzak/code/rails/activesupport/lib/active_support/callbacks.rb:100:in `run_callbacks'
    lib/action_dispatch/middleware/callbacks.rb:30:in `call'
    lib/action_dispatch/middleware/actionable_exceptions.rb:18:in `call'
    lib/action_dispatch/middleware/debug_exceptions.rb:31:in `call'
    lib/action_dispatch/middleware/show_exceptions.rb:32:in `call'
    test/abstract_unit.rb:110:in `call'
    /home/zzak/.rbenv/versions/3.3.6/lib/ruby/gems/3.3.0/gems/rack-test-2.1.0/lib/rack/test.rb:360:in `process_request'
    /home/zzak/.rbenv/versions/3.3.6/lib/ruby/gems/3.3.0/gems/rack-test-2.1.0/lib/rack/test.rb:153:in `request'
    lib/action_dispatch/testing/integration.rb:297:in `process'
    lib/action_dispatch/testing/integration.rb:19:in `get'
    lib/action_dispatch/testing/integration.rb:388:in `get'
    test/dispatch/request/session_test.rb:224:in `test_session_follows_rack_api_contract_1'
```

Co-authored-by: viralpraxis <[email protected]>
Co-authored-by: zzak <[email protected]>
Co-authored-by: Hartley McGuire <[email protected]>
  • Loading branch information
3 people committed Nov 18, 2024
1 parent 67c6ef2 commit 226d7c6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
1 change: 1 addition & 0 deletions actionpack/lib/action_dispatch/request/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ def []=(key, value)
load_for_write!
@delegate[key.to_s] = value
end
alias store []=

# Clears the session.
def clear
Expand Down
12 changes: 11 additions & 1 deletion actionpack/test/dispatch/request/session_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ def test_destroy
assert_empty s
end

def test_store
s = Session.create(store, req, {})
s.store("foo", "bar")
assert_equal "bar", s["foo"]
end

def test_keys
s = Session.create(store, req, {})
s["rails"] = "ftw"
Expand Down Expand Up @@ -205,7 +211,11 @@ def call(env)
end

def app
@app ||= RoutedRackApp.new(Router)
@app ||= RoutedRackApp.new(Router) do |middleware|
@cache = ActiveSupport::Cache::MemoryStore.new
middleware.use ActionDispatch::Session::CacheStore, key: "_session_id", cache: @cache
middleware.use Rack::Lint
end
end

def test_session_follows_rack_api_contract_1
Expand Down

0 comments on commit 226d7c6

Please sign in to comment.