summaryrefslogtreecommitdiff
path: root/test/spec_session_abstract_id.rb
blob: 9812b530b724d05cba9e40257ad33a67b7e60f2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# frozen_string_literal: true

require_relative 'helper'
### WARNING: there be hax in this file.

require_relative '../lib/rack/session/abstract/id'
separate_testing do
  require_relative '../lib/rack/request'
end

describe Rack::Session::Abstract::ID do
  attr_reader :id

  def setup
    super
    @id = Rack::Session::Abstract::ID
  end

  it "use securerandom" do
    assert_equal ::SecureRandom, id::DEFAULT_OPTIONS[:secure_random]

    id = @id.new nil
    assert_equal ::SecureRandom, id.sid_secure
  end

  it "allow to use another securerandom provider" do
    secure_random = Class.new do
      def hex(*args)
        'fake_hex'
      end
    end
    id = Rack::Session::Abstract::ID.new nil, secure_random: secure_random.new
    id.send(:generate_sid).must_equal 'fake_hex'
  end

  it "should warn when subclassing" do
    verbose = $VERBOSE
    begin
      $VERBOSE = true
      warn_arg = nil
      @id.define_singleton_method(:warn) do |arg|
        warn_arg = arg
      end
      c = Class.new(@id)
      regexp = /is inheriting from Rack::Session::Abstract::ID.  Inheriting from Rack::Session::Abstract::ID is deprecated, please inherit from Rack::Session::Abstract::Persisted instead/
      warn_arg.must_match(regexp)

      warn_arg = nil
      c = Class.new(c)
      warn_arg.must_be_nil
    ensure
      $VERBOSE = verbose
      @id.singleton_class.send(:remove_method, :warn)
    end
  end

  it "#find_session should find session in request" do
    id = @id.new(nil)
    def id.get_session(env, sid)
      [env['rack.session'], generate_sid]
    end
    req = Rack::Request.new('rack.session' => {})
    session, sid = id.find_session(req, nil)
    session.must_equal({})
    sid.must_match(/\A\h+\z/)
  end

  it "#write_session should write session to request" do
    id = @id.new(nil)
    def id.set_session(env, sid, session, options)
      [env, sid, session, options]
    end
    req = Rack::Request.new({})
    id.write_session(req, 1, 2, 3).must_equal [{}, 1, 2, 3]
  end

  it "#delete_session should remove session from request" do
    id = @id.new(nil)
    def id.destroy_session(env, sid, options)
      [env, sid, options]
    end
    req = Rack::Request.new({})
    id.delete_session(req, 1, 2).must_equal [{}, 1, 2]
  end
end