-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy pathtest_css_parser_loading.rb
224 lines (172 loc) · 6.73 KB
/
test_css_parser_loading.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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
require File.expand_path(File.dirname(__FILE__) + '/test_helper')
# Test cases for the CssParser's loading functions.
class CssParserLoadingTests < Minitest::Test
include CssParser
include WEBrick
def setup
# from http://nullref.se/blog/2006/5/17/testing-with-webrick
@cp = Parser.new
@uri_base = 'http://localhost:12000'
@www_root = File.dirname(__FILE__) + '/fixtures/'
@server_thread = Thread.new do
s = WEBrick::HTTPServer.new(:Port => 12000, :DocumentRoot => @www_root, :Logger => Log.new(nil, BasicLog::FATAL), :AccessLog => [])
s.mount_proc('/redirect301') do |request, response|
response['Location'] = '/simple.css'
raise WEBrick::HTTPStatus::MovedPermanently
end
s.mount_proc('/redirect302') do |request, response|
response['Location'] = '/simple.css'
raise WEBrick::HTTPStatus::TemporaryRedirect
end
@port = s.config[:Port]
begin
s.start
ensure
s.shutdown
end
end
sleep 1 # ensure the server has time to load
end
def teardown
@server_thread.kill
@server_thread.join(5)
@server_thread = nil
end
def test_loading_301_redirect
@cp.load_uri!("#{@uri_base}/redirect301")
assert_equal 'margin: 0px;', @cp.find_by_selector('p').join(' ')
end
def test_loading_302_redirect
@cp.load_uri!("#{@uri_base}/redirect302")
assert_equal 'margin: 0px;', @cp.find_by_selector('p').join(' ')
end
def test_loading_a_local_file
file_name = File.dirname(__FILE__) + '/fixtures/simple.css'
@cp.load_file!(file_name)
assert_equal 'margin: 0px;', @cp.find_by_selector('p').join(' ')
end
def test_loading_a_local_file_with_scheme
file_name = 'file://' + File.expand_path(File.dirname(__FILE__)) + '/fixtures/simple.css'
@cp.load_uri!(file_name)
assert_equal 'margin: 0px;', @cp.find_by_selector('p').join(' ')
end
def test_loading_a_remote_file
@cp.load_uri!("#{@uri_base}/simple.css")
assert_equal 'margin: 0px;', @cp.find_by_selector('p').join(' ')
end
# http://github.com/premailer/css_parser/issues#issue/4
def test_loading_a_remote_file_over_ssl
# TODO: test SSL locally
if RUBY_PLATFORM == 'java'
skip "SSL: does not work on jruby"
else
@cp.load_uri!("https://dialect.ca/inc/screen.css")
assert_includes( @cp.find_by_selector('body').join(' '), "margin: 0;" )
end
end
def test_loading_a_string
@cp.load_string!("p{margin:0px}")
assert_equal 'margin: 0px;', @cp.find_by_selector('p').join(' ')
end
def test_following_at_import_rules_local
base_dir = File.dirname(__FILE__) + '/fixtures'
@cp.load_file!('import1.css', base_dir)
# from '/import1.css'
assert_equal 'color: lime;', @cp.find_by_selector('div').join(' ')
# from '/subdir/import2.css'
assert_equal 'text-decoration: none;', @cp.find_by_selector('a').join(' ')
# from '/subdir/../simple.css'
assert_equal 'margin: 0px;', @cp.find_by_selector('p').join(' ')
end
def test_following_at_import_rules_remote
@cp.load_uri!("#{@uri_base}/import1.css")
# from '/import1.css'
assert_equal 'color: lime;', @cp.find_by_selector('div').join(' ')
# from '/subdir/import2.css'
assert_equal 'text-decoration: none;', @cp.find_by_selector('a').join(' ')
# from '/subdir/../simple.css'
assert_equal 'margin: 0px;', @cp.find_by_selector('p').join(' ')
end
def test_imports_disabled
cp = Parser.new(:import => false)
cp.load_uri!("#{@uri_base}/import1.css")
# from '/import1.css'
assert_equal 'color: lime;', cp.find_by_selector('div').join(' ')
# from '/subdir/import2.css'
assert_equal '', cp.find_by_selector('a').join(' ')
# from '/subdir/../simple.css'
assert_equal '', cp.find_by_selector('p').join(' ')
end
def test_following_remote_import_rules
css_block = '@import "http://example.com/css";'
assert_raises CssParser::RemoteFileError do
@cp.add_block!(css_block, :base_uri => "#{@uri_base}/subdir/")
end
end
def test_following_badly_escaped_import_rules
css_block = '@import "http://example.com/css?family=Droid+Sans:regular,bold|Droid+Serif:regular,italic,bold,bolditalic&subset=latin";'
assert_raises CssParser::RemoteFileError do
@cp.add_block!(css_block, :base_uri => "#{@uri_base}/subdir/")
end
end
def test_loading_malformed_content_strings
file_name = File.dirname(__FILE__) + '/fixtures/import-malformed.css'
@cp.load_file!(file_name)
@cp.each_selector do |sel, dec, spec|
assert_nil dec =~ /wellformed/
end
end
def test_loading_malformed_css_brackets
file_name = File.dirname(__FILE__) + '/fixtures/import-malformed.css'
@cp.load_file!(file_name)
selector_count = 0
@cp.each_selector do |sel, dec, spec|
selector_count += 1
end
assert_equal 8, selector_count
end
def test_following_at_import_rules_from_add_block
css_block = '@import "../simple.css";'
@cp.add_block!(css_block, :base_uri => "#{@uri_base}/subdir/")
# from 'simple.css'
assert_equal 'margin: 0px;', @cp.find_by_selector('p').join(' ')
end
def test_importing_with_media_types
@cp.load_uri!("#{@uri_base}/import-with-media-types.css")
# from simple.css with :screen media type
assert_equal 'margin: 0px;', @cp.find_by_selector('p', :screen).join(' ')
assert_equal '', @cp.find_by_selector('p', :tty).join(' ')
end
def test_local_circular_reference_exception
assert_raises CircularReferenceError do
@cp.load_file!(File.dirname(__FILE__) + '/fixtures/import-circular-reference.css')
end
end
def test_remote_circular_reference_exception
assert_raises CircularReferenceError do
@cp.load_uri!("#{@uri_base}/import-circular-reference.css")
end
end
def test_suppressing_circular_reference_exceptions
cp_without_exceptions = Parser.new(:io_exceptions => false)
cp_without_exceptions.load_uri!("#{@uri_base}/import-circular-reference.css")
end
def test_toggling_not_found_exceptions
cp_with_exceptions = Parser.new(:io_exceptions => true)
err = assert_raises RemoteFileError do
cp_with_exceptions.load_uri!("#{@uri_base}/no-exist.xyz")
end
assert_includes err.message, "#{@uri_base}/no-exist.xyz"
cp_without_exceptions = Parser.new(:io_exceptions => false)
cp_without_exceptions.load_uri!("#{@uri_base}/no-exist.xyz")
end
def test_invalid_content_type
cp_with_exceptions = Parser.new(:io_exceptions => true)
err = assert_raises RemoteFileError do
cp_with_exceptions.load_uri!("#{@uri_base}/LadylikeBB.otf")
end
assert_includes err.message, "#{@uri_base}/LadylikeBB.otf"
cp_without_exceptions = Parser.new(:io_exceptions => false)
cp_without_exceptions.load_uri!("#{@uri_base}/LadylikeBB.otf")
end
end