cgialt 0.0.2 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,6 @@
1
1
  require 'test/unit'
2
2
  #require 'cgi'
3
- require(ENV['CGI'] || 'cgi')
3
+ require(ENV['CGI'] || 'cgialt')
4
4
  require 'stringio'
5
5
 
6
6
 
@@ -1,6 +1,6 @@
1
1
  require 'test/unit'
2
2
  #require 'cgi'
3
- require(ENV['CGI'] || 'cgi')
3
+ require(ENV['CGI'] || 'cgialt')
4
4
  require 'stringio'
5
5
 
6
6
 
@@ -18,11 +18,19 @@ class CGICoreTest < Test::Unit::TestCase
18
18
 
19
19
 
20
20
  def teardown
21
- @environ.each do |key, val| ENV.delete(key) end
21
+ @environ and @environ.each do |key, val| ENV.delete(key) end
22
22
  $stdout = STDOUT
23
23
  end
24
24
 
25
25
 
26
+ def test_cgi_core_parse
27
+ assert_equal({'foo'=>['bar']}, CGI.parse('foo=bar'))
28
+ assert_equal({'a'=>['10'], 'b'=>['20']}, CGI.parse('a=10&b=20'))
29
+ assert_equal({'noval'=>['']}, CGI.parse('noval='))
30
+ assert_equal({'noeq'=>[nil]}, CGI.parse('noeq')) # same spec as cgi.rb
31
+ end
32
+
33
+
26
34
  def test_cgi_core_params_GET
