diff options
Diffstat (limited to 'spec/rubyspec/library/cgi/cookie')
-rw-r--r-- | spec/rubyspec/library/cgi/cookie/domain_spec.rb | 23 | ||||
-rw-r--r-- | spec/rubyspec/library/cgi/cookie/expires_spec.rb | 23 | ||||
-rw-r--r-- | spec/rubyspec/library/cgi/cookie/initialize_spec.rb | 147 | ||||
-rw-r--r-- | spec/rubyspec/library/cgi/cookie/name_spec.rb | 23 | ||||
-rw-r--r-- | spec/rubyspec/library/cgi/cookie/parse_spec.rb | 18 | ||||
-rw-r--r-- | spec/rubyspec/library/cgi/cookie/path_spec.rb | 23 | ||||
-rw-r--r-- | spec/rubyspec/library/cgi/cookie/secure_spec.rb | 70 | ||||
-rw-r--r-- | spec/rubyspec/library/cgi/cookie/to_s_spec.rb | 29 | ||||
-rw-r--r-- | spec/rubyspec/library/cgi/cookie/value_spec.rb | 76 |
9 files changed, 432 insertions, 0 deletions
diff --git a/spec/rubyspec/library/cgi/cookie/domain_spec.rb b/spec/rubyspec/library/cgi/cookie/domain_spec.rb new file mode 100644 index 0000000000..05137dd4d1 --- /dev/null +++ b/spec/rubyspec/library/cgi/cookie/domain_spec.rb @@ -0,0 +1,23 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require 'cgi' + +describe "CGI::Cookie#domain" do + it "returns self's domain" do + cookie = CGI::Cookie.new("test-cookie") + cookie.domain.should be_nil + + cookie = CGI::Cookie.new("name" => "test-cookie", "domain" => "example.com") + cookie.domain.should == "example.com" + end +end + +describe "CGI::Cookie#domain=" do + it "sets self's domain" do + cookie = CGI::Cookie.new("test-cookie") + cookie.domain = "test.com" + cookie.domain.should == "test.com" + + cookie.domain = "example.com" + cookie.domain.should == "example.com" + end +end diff --git a/spec/rubyspec/library/cgi/cookie/expires_spec.rb b/spec/rubyspec/library/cgi/cookie/expires_spec.rb new file mode 100644 index 0000000000..89f18fe071 --- /dev/null +++ b/spec/rubyspec/library/cgi/cookie/expires_spec.rb @@ -0,0 +1,23 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require 'cgi' + +describe "CGI::Cookie#expires" do + it "returns self's expiration date" do + cookie = CGI::Cookie.new("test-cookie") + cookie.expires.should be_nil + + cookie = CGI::Cookie.new("name" => "test-cookie", "expires" => Time.at(1196524602)) + cookie.expires.should == Time.at(1196524602) + end +end + +describe "CGI::Cookie#expires=" do + it "sets self's expiration date" do + cookie = CGI::Cookie.new("test-cookie") + cookie.expires = Time.at(1196524602) + cookie.expires.should == Time.at(1196524602) + + cookie.expires = Time.at(1196525000) + cookie.expires.should == Time.at(1196525000) + end +end diff --git a/spec/rubyspec/library/cgi/cookie/initialize_spec.rb b/spec/rubyspec/library/cgi/cookie/initialize_spec.rb new file mode 100644 index 0000000000..02f2a8ed2b --- /dev/null +++ b/spec/rubyspec/library/cgi/cookie/initialize_spec.rb @@ -0,0 +1,147 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require 'cgi' + +describe "CGI::Cookie#initialize when passed String" do + before :each do + @cookie = CGI::Cookie.allocate + end + + it "sets the self's name to the passed String" do + @cookie.send(:initialize, "test-cookie") + @cookie.name.should == "test-cookie" + end + + it "sets the self's value to an empty Array" do + @cookie.send(:initialize, "test-cookie") + @cookie.value.should == [] + end + + it "sets self to a non-secure cookie" do + @cookie.send(:initialize, "test") + @cookie.secure.should be_false + end + + it "does set self's path to an empty String when ENV[\"SCRIPT_NAME\"] is not set" do + @cookie.send(:initialize, "test-cookie") + @cookie.path.should == "" + end + + it "does set self's path based on ENV[\"SCRIPT_NAME\"] when ENV[\"SCRIPT_NAME\"] is set" do + old_script_name = ENV["SCRIPT_NAME"] + + begin + ENV["SCRIPT_NAME"] = "some/path/script.rb" + @cookie.send(:initialize, "test-cookie") + @cookie.path.should == "some/path/" + + ENV["SCRIPT_NAME"] = "script.rb" + @cookie.send(:initialize, "test-cookie") + @cookie.path.should == "" + + ENV["SCRIPT_NAME"] = nil + @cookie.send(:initialize, "test-cookie") + @cookie.path.should == "" + ensure + ENV["SCRIPT_NAME"] = old_script_name + end + end + + it "does not set self's expiration date" do + @cookie.expires.should be_nil + end + + it "does not set self's domain" do + @cookie.domain.should be_nil + end +end + +describe "CGI::Cookie#initialize when passed Hash" do + before :each do + @cookie = CGI::Cookie.allocate + end + + it "sets self's contents based on the passed Hash" do + @cookie.send(:initialize, + 'name' => 'test-cookie', + 'value' => ["one", "two", "three"], + 'path' => 'some/path/', + 'domain' => 'example.com', + 'expires' => Time.at(1196524602), + 'secure' => true) + + @cookie.name.should == "test-cookie" + @cookie.value.should == ["one", "two", "three"] + @cookie.path.should == "some/path/" + @cookie.domain.should == "example.com" + @cookie.expires.should == Time.at(1196524602) + @cookie.secure.should be_true + end + + it "does set self's path based on ENV[\"SCRIPT_NAME\"] when the Hash has no 'path' entry" do + old_script_name = ENV["SCRIPT_NAME"] + + begin + ENV["SCRIPT_NAME"] = "some/path/script.rb" + @cookie.send(:initialize, 'name' => 'test-cookie') + @cookie.path.should == "some/path/" + + ENV["SCRIPT_NAME"] = "script.rb" + @cookie.send(:initialize, 'name' => 'test-cookie') + @cookie.path.should == "" + + ENV["SCRIPT_NAME"] = nil + @cookie.send(:initialize, 'name' => 'test-cookie') + @cookie.path.should == "" + ensure + ENV["SCRIPT_NAME"] = old_script_name + end + end + + it "tries to convert the Hash's 'value' to an Array using #Array" do + obj = mock("Converted To Array") + obj.should_receive(:to_ary).and_return(["1", "2", "3"]) + @cookie.send(:initialize, + 'name' => 'test-cookie', + 'value' => obj) + @cookie.value.should == [ "1", "2", "3" ] + + obj = mock("Converted To Array") + obj.should_receive(:to_a).and_return(["one", "two", "three"]) + @cookie.send(:initialize, + 'name' => 'test-cookie', + 'value' => obj) + @cookie.value.should == [ "one", "two", "three" ] + + obj = mock("Put into an Array") + @cookie.send(:initialize, + 'name' => 'test-cookie', + 'value' => obj) + @cookie.value.should == [ obj ] + end + + it "raises a ArgumentError when the passed Hash has no 'name' entry" do + lambda { @cookie.send(:initialize, {}) }.should raise_error(ArgumentError) + lambda { @cookie.send(:initialize, "value" => "test") }.should raise_error(ArgumentError) + end +end + +describe "CGI::Cookie#initialize when passed String, values ..." do + before :each do + @cookie = CGI::Cookie.allocate + end + + it "sets the self's name to the passed String" do + @cookie.send(:initialize, "test-cookie", "one", "two", "three") + @cookie.name.should == "test-cookie" + end + + it "sets the self's value to an Array containing all passed values" do + @cookie.send(:initialize, "test-cookie", "one", "two", "three") + @cookie.value.should == ["one", "two", "three"] + end + + it "sets self to a non-secure cookie" do + @cookie.send(:initialize, "test", "one", "two", "three") + @cookie.secure.should be_false + end +end diff --git a/spec/rubyspec/library/cgi/cookie/name_spec.rb b/spec/rubyspec/library/cgi/cookie/name_spec.rb new file mode 100644 index 0000000000..8a6be95944 --- /dev/null +++ b/spec/rubyspec/library/cgi/cookie/name_spec.rb @@ -0,0 +1,23 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require 'cgi' + +describe "CGI::Cookie#name" do + it "returns self's name" do + cookie = CGI::Cookie.new("test-cookie") + cookie.name.should == "test-cookie" + + cookie = CGI::Cookie.new("name" => "another cookie") + cookie.name.should == "another cookie" + end +end + +describe "CGI::Cookie#name=" do + it "sets self's expiration date" do + cookie = CGI::Cookie.new("test-cookie") + cookie.name = "another name" + cookie.name.should == "another name" + + cookie.name = "and one more" + cookie.name.should == "and one more" + end +end diff --git a/spec/rubyspec/library/cgi/cookie/parse_spec.rb b/spec/rubyspec/library/cgi/cookie/parse_spec.rb new file mode 100644 index 0000000000..dc8498dcb4 --- /dev/null +++ b/spec/rubyspec/library/cgi/cookie/parse_spec.rb @@ -0,0 +1,18 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require 'cgi' + +describe "CGI::Cookie.parse" do + it "parses a raw cookie string into a hash of Cookies" do + expected = { "test-cookie" => ["one", "two", "three"] } + CGI::Cookie.parse("test-cookie=one&two&three").should == expected + + expected = { "second cookie" => ["three", "four"], "first cookie" => ["one", "two"] } + CGI::Cookie.parse("first cookie=one&two;second cookie=three&four").should == expected + end + + it "unescapes the Cookie values" do + cookie = "test-cookie=+%21%22%23%24%25%26%27%28%29%2A%2B%2C-.%2F0123456789%3A%3B%3C%3D%3E%3F%40ABCDEFGHIJKLMNOPQRSTUVWXYZ%5B%5C%5D%5E_%60abcdefghijklmnopqrstuvwxyz%7B%7C%7D%7E" + expected = { "test-cookie" => [ " !\"\#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~" ] } + CGI::Cookie.parse(cookie).should == expected + end +end diff --git a/spec/rubyspec/library/cgi/cookie/path_spec.rb b/spec/rubyspec/library/cgi/cookie/path_spec.rb new file mode 100644 index 0000000000..2a21a55264 --- /dev/null +++ b/spec/rubyspec/library/cgi/cookie/path_spec.rb @@ -0,0 +1,23 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require 'cgi' + +describe "CGI::Cookie#path" do + it "returns self's path" do + cookie = CGI::Cookie.new("test-cookie") + cookie.path.should == "" + + cookie = CGI::Cookie.new("name" => "test-cookie", "path" => "/some/path/") + cookie.path.should == "/some/path/" + end +end + +describe "CGI::Cookie#path=" do + it "sets self's path" do + cookie = CGI::Cookie.new("test-cookie") + cookie.path = "/some/path/" + cookie.path.should == "/some/path/" + + cookie.path = "/another/path/" + cookie.path.should == "/another/path/" + end +end diff --git a/spec/rubyspec/library/cgi/cookie/secure_spec.rb b/spec/rubyspec/library/cgi/cookie/secure_spec.rb new file mode 100644 index 0000000000..37e9dbfda9 --- /dev/null +++ b/spec/rubyspec/library/cgi/cookie/secure_spec.rb @@ -0,0 +1,70 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require 'cgi' + +describe "CGI::Cookie#secure" do + before :each do + @cookie = CGI::Cookie.new("test-cookie") + end + + it "returns whether self is a secure cookie or not" do + @cookie.secure = true + @cookie.secure.should be_true + + @cookie.secure = false + @cookie.secure.should be_false + end +end + +describe "CGI::Cookie#secure= when passed true" do + before :each do + @cookie = CGI::Cookie.new("test-cookie") + end + + it "returns true" do + (@cookie.secure = true).should be_true + end + + it "sets self to a secure cookie" do + @cookie.secure = true + @cookie.secure.should be_true + end +end + +describe "CGI::Cookie#secure= when passed false" do + before :each do + @cookie = CGI::Cookie.new("test-cookie") + end + + it "returns false" do + (@cookie.secure = false).should be_false + end + + it "sets self to a non-secure cookie" do + @cookie.secure = false + @cookie.secure.should be_false + end +end + +describe "CGI::Cookie#secure= when passed Object" do + before :each do + @cookie = CGI::Cookie.new("test-cookie") + end + + it "does not change self's secure value" do + @cookie.secure = false + + @cookie.secure = Object.new + @cookie.secure.should be_false + + @cookie.secure = "Test" + @cookie.secure.should be_false + + @cookie.secure = true + + @cookie.secure = Object.new + @cookie.secure.should be_true + + @cookie.secure = "Test" + @cookie.secure.should be_true + end +end diff --git a/spec/rubyspec/library/cgi/cookie/to_s_spec.rb b/spec/rubyspec/library/cgi/cookie/to_s_spec.rb new file mode 100644 index 0000000000..61eb9fb984 --- /dev/null +++ b/spec/rubyspec/library/cgi/cookie/to_s_spec.rb @@ -0,0 +1,29 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require 'cgi' + +describe "CGI::Cookie#to_s" do + it "returns a String representation of self" do + cookie = CGI::Cookie.new("test-cookie") + cookie.to_s.should == "test-cookie=; path=" + + cookie = CGI::Cookie.new("test-cookie", "value") + cookie.to_s.should == "test-cookie=value; path=" + + cookie = CGI::Cookie.new("test-cookie", "one", "two", "three") + cookie.to_s.should == "test-cookie=one&two&three; path=" + + cookie = CGI::Cookie.new( + 'name' => 'test-cookie', + 'value' => ["one", "two", "three"], + 'path' => 'some/path/', + 'domain' => 'example.com', + 'expires' => Time.at(1196524602), + 'secure' => true) + cookie.to_s.should == "test-cookie=one&two&three; domain=example.com; path=some/path/; expires=Sat, 01 Dec 2007 15:56:42 GMT; secure" + end + + it "escapes the self's values" do + cookie = CGI::Cookie.new("test-cookie", " !\"\#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~") + cookie.to_s.should == "test-cookie=+%21%22%23%24%25%26%27%28%29%2A%2B%2C-.%2F0123456789%3A%3B%3C%3D%3E%3F%40ABCDEFGHIJKLMNOPQRSTUVWXYZ%5B%5C%5D%5E_%60abcdefghijklmnopqrstuvwxyz%7B%7C%7D%7E; path=" + end +end diff --git a/spec/rubyspec/library/cgi/cookie/value_spec.rb b/spec/rubyspec/library/cgi/cookie/value_spec.rb new file mode 100644 index 0000000000..81e3daf7de --- /dev/null +++ b/spec/rubyspec/library/cgi/cookie/value_spec.rb @@ -0,0 +1,76 @@ +require File.expand_path('../../../../spec_helper', __FILE__) +require 'cgi' + +describe "CGI::Cookie#value" do + it "returns self's value" do + cookie = CGI::Cookie.new("test-cookie") + cookie.value.should == [] + + cookie = CGI::Cookie.new("test-cookie", "one") + cookie.value.should == ["one"] + + cookie = CGI::Cookie.new("test-cookie", "one", "two", "three") + cookie.value.should == ["one", "two", "three"] + + cookie = CGI::Cookie.new("name" => "test-cookie", "value" => ["one", "two", "three"]) + cookie.value.should == ["one", "two", "three"] + end + + it "is in synch with self" do + fail = [] + [ + :pop, + :shift, + [:<<, "Hello"], + [:push, "Hello"], + [:unshift, "World"], + [:replace, ["A", "B"]], + [:[]=, 1, "Set"], + [:delete, "first"], + [:delete_at, 0], + ].each do |method, *args| + cookie1 = CGI::Cookie.new("test-cookie", "first", "second") + cookie2 = CGI::Cookie.new("test-cookie", "first", "second") + cookie1.send(method, *args) + cookie2.value.send(method, *args) + fail << method unless cookie1.value == cookie2.value + end + fail.should be_empty + end +end + +describe "CGI::Cookie#value=" do + before :each do + @cookie = CGI::Cookie.new("test-cookie") + end + + it "sets self's value" do + @cookie.value = ["one"] + @cookie.value.should == ["one"] + + @cookie.value = ["one", "two", "three"] + @cookie.value.should == ["one", "two", "three"] + end + + it "automatically converts the passed Object to an Array using #Array" do + @cookie.value = "test" + @cookie.value.should == ["test"] + + obj = mock("to_a") + obj.should_receive(:to_a).and_return(["1", "2"]) + @cookie.value = obj + @cookie.value.should == ["1", "2"] + + obj = mock("to_ary") + obj.should_receive(:to_ary).and_return(["1", "2"]) + @cookie.value = obj + @cookie.value.should == ["1", "2"] + end + + it "does keep self and the values in sync" do + @cookie.value = ["one", "two", "three"] + @cookie[0].should == "one" + @cookie[1].should == "two" + @cookie[2].should == "three" + end +end |