valid_hostname 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,529 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Record do
6
+ it 'valid with valid hostnames' do
7
+ record = described_class.new name: 'test',
8
+ name_without_verbose: 'test',
9
+ name_with_underscores: 'test',
10
+ name_with_wildcard: 'test.org',
11
+ name_with_valid_tld: 'test.org',
12
+ name_with_test_tld: 'test.test',
13
+ name_with_numeric_hostname: 'test',
14
+ name_with_blank: 'test',
15
+ name_with_nil: 'test'
16
+
17
+ expect(record).to be_valid
18
+ end
19
+
20
+ it 'valid with hostnames with hyphens' do
21
+ record = described_class.new name: 't-est',
22
+ name_without_verbose: 'test',
23
+ name_with_underscores: 't-est',
24
+ name_with_wildcard: 't-est.org',
25
+ name_with_valid_tld: 't-est.org',
26
+ name_with_test_tld: 't-test.test',
27
+ name_with_numeric_hostname: 't-est',
28
+ name_with_blank: 't-est',
29
+ name_with_nil: 't-est'
30
+
31
+ expect(record).to be_valid
32
+ end
33
+
34
+ it 'valid with hostnames with underscores if option is true' do
35
+ record = described_class.new name_with_underscores: '_test',
36
+ name_with_wildcard: 'test.org',
37
+ name: 'test',
38
+ name_without_verbose: 'test',
39
+ name_with_valid_tld: 'test.org',
40
+ name_with_test_tld: 'test.test',
41
+ name_with_numeric_hostname: 'test',
42
+ name_with_blank: 'test',
43
+ name_with_nil: 'test'
44
+
45
+ expect(record).to be_valid
46
+ end
47
+
48
+ it 'invalid with hostnames with underscores if option is false' do
49
+ record = described_class.new name_with_underscores: '_test',
50
+ name_with_wildcard: '_test.org',
51
+ name: '_test',
52
+ name_with_valid_tld: '_test.org',
53
+ name_with_test_tld: '_test.test',
54
+ name_with_numeric_hostname: '_test',
55
+ name_with_blank: '_test',
56
+ name_with_nil: '_test'
57
+
58
+ expect(record).not_to be_valid
59
+
60
+ expect(record.errors).to have_key :name
61
+ expect(record.errors).to have_key :name_with_valid_tld
62
+ expect(record.errors).to have_key :name_with_test_tld
63
+ expect(record.errors).to have_key :name_with_numeric_hostname
64
+ expect(record.errors).to have_key :name_with_wildcard
65
+ expect(record.errors).to have_key :name_with_blank
66
+ expect(record.errors).to have_key :name_with_nil
67
+ end
68
+
69
+ it 'valid with hostnames with wildcard if option is true' do
70
+ record = described_class.new name: 'test',
71
+ name_without_verbose: 'test',
72
+ name_with_wildcard: '*.test.org',
73
+ name_with_underscores: 'test.org',
74
+ name_with_valid_tld: 'test.org',
75
+ name_with_test_tld: 'test.test',
76
+ name_with_numeric_hostname: 'test',
77
+ name_with_blank: 'test',
78
+ name_with_nil: 'test'
79
+
80
+ expect(record).to be_valid
81
+ end
82
+
83
+ it 'invalid with hostnames with wildcard if option is false' do
84
+ record = described_class.new name: '*.test',
85
+ name_with_wildcard: '*.test.org',
86
+ name_with_underscores: '*.test',
87
+ name_with_valid_tld: '*.test.org',
88
+ name_with_test_tld: '*.test.test',
89
+ name_with_numeric_hostname: '*.test',
90
+ name_with_blank: '*.test',
91
+ name_with_nil: '*.test'
92
+
93
+ expect(record).not_to be_valid
94
+
95
+ expect(record.errors).to have_key :name
96
+ expect(record.errors).to have_key :name_with_underscores
97
+ expect(record.errors).to have_key :name_with_valid_tld
98
+ expect(record.errors).to have_key :name_with_test_tld
99
+ expect(record.errors).to have_key :name_with_numeric_hostname
100
+ expect(record.errors).to have_key :name_with_blank
101
+ expect(record.errors).to have_key :name_with_nil
102
+ end
103
+
104
+ it 'valid with blank hostname' do
105
+ record = described_class.new name_with_blank: '',
106
+ name: 'test',
107
+ name_without_verbose: 'test',
108
+ name_with_wildcard: 'test.org',
109
+ name_with_valid_tld: 'test.org',
110
+ name_with_test_tld: 'test.test',
111
+ name_with_numeric_hostname: 'test',
112
+ name_with_underscores: 'test',
113
+ name_with_nil: 'test'
114
+
115
+ expect(record).to be_valid
116
+ end
117
+
118
+ it 'invalid with blank hostname' do
119
+ record = described_class.new name: '',
120
+ name_with_underscores: '',
121
+ name_with_wildcard: '',
122
+ name_with_valid_tld: '',
123
+ name_with_test_tld: '',
124
+ name_with_numeric_hostname: '',
125
+ name_with_nil: '',
126
+ name_with_blank: ''
127
+
128
+ expect(record).not_to be_valid
129
+
130
+ expect(record.errors).to have_key :name
131
+ expect(record.errors).to have_key :name_with_underscores
132
+ expect(record.errors).to have_key :name_with_wildcard
133
+ expect(record.errors).to have_key :name_with_valid_tld
134
+ expect(record.errors).to have_key :name_with_test_tld
135
+ expect(record.errors).to have_key :name_with_numeric_hostname
136
+ expect(record.errors).to have_key :name_with_nil
137
+ end
138
+
139
+ it 'valid with nil hostname' do
140
+ record = described_class.new name_with_nil: nil,
141
+ name: 'test',
142
+ name_without_verbose: 'test',
143
+ name_with_underscores: 'test',
144
+ name_with_wildcard: 'test.org',
145
+ name_with_valid_tld: 'test.org',
146
+ name_with_test_tld: 'test.test',
147
+ name_with_numeric_hostname: 'test',
148
+ name_with_blank: 'test'
149
+
150
+ expect(record).to be_valid
151
+ end
152
+
153
+ it 'valid when domain name length between 64 and 255' do
154
+ long_labels = "#{'t' * 60}.#{'t' * 60}"
155
+ record = described_class.new name: long_labels,
156
+ name_without_verbose: 'test',
157
+ name_with_underscores: long_labels,
158
+ name_with_wildcard: long_labels,
159
+ name_with_valid_tld: "#{long_labels}.org",
160
+ name_with_test_tld: "#{long_labels}.test",
161
+ name_with_numeric_hostname: long_labels,
162
+ name_with_blank: long_labels,
163
+ name_with_nil: long_labels
164
+
165
+ expect(record).to be_valid
166
+ end
167
+
168
+ it 'invalid with too long hostname' do
169
+ longname = ('t' * 256)
170
+ record = described_class.new name: longname,
171
+ name_with_underscores: longname,
172
+ name_with_wildcard: longname,
173
+ name_with_valid_tld: "#{longname}.org",
174
+ name_with_test_tld: "#{longname}.test",
175
+ name_with_numeric_hostname: longname,
176
+ name_with_blank: longname,
177
+ name_with_nil: longname
178
+
179
+ expect(record).not_to be_valid
180
+
181
+ expect(record.errors).to have_key :name
182
+ expect(record.errors).to have_key :name_with_underscores
183
+ expect(record.errors).to have_key :name_with_wildcard
184
+ expect(record.errors).to have_key :name_with_valid_tld
185
+ expect(record.errors).to have_key :name_with_test_tld
186
+ expect(record.errors).to have_key :name_with_numeric_hostname
187
+ expect(record.errors).to have_key :name_with_blank
188
+ expect(record.errors).to have_key :name_with_nil
189
+ end
190
+
191
+ it 'invalid with too long hostname label' do
192
+ long_labels = "#{'t' * 64}.#{'t' * 64}"
193
+ record = described_class.new name: long_labels,
194
+ name_with_underscores: long_labels,
195
+ name_with_wildcard: long_labels,
196
+ name_with_valid_tld: "#{long_labels}.org",
197
+ name_with_test_tld: "#{long_labels}.test",
198
+ name_with_numeric_hostname: long_labels,
199
+ name_with_blank: long_labels,
200
+ name_with_nil: long_labels
201
+
202
+ expect(record).not_to be_valid
203
+ expect(record.errors).to have_key :name
204
+ end
205
+
206
+ it 'invalid with invalid characters' do
207
+ record = described_class.new
208
+ %w[; : * ^ ~ + ' ! # " % & / ( ) = ? $ \\ ].each do |char|
209
+ testname = "#{char}test"
210
+ record.name = testname
211
+ record.name_with_underscores = testname
212
+ record.name_with_wildcard = testname
213
+ record.name_with_valid_tld = "#{testname}.org"
214
+ record.name_with_test_tld = "#{testname}.test"
215
+ record.name_with_numeric_hostname = testname
216
+ record.name_with_blank = testname
217
+ record.name_with_nil = testname
218
+
219
+ expect(record).not_to be_valid
220
+
221
+ expect(record.errors).to have_key :name
222
+ expect(record.errors).to have_key :name_with_underscores
223
+ expect(record.errors).to have_key :name_with_wildcard
224
+ expect(record.errors).to have_key :name_with_valid_tld
225
+ expect(record.errors).to have_key :name_with_test_tld
226
+ expect(record.errors).to have_key :name_with_numeric_hostname
227
+ expect(record.errors).to have_key :name_with_blank
228
+ expect(record.errors).to have_key :name_with_nil
229
+ end
230
+ end
231
+
232
+ it 'invalid with hostname labels beginning with a hyphen' do
233
+ record = described_class.new name: '-test',
234
+ name_with_underscores: '-test',
235
+ name_with_wildcard: '-test',
236
+ name_with_valid_tld: '-test.org',
237
+ name_with_test_tld: '-test.test',
238
+ name_with_numeric_hostname: '-test',
239
+ name_with_blank: '-test',
240
+ name_with_nil: '-test'
241
+
242
+ expect(record).not_to be_valid
243
+
244
+ expect(record.errors).to have_key :name
245
+ expect(record.errors).to have_key :name_with_underscores
246
+ expect(record.errors).to have_key :name_with_wildcard
247
+ expect(record.errors).to have_key :name_with_valid_tld
248
+ expect(record.errors).to have_key :name_with_test_tld
249
+ expect(record.errors).to have_key :name_with_numeric_hostname
250
+ expect(record.errors).to have_key :name_with_blank
251
+ expect(record.errors).to have_key :name_with_nil
252
+ end
253
+
254
+ it 'invalid with hostname labels ending with a hyphen' do
255
+ record = described_class.new name: 'test-',
256
+ name_with_underscores: 'test-',
257
+ name_with_wildcard: 'test-',
258
+ name_with_valid_tld: 'test-.org',
259
+ name_with_test_tld: 'test-.test',
260
+ name_with_numeric_hostname: 'test-',
261
+ name_with_blank: 'test-',
262
+ name_with_nil: 'test-'
263
+
264
+ expect(record).not_to be_valid
265
+
266
+ expect(record.errors).to have_key :name
267
+ expect(record.errors).to have_key :name_with_underscores
268
+ expect(record.errors).to have_key :name_with_wildcard
269
+ expect(record.errors).to have_key :name_with_valid_tld
270
+ expect(record.errors).to have_key :name_with_test_tld
271
+ expect(record.errors).to have_key :name_with_numeric_hostname
272
+ expect(record.errors).to have_key :name_with_blank
273
+ expect(record.errors).to have_key :name_with_nil
274
+ end
275
+
276
+ it 'invalid hostnames with numeric only hostname labels' do
277
+ record = described_class.new name: '12345',
278
+ name_with_underscores: '12345',
279
+ name_with_wildcard: '12345',
280
+ name_with_valid_tld: '12345.org',
281
+ name_with_test_tld: '12345.test',
282
+ name_with_numeric_hostname: '0x12345',
283
+ name_with_blank: '12345',
284
+ name_with_nil: '12345'
285
+
286
+ expect(record).not_to be_valid
287
+
288
+ expect(record.errors).to have_key :name
289
+ expect(record.errors).to have_key :name_with_underscores
290
+ expect(record.errors).to have_key :name_with_wildcard
291
+ expect(record.errors).to have_key :name_with_valid_tld
292
+ expect(record.errors).to have_key :name_with_test_tld
293
+ expect(record.errors).to have_key :name_with_blank
294
+ expect(record.errors).to have_key :name_with_nil
295
+ end
296
+
297
+ it 'valid hostnames with numeric only hostname labels if option is true' do
298
+ record = described_class.new name_with_numeric_hostname: '12345',
299
+ name: 'test',
300
+ name_without_verbose: 'test',
301
+ name_with_underscores: 'test',
302
+ name_with_wildcard: 'test',
303
+ name_with_valid_tld: 'test.org',
304
+ name_with_test_tld: 'test.test',
305
+ name_with_blank: 'test',
306
+ name_with_nil: 'test'
307
+
308
+ expect(record).to be_valid
309
+ end
310
+
311
+ it 'invalid hostnames with invalid tld if option is true' do
312
+ record = described_class.new name_with_valid_tld: 'test.invalidtld',
313
+ name: 'test',
314
+ name_with_underscores: 'test',
315
+ name_with_wildcard: 'test',
316
+ name_with_test_tld: 'test.test',
317
+ name_with_numeric_hostname: 'test',
318
+ name_with_blank: 'test',
319
+ name_with_nil: 'test'
320
+
321
+ expect(record).not_to be_valid
322
+ expect(record.errors).to have_key :name_with_valid_tld
323
+ end
324
+
325
+ it 'valid hostnames with valid tld if option is true' do
326
+ record = described_class.new name_with_valid_tld: 'test.org',
327
+ name: 'test',
328
+ name_without_verbose: 'test',
329
+ name_with_underscores: 'test',
330
+ name_with_wildcard: 'test',
331
+ name_with_test_tld: 'test.test',
332
+ name_with_numeric_hostname: 'test',
333
+ name_with_blank: 'test',
334
+ name_with_nil: 'test'
335
+
336
+ expect(record).to be_valid
337
+ end
338
+
339
+ it 'valid hostnames with invalid tld if option is false' do
340
+ record = described_class.new name: 'test.invalidtld',
341
+ name_without_verbose: 'test.invalidtld',
342
+ name_with_underscores: 'test.invalidtld',
343
+ name_with_wildcard: 'test.invalidtld',
344
+ name_with_valid_tld: 'test.org',
345
+ name_with_test_tld: 'test.test',
346
+ name_with_numeric_hostname: 'test.invalidtld',
347
+ name_with_blank: 'test.invalidtld',
348
+ name_with_nil: 'test.invalidtld'
349
+
350
+ expect(record).to be_valid
351
+ end
352
+
353
+ it 'valid hostnames with tld from list' do
354
+ record = described_class.new name_with_test_tld: 'test.test',
355
+ name: 'test',
356
+ name_without_verbose: 'test',
357
+ name_with_underscores: 'test',
358
+ name_with_wildcard: 'test',
359
+ name_with_valid_tld: 'test.org',
360
+ name_with_numeric_hostname: 'test',
361
+ name_with_blank: 'test',
362
+ name_with_nil: 'test'
363
+
364
+ expect(record).to be_valid
365
+ end
366
+
367
+ it 'invalid hostnames with invalid tld from list' do
368
+ record = described_class.new name_with_test_tld: 'test.invalidtld',
369
+ name: 'test',
370
+ name_with_underscores: 'test',
371
+ name_with_wildcard: 'test',
372
+ name_with_valid_tld: 'test.org',
373
+ name_with_numeric_hostname: 'test',
374
+ name_with_blank: 'test',
375
+ name_with_nil: 'test'
376
+
377
+ expect(record).not_to be_valid
378
+ expect(record.errors).to have_key :name_with_test_tld
379
+ end
380
+
381
+ it 'invalid domainnames with single numeric hostname labels' do
382
+ record = described_class.new domainname_with_numeric_hostname: '12345',
383
+ name: 'test',
384
+ name_with_underscores: 'test',
385
+ name_with_wildcard: 'test',
386
+ name_with_valid_tld: 'test.org',
387
+ name_with_test_tld: 'test.test',
388
+ name_with_numeric_hostname: 'test',
389
+ name_with_blank: 'test',
390
+ name_with_nil: 'test'
391
+
392
+ expect(record).not_to be_valid
393
+ expect(record.errors).to have_key :domainname_with_numeric_hostname
394
+ end
395
+
396
+ it 'valid domainnames with numeric hostname labels' do
397
+ record = described_class.new domainname_with_numeric_hostname: '12345.org',
398
+ name: 'test',
399
+ name_without_verbose: 'test',
400
+ name_with_underscores: 'test',
401
+ name_with_wildcard: 'test',
402
+ name_with_valid_tld: 'test.org',
403
+ name_with_test_tld: 'test.test',
404
+ name_with_numeric_hostname: 'test',
405
+ name_with_blank: 'test',
406
+ name_with_nil: 'test'
407
+
408
+ expect(record).to be_valid
409
+ end
410
+
411
+ it 'invalid hostnames containing consecutive dots' do
412
+ record = described_class.new name: 'te...st',
413
+ name_with_underscores: 'test',
414
+ name_with_wildcard: 'test',
415
+ name_with_valid_tld: 'test.org',
416
+ name_with_test_tld: 'test.test',
417
+ name_with_numeric_hostname: 'test',
418
+ name_with_blank: 'test',
419
+ name_with_nil: 'test'
420
+
421
+ expect(record).not_to be_valid
422
+ expect(record.errors).to have_key :name
423
+ end
424
+
425
+ it 'valid hostnames with trailing dot if option is true' do
426
+ record = described_class.new name_with_valid_root_label: 'test.org.',
427
+ name: 'test',
428
+ name_without_verbose: 'test',
429
+ name_with_underscores: 'test',
430
+ name_with_wildcard: 'test',
431
+ name_with_valid_tld: 'test.org',
432
+ name_with_test_tld: 'test.test',
433
+ name_with_numeric_hostname: 'test',
434
+ name_with_blank: 'test',
435
+ name_with_nil: 'test'
436
+
437
+ expect(record).to be_valid
438
+ end
439
+
440
+ it 'invalid hostnames with trailing dot if option is false' do
441
+ record = described_class.new name_with_invalid_root_label: 'test.org.',
442
+ name: 'test',
443
+ name_with_underscores: 'test',
444
+ name_with_wildcard: 'test',
445
+ name_with_valid_tld: 'test.org',
446
+ name_with_test_tld: 'test.test',
447
+ name_with_numeric_hostname: 'test',
448
+ name_with_blank: 'test',
449
+ name_with_nil: 'test'
450
+
451
+ expect(record).not_to be_valid
452
+ expect(record.errors).to have_key :name_with_invalid_root_label
453
+ end
454
+
455
+ it 'valid hostnames consisting of a single dot if option is true' do
456
+ record = described_class.new name_with_valid_root_label: '.',
457
+ name: 'test',
458
+ name_without_verbose: 'test',
459
+ name_with_underscores: 'test',
460
+ name_with_wildcard: 'test',
461
+ name_with_valid_tld: 'test.org',
462
+ name_with_test_tld: 'test.test',
463
+ name_with_numeric_hostname: 'test',
464
+ name_with_blank: 'test',
465
+ name_with_nil: 'test'
466
+
467
+ expect(record).to be_valid
468
+ end
469
+
470
+ it 'invalid hostnames consisting of a single dot' do
471
+ record = described_class.new name_with_invalid_root_label: '.',
472
+ name: 'test',
473
+ name_with_underscores: 'test',
474
+ name_with_wildcard: 'test',
475
+ name_with_valid_tld: 'test.org',
476
+ name_with_test_tld: 'test.test',
477
+ name_with_numeric_hostname: 'test',
478
+ name_with_blank: 'test',
479
+ name_with_nil: 'test'
480
+
481
+ expect(record).not_to be_valid
482
+ expect(record.errors).to have_key :name_with_invalid_root_label
483
+ end
484
+
485
+ it 'valid domainnames consisting of a single dot if option is true' do
486
+ record = described_class.new domainname_with_valid_root_label: '.',
487
+ name: 'test',
488
+ name_without_verbose: 'test',
489
+ name_with_underscores: 'test',
490
+ name_with_wildcard: 'test',
491
+ name_with_valid_tld: 'test.org',
492
+ name_with_test_tld: 'test.test',
493
+ name_with_numeric_hostname: 'test',
494
+ name_with_blank: 'test',
495
+ name_with_nil: 'test'
496
+
497
+ expect(record).to be_valid
498
+ end
499
+
500
+ it 'invalid domainnames consisting of a single dot' do
501
+ record = described_class.new domainname_with_invalid_root_label: '.',
502
+ name: 'test',
503
+ name_with_underscores: 'test',
504
+ name_with_wildcard: 'test',
505
+ name_with_valid_tld: 'test.org',
506
+ name_with_test_tld: 'test.test',
507
+ name_with_numeric_hostname: 'test',
508
+ name_with_blank: 'test',
509
+ name_with_nil: 'test'
510
+
511
+ expect(record).not_to be_valid
512
+ expect(record.errors).to have_key :domainname_with_invalid_root_label
513
+ end
514
+
515
+ it 'incorrect hostname without verbose' do
516
+ record = described_class.new name: 'test',
517
+ name_without_verbose: 'te..st',
518
+ name_with_underscores: 'test',
519
+ name_with_wildcard: 'test.org',
520
+ name_with_valid_tld: 'test.org',
521
+ name_with_test_tld: 'test.test',
522
+ name_with_numeric_hostname: 'test',
523
+ name_with_blank: 'test',
524
+ name_with_nil: 'test'
525
+
526
+ expect(record).not_to be_valid
527
+ expect(record.errors).to have_key :name_without_verbose
528
+ end
529
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rspec'
4
+
5
+ require 'valid_hostname'
6
+ require 'test_model'
7
+
8
+ RSpec.configure do |config|
9
+ config.mock_with :rspec
10
+ end
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Record
4
+ include ActiveModel::Model
5
+ include ActiveModel::Validations
6
+
7
+ attr_accessor :name,
8
+ :name_without_verbose,
9
+ :name_with_underscores,
10
+ :name_with_wildcard,
11
+ :name_with_numeric_hostname,
12
+ :name_with_blank,
13
+ :name_with_nil,
14
+ :name_with_valid_tld,
15
+ :name_with_test_tld,
16
+ :name_with_valid_root_label,
17
+ :name_with_invalid_root_label,
18
+ :domainname_with_numeric_hostname,
19
+ :domainname_with_valid_root_label,
20
+ :domainname_with_invalid_root_label
21
+
22
+ validates :name,
23
+ hostname: true
24
+
25
+ validates :name_without_verbose,
26
+ hostname: { verbose: false }
27
+
28
+ validates :name_with_underscores,
29
+ hostname: { allow_underscore: true }
30
+
31
+ validates :name_with_wildcard,
32
+ hostname: { allow_wildcard_hostname: true }
33
+
34
+ validates :name_with_numeric_hostname,
35
+ hostname: { allow_numeric_hostname: true }
36
+
37
+ validates :name_with_blank,
38
+ hostname: true,
39
+ allow_blank: true
40
+
41
+ validates :name_with_nil,
42
+ hostname: true,
43
+ allow_nil: true
44
+
45
+ validates :name_with_valid_tld,
46
+ hostname: { require_valid_tld: true }
47
+
48
+ validates :name_with_test_tld,
49
+ hostname: { require_valid_tld: true, valid_tlds: %w[test] }
50
+
51
+ validates :name_with_valid_root_label,
52
+ hostname: { allow_root_label: true },
53
+ allow_nil: true,
54
+ allow_blank: true
55
+
56
+ validates :name_with_invalid_root_label,
57
+ hostname: true,
58
+ allow_nil: true,
59
+ allow_blank: true
60
+
61
+ validates :domainname_with_numeric_hostname,
62
+ domainname: true,
63
+ allow_nil: true,
64
+ allow_blank: true
65
+
66
+ validates :domainname_with_valid_root_label,
67
+ domainname: { allow_root_label: true },
68
+ allow_nil: true,
69
+ allow_blank: true
70
+
71
+ validates :domainname_with_invalid_root_label,
72
+ domainname: true,
73
+ allow_nil: true,
74
+ allow_blank: true
75
+ end