forked from kjvarga/sitemap_generator
/
link_set_spec.rb
606 lines (509 loc) · 19.3 KB
/
link_set_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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
require 'spec_helper'
describe SitemapGenerator::LinkSet do
before :each do
@default_host = 'http://example.com'
@ls = SitemapGenerator::LinkSet.new
end
describe "initializer options" do
options = [:public_path, :sitemaps_path, :default_host, :filename, :search_engines]
values = [File.expand_path(SitemapGenerator.app.root + 'tmp/'), 'mobile/', 'http://myhost.com', :xxx, { :abc => '123' }]
options.zip(values).each do |option, value|
it "should set #{option} to #{value}" do
@ls = SitemapGenerator::LinkSet.new(option => value)
@ls.send(option).should == value
end
end
end
describe "default options" do
default_options = {
:filename => :sitemap,
:sitemaps_path => nil,
:public_path => SitemapGenerator.app.root + 'public/',
:default_host => nil,
:include_index => true,
:include_root => true
}
default_options.each do |option, value|
it "#{option} should default to #{value}" do
@ls.send(option).should == value
end
end
end
describe "include_root include_index option" do
it "should not include the root url" do
@ls = SitemapGenerator::LinkSet.new(:default_host => @default_host, :include_root => false)
@ls.include_root.should be_false
@ls.include_index.should be_true
@ls.add_links { |sitemap| }
@ls.sitemap.link_count.should == 1
end
it "should not include the sitemap index url" do
@ls = SitemapGenerator::LinkSet.new(:default_host => @default_host, :include_index => false)
@ls.include_root.should be_true
@ls.include_index.should be_false
@ls.add_links { |sitemap| }
@ls.sitemap.link_count.should == 1
end
it "should not include the root url or the sitemap index url" do
@ls = SitemapGenerator::LinkSet.new(:default_host => @default_host, :include_root => false, :include_index => false)
@ls.include_root.should be_false
@ls.include_index.should be_false
@ls.add_links { |sitemap| }
@ls.sitemap.link_count.should == 0
end
end
describe "sitemaps public_path" do
it "should default to public/" do
@ls.public_path.should == SitemapGenerator.app.root + 'public/'
@ls.sitemap.location.public_path.should == @ls.public_path
@ls.sitemap_index.location.public_path.should == @ls.public_path
end
it "should change when the public_path is changed" do
@ls.public_path = 'tmp'
@ls.sitemap.location.public_path.should == @ls.public_path
@ls.sitemap_index.location.public_path.should == @ls.public_path
end
end
describe "sitemaps url" do
it "should change when the default_host is changed" do
@ls.default_host = 'http://one.com'
@ls.default_host.should == 'http://one.com'
@ls.default_host.should == @ls.sitemap.location.host
@ls.default_host.should == @ls.sitemap_index.location.host
end
it "should change when the sitemaps_path is changed" do
@ls.default_host = 'http://one.com'
@ls.sitemaps_path = 'sitemaps/'
@ls.sitemap.location.url.should == 'http://one.com/sitemaps/sitemap1.xml.gz'
@ls.sitemap_index.location.url.should == 'http://one.com/sitemaps/sitemap_index.xml.gz'
end
end
describe "sitemap_index_url" do
it "should return the url to the index file" do
@ls.default_host = @default_host
@ls.sitemap_index.location.url.should == "#{@default_host}/sitemap_index.xml.gz"
@ls.sitemap_index_url.should == @ls.sitemap_index.location.url
end
end
describe "search_engines" do
it "should have search engines by default" do
@ls.search_engines.should be_a(Hash)
@ls.search_engines.size.should == 4
end
it "should support being modified" do
@ls.search_engines[:newengine] = 'abc'
@ls.search_engines.size.should == 5
end
it "should support being set to nil" do
ls = SitemapGenerator::LinkSet.new(:default_host => 'http://one.com', :search_engines => nil)
ls.search_engines.should be_a(Hash)
ls.search_engines.should be_empty
ls.search_engines = nil
ls.search_engines.should be_a(Hash)
ls.search_engines.should be_empty
end
end
describe "ping search engines" do
before do
@ls = SitemapGenerator::LinkSet.new :default_host => 'http://one.com'
end
it "should not fail" do
@ls.expects(:open).at_least_once
lambda { @ls.ping_search_engines }.should_not raise_error
end
it "should raise if no host is set" do
lambda { SitemapGenerator::LinkSet.new.ping_search_engines }.should raise_error(SitemapGenerator::SitemapError, 'No value set for host')
end
it "should use the sitemap index url provided" do
index_url = 'http://example.com/index.xml'
ls = SitemapGenerator::LinkSet.new(:search_engines => { :google => 'http://google.com/?url=%s' })
ls.expects(:open).with("http://google.com/?url=#{CGI.escape(index_url)}")
ls.ping_search_engines(index_url)
end
it "should use the sitemap index url from the link set" do
ls = SitemapGenerator::LinkSet.new(
:default_host => 'http://one.com',
:search_engines => { :google => 'http://google.com/?url=%s' })
index_url = ls.sitemap_index_url
ls.expects(:open).with("http://google.com/?url=#{CGI.escape(index_url)}")
ls.ping_search_engines(index_url)
end
it "should include the given search engines" do
@ls.search_engines = nil
@ls.expects(:open).with(regexp_matches(/^http:\/\/newnegine\.com\?/))
@ls.ping_search_engines(:newengine => 'http://newnegine.com?%s')
@ls.expects(:open).with(regexp_matches(/^http:\/\/newnegine\.com\?/)).twice
@ls.ping_search_engines(:newengine => 'http://newnegine.com?%s', :anotherengine => 'http://newnegine.com?%s')
end
end
describe "verbose" do
before do
@ls = SitemapGenerator::LinkSet.new(:default_host => 'http://one.com')
end
it "should be set as an initialize option" do
SitemapGenerator::LinkSet.new(:default_host => 'http://one.com', :verbose => false).verbose.should be_false
SitemapGenerator::LinkSet.new(:default_host => 'http://one.com', :verbose => true).verbose.should be_true
end
it "should be set as an accessor" do
@ls.verbose = true
@ls.verbose.should be_true
@ls.verbose = false
@ls.verbose.should be_false
end
it "should use SitemapGenerator.verbose as a default" do
SitemapGenerator.expects(:verbose).returns(true).at_least_once
SitemapGenerator::LinkSet.new.verbose.should be_true
SitemapGenerator.expects(:verbose).returns(false).at_least_once
SitemapGenerator::LinkSet.new.verbose.should be_false
end
end
describe "when finalizing" do
before do
@ls = SitemapGenerator::LinkSet.new(:default_host => 'http://one.com', :verbose => true)
end
it "should output summary lines" do
@ls.sitemap.expects(:finalize!)
@ls.sitemap.expects(:summary)
@ls.sitemap_index.expects(:finalize!)
@ls.sitemap_index.expects(:summary)
@ls.finalize!
end
end
describe "sitemaps host" do
before do
@new_host = 'http://wowza.com'
@ls = SitemapGenerator::LinkSet.new(:default_host => @default_host)
end
it "should have a host" do
@ls.default_host = @default_host
@ls.default_host.should == @default_host
end
it "should default to default host" do
@ls.sitemaps_host.should == @ls.default_host
end
it "should update the host in the sitemaps when changed" do
@ls.sitemaps_host = @new_host
@ls.sitemaps_host.should == @new_host
@ls.sitemap.location.host.should == @ls.sitemaps_host
@ls.sitemap_index.location.host.should == @ls.sitemaps_host
end
it "should not change the default host for links" do
@ls.sitemaps_host = @new_host
@ls.default_host.should == @default_host
end
end
describe "with a sitemap index specified" do
before :each do
@index = SitemapGenerator::Builder::SitemapIndexFile.new(:host => @default_host)
@ls = SitemapGenerator::LinkSet.new(:sitemap_index => @index, :sitemaps_host => 'http://newhost.com')
end
it "should not modify the index" do
@ls.filename = :newname
@ls.sitemap.location.filename.should =~ /newname/
@ls.sitemap_index.location.filename =~ /sitemap_index/
end
it "should not modify the index" do
@ls.sitemaps_host = 'http://newhost.com'
@ls.sitemap.location.host.should == 'http://newhost.com'
@ls.sitemap_index.location.host.should == @default_host
end
it "should not finalize the index" do
@ls.send(:finalize_sitemap_index!)
@ls.sitemap_index.finalized?.should be_false
end
end
describe "new group" do
before :each do
@ls = SitemapGenerator::LinkSet.new(:default_host => @default_host)
end
describe "general behaviour" do
it "should return a LinkSet" do
@ls.group.should be_a(SitemapGenerator::LinkSet)
end
it "should inherit the index" do
@ls.group.sitemap_index.should == @ls.sitemap_index
end
it "should protect the sitemap_index" do
@ls.group.instance_variable_get(:@protect_index).should be_true
end
it "should not allow chaning the public_path" do
@ls.group(:public_path => 'new/path/').public_path.to_s.should == @ls.public_path.to_s
end
end
describe "include_index" do
it "should set the value" do
@ls.group(:include_index => !@ls.include_index).include_index.should_not == @ls.include_index
end
it "should default to false" do
@ls.group.include_index.should be_false
end
end
describe "include_root" do
it "should set the value" do
@ls.group(:include_root => !@ls.include_root).include_root.should_not == @ls.include_root
end
it "should default to false" do
@ls.group.include_root.should be_false
end
end
describe "filename" do
it "should inherit the value" do
@ls.group.filename.should == :sitemap
end
it "should set the value" do
group = @ls.group(:filename => :xxx)
group.filename.should == :xxx
group.sitemap.location.filename.should =~ /xxx/
end
end
describe "verbose" do
it "should inherit the value" do
@ls.group.verbose.should == @ls.verbose
end
it "should set the value" do
@ls.group(:verbose => !@ls.verbose).verbose.should_not == @ls.verbose
end
end
describe "sitemaps_path" do
it "should inherit the sitemaps_path" do
group = @ls.group
group.sitemaps_path.should == @ls.sitemaps_path
group.sitemap.location.sitemaps_path.should == @ls.sitemap.location.sitemaps_path
end
it "should set the sitemaps_path" do
path = 'new/path'
group = @ls.group(:sitemaps_path => path)
group.sitemaps_path.should == path
group.sitemap.location.sitemaps_path.to_s.should == path
end
end
describe "default_host" do
it "should inherit the default_host" do
@ls.group.default_host.should == @default_host
end
it "should set the default_host" do
host = 'http://defaulthost.com'
group = @ls.group(:default_host => host)
group.default_host.should == host
group.sitemap.location.host.should == host
end
end
describe "sitemaps_host" do
it "should set the sitemaps host" do
@host = 'http://sitemaphost.com'
@group = @ls.group(:sitemaps_host => @host)
@group.sitemaps_host.should == @host
@group.sitemap.location.host.should == @host
end
it "should finalize the sitemap if it is the only option" do
@ls.expects(:finalize_sitemap!)
@ls.group(:sitemaps_host => 'http://test.com') {}
end
it "should use the same sitemaps_namer" do
@group = @ls.group(:sitemaps_host => 'http://test.com') {}
@group.sitemap.location.namer.should == @ls.sitemap.location.namer
end
end
describe "sitemaps_namer" do
it "should inherit the value" do
@ls.group.sitemaps_namer.should == @ls.sitemaps_namer
@ls.group.sitemap.location.namer.should == @ls.sitemaps_namer
end
it "should set the value" do
namer = SitemapGenerator::SitemapNamer.new(:xxx)
group = @ls.group(:sitemaps_namer => namer)
group.sitemaps_namer.should == namer
group.sitemap.location.namer.should == namer
group.sitemap.location.filename.should =~ /xxx/
end
end
describe "should share the current sitemap" do
it "if only default_host is passed" do
group = @ls.group(:default_host => 'http://newhost.com')
group.sitemap.should == @ls.sitemap
group.sitemap.location.host.should == 'http://newhost.com'
end
end
describe "should not share the current sitemap" do
{
:filename => :xxx,
:sitemaps_path => 'en/',
:filename => :example,
:sitemaps_namer => SitemapGenerator::SitemapNamer.new(:sitemap)
}.each do |key, value|
it "if #{key} is present" do
@ls.group(key => value).sitemap.should_not == @ls.sitemap
end
end
end
describe "finalizing" do
it "should finalize the sitemaps if a block is passed" do
@group = @ls.group
@group.sitemap.finalized?.should be_false
end
it "should only finalize the sitemaps if a block is passed" do
@group = @ls.group
@group.sitemap.finalized?.should be_false
end
it "should not finalize the sitemap if a group is created" do
@ls.create { group {} }
@ls.sitemap.empty?.should be_true
@ls.sitemap.finalized?.should be_false
end
{:sitemaps_path => 'en/',
:filename => :example,
:sitemaps_namer => SitemapGenerator::SitemapNamer.new(:sitemap)}.each do |k, v|
it "should not finalize the sitemap if #{k} is present" do
@ls.expects(:finalize_sitemap!).never
@ls.group(k => v) { }
end
end
end
end
describe "after create" do
before :each do
@ls = SitemapGenerator::LinkSet.new :default_host => @default_host
end
it "should finalize the sitemap index" do
@ls.create {}
@ls.sitemap_index.finalized?.should be_true
end
it "should finalize the sitemap" do
@ls.create {}
@ls.sitemap.finalized?.should be_true
end
it "should not finalize the sitemap if a group was created" do
@ls.instance_variable_set(:@created_group, true)
@ls.send(:finalize_sitemap!)
@ls.sitemap.finalized?.should be_false
end
end
describe "options to create" do
before :each do
@ls = SitemapGenerator::LinkSet.new(:default_host => @default_host)
@ls.expects(:finalize!)
end
it "should set include_index" do
original = @ls.include_index
@ls.create(:include_index => !original).include_index.should_not == original
end
it "should set include_root" do
original = @ls.include_root
@ls.create(:include_root => !original).include_root.should_not == original
end
it "should set the filename" do
ls = @ls.create(:filename => :xxx)
ls.filename.should == :xxx
ls.sitemap.location.filename.should =~ /xxx/
end
it "should set verbose" do
original = @ls.verbose
@ls.create(:verbose => !original).verbose.should_not == original
end
it "should set the sitemaps_path" do
path = 'new/path'
ls = @ls.create(:sitemaps_path => path)
ls.sitemaps_path.should == path
ls.sitemap.location.sitemaps_path.to_s.should == path
end
it "should set the default_host" do
host = 'http://defaulthost.com'
ls = @ls.create(:default_host => host)
ls.default_host.should == host
ls.sitemap.location.host.should == host
end
it "should set the sitemaps host" do
@host = 'http://sitemaphost.com'
ls = @ls.create(:sitemaps_host => @host)
ls.sitemaps_host.should == @host
ls.sitemap.location.host.should == @host
end
it "should set the sitemaps_namer" do
namer = SitemapGenerator::SitemapNamer.new(:xxx)
ls = @ls.create(:sitemaps_namer => namer)
ls.sitemaps_namer.should == namer
ls.sitemap.location.namer.should == namer
ls.sitemap.location.filename.should =~ /xxx/
end
it "should support both sitemaps_namer and filename options" do
namer = SitemapGenerator::SitemapNamer.new("sitemap1_")
ls = @ls.create(:sitemaps_namer => namer, :filename => "sitemap1")
ls.sitemaps_namer.should == namer
ls.sitemap.location.namer.should == namer
ls.sitemap.location.filename.should =~ /sitemap1_1/
ls.sitemap_index.location.filename.should =~ /sitemap1_index/
end
it "should support both sitemaps_namer and filename options no matter the order" do
namer = SitemapGenerator::SitemapNamer.new("sitemap1_")
options = {} #ActiveSupport::OrderedHash.new
options[:sitemaps_namer] = namer
options[:filename] = "sitemap1"
ls = @ls.create(options)
ls.sitemap.location.filename.should =~ /sitemap1_1/
ls.sitemap_index.location.filename.should =~ /sitemap1_index/
end
end
describe "reset!" do
it "should reset the sitemap namer" do
SitemapGenerator::Sitemap.sitemaps_namer.expects(:reset)
SitemapGenerator::Sitemap.create(:default_host => 'http://cnn.com')
end
it "should reset the default link variable" do
SitemapGenerator::Sitemap.instance_variable_set(:@added_default_links, true)
SitemapGenerator::Sitemap.create(:default_host => 'http://cnn.com')
SitemapGenerator::Sitemap.instance_variable_set(:@added_default_links, false)
end
end
describe "include_root?" do
it "should return false" do
@ls.include_root = false
@ls.include_root.should be_false
end
it "should return true" do
@ls.include_root = true
@ls.include_root.should be_true
end
end
describe "include_index?" do
let(:sitemaps_host) { 'http://amazon.com' }
before :each do
@ls.default_host = @default_host
end
it "should be true if no sitemaps_host set, or it is the same" do
@ls.include_index = true
@ls.sitemaps_host = @default_host
@ls.include_index?.should be_true
@ls.sitemaps_host = nil
@ls.include_index?.should be_true
end
it "should be false if include_index is false or sitemaps_host differs" do
@ls.include_index = false
@ls.sitemaps_host = @default_host
@ls.include_index?.should be_false
@ls.include_index = true
@ls.sitemaps_host = sitemaps_host
@ls.include_index?.should be_false
end
it "should return false" do
ls = SitemapGenerator::LinkSet.new(:default_host => @default_host, :sitemaps_host => sitemaps_host)
ls.include_index?.should be_false
end
it "should return true" do
ls = SitemapGenerator::LinkSet.new(:default_host => @default_host, :sitemaps_host => @default_host)
ls.include_index?.should be_true
end
end
describe "output" do
let(:ls) { SitemapGenerator::LinkSet.new }
it "should not output" do
ls.verbose = false
ls.expects(:puts).never
ls.send(:output, '')
end
it "should print the given string" do
ls.verbose = true
ls.expects(:puts).with('')
ls.send(:output, '')
end
end
end