/
parser_spec.rb
422 lines (342 loc) · 15.6 KB
/
parser_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
require 'spec_helper'
describe Whois::Record::Parser do
subject { described_class.new(record) }
let(:record) { Whois::Record.new(nil, []) }
describe ".parser_klass" do
it "returns the parser hostname converted into a class" do
require 'whois/record/parser/whois.crsnic.net'
described_class.parser_klass("whois.crsnic.net").should == Whois::Record::Parser::WhoisCrsnicNet
end
it "recognizes and lazy-loads classes" do
described_class.parser_klass("whois.nic.it").name.should == "Whois::Record::Parser::WhoisNicIt"
end
it "recognizes preloaded classes" do
described_class.class_eval <<-RUBY
class PreloadedParserTest
end
RUBY
described_class.parser_klass("preloaded.parser.test").name.should == "Whois::Record::Parser::PreloadedParserTest"
end
it "returns the blank parser if the parser doesn't exist" do
described_class.parser_klass("whois.missing.test").name.should == "Whois::Record::Parser::Blank"
end
end
describe ".host_to_parser" do
it "works" do
described_class.host_to_parser("whois.it").should == "WhoisIt"
described_class.host_to_parser("whois.nic.it").should == "WhoisNicIt"
described_class.host_to_parser("whois.domain-registry.nl").should == "WhoisDomainRegistryNl"
end
it "downcases hostnames" do
described_class.host_to_parser("whois.PublicDomainRegistry.com").should == "WhoisPublicdomainregistryCom"
end
end
describe "#initialize" do
it "requires an record" do
lambda { described_class.new }.should raise_error(ArgumentError)
lambda { described_class.new(record) }.should_not raise_error
end
it "sets record from argument" do
described_class.new(record).record.should be(record)
end
end
describe "#respond_to?" do
before(:all) do
@_properties = Whois::Record::Parser::PROPERTIES.dup
@_methods = Whois::Record::Parser::METHODS.dup
end
after(:all) do
Whois::Record::Parser::PROPERTIES.clear
Whois::Record::Parser::PROPERTIES.push(*@_properties)
Whois::Record::Parser::METHODS.clear
Whois::Record::Parser::METHODS.push(*@_methods)
end
it "returns true if method is in self" do
subject.respond_to?(:to_s).should be_true
end
it "returns true if method is in hierarchy" do
subject.respond_to?(:nil?).should be_true
end
it "returns true if method is a property" do
Whois::Record::Parser::PROPERTIES << :test_property
subject.respond_to?(:test_property).should be_true
end
it "returns false if method is a property?" do
Whois::Record::Parser::PROPERTIES << :test_property
subject.respond_to?(:test_property?).should be_false
end
it "returns true if method is a method" do
Whois::Record::Parser::METHODS << :test_method
subject.respond_to?(:test_method).should be_true
end
it "returns false if method is a method" do
Whois::Record::Parser::METHODS << :test_method
subject.respond_to?(:test_method?).should be_false
end
end
describe "property lookup" do
class Whois::Record::Parser::ParserSupportedTest < Whois::Record::Parser::Base
property_supported :status do
:status_supported
end
property_supported :created_on do
:created_on_supported
end
property_supported :updated_on do
:updated_on_supported
end
property_supported :expires_on do
:expires_on_supported
end
end
class Whois::Record::Parser::ParserUndefinedTest < Whois::Record::Parser::Base
property_supported :status do
:status_undefined
end
# not defined :created_on
# not defined :updated_on
# not defined :expires_on
end
class Whois::Record::Parser::ParserUnsupportedTest < Whois::Record::Parser::Base
property_not_supported :status
property_not_supported :created_on
property_not_supported :updated_on
property_not_supported :expires_on
end
it "delegates to first parser when all supported" do
r = Whois::Record.new(nil, [Whois::Record::Part.new(:body => "", :host => "parser.supported.test"), Whois::Record::Part.new(:body => "", :host => "parser.undefined.test")])
described_class.new(r).status.should == :status_undefined
r = Whois::Record.new(nil, [Whois::Record::Part.new(:body => "", :host => "parser.undefined.test"), Whois::Record::Part.new(:body => "", :host => "parser.supported.test")])
described_class.new(r).status.should == :status_supported
end
it "delegates to first parser when one supported" do
r = Whois::Record.new(nil, [Whois::Record::Part.new(:body => "", :host => "parser.supported.test"), Whois::Record::Part.new(:body => "", :host => "parser.undefined.test")])
described_class.new(r).created_on.should == :created_on_supported
r = Whois::Record.new(nil, [Whois::Record::Part.new(:body => "", :host => "parser.undefined.test"), Whois::Record::Part.new(:body => "", :host => "parser.supported.test")])
described_class.new(r).created_on.should == :created_on_supported
end
it "raises unless at least one is supported" do
lambda do
r = Whois::Record.new(nil, [Whois::Record::Part.new(:body => "", :host => "parser.unsupported.test"), Whois::Record::Part.new(:body => "", :host => "parser.unsupported.test")])
described_class.new(r).created_on
end.should raise_error(Whois::AttributeNotSupported)
end
it "raises when parsers are undefined" do
lambda do
r = Whois::Record.new(nil, [Whois::Record::Part.new(:body => "", :host => "parser.undefined.test"), Whois::Record::Part.new(:body => "", :host => "parser.undefined.test")])
described_class.new(r).created_on
end.should raise_error(Whois::AttributeNotImplemented)
end
it "raises when zero parts" do
lambda do
r = Whois::Record.new(nil, [])
described_class.new(r).created_on
end.should raise_error(Whois::ParserError, /the Record is empty/)
end
it "does not delegate unknown properties" do
lambda do
r = Whois::Record.new(nil, [Whois::Record::Part.new(:body => "", :host => "parser.undefined.test")])
described_class.new(r).unknown_method
end.should raise_error(NoMethodError)
end
end
describe "#parsers" do
it "returns 0 parsers when 0 parts" do
record = Whois::Record.new(nil, [])
parser = described_class.new(record)
parser.parsers.should have(0).parsers
parser.parsers.should == []
end
it "returns 1 parser when 1 part" do
record = Whois::Record.new(nil, [Whois::Record::Part.new(:body => nil, :host => "whois.nic.it")])
parser = described_class.new(record)
parser.parsers.should have(1).parsers
end
it "returns 2 parsers when 2 part" do
record = Whois::Record.new(nil, [Whois::Record::Part.new(:body => nil, :host => "whois.crsnic.net"), Whois::Record::Part.new(:body => nil, :host => "whois.nic.it")])
parser = described_class.new(record)
parser.parsers.should have(2).parsers
end
it "initializes the parsers in reverse order" do
record = Whois::Record.new(nil, [Whois::Record::Part.new(:body => nil, :host => "whois.crsnic.net"), Whois::Record::Part.new(:body => nil, :host => "whois.nic.it")])
parser = described_class.new(record)
parser.parsers[0].should be_a(Whois::Record::Parser::WhoisNicIt)
parser.parsers[1].should be_a(Whois::Record::Parser::WhoisCrsnicNet)
end
it "returns the host parser when the part is supported" do
record = Whois::Record.new(nil, [Whois::Record::Part.new(:body => nil, :host => "whois.nic.it")])
parser = described_class.new(record)
parser.parsers.first.should be_a(Whois::Record::Parser::WhoisNicIt)
end
it "returns the Blank parser when the part is not supported" do
record = Whois::Record.new(nil, [Whois::Record::Part.new(:body => nil, :host => "missing.nic.it")])
parser = described_class.new(record)
parser.parsers.first.should be_a(Whois::Record::Parser::Blank)
end
end
describe "#property_any_supported?" do
it "returns false when 0 parts" do
record = Whois::Record.new(nil, [])
described_class.new(record).property_any_supported?(:disclaimer).should be_false
end
it "returns true when 1 part supported" do
record = Whois::Record.new(nil, [Whois::Record::Part.new(host: "whois.nic.it")])
described_class.new(record).property_any_supported?(:disclaimer).should be_true
end
it "returns false when 1 part supported" do
record = Whois::Record.new(nil, [Whois::Record::Part.new(host: "missing.nic.it")])
described_class.new(record).property_any_supported?(:disclaimer).should be_false
end
it "returns true when 2 parts" do
record = Whois::Record.new(nil, [Whois::Record::Part.new(host: "whois.crsnic.net"), Whois::Record::Part.new(host: "whois.nic.it")])
described_class.new(record).property_any_supported?(:disclaimer).should be_true
end
it "returns true when 1 part supported 1 part not supported" do
record = Whois::Record.new(nil, [Whois::Record::Part.new(host: "missing.nic.it"), Whois::Record::Part.new(host: "whois.nic.it")])
described_class.new(record).property_any_supported?(:disclaimer).should be_true
end
end
describe "#contacts" do
class Whois::Record::Parser::Contacts1Test < Whois::Record::Parser::Base
end
class Whois::Record::Parser::Contacts2Test < Whois::Record::Parser::Base
property_supported(:technical_contacts) { ["p2-t1"] }
property_supported(:admin_contacts) { ["p2-a1"] }
property_supported(:registrant_contacts) { [] }
end
class Whois::Record::Parser::Contacts3Test< Whois::Record::Parser::Base
property_supported(:technical_contacts) { ["p3-t1"] }
end
it "returns an empty array when 0 parts" do
record = Whois::Record.new(nil, [])
parser = described_class.new(record)
parser.contacts.should == []
end
it "returns an array of contact when 1 part is supported" do
record = Whois::Record.new(nil, [Whois::Record::Part.new(:body => nil, :host => "contacts2.test")])
parser = described_class.new(record)
parser.contacts.should have(2).contacts
parser.contacts.should == %w( p2-a1 p2-t1 )
end
it "returns an array of contact when 1 part is not supported" do
record = Whois::Record.new(nil, [Whois::Record::Part.new(:body => nil, :host => "contacts1.test")])
parser = described_class.new(record)
parser.contacts.should have(0).contacts
parser.contacts.should == %w()
end
it "merges the contacts and returns an array of contact when 2 parts" do
record = Whois::Record.new(nil, [Whois::Record::Part.new(:body => nil, :host => "contacts2.test"), Whois::Record::Part.new(:body => nil, :host => "contacts3.test")])
parser = described_class.new(record)
parser.contacts.should have(3).contacts
parser.contacts.should == %w( p3-t1 p2-a1 p2-t1 )
end
end
describe "#changed?" do
it "raises if the argument is not an instance of the same class" do
lambda do
described_class.new(record).changed?(Object.new)
end.should raise_error
lambda do
described_class.new(record).changed?(described_class.new(record))
end.should_not raise_error
end
end
describe "#unchanged?" do
it "raises if the argument is not an instance of the same class" do
lambda do
described_class.new(record).unchanged?(Object.new)
end.should raise_error
lambda do
described_class.new(record).unchanged?(described_class.new(record))
end.should_not raise_error
end
it "returns true if self and other references the same object" do
instance = described_class.new(record)
instance.unchanged?(instance).should be_true
end
it "returns false if parser and other.parser have different number of elements" do
instance = described_class.new(Whois::Record.new(nil, []))
other = described_class.new(Whois::Record.new(nil, [Whois::Record::Part.new(:body => "", :host => "foo.example.test")]))
instance.unchanged?(other).should be_false
end
it "returns true if parsers and other.parsers have 0 elements" do
instance = described_class.new(Whois::Record.new(nil, []))
other = described_class.new(Whois::Record.new(nil, []))
instance.unchanged?(other).should be_true
end
it "returns true if every parser in self marches the corresponding parser in other" do
instance = described_class.new(Whois::Record.new(nil, [Whois::Record::Part.new(:body => "hello", :host => "foo.example.test"), Whois::Record::Part.new(:body => "hello", :host => "bar.example.test")]))
other = described_class.new(Whois::Record.new(nil, [Whois::Record::Part.new(:body => "hello", :host => "foo.example.test"), Whois::Record::Part.new(:body => "hello", :host => "bar.example.test")]))
instance.unchanged?(other).should be_true
end
it "returns false unless every parser in self marches the corresponding parser in other" do
instance = described_class.new(Whois::Record.new(nil, [Whois::Record::Part.new(:body => "hello", :host => "foo.example.test"), Whois::Record::Part.new(:body => "world", :host => "bar.example.test")]))
other = described_class.new(Whois::Record.new(nil, [Whois::Record::Part.new(:body => "hello", :host => "foo.example.test"), Whois::Record::Part.new(:body => "baby!", :host => "bar.example.test")]))
instance.unchanged?(other).should be_false
end
end
describe "#response_incomplete?" do
it "returns false when all parts are complete" do
i = parsers("defined-false", "defined-false")
i.response_incomplete?.should == false
end
it "returns true when at least one part is incomplete" do
i = parsers("defined-false", "defined-true")
i.response_incomplete?.should == true
i = parsers("defined-true", "defined-false")
i.response_incomplete?.should == true
end
end
describe "#response_throttled?" do
it "returns false when all parts are not throttled" do
i = parsers("defined-false", "defined-false")
i.response_throttled?.should == false
end
it "returns true when at least one part is throttled" do
i = parsers("defined-false", "defined-true")
i.response_throttled?.should == true
i = parsers("defined-true", "defined-false")
i.response_throttled?.should == true
end
end
describe "#response_unavailable?" do
it "returns false when all parts are available" do
i = parsers("defined-false", "defined-false")
i.response_unavailable?.should == false
end
it "returns true when at least one part is unavailable" do
i = parsers("defined-false", "defined-true")
i.response_unavailable?.should == true
i = parsers("defined-true", "defined-false")
i.response_unavailable?.should == true
end
end
private
class Whois::Record::Parser::ResponseDefinedTrueTest < Whois::Record::Parser::Base
def response_incomplete?
true
end
def response_throttled?
true
end
def response_unavailable?
true
end
end
class Whois::Record::Parser::ResponseDefinedFalseTest < Whois::Record::Parser::Base
def response_incomplete?
false
end
def response_throttled?
false
end
def response_unavailable?
false
end
end
class Whois::Record::Parser::ResponseUndefinedTest < Whois::Record::Parser::Base
end
def parsers(*types)
described_class.new(Whois::Record.new(nil, types.map { |type| Whois::Record::Part.new(:body => "", :host => "response-#{type}.test") }))
end
end