be_valid_asset 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,290 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ unless defined?(SpecFailed)
4
+ SpecFailed = Spec::Expectations::ExpectationNotMetError
5
+ end
6
+
7
+ describe 'be_valid_xhtml' do
8
+
9
+ describe "without caching" do
10
+ it "should validate a valid string" do
11
+ html = get_file('valid.html')
12
+ html.should be_valid_xhtml
13
+ end
14
+
15
+ it "should validate a valid response" do
16
+ response = MockResponse.new(get_file('valid.html'))
17
+ response.should be_valid_xhtml
18
+ end
19
+
20
+ it "should validate if body is not a string but can be converted to valid string" do
21
+ response = MockResponse.new(stub("XHTML", :to_s => get_file('valid.html')))
22
+ response.should be_valid_xhtml
23
+ end
24
+
25
+ it "should validate a valid fragment" do
26
+ "<p>This is a Fragment</p>".should be_valid_xhtml_fragment
27
+ end
28
+
29
+ it "should not validate an invalid string" do
30
+ html = get_file('invalid.html')
31
+ lambda {
32
+ html.should be_valid_xhtml
33
+ }.should raise_error(SpecFailed) { |e|
34
+ e.message.should match(/expected xhtml to be valid, but validation produced these errors/)
35
+ e.message.should match(/Invalid markup: line 12: end tag for "b" omitted, but OMITTAG NO was specified/)
36
+ e.message.should match(/Invalid markup: line 12: end tag for element "b" which is not open/)
37
+ e.message.should match(/Invalid markup: line 12: XML Parsing Error: Opening and ending tag mismatch: b line 12 and p/)
38
+ e.message.should match(/Invalid markup: line 12: XML Parsing Error: Opening and ending tag mismatch: p line 12 and b/)
39
+ }
40
+ end
41
+
42
+ it "should not validate an invalid response" do
43
+ response = MockResponse.new(get_file('invalid.html'))
44
+ lambda {
45
+ response.should be_valid_xhtml
46
+ }.should raise_error(SpecFailed) { |e|
47
+ e.message.should match(/expected xhtml to be valid, but validation produced these errors/)
48
+ e.message.should match(/Invalid markup: line 12: end tag for "b" omitted, but OMITTAG NO was specified/)
49
+ e.message.should match(/Invalid markup: line 12: end tag for element "b" which is not open/)
50
+ e.message.should match(/Invalid markup: line 12: XML Parsing Error: Opening and ending tag mismatch: b line 12 and p/)
51
+ e.message.should match(/Invalid markup: line 12: XML Parsing Error: Opening and ending tag mismatch: p line 12 and b/)
52
+ }
53
+ end
54
+
55
+ it "should display invalid content when requested" do
56
+ BeValidAsset::Configuration.display_invalid_content = true
57
+ html = get_file('invalid.html')
58
+ lambda {
59
+ html.should be_valid_xhtml
60
+ }.should raise_error(SpecFailed) { |e|
61
+ e.message.should match(/<p><b>This is an example invalid html file<\/p><\/b>/)
62
+ }
63
+ BeValidAsset::Configuration.display_invalid_content = false
64
+ end
65
+
66
+ describe "displaying invalid lines" do
67
+ before :each do
68
+ BeValidAsset::Configuration.display_invalid_lines = true
69
+ end
70
+ after :each do
71
+ BeValidAsset::Configuration.display_invalid_lines = false
72
+ BeValidAsset::Configuration.display_invalid_lines_count = 5 # Restore the default value
73
+ end
74
+
75
+ it "should display invalid lines when requested" do
76
+ html = get_file('invalid.html')
77
+ lambda do
78
+ html.should be_valid_xhtml
79
+ end.should raise_error(SpecFailed) { |e|
80
+ e.message.should match(/expected xhtml to be valid, but validation produced these errors/)
81
+ e.message.should_not match(/0009 :/)
82
+ e.message.should match(/0010 :/)
83
+ e.message.should match(/0011 :/)
84
+ e.message.should match(/0012>>:/)
85
+ e.message.should match(/0013 :/)
86
+ e.message.should match(/0014 :/)
87
+ e.message.should_not match(/0015 :/)
88
+ }
89
+ end
90
+
91
+ it "should display specified invalid lines window when requested" do
92
+ BeValidAsset::Configuration.display_invalid_lines_count = 3
93
+ html = get_file('invalid.html')
94
+ lambda do
95
+ html.should be_valid_xhtml
96
+ end.should raise_error(SpecFailed) { |e|
97
+ e.message.should match(/expected xhtml to be valid, but validation produced these errors/)
98
+ e.message.should_not match(/0010 :/)
99
+ e.message.should match(/0011 :/)
100
+ e.message.should match(/0012>>:/)
101
+ e.message.should match(/0013 :/)
102
+ e.message.should_not match(/0014 :/)
103
+ }
104
+ end
105
+
106
+ it "should not underrun the beginning of the source" do
107
+ BeValidAsset::Configuration.display_invalid_lines_count = 7
108
+ html = get_file('invalid2.html')
109
+ lambda do
110
+ html.should be_valid_xhtml
111
+ end.should raise_error(SpecFailed) { |e|
112
+ e.message.should match(/expected xhtml to be valid, but validation produced these errors/)
113
+ e.message.should_not match(/0000 :/)
114
+ e.message.should match(/0001 :/)
115
+ e.message.should match(/0003>>:/)
116
+ }
117
+ end
118
+
119
+ it "should not overrun the end of the source" do
120
+ BeValidAsset::Configuration.display_invalid_lines_count = 11
121
+ html = get_file('invalid.html')
122
+ lambda do
123
+ html.should be_valid_xhtml
124
+ end.should raise_error(SpecFailed) { |e|
125
+ e.message.should match(/expected xhtml to be valid, but validation produced these errors/)
126
+ e.message.should match(/0012>>:/)
127
+ e.message.should match(/0015 :/)
128
+ e.message.should_not match(/0016 :/)
129
+ }
130
+ end
131
+ end
132
+
133
+ it "should fail when passed a response with a blank body" do
134
+ response = MockResponse.new('')
135
+ lambda {
136
+ response.should be_valid_xhtml
137
+ }.should raise_error(SpecFailed)
138
+ end
139
+
140
+ it "should fail unless resposne is HTTP OK" do
141
+ html = get_file('valid.html')
142
+
143
+ r = Net::HTTPServiceUnavailable.new('1.1', 503, 'Service Unavailable')
144
+ h = Net::HTTP.new(BeValidAsset::Configuration.markup_validator_host)
145
+ h.stub!(:post2).and_return(r)
146
+ Net::HTTP.stub!(:start).and_return(h)
147
+
148
+ lambda {
149
+ html.should be_valid_xhtml
150
+ }.should raise_error
151
+ end
152
+
153
+ it "should mark test as pending if network tests are disabled" do
154
+ ENV['NONET'] = 'true'
155
+
156
+ html = get_file('valid.html')
157
+ lambda {
158
+ html.should be_valid_xhtml
159
+ }.should raise_error(Spec::Example::ExamplePendingError)
160
+
161
+ ENV.delete('NONET')
162
+ end
163
+ end
164
+
165
+ describe "with caching" do
166
+ before(:each) do
167
+ BeValidAsset::Configuration.enable_caching = true
168
+ FileUtils.rm Dir.glob(BeValidAsset::Configuration.cache_path + '/*')
169
+ end
170
+ after(:each) do
171
+ BeValidAsset::Configuration.enable_caching = false
172
+ end
173
+
174
+ it "should validate a valid string and cache the response" do
175
+ html = get_file('valid.html')
176
+ count = Dir.glob(BeValidAsset::Configuration.cache_path + '/*').size
177
+ html.should be_valid_xhtml
178
+ Dir.glob(BeValidAsset::Configuration.cache_path + '/*').size.should eql(count + 1)
179
+ end
180
+
181
+ it "should validate a valid string using the cached response" do
182
+ html = get_file('valid.html')
183
+ html.should be_valid_xhtml
184
+
185
+ Net::HTTP.should_not_receive(:start)
186
+ html.should be_valid_xhtml
187
+ end
188
+
189
+ it "should not validate an invalid response, but still cache the response" do
190
+ response = MockResponse.new(get_file('invalid.html'))
191
+ count = Dir.glob(BeValidAsset::Configuration.cache_path + '/*').size
192
+ lambda {
193
+ response.should be_valid_xhtml
194
+ }.should raise_error(SpecFailed) { |e|
195
+ e.message.should match(/expected xhtml to be valid, but validation produced these errors/)
196
+ e.message.should match(/Invalid markup: line 12: end tag for "b" omitted, but OMITTAG NO was specified/)
197
+ e.message.should match(/Invalid markup: line 12: end tag for element "b" which is not open/)
198
+ e.message.should match(/Invalid markup: line 12: XML Parsing Error: Opening and ending tag mismatch: b line 12 and p/)
199
+ e.message.should match(/Invalid markup: line 12: XML Parsing Error: Opening and ending tag mismatch: p line 12 and b/)
200
+ }
201
+ Dir.glob(BeValidAsset::Configuration.cache_path + '/*').size.should eql(count + 1)
202
+ end
203
+
204
+ it "should not validate an invalid response, but use the cached response" do
205
+ response = MockResponse.new(get_file('invalid.html'))
206
+ response.should_not be_valid_xhtml
207
+
208
+ Net::HTTP.should_not_receive(:start)
209
+ lambda {
210
+ response.should be_valid_xhtml
211
+ }.should raise_error(SpecFailed) { |e|
212
+ e.message.should match(/expected xhtml to be valid, but validation produced these errors/)
213
+ e.message.should match(/Invalid markup: line 12: end tag for "b" omitted, but OMITTAG NO was specified/)
214
+ e.message.should match(/Invalid markup: line 12: end tag for element "b" which is not open/)
215
+ e.message.should match(/Invalid markup: line 12: XML Parsing Error: Opening and ending tag mismatch: b line 12 and p/)
216
+ e.message.should match(/Invalid markup: line 12: XML Parsing Error: Opening and ending tag mismatch: p line 12 and b/)
217
+ }
218
+ end
219
+
220
+ it "should not cache the result unless it is an HTTP OK response" do
221
+ html = get_file('valid.html')
222
+ count = Dir.glob(BeValidAsset::Configuration.cache_path + '/*').size
223
+
224
+ r = Net::HTTPServiceUnavailable.new('1.1', 503, 'Service Unavailable')
225
+ h = Net::HTTP.new(BeValidAsset::Configuration.markup_validator_host)
226
+ h.stub!(:post2).and_return(r)
227
+ Net::HTTP.stub!(:start).and_return(h)
228
+
229
+ lambda {
230
+ html.should be_valid_xhtml
231
+ }.should raise_error
232
+ Dir.glob(BeValidAsset::Configuration.cache_path + '/*').size.should eql(count)
233
+ end
234
+
235
+ it "should use the cached result (if available) when network tests disabled" do
236
+ html = get_file('valid.html')
237
+ html.should be_valid_xhtml
238
+
239
+ ENV['NONET'] = 'true'
240
+
241
+ Net::HTTP.should_not_receive(:start)
242
+ html.should be_valid_xhtml
243
+
244
+ ENV.delete('NONET')
245
+ end
246
+
247
+ it "should mark test as pending if network tests are disabled, and no cached result is available" do
248
+ ENV['NONET'] = 'true'
249
+
250
+ html = get_file('valid.html')
251
+ lambda {
252
+ html.should be_valid_xhtml
253
+ }.should raise_error(Spec::Example::ExamplePendingError)
254
+
255
+ ENV.delete('NONET')
256
+ end
257
+ end
258
+
259
+ describe "Proxying" do
260
+ before :each do
261
+ r = Net::HTTPSuccess.new('1.1', 200, 'HTTPOK')
262
+ r['x-w3c-validator-status'] = 'Valid'
263
+ @http = mock('HTTP')
264
+ @http.stub!(:post2).and_return(r)
265
+
266
+ @html = MockResponse.new(get_file('valid.html'))
267
+ end
268
+
269
+ it "should use direct http without ENV['http_proxy']" do
270
+ ENV.delete('http_proxy')
271
+ Net::HTTP.should_receive(:start).with(BeValidAsset::Configuration.markup_validator_host).and_return(@http)
272
+ @html.should be_valid_xhtml
273
+ end
274
+
275
+ it "should use proxied http connection with ENV['http_proxy']" do
276
+ ENV['http_proxy'] = "http://user:pw@localhost:3128"
277
+ Net::HTTP.should_receive(:start).with(BeValidAsset::Configuration.markup_validator_host, nil, 'localhost', 3128, "user", "pw").and_return(@http)
278
+ @html.should be_valid_xhtml
279
+ ENV.delete('http_proxy')
280
+ end
281
+
282
+ it "should raise exception with invalid http_proxy" do
283
+ ENV['http_proxy'] = "http://invalid:uri"
284
+ lambda {
285
+ @html.should be_valid_xhtml
286
+ }.should raise_error(URI::InvalidURIError)
287
+ ENV.delete('http_proxy')
288
+ end
289
+ end
290
+ end
@@ -0,0 +1,43 @@
1
+ html, body
2
+ {
3
+ background:#007086;
4
+ font-family:Arial,Helvetica,sans-serif;
5
+ font-size:0.83em;
6
+ margin:0;
7
+ padding:0;
8
+ wibble:0;
9
+ }
10
+
11
+ *
12
+ {
13
+ margin:0;
14
+ padding:0;
15
+ }
16
+ ul, ul li
17
+ {
18
+ list-style-type: none;
19
+ }
20
+
21
+ .hidden
22
+ {
23
+ display: none;
24
+ }
25
+ a img
26
+ {
27
+ border: none;
28
+ }
29
+ a
30
+ {
31
+ text-decoration: none;
32
+ }
33
+ a:hover
34
+ {
35
+ text-decoration: underline;
36
+ }
37
+ .clear-float
38
+ {
39
+ clear: both;
40
+ font-size: 1px;
41
+ height: 0px;
42
+ line-height: 0px;
43
+ }
@@ -0,0 +1,15 @@
1
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2
+
3
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
4
+ <head>
5
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
6
+ <title>Test Invalid Page</title>
7
+ </head>
8
+
9
+ <body>
10
+
11
+ <h1>Test#invalid</h1>
12
+ <p><b>This is an example invalid html file</p></b>
13
+
14
+ </body>
15
+ </html>
@@ -0,0 +1,16 @@
1
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2
+
3
+ <p>This isn't valid here</p>
4
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
5
+ <head>
6
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
7
+ <title>Test Invalid Page</title>
8
+ </head>
9
+
10
+ <body>
11
+
12
+ <h1>Test#invalid</h1>
13
+ <p><b>This is an example invalid html file</b></p>
14
+
15
+ </body>
16
+ </html>
@@ -0,0 +1,37 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
3
+ <channel>
4
+ <title>Interesting Stuff</title>
5
+ <link>http://site.example.com/</link>
6
+ <description>Description about interesting Stuff...</description>
7
+ <language>en-gb</language>
8
+ <atom:link type="application/rss+xml" href="http://site.example.com/index.rss" rel="self"/>
9
+ <item>
10
+ <title>Article 1 title</title>
11
+ <category>Category 1</category>
12
+ <author>Arthur Penderghast</author>
13
+ <description>&lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.&lt;/p&gt;&lt;br /&gt;&lt;img src="http://site.example.com/articles/article-1-title/images/1.jpg" alt="Some Alt Text" /&gt;</description>
14
+ <pubDate>Wed, 15 Jul 2009 17:03:36 +0100</pubDate>
15
+ <link>http://site.example.com/articles/article-1-title</link>
16
+ <guid>http://site.example.com/articles/article-1-title</guid>
17
+ </item>
18
+ <item>
19
+ <title>Article 2 title</title>
20
+ <category>Category 2</category>
21
+ <author>Arthur Penderghast</author>
22
+ <description>&lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.&lt;/p&gt;&lt;br /&gt;&lt;img src="http://site.example.com/articles/article-2-title/images/1.jpg" alt="Some Alt Text" /&gt;</description>
23
+ <pubDate>Wed, 15 Jul 2009 13:15:57 +0100</pubDate>
24
+ <link>http://site.example.com/articles/article-2-title</link>
25
+ <guid>http://site.example.com/articles/article-2-title</guid>
26
+ </item>
27
+ <item>
28
+ <title>Article 3 title</title>
29
+ <category>Category 1</category>
30
+ <author>Arthur Penderghast</author>
31
+ <description>&lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.&lt;/p&gt;&lt;br /&gt;&lt;img src="http://site.example.com/articles/article-3-title/images/1.jpg" alt="Some Alt Text" /&gt;</description>
32
+ <pubDate>Wed, 15 Jul 2009 13:11:56 +0100</pubDate>
33
+ <link>http://site.example.com/articles/article-3-title</link>
34
+ <guid>http://site.example.com/articles/article-3-title</guid>
35
+ </item>
36
+ </channel>
37
+ </rss>
@@ -0,0 +1,31 @@
1
+ html, body
2
+ {
3
+ background:#007086;
4
+ font-family:Arial,Helvetica,sans-serif;
5
+ font-size:0.83em;
6
+ margin:0;
7
+ padding:0;
8
+ }
9
+ ul, ul li
10
+ {
11
+ list-style-type: none;
12
+ }
13
+ .hidden
14
+ {
15
+ display: none;
16
+ }
17
+ a img
18
+ {
19
+ border: none;
20
+ }
21
+ a
22
+ {
23
+ text-decoration: none;
24
+ }
25
+ .clear-float
26
+ {
27
+ clear: both;
28
+ font-size: 1px;
29
+ height: 0px;
30
+ line-height: 0px;
31
+ }
@@ -0,0 +1,42 @@
1
+ html, body
2
+ {
3
+ background:#007086;
4
+ font-family:Arial,Helvetica,sans-serif;
5
+ font-size:0.83em;
6
+ margin:0;
7
+ padding:0;
8
+ }
9
+
10
+ *
11
+ {
12
+ margin:0;
13
+ padding:0;
14
+ }
15
+ ul, ul li
16
+ {
17
+ list-style-type: none;
18
+ }
19
+
20
+ .hidden
21
+ {
22
+ display: none;
23
+ }
24
+ a img
25
+ {
26
+ border: none;
27
+ }
28
+ a
29
+ {
30
+ text-decoration: none;
31
+ }
32
+ a:hover
33
+ {
34
+ text-decoration: underline;
35
+ }
36
+ .clear-float
37
+ {
38
+ clear: both;
39
+ font-size: 1px;
40
+ height: 0px;
41
+ line-height: 0px;
42
+ }