Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
210 changes: 105 additions & 105 deletions src/spec/ruby/action_controller/session/java_servlet_store_spec.rb

Large diffs are not rendered by default.

90 changes: 45 additions & 45 deletions src/spec/ruby/cgi/session/java_servlet_store_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
before :each do
@session = double "servlet session"
@request = double "servlet request"
@options = {"java_servlet_request" => @request}
@options = { "java_servlet_request" => @request }
end

def session_store
Expand All @@ -29,47 +29,47 @@ def session_store

describe "#restore" do
it "should do nothing if no session established" do
@request.should_receive(:getSession).and_return nil
session_store.restore.should == {}
expect(@request).to receive(:getSession).and_return nil
expect(session_store.restore).to eq({})
end

it "should do nothing if the session does not have anything in it" do
@request.should_receive(:getSession).with(false).and_return @session
@session.should_receive(:getAttributeNames).and_return []
session_store.restore.should == {}
expect(@request).to receive(:getSession).with(false).and_return @session
expect(@session).to receive(:getAttributeNames).and_return []
expect(session_store.restore).to eq({})
end

it "should retrieve the marshalled session from the java session" do
hash = {"foo" => 1, "bar" => true}
hash = { "foo" => 1, "bar" => true }
marshal_data = Marshal.dump hash
@request.should_receive(:getSession).with(false).and_return @session
@session.should_receive(:getAttributeNames).and_return(
expect(@request).to receive(:getSession).with(false).and_return @session
expect(@session).to receive(:getAttributeNames).and_return(
[CGI::Session::JavaServletStore::RAILS_SESSION_KEY])
@session.should_receive(:getAttribute).with(
expect(@session).to receive(:getAttribute).with(
CGI::Session::JavaServletStore::RAILS_SESSION_KEY).and_return marshal_data.to_java_bytes
session_store.restore.should == hash
expect(session_store.restore).to eq hash
end

it "should retrieve values from other keys in the session" do
hash = {"foo" => 1, "bar" => true}
@request.should_receive(:getSession).with(false).and_return @session
@session.should_receive(:getAttributeNames).and_return ["foo", "bar"]
@session.should_receive(:getAttribute).with("foo").and_return hash["foo"]
@session.should_receive(:getAttribute).with("bar").and_return hash["bar"]
session_store.restore.should == hash
hash = { "foo" => 1, "bar" => true }
expect(@request).to receive(:getSession).with(false).and_return @session
expect(@session).to receive(:getAttributeNames).and_return ["foo", "bar"]
expect(@session).to receive(:getAttribute).with("foo").and_return hash["foo"]
expect(@session).to receive(:getAttribute).with("bar").and_return hash["bar"]
expect(session_store.restore).to eq hash
end

it "should retrieve java objects in the session" do
@request.should_receive(:getSession).with(false).and_return @session
@session.should_receive(:getAttributeNames).and_return ["foo"]
@session.should_receive(:getAttribute).with("foo").and_return java.lang.Object.new
session_store.restore["foo"].should be_instance_of(java.lang.Object)
expect(@request).to receive(:getSession).with(false).and_return @session
expect(@session).to receive(:getAttributeNames).and_return ["foo"]
expect(@session).to receive(:getAttribute).with("foo").and_return java.lang.Object.new
expect(session_store.restore["foo"]).to be_instance_of(java.lang.Object)
end
end

describe "#update" do
before :each do
@request.should_receive(:getSession).with(true).and_return @session
expect(@request).to receive(:getSession).with(true).and_return @session
end

it "should do nothing if the session data is empty" do
Expand All @@ -79,26 +79,26 @@ def session_store
end

it "should marshal the session to the java session" do
@session.should_receive(:setAttribute).with(
expect(@session).to receive(:setAttribute).with(
CGI::Session::JavaServletStore::RAILS_SESSION_KEY,
an_instance_of(Java::byte[]))
session_store.update
end

it "should store entries with string keys and values as java session attributes" do
@session.should_receive(:setAttribute).with(
expect(@session).to receive(:setAttribute).with(
CGI::Session::JavaServletStore::RAILS_SESSION_KEY, anything)
@session.should_receive(:setAttribute).with("foo", "bar")
expect(@session).to receive(:setAttribute).with("foo", "bar")
store = session_store
store.data["foo"] = "bar"
store.update
end

it "should store numeric, nil, or boolean values as java session attributes" do
@session.should_receive(:setAttribute).with("foo", true)
@session.should_receive(:setAttribute).with("bar", 20)
@session.should_receive(:setAttribute).with("baz", nil)
@session.should_receive(:setAttribute).with("quux", false)
expect(@session).to receive(:setAttribute).with("foo", true)
expect(@session).to receive(:setAttribute).with("bar", 20)
expect(@session).to receive(:setAttribute).with("baz", nil)
expect(@session).to receive(:setAttribute).with("quux", false)
store = session_store
store.data.clear
store.data["foo"] = true
Expand All @@ -109,15 +109,15 @@ def session_store
end

it "should store java object values as java session attributes" do
@session.should_receive(:setAttribute).with("foo", an_instance_of(java.lang.Object))
expect(@session).to receive(:setAttribute).with("foo", an_instance_of(java.lang.Object))
store = session_store
store.data.clear
store.data["foo"] = java.lang.Object.new
store.update
end

it "should not store entries with non-primitive values in the java session" do
@session.should_receive(:setAttribute).with(
expect(@session).to receive(:setAttribute).with(
CGI::Session::JavaServletStore::RAILS_SESSION_KEY, anything)
store = session_store
store.data["foo"] = Object.new
Expand All @@ -128,8 +128,8 @@ def session_store

describe "#close" do
it "should do the same as update" do
@request.should_receive(:getSession).with(true).and_return @session
@session.should_receive(:setAttribute).with(
expect(@request).to receive(:getSession).with(true).and_return @session
expect(@session).to receive(:setAttribute).with(
CGI::Session::JavaServletStore::RAILS_SESSION_KEY,
an_instance_of(Java::byte[]))
session_store.close
Expand All @@ -138,20 +138,20 @@ def session_store

describe "#delete" do
it "should invalidate the servlet session" do
@request.should_receive(:getSession).with(false).and_return @session
@session.should_receive(:invalidate)
expect(@request).to receive(:getSession).with(false).and_return @session
expect(@session).to receive(:invalidate)
session_store.delete
end

it "should do nothing if no session is established" do
@request.should_receive(:getSession).with(false).and_return nil
expect(@request).to receive(:getSession).with(false).and_return nil
session_store.delete
end
end

describe "#generate_digest" do
before :each do
@request.should_receive(:getSession).with(true).and_return @session
expect(@request).to receive(:getSession).with(true).and_return @session
@dbman = session_store
end

Expand All @@ -160,17 +160,17 @@ def hmac(key, data)
end

it "should look for the secret in the java session" do
@session.should_receive(:getAttribute).with("__rails_secret").and_return "secret"
@dbman.generate_digest("key").should == hmac("secret", "key")
expect(@session).to receive(:getAttribute).with("__rails_secret").and_return "secret"
expect(@dbman.generate_digest("key")).to eq(hmac("secret", "key"))
end

it "should generate a secret from the java session id and last accessed time" do
OpenSSL::Random.should_receive(:random_bytes).with(32).and_return "random"
@session.should_receive(:getAttribute).with("__rails_secret").and_return nil
@session.should_receive(:getId).and_return "abc"
@session.should_receive(:getLastAccessedTime).and_return 123
@session.should_receive(:setAttribute).with("__rails_secret", "abcrandom123")
@dbman.generate_digest("key").should == hmac("abcrandom123", "key")
expect(OpenSSL::Random).to receive(:random_bytes).with(32).and_return "random"
expect(@session).to receive(:getAttribute).with("__rails_secret").and_return nil
expect(@session).to receive(:getId).and_return "abc"
expect(@session).to receive(:getLastAccessedTime).and_return 123
expect(@session).to receive(:setAttribute).with("__rails_secret", "abcrandom123")
expect(@dbman.generate_digest("key")).to eq(hmac("abcrandom123", "key"))
end unless JRUBY_VERSION < '1.7.0'
end
end
Loading