summaryrefslogtreecommitdiff
path: root/spec/ruby/library/cgi/cookie/initialize_spec.rb
blob: 4b6e104b10cdbac950343a35ee742f43d65792b7 (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
require_relative '../../../spec_helper'
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
    -> { @cookie.send(:initialize, {}) }.should raise_error(ArgumentError)
    -> { @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