27
35
  @environ = {
28
36
  'REQUEST_METHOD' => 'GET',
@@ -174,6 +182,8 @@ class CGICoreTest < Test::Unit::TestCase
174
182
  "Content-Language: ja\r\n" +
175
183
  "\r\n" +
176
184
  euc_str
185
+ # actual.encoding => US-ASCII, expected.encoding => ASCII-8BIT
186
+ expected = _convert_encoding(expected, actual)
177
187
  assert_equal(expected, actual)
178
188
  ## shift_jis
179
189
  options = { 'charset'=>'Shift_JIS' }
@@ -186,6 +196,8 @@ class CGICoreTest < Test::Unit::TestCase
186
196
  "Content-Language: ja\r\n" +
187
197
  "\r\n" +
188
198
  sjis_str
199
+ # actual.encoding => US-ASCII, expected.encoding => ASCII-8BIT
200
+ expected = _convert_encoding(expected, actual)
189
201
  assert_equal(expected, actual)
190
202
  ## utf8 (not converted)
191
203
  options = { 'charset'=>'utf8' }
@@ -197,6 +209,8 @@ class CGICoreTest < Test::Unit::TestCase
197
209
  "Content-Length: 22\r\n" +
198
210
  "\r\n" +
199
211
  euc_str
212
+ # actual.encoding => US-ASCII, expected.encoding => ASCII-8BIT
213
+ expected = _convert_encoding(expected, actual)
200
214
  assert_equal(expected, actual)
201
215
  ## language is keeped
202
216
  options = { 'charset'=>'Shift_JIS', 'language'=>'en' }
@@ -215,6 +229,11 @@ class CGICoreTest < Test::Unit::TestCase
215
229
  assert_equal(expected, actual)
216
230
  end
217
231
 
232
+ def _convert_encoding(expected, actual)
233
+ "".respond_to?(:encoding) && actual.encoding != expected.encoding \
234
+ ? expected.force_encoding(actual.encoding) \
235
+ : expected
236
+ end
218
237
 
219
238
  def test_cgi_core_print
220
239
  @environ = {
@@ -1,6 +1,6 @@
1
1
  require 'test/unit'
2
2
  #require 'cgi'
3
- require(ENV['CGI'] || 'cgi')
3
+ require(ENV['CGI'] || 'cgialt')
4
4
 
5
5
 
6
6
  class CGIHeaderTest < Test::Unit::TestCase
@@ -1,6 +1,6 @@
1
1
  require 'test/unit'
2
2
  #require 'cgi'
3
- require(ENV['CGI'] || 'cgi')
3
+ require(ENV['CGI'] || 'cgialt')
4
4
 
5
5
 
6
6
  class CGIModrubyTest < Test::Unit::TestCase
@@ -1,6 +1,6 @@
1
1
  require 'test/unit'
2
2
  #require 'cgi'
3
- require(ENV['CGI'] || 'cgi')
3
+ require(ENV['CGI'] || 'cgialt')
4
4
  require 'tempfile'
5
5
  require 'stringio'
6
6
 
@@ -36,6 +36,16 @@ class MultiPart
36
36
  end
37
37
  attr_reader :boundary
38
38
 
39
+ if "".respond_to?(:encoding) && "".respond_to?(:force_encoding) # Ruby1.9
40
+ def _convert_encoding(str, str2)
41
+ str.force_encoding(str2.encoding)
42
+ end
43
+ else
44
+ def _convert_encoding(str, str2)
45
+ str
46
+ end
47
+ end
48
+
39
49
  def append(name, value, filename=nil, content_type=nil)
40
50
  content_type = detect_content_type(filename) if filename && content_type.nil?
41
51
  s = filename ? "; filename=\"#{filename}\"" : ''
@@ -44,6 +54,7 @@ class MultiPart
44
54
  buf << "Content-Disposition: form-data: name=\"#{name}\"#{s}\r\n"
45
55
  buf << "Content-Type: #{content_type}\r\n" if content_type
46
56
  buf << "\r\n"
57
+ buf = _convert_encoding(buf, value)
47
58
  buf << value
48
59
  buf << "\r\n"
49
60
  return self
@@ -109,7 +120,7 @@ class CGIMultipartTest < Test::Unit::TestCase
109
120
  end
110
121
 
111
122
  def teardown
112
- %[REQUEST_METHOD CONTENT_TYPE CONTENT_LENGTH REQUEST_METHOD].each do |name|
123
+ %w[REQUEST_METHOD CONTENT_TYPE CONTENT_LENGTH REQUEST_METHOD].each do |name|
113
124
  ENV.delete(name)
114
125
  end
115
126
  $stdin.close() if $stdin.is_a?(Tempfile)
@@ -151,7 +162,11 @@ class CGIMultipartTest < Test::Unit::TestCase
151
162
  expected = hash[:value]
152
163
  expected_class = @expected_class || (hash[:value].length < threshold ? StringIO : Tempfile)
153
164
  assert_kind_of(expected_class, cgi[name])
154
- assert_equal(expected, cgi[name].read())
165
+ actual = cgi[name].read()
166
+ if "".respond_to?(:encoding) # Ruby1.9
167
+ expected = expected.force_encoding(actual.encoding)
168
+ end
169
+ assert_equal(expected, actual)
155
170
  assert_equal(hash[:filename] || '', cgi[name].original_filename) #if hash[:filename]
156
171
  assert_equal(hash[:content_type] || '', cgi[name].content_type) #if hash[:content_type]
157
172
  end
@@ -190,7 +205,11 @@ class CGIMultipartTest < Test::Unit::TestCase
190
205
  {:name=>'image1', :value=>_read('large.png'),
191
206
  :filename=>'large.png', :content_type=>'image/png'}, # large image
192
207
  ]
193
- @expected_class = Tempfile
208
+ if "".respond_to?(:encoding) # Ruby1.9
209
+ @expected_class = File
210
+ else
211
+ @expected_class = Tempfile
212
+ end
194
213
  _test_multipart()
195
214
  end
196
215
 
@@ -248,6 +267,9 @@ class CGIMultipartTest < Test::Unit::TestCase
248
267
  :filename=>'file1.html', :content_type=>'text/html'},
249
268
  ]
250
269
  _prepare(@data) do |input|
270
+ if input.respond_to?(:encoding) # Ruby1.9
271
+ input = input.force_encoding('ASCII-8BIT')
272
+ end
251
273
  input2 = input.sub(/--(\r\n)?\z/, "\r\n")
