/
escape_spec.rb
56 lines (47 loc) · 1.75 KB
/
escape_spec.rb
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
# encoding: UTF-8
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper.rb')
require 'uri'
describe EscapeUtils, "escape_uri" do
it "should respond to escape_uri" do
EscapeUtils.should respond_to(:escape_uri)
end
it "should escape each byte exactly like URI.escape" do
(0..127).each do |i|
c = i.chr
EscapeUtils.escape_uri(c).should eql(URI.escape(c))
end
end
# NOTE: from Rack's test suite
it "should escape a url containing tags" do
EscapeUtils.escape_uri("fo<o>bar").should eql("fo%3Co%3Ebar")
end
# NOTE: from Rack's test suite
it "should escape a url with spaces" do
EscapeUtils.escape_uri("a space").should eql("a%20space")
EscapeUtils.escape_uri("a sp ace ").should eql("a%20%20%20sp%20ace%20")
end
# NOTE: from Rack's test suite
it "should escape correctly for multibyte characters" do
matz_name = "\xE3\x81\xBE\xE3\x81\xA4\xE3\x82\x82\xE3\x81\xA8" # Matsumoto
EscapeUtils.escape_uri(matz_name).should eql('%E3%81%BE%E3%81%A4%E3%82%82%E3%81%A8')
matz_name_sep = "\xE3\x81\xBE\xE3\x81\xA4 \xE3\x82\x82\xE3\x81\xA8" # Matsu moto
EscapeUtils.escape_uri(matz_name_sep).should eql('%E3%81%BE%E3%81%A4%20%E3%82%82%E3%81%A8')
end
if RUBY_VERSION =~ /^1.9/
it "input must be UTF-8 or US-ASCII" do
str = "fo<o>bar"
str.force_encoding 'ISO-8859-1'
lambda {
EscapeUtils.escape_uri(str)
}.should raise_error(Encoding::CompatibilityError)
str.force_encoding 'UTF-8'
lambda {
EscapeUtils.escape_uri(str)
}.should_not raise_error(Encoding::CompatibilityError)
end
it "return value should be in UTF-8" do
str = "fo<o>bar"
EscapeUtils.escape_uri(str).encoding.should eql(Encoding.find('UTF-8'))
end
end
end