File: initialize_spec.rb

package info (click to toggle)
jruby 1.7.26-1%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 84,572 kB
  • sloc: ruby: 669,910; java: 253,056; xml: 35,152; ansic: 9,187; yacc: 7,267; cpp: 5,244; sh: 1,036; makefile: 345; jsp: 48; tcl: 40
file content (172 lines) | stat: -rw-r--r-- 5,355 bytes parent folder | download
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
require File.expand_path('../../../spec_helper', __FILE__)
require 'cgi'

describe "CGI#initialize" do
  it "is private" do
    CGI.should have_private_instance_method(:initialize)
  end
end

describe "CGI#initialize when passed no arguments" do
  before(:each) do
    ENV['REQUEST_METHOD'], @old_request_method = "GET", ENV['REQUEST_METHOD']
    @cgi = CGI.allocate
  end

  after(:each) do
    ENV['REQUEST_METHOD'] = @old_request_method
  end

  it "extends self with CGI::QueryExtension" do
    @cgi.send(:initialize)
    @cgi.should be_kind_of(CGI::QueryExtension)
  end

  it "does not extend self with CGI::HtmlExtension" do
    @cgi.send(:initialize)
    @cgi.should_not be_kind_of(CGI::HtmlExtension)
  end

  it "does not extend self with any of the other HTML modules" do
    @cgi.send(:initialize)
    @cgi.should_not be_kind_of(CGI::Html3)
    @cgi.should_not be_kind_of(CGI::HtmlExtension)
    @cgi.should_not be_kind_of(CGI::Html4)
    @cgi.should_not be_kind_of(CGI::Html4Tr)
    @cgi.should_not be_kind_of(CGI::Html4Fr)
  end

  it "sets #cookies based on ENV['HTTP_COOKIE']" do
    begin
      old_env, ENV["HTTP_COOKIE"] = ENV["HTTP_COOKIE"], "test=test yay"
      @cgi.send(:initialize)
      @cgi.cookies.should == { "test"=>[ "test yay" ] }
    ensure
      ENV["HTTP_COOKIE"] = old_env
    end
  end

  it "sets #params based on ENV['QUERY_STRING'] when ENV['REQUEST_METHOD'] is GET" do
    begin
      old_env_query, ENV["QUERY_STRING"] = ENV["QUERY_STRING"], "?test=a&test2=b"
      old_env_method, ENV["REQUEST_METHOD"] = ENV["REQUEST_METHOD"], "GET"
      @cgi.send(:initialize)
      @cgi.params.should == { "test2" => ["b"], "?test" => ["a"] }
    ensure
      ENV["QUERY_STRING"] = old_env_query
      ENV["REQUEST_METHOD"] = old_env_method
    end
  end

  it "sets #params based on ENV['QUERY_STRING'] when ENV['REQUEST_METHOD'] is HEAD" do
    begin
      old_env_query, ENV["QUERY_STRING"] = ENV["QUERY_STRING"], "?test=a&test2=b"
      old_env_method, ENV["REQUEST_METHOD"] = ENV["REQUEST_METHOD"], "HEAD"
      @cgi.send(:initialize)
      @cgi.params.should == { "test2" => ["b"], "?test" => ["a"] }
    ensure
      ENV["QUERY_STRING"] = old_env_query
      ENV["REQUEST_METHOD"] = old_env_method
    end
  end
end

describe "CGI#initialize when passed type" do
  before(:each) do
    ENV['REQUEST_METHOD'], @old_request_method = "GET", ENV['REQUEST_METHOD']
    @cgi = CGI.allocate
  end

  after(:each) do
    ENV['REQUEST_METHOD'] = @old_request_method
  end


  it "extends self with CGI::QueryExtension" do
    @cgi.send(:initialize, "test")
    @cgi.should be_kind_of(CGI::QueryExtension)
  end

  it "extends self with CGI::QueryExtension, CGI::Html3 and CGI::HtmlExtension when the passed type is 'html3'" do
    @cgi.send(:initialize, "html3")
    @cgi.should be_kind_of(CGI::Html3)
    @cgi.should be_kind_of(CGI::HtmlExtension)
    @cgi.should be_kind_of(CGI::QueryExtension)

    @cgi.should_not be_kind_of(CGI::Html4)
    @cgi.should_not be_kind_of(CGI::Html4Tr)
    @cgi.should_not be_kind_of(CGI::Html4Fr)
  end

  it "extends self with CGI::QueryExtension, CGI::Html4 and CGI::HtmlExtension when the passed type is 'html4'" do
    @cgi.send(:initialize, "html4")
    @cgi.should be_kind_of(CGI::Html4)
    @cgi.should be_kind_of(CGI::HtmlExtension)
    @cgi.should be_kind_of(CGI::QueryExtension)

    @cgi.should_not be_kind_of(CGI::Html3)
    @cgi.should_not be_kind_of(CGI::Html4Tr)
    @cgi.should_not be_kind_of(CGI::Html4Fr)
  end

  it "extends self with CGI::QueryExtension, CGI::Html4Tr and CGI::HtmlExtension when the passed type is 'html4Tr'" do
    @cgi.send(:initialize, "html4Tr")
    @cgi.should be_kind_of(CGI::Html4Tr)
    @cgi.should be_kind_of(CGI::HtmlExtension)
    @cgi.should be_kind_of(CGI::QueryExtension)

    @cgi.should_not be_kind_of(CGI::Html3)
    @cgi.should_not be_kind_of(CGI::Html4)
    @cgi.should_not be_kind_of(CGI::Html4Fr)
  end

  it "extends self with CGI::QueryExtension, CGI::Html4Tr, CGI::Html4Fr and CGI::HtmlExtension when the passed type is 'html4Fr'" do
    @cgi.send(:initialize, "html4Fr")
    @cgi.should be_kind_of(CGI::Html4Tr)
    @cgi.should be_kind_of(CGI::Html4Fr)
    @cgi.should be_kind_of(CGI::HtmlExtension)
    @cgi.should be_kind_of(CGI::QueryExtension)

    @cgi.should_not be_kind_of(CGI::Html3)
    @cgi.should_not be_kind_of(CGI::Html4)
  end
end

describe "CGI#initialize when CGI_PARAMS is set" do
  before(:each) do
    ENV['REQUEST_METHOD'], @old_request_method = "GET", ENV['REQUEST_METHOD']
    @cgi = CGI.allocate
  end

  after(:each) do
    ENV['REQUEST_METHOD'] = @old_request_method
  end


  ruby_version_is "" ... "1.9" do
    it "prints out a warning" do
      begin
	CGI_PARAMS = { "test" => "test" }
	CGI_COOKIES = [ "cookie!" ]
	lambda { @cgi.send(:initialize) }.should complain("do not use CGI_PARAMS and CGI_COOKIES\n")
      ensure
	Object.send(:remove_const, :CGI_PARAMS)
	Object.send(:remove_const, :CGI_COOKIES)
      end
    end

    it "sets #cookies and #params to the contents of CGI_PARAMS and CGI_COOKIES" do
      begin
	CGI_PARAMS = { "test" => "test" }
	CGI_COOKIES = [ "cookie!" ]

	@cgi.send(:initialize)
	@cgi.params.should == CGI_PARAMS
	@cgi.cookies.should == CGI_COOKIES
      ensure
	Object.send(:remove_const, :CGI_PARAMS)
	Object.send(:remove_const, :CGI_COOKIES)
      end
    end
  end
end