252
274
  assert input2 != input
253
275
  #p input2
@@ -259,6 +281,9 @@ class CGIMultipartTest < Test::Unit::TestCase
259
281
  assert_equal("bad boundary end of body part", ex.message)
260
282
  #
261
283
  _prepare(@data) do |input|
284
+ if input.respond_to?(:encoding) # Ruby1.9
285
+ input = input.force_encoding('ASCII-8BIT')
286
+ end
262
287
  input2 = input.sub(/--(\r\n)?\z/, "")
263
288
  assert input2 != input
264
289
  #p input2
@@ -0,0 +1,240 @@
1
+ require 'test/unit'
2
+ #require 'cgi'
3
+ require(ENV['CGI'] || 'cgialt')
4
+
5
+
6
+ if defined?(CGIExt)
7
+ def if_cgiext; yield if block_given?; true; end
8
+ def if_cgialt; false; end
9
+ else
10
+ def if_cgiext; false; end
11
+ def if_cgialt; yield if block_given?; true; end
12
+ end
13
+
14
+
15
+ class CGIUtilTest < Test::Unit::TestCase
16
+
17
+ def test_escape_html
18
+ ## escape html characters
19
+ input = '<>&"\''
20
+ expected = '&lt;&gt;&amp;&quot;\''
21
+ actual = nil
22
+ if_cgiext { actual = CGIExt.escape_html(input) }
23
+ if_cgialt { actual = CGI.escapeHTML(input) }
24
+ assert_equal(expected, actual)
25
+ if_cgiext { assert_equal(CGI.escapeHTML(input), actual) }
26
+ ## if no html characters found, return the passed argument as is
27
+ input = 'foobar'
28
+ expected = input
29
+ if_cgiext { actual = CGIExt.escape_html(input) }
30
+ if_cgialt { actual = CGI.escapeHTML(input) }
31
+ assert_equal(expected, actual)
32
+ #assert_same(expected, actual)
33
+ ## when non-string is passed, error raised
34
+ input = nil
35
+ if_cgiext { assert_raise(TypeError) { CGIExt.escape_html(input) } }
36
+ if_cgialt { assert_raise(NoMethodError) { CGI.escapeHTML(input) } }
37
+ input = 123
38
+ if_cgiext { assert_raise(TypeError) { CGIExt.escape_html(input) } }
39
+ if_cgialt { assert_raise(NoMethodError) { CGI.escapeHTML(input) } }
40
+ end
41
+
42
+
43
+ if_cgiext {
44
+ def test_escape_html!
45
+ ## escape html characters
46
+ input = '<>&"\''
47
+ expected = '&lt;&gt;&amp;&quot;\''
48
+ actual = CGIExt.escape_html!(input)
49
+ assert_equal(expected, actual)
50
+ assert_equal(CGI.escapeHTML(input), actual)
51
+ ## if no html characters found, return the string as is
52
+ input = 'foobar'
53
+ expected = input
54
+ actual = CGIExt.escape_html!(input)
55
+ assert_equal(expected, actual)
56
+ assert_same(expected, actual)
57
+ ## non-string value are converted into string
58
+ input = nil
59
+ expected = ''
60
+ actual = CGIExt.escape_html!(input)
61
+ assert_equal(expected, actual)
62
+ input = 123
63
+ expected = '123'
64
+ actual = CGIExt.escape_html!(input)
65
+ assert_equal(expected, actual)
66
+ end
67
+ }
68
+
69
+
70
+ def test_unescape_html
71
+ ## unescape html characters
72
+ tdata = [
73
+ ## html entities ('<>&"')
74
+ ['&lt;&gt;&amp;&quot;', '<>&"'],
75
+ ## otehr html entities (ex. '&copy;')
76
+ ['&copy;&heart;', '&copy;&heart;'],
77
+ ## '&#99' format
78
+ ['&#34;&#38;&#39;&#60;&#62;', '"&\'<>'],
79
+ ## '&#x9999' format
80
+ ['&#x0022;&#x0026;&#x0027;&#x003c;&#x003E;', '"&\'<>'],
81
+ ## invalid format
82
+ ['&&lt;&amp&gt;&quot&abcdefghijklmn', '&<&amp>&quot&abcdefghijklmn'],
83
+ ]
84
+ actual = nil
85
+ tdata.each do |input, expected|
86
+ if_cgiext { actual = CGIExt.unescape_html(input) }
87
+ if_cgialt { actual = CGI.unescapeHTML(input) }
88
+ assert_equal(expected, actual)
89
+ if_cgiext { assert_equal(CGI.unescapeHTML(input), actual) }
90
+ end
91
+ ## return as-is when no html entity found
92
+ input = "foobar"
93
+ expected = input
94
+ if_cgiext { actual = CGIExt.unescape_html(input) }
95
+ if_cgialt { actual = CGI.unescapeHTML(input) }
96
+ assert_equal(expected, actual)
97
+ if_cgiext { assert_same(expected, actual) }
98
+ ## when non-string is passed, error raised
99
+ input = nil
100
+ if_cgiext { assert_raise(TypeError) { CGIExt.unescape_html(input) } }
101
+ if_cgialt { assert_raise(NoMethodError) { CGI.unescapeHTML(input) } }
102
+ input = 123
103
+ if_cgiext { assert_raise(TypeError) { CGIExt.unescape_html(input) } }
104
+ if_cgialt { assert_raise(NoMethodError) { CGI.unescapeHTML(input) } }
105
+ end
106
+
107
+
108
+ TESTDATA_FOR_ESCAPE_URL = [
109
+ ## example data
110
+ ["'Stop!' said Fred._-+", '%27Stop%21%27+said+Fred._-%2B'],
111
+ ## characters not to be escaped
112
+ ["abcdefgxyzABCDEFGXYZ0123456789_.-", "abcdefgxyzABCDEFGXYZ0123456789_.-"],
113
+ ## characters to be escaped
114
+ [' !"#$%&\'()*+,/:;<=>?@[\\]^`{|}~', "+%21%22%23%24%25%26%27%28%29%2A%2B%2C%2F%3A%3B%3C%3D%3E%3F%40%5B%5C%5D%5E%60%7B%7C%7D%7E"],
115
+ ## '%' format
116
+ ["k[]", 'k%5B%5D'],
117
+ ## unicode characters
118
+ ["\244\242\244\244\244\246\244\250\244\252", "%A4%A2%A4%A4%A4%A6%A4%A8%A4%AA"],
119
+ ]
120
+
121
+
122
+ def test_escape_url
123
+ ## encode url string
124
+ testdata = TESTDATA_FOR_ESCAPE_URL + [
125
+ ## containing '%' character
126
+ ["%XX%5%%%", "%25XX%255%25%25%25"],
127
+ ]
128
+ actual = nil
129
+ testdata.each do |input, expected|
130
+ if_cgiext { actual = CGIExt.escape_url(input) }
131
+ if_cgialt { actual = CGI.escape(input) }
132
+ assert_equal(expected, actual)
133
+ if_cgiext { assert_equal(CGI.escape(input), actual) }
134
+ #assert_equal(expected, CGI.escape(input))
135
+ end
136
+ ## error when non-string
137
+ if_cgiext { assert_raise(TypeError) { CGIExt.escape_url(nil) } }
138
+ if_cgialt { assert_raise(NoMethodError) { CGI.escape(nil) } }
139
+ if_cgiext { assert_raise(TypeError) { CGIExt.escape_url(123) } }
140
+ if_cgialt { assert_raise(NoMethodError) { CGI.escape(123) } }
141
+ end
142
+
143
+
144
+ def test_unescape_url
145
+ ## decode url string
146
+ testdata = TESTDATA_FOR_ESCAPE_URL + [
147
+ ## invalid '%' format
148
+ ["%XX%5%%%", "%XX%5%%%"],
149
+ ## combination of lower case and upper case
150
+ ["\244\252", "%a4%Aa"],
151
+ ]
152
+ actual = nil
153
+ testdata.each do |expected, input|
154
+ if_cgiext { actual = CGIExt.unescape_url(input) }
155
+ if_cgialt { actual = CGI.unescape(input) }
156
+ assert_equal(expected, actual)
157
+ if_cgiext { assert_equal(CGI.unescape(input), actual) }
158
+ #assert_equal(expected, CGI.unescape(input))
159
+ end
160
+ ## error when non-string
161
+ if_cgiext { assert_raise(TypeError) { CGIExt.unescape_url(nil) } }
162
+ if_cgialt { assert_raise(NoMethodError) { CGI.unescape(nil) } }
163
+ if_cgiext { assert_raise(TypeError) { CGIExt.unescape_url(123) } }
164
+ if_cgialt { assert_raise(NoMethodError) { CGI.unescape(123) } }
165
+ end
166
+
167
+
168
+ def test_rfc1123_date
169
+ sec = 1196524602
170
+ t = Time.at(sec)
171
+ expected = 'Sat, 01 Dec 2007 15:56:42 GMT'
172
+ actual = nil
173
+ if_cgiext { actual = CGIExt.rfc1123_date(t) }
174
+ if_cgialt { actual = CGI.rfc1123_date(t) }
175
+ assert_equal(expected, actual)
176
+ if_cgiext { assert_equal(CGI.rfc1123_date(t), actual) }
177
+ ## error when not Time object
178
+ if_cgiext { assert_raise(TypeError) { CGIExt.rfc1123_date(nil) } }
179
+ if_cgialt { assert_raise(TypeError) { CGI.rfc1123_date(nil) } }
180
+ if_cgiext { assert_raise(TypeError) { CGIExt.rfc1123_date(123) } }
181
+ if_cgialt { assert_raise(TypeError) { CGI.rfc1123_date(123) } }
182
+ end
183
+
184
+
185
+ def test_parse_query_string
186
+ ## equal with CGI.parse()
187
+ tuples = [
188
+ ## '&' separator
189
+ ['a=10&b=20&key1=val1', {'a'=>['10'], 'b'=>['20'], 'key1'=>['val1']} ],
190
+ ## ';' separator
191
+ ['a=10;b=20;key1=val1', {'a'=>['10'], 'b'=>['20'], 'key1'=>['val1']} ],
192
+ ## same keys
193
+ ['a=1&a=2&a=3', {'a'=>['1', '2', '3']} ],
194
+ ## no value
195
+ ['key=&key=', {'key'=>['', '']} ],
196
+ ## unescape ascii string
197
+ ['k%5B%5D=%5B%5D%26%3B', {'k[]'=>['[]&;']} ],
198
+ ## unescape unicode string
199
+ ["%A4%A2%a4%a4=%A4%a6%A4%A8%A4%Aa", {"\244\242\244\244"=>["\244\246\244\250\244\252"]} ],
200
+ ## invalid '%' format
201
+ ['k%XX%5=%%%', {'k%XX%5'=>['%%%']} ],
202
+ ]
203
+ actual = nil
204
+ tuples.each do |input, expected|
205
+ if_cgiext { actual = CGIExt.parse_query_string(input) }
206
+ if_cgialt { actual = CGI.parse(input) }
207
+ assert_equal(expected, actual)
208
+ if_cgiext { assert_equal(CGI.parse(input), actual) }
209
+ end
210
+ ## different with CGI.parse()
211
+ tdata = {}
212
+ ## without '=' (CGI: {"a"=>[nil], "b"=>[nil]})
213
+ if_cgiext { tdata['a&b'] = {'a'=>[''], 'b'=>['']} }
214
+ if_cgialt { tdata['a&b'] = {"a"=>[nil], "b"=>[nil]} }
215
+ ## no key (CGI: {""=>["a", "b"], nil=>[nil]})
216
+ if_cgiext { tdata['&=a&=b'] = {''=>['', 'a', 'b']} }
217
+ if_cgialt { tdata['&=a&=b'] = {""=>["a", "b"], nil=>[nil]} }
218
+ ## invalid format (CGI: {"a"=>[""], "b"=>[nil]})
219
+ if_cgiext { tdata['a=&b&&'] = {'a'=>[''], 'b'=>[''], ''=>['', '']} }
220
+ if_cgialt { tdata['a=&b&&'] = {"a"=>[""], "b"=>[nil]} }
221
+ tdata.each do |input, expected|
222
+ if_cgiext { actual = CGIExt.parse_query_string(input) }
223
+ if_cgialt { actual = CGI.parse(input) }
224
+ assert_equal(expected, actual)
225
+ if_cgiext { assert_equal(CGI.parse(input), actual) }
226
+ end
227
+ ## default value is frozen empty array
228
+ assert_equal(actual['unknownkey'], [])
229
+ ex = nil
230
+ if "".respond_to?(:encoding) # Ruby1.9
231
+ errclass = RuntimeError
232
+ else
233
+ errclass = TypeError
234
+ end
235
+ ex = assert_raise(errclass) { actual['unknownkey'] << 'foo' }
236
+ assert_equal("can't modify frozen array", ex.message)
237
+ end
238
+
239
+
240
+ end
metadata CHANGED
@@ -1,33 +1,26 @@
1
1
  --- !ruby/object:Gem::Specification
2
- rubygems_version: 0.9.2
3
- specification_version: 1
4
2
  name: cgialt
5
3
  version: !ruby/object:Gem::Version
6
- version: 0.0.2
7
- date: 2007-12-11 00:00:00 +09:00
8
- summary: an alternative of 'cgi.rb' in pure Ruby
9
- require_paths:
10
- - lib
11
- email:
12
- homepage: http://cgialt.rubyforge.org/
13
- rubyforge_project:
14
- description: CGIAlt is an alternative library of 'cgi.rb'. It is compatible with and faster than 'cgi.rb'. It is able to install with CGIExt (which is re-implementation of cgi.rb in C extension).
15
- autorequire:
16
- default_executable:
17
- bindir: bin
18
- has_rdoc: false
19
- required_ruby_version: !ruby/object:Gem::Version::Requirement
20
- requirements:
21
- - - ">"
22
- - !ruby/object:Gem::Version
23
- version: 0.0.0
24
- version:
4
+ version: 1.0.0
25
5
  platform: ruby
26
- signing_key:
27
- cert_chain:
28
- post_install_message:
29
6
  authors:
30
7
  - makoto kuwata
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-11-09 00:00:00 +09:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: CGIAlt is an alternative library of 'cgi.rb'. It is compatible with and faster than 'cgi.rb'. It is able to install with CGIExt (which is re-implementation of cgi.rb in C extension).
17
+ email:
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
31
24
  files:
32
25
  - lib/cgialt
33
26
  - lib/cgialt/cookie.rb
@@ -44,6 +37,7 @@ files:
44
37
  - test/test_cgi_header.rb
45
38
  - test/test_cgi_modruby.rb
46
39
  - test/test_cgi_multipart.rb
40
+ - test/test_cgi_util.rb
47
41
  - test/testdata
48
42
  - test/testdata/file1.html
49
43
  - test/testdata/large.png
@@ -52,17 +46,31 @@ files:
52
46
  - bench.rb
53
47
  - setup.rb
54
48
  - CHANGES.txt
55
- test_files: []
56
-
49
+ has_rdoc: false
50
+ homepage: http://cgialt.rubyforge.org/
51
+ post_install_message:
57
52
  rdoc_options: []
58
53
 
59
- extra_rdoc_files: []
60
-
61
- executables: []
62
-
63
- extensions: []
64
-
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: "0"
61
+ version:
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: "0"
67
+ version:
65
68
  requirements: []
66
69
 
67
- dependencies: []
70
+ rubyforge_project:
71
+ rubygems_version: 1.2.0
72
+ signing_key:
73
+ specification_version: 2
74
+ summary: an alternative of 'cgi.rb' in pure Ruby
75
+ test_files: []
68
76