Source

web2py / gluon / tests / test_is_url.py

Full commit
  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
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Unit tests for IS_URL()
"""

import sys
import os
sys.path.append(os.path.realpath('../'))

import unittest
from validators import IS_URL, IS_HTTP_URL, IS_GENERIC_URL, \
    unicode_to_ascii_authority


class TestIsUrl(unittest.TestCase):

    def testModeHttp(self):

        # defaults to mode='http'

        x = IS_URL()
        self.assertEqual(x('http://google.ca'), ('http://google.ca',
                         None))
        self.assertEqual(x('google.ca'), ('http://google.ca', None))
        self.assertEqual(x('google.ca:80'), ('http://google.ca:80',
                         None))
        self.assertEqual(x('unreal.blargg'), ('unreal.blargg',
                         'invalid url!'))
        self.assertEqual(x('google..ca'), ('google..ca', 'invalid url!'))
        self.assertEqual(x('google.ca..'), ('google.ca..', 'invalid url!'))

        # explicit use of 'http' mode

        x = IS_URL(mode='http')
        self.assertEqual(x('http://google.ca'), ('http://google.ca',
                         None))
        self.assertEqual(x('google.ca'), ('http://google.ca', None))
        self.assertEqual(x('google.ca:80'), ('http://google.ca:80',
                         None))
        self.assertEqual(x('unreal.blargg'), ('unreal.blargg',
                         'invalid url!'))

        # prepends 'https' instead of 'http'

        x = IS_URL(mode='http', prepend_scheme='https')
        self.assertEqual(x('http://google.ca'), ('http://google.ca',
                         None))
        self.assertEqual(x('google.ca'), ('https://google.ca', None))
        self.assertEqual(x('google.ca:80'), ('https://google.ca:80',
                         None))
        self.assertEqual(x('unreal.blargg'), ('unreal.blargg',
                         'invalid url!'))

        # prepending disabled

        x = IS_URL(prepend_scheme=None)
        self.assertEqual(x('http://google.ca'), ('http://google.ca',
                         None))
        self.assertEqual(x('google.ca'), ('google.ca', None))
        self.assertEqual(x('google.ca:80'), ('google.ca:80', None))
        self.assertEqual(x('unreal.blargg'), ('unreal.blargg',
                         'invalid url!'))

        # custom allowed_schemes

        x = IS_URL(mode='http', allowed_schemes=[None, 'http'])
        self.assertEqual(x('http://google.ca'), ('http://google.ca',
                         None))
        self.assertEqual(x('https://google.ca'), ('https://google.ca',
                         'invalid url!'))
        self.assertEqual(x('google.ca'), ('http://google.ca', None))
        self.assertEqual(x('google.ca:80'), ('http://google.ca:80',
                         None))
        self.assertEqual(x('unreal.blargg'), ('unreal.blargg',
                         'invalid url!'))

        # custom allowed_schemes, excluding None

        x = IS_URL(allowed_schemes=['http'])
        self.assertEqual(x('http://google.ca'), ('http://google.ca',
                         None))
        self.assertEqual(x('https://google.ca'), ('https://google.ca',
                         'invalid url!'))
        self.assertEqual(x('google.ca'), ('google.ca', 'invalid url!'))
        self.assertEqual(x('google.ca:80'), ('google.ca:80',
                         'invalid url!'))
        self.assertEqual(x('unreal.blargg'), ('unreal.blargg',
                         'invalid url!'))

        # custom allowed_schemes and prepend_scheme

        x = IS_URL(allowed_schemes=[None, 'https'],
                   prepend_scheme='https')
        self.assertEqual(x('http://google.ca'), ('http://google.ca',
                         'invalid url!'))
        self.assertEqual(x('https://google.ca'), ('https://google.ca',
                         None))
        self.assertEqual(x('google.ca'), ('https://google.ca', None))
        self.assertEqual(x('google.ca:80'), ('https://google.ca:80',
                         None))
        self.assertEqual(x('unreal.blargg'), ('unreal.blargg',
                         'invalid url!'))

        # Now any URL requiring prepending will fail, but prepending is still
        # enabled!

        x = IS_URL(allowed_schemes=['http'])
        self.assertEqual(x('google.ca'), ('google.ca', 'invalid url!'))

    def testModeGeneric(self):

        # 'generic' mode

        x = IS_URL(mode='generic')
        self.assertEqual(x('http://google.ca'), ('http://google.ca',None))
        self.assertEqual(x('google.ca'), ('google.ca', None))
        self.assertEqual(x('google.ca:80'), ('http://google.ca:80',None))
        self.assertEqual(x('blargg://unreal'), ('blargg://unreal',
                         'invalid url!'))

        # 'generic' mode with custom allowed_schemes that still includes
        # 'http' (the default for prepend_scheme)

        x = IS_URL(mode='generic', allowed_schemes=['http', 'blargg'])
        self.assertEqual(x('http://google.ca'), ('http://google.ca',
                         None))
        self.assertEqual(x('ftp://google.ca'), ('ftp://google.ca',
                         'invalid url!'))
        self.assertEqual(x('google.ca'), ('google.ca', 'invalid url!'))
        self.assertEqual(x('google.ca:80'), ('google.ca:80',
                         'invalid url!'))
        self.assertEqual(x('blargg://unreal'), ('blargg://unreal',
                         None))

        # 'generic' mode with overriden prepend_scheme

        x = IS_URL(mode='generic', prepend_scheme='ftp')
        self.assertEqual(x('http://google.ca'), ('http://google.ca',
                         None))
        self.assertEqual(x('ftp://google.ca'), ('ftp://google.ca',
                         None))
        self.assertEqual(x('google.ca'), ('google.ca', None))
        self.assertEqual(x('google.ca:80'), ('ftp://google.ca:80',
                         None))
        self.assertEqual(x('blargg://unreal'), ('blargg://unreal',
                         'invalid url!'))

        # 'generic' mode with overriden allowed_schemes and prepend_scheme

        x = IS_URL(mode='generic', allowed_schemes=[None, 'ftp', 'ftps'
                   ], prepend_scheme='ftp')
        self.assertEqual(x('http://google.ca'), ('http://google.ca',
                         'invalid url!'))
        self.assertEqual(x('google.ca'), ('google.ca', None))
        self.assertEqual(x('ftp://google.ca'), ('ftp://google.ca',
                         None))
        self.assertEqual(x('google.ca:80'), ('ftp://google.ca:80',
                         None))
        self.assertEqual(x('blargg://unreal'), ('blargg://unreal',
                         'invalid url!'))

        # Now any URL requiring prepending will fail, but prepending is still
        # enabled!

        x = IS_URL(mode='generic', allowed_schemes=['http'])
        self.assertEqual(x('google.ca'), ('google.ca', 'invalid url!'))

    def testExceptionalUse(self):

        # mode must be in set ['http', 'generic']

        try:
            x = IS_URL(mode='ftp')
            x('http://www.google.ca')
        except Exception, e:
            if str(e) != "invalid mode 'ftp' in IS_URL":
                self.fail('Wrong exception: ' + str(e))
        else:
            self.fail("Accepted invalid mode: 'ftp'")

        # allowed_schemes in 'http' mode must be in set [None, 'http', 'https']

        try:
            x = IS_URL(allowed_schemes=[None, 'ftp', 'ftps'],
                       prepend_scheme='ftp')
            x('http://www.benn.ca')  # we can only reasonably know about the
                                     # error at calling time
        except Exception, e:
            if str(e)\
                 != "allowed_scheme value 'ftp' is not in [None, 'http', 'https']":
                self.fail('Wrong exception: ' + str(e))
        else:
            self.fail("Accepted invalid allowed_schemes: [None, 'ftp', 'ftps']")

        # prepend_scheme's value must be in allowed_schemes (default for 'http'
        # mode is [None, 'http', 'https'])

        try:
            x = IS_URL(prepend_scheme='ftp')
            x('http://www.benn.ca')  # we can only reasonably know about the
                                     # error at calling time
        except Exception, e:
            if str(e)\
                 != "prepend_scheme='ftp' is not in allowed_schemes=[None, 'http', 'https']":
                self.fail('Wrong exception: ' + str(e))
        else:
            self.fail("Accepted invalid prepend_scheme: 'ftp'")

        # custom allowed_schemes that excludes 'http', so prepend_scheme must be
        # specified!

        try:
            x = IS_URL(allowed_schemes=[None, 'https'])
        except Exception, e:
            if str(e)\
                 != "prepend_scheme='http' is not in allowed_schemes=[None, 'https']":
                self.fail('Wrong exception: ' + str(e))
        else:
            self.fail("Accepted invalid prepend_scheme: 'http'")

        # prepend_scheme must be in allowed_schemes

        try:
            x = IS_URL(allowed_schemes=[None, 'http'],
                       prepend_scheme='https')
        except Exception, e:
            if str(e)\
                 != "prepend_scheme='https' is not in allowed_schemes=[None, 'http']":
                self.fail('Wrong exception: ' + str(e))
        else:
            self.fail("Accepted invalid prepend_scheme: 'https'")

        # prepend_scheme's value (default is 'http') must be in allowed_schemes

        try:
            x = IS_URL(mode='generic', allowed_schemes=[None, 'ftp',
                       'ftps'])
        except Exception, e:
            if str(e)\
                 != "prepend_scheme='http' is not in allowed_schemes=[None, 'ftp', 'ftps']":
                self.fail('Wrong exception: ' + str(e))
        else:
            self.fail("Accepted invalid prepend_scheme: 'http'")

        # prepend_scheme's value must be in allowed_schemes, which by default
        # is all schemes that really exist

        try:
            x = IS_URL(mode='generic', prepend_scheme='blargg')
            x('http://www.google.ca')  # we can only reasonably know about the error at calling time
        except Exception, e:
            if not str(e).startswith(
                    "prepend_scheme='blargg' is not in allowed_schemes="):
                self.fail('Wrong exception: ' + str(e))
        else:
            self.fail("Accepted invalid prepend_scheme: 'blargg'")

        # prepend_scheme's value must be in allowed_schemes

        try:
            x = IS_URL(mode='generic', allowed_schemes=[None, 'http'],
                       prepend_scheme='blargg')
        except Exception, e:
            if str(e)\
                 != "prepend_scheme='blargg' is not in allowed_schemes=[None, 'http']":
                self.fail('Wrong exception: ' + str(e))
        else:
            self.fail("Accepted invalid prepend_scheme: 'blargg'")

        # Not inluding None in the allowed_schemes essentially disabled
        # prepending, so even though
        # prepend_scheme has the invalid value 'http', we don't care!

        x = IS_URL(allowed_schemes=['https'], prepend_scheme='https')
        self.assertEqual(x('google.ca'), ('google.ca', 'invalid url!'))

        # Not inluding None in the allowed_schemes essentially disabled prepending, so even though
        # prepend_scheme has the invalid value 'http', we don't care!

        x = IS_URL(mode='generic', allowed_schemes=['https'],
            prepend_scheme='https')
        self.assertEqual(x('google.ca'), ('google.ca', 'invalid url!'))


# ##############################################################################


class TestIsGenericUrl(unittest.TestCase):

    x = IS_GENERIC_URL()

    def testInvalidUrls(self):
        urlsToCheckA = []
        for i in range(0, 32) + [127]:

            # Control characters are disallowed in any part of a URL

            urlsToCheckA.append('http://www.benn' + chr(i) + '.ca')

        urlsToCheckB = [
            None,
            '',
            'http://www.no spaces allowed.com',
            'http://www.benn.ca/no spaces allowed/',
            'http://www.benn.ca/angle_<bracket/',
            'http://www.benn.ca/angle_>bracket/',
            'http://www.benn.ca/invalid%character',
            'http://www.benn.ca/illegal%%20use',
            'http://www.benn.ca/illegaluse%',
            'http://www.benn.ca/illegaluse%0',
            'http://www.benn.ca/illegaluse%x',
            'http://www.benn.ca/ill%egaluse%x',
            'http://www.benn.ca/double"quote/',
            'http://www.curly{brace.com',
            'http://www.benn.ca/curly}brace/',
            'http://www.benn.ca/or|symbol/',
            'http://www.benn.ca/back\slash',
            'http://www.benn.ca/the^carat',
            'http://left[bracket.me',
            'http://www.benn.ca/right]bracket',
            'http://www.benn.ca/angle`quote',
            '-ttp://www.benn.ca',
            '+ttp://www.benn.ca',
            '.ttp://www.benn.ca',
            '9ttp://www.benn.ca',
            'ht;tp://www.benn.ca',
            'ht@tp://www.benn.ca',
            'ht&tp://www.benn.ca',
            'ht=tp://www.benn.ca',
            'ht$tp://www.benn.ca',
            'ht,tp://www.benn.ca',
            'ht:tp://www.benn.ca',
            'htp://invalid_scheme.com',
            ]

        failures = []

        for url in urlsToCheckA + urlsToCheckB:
            if self.x(url)[1] == None:
                failures.append('Incorrectly accepted: ' + str(url))

        if len(failures) > 0:
            self.fail(failures)

    def testValidUrls(self):
        urlsToCheck = [
            'ftp://ftp.is.co.za/rfc/rfc1808.txt',
            'gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles',
            'http://www.math.uio.no/faq/compression-faq/part1.html',
            'mailto:mduerst@ifi.unizh.ch',
            'news:comp.infosystems.www.servers.unix',
            'telnet://melvyl.ucop.edu/',
            'hTTp://www.benn.ca',
            '%66%74%70://ftp.is.co.za/rfc/rfc1808.txt',
            '%46%74%70://ftp.is.co.za/rfc/rfc1808.txt',
            '/faq/compression-faq/part1.html',
            'google.com',
            'www.google.com:8080',
            '128.127.123.250:8080',
            'blargg:ping',
            'http://www.benn.ca',
            'http://benn.ca',
            'http://amazon.com/books/',
            'https://amazon.com/movies',
            'rtsp://idontknowthisprotocol',
            'HTTP://allcaps.com',
            'http://localhost',
            'http://localhost#fragment',
            'http://localhost/hello',
            'http://localhost/hello?query=True',
            'http://localhost/hello/',
            'http://localhost:8080',
            'http://localhost:8080/',
            'http://localhost:8080/hello',
            'http://localhost:8080/hello/',
            'file:///C:/Documents%20and%20Settings/Jonathan/Desktop/view.py'
                ,
            ]

        failures = []

        for url in urlsToCheck:
            if self.x(url)[1] != None:
                failures.append('Incorrectly rejected: ' + str(url))

        if len(failures) > 0:
            self.fail(failures)

    def testPrepending(self):
        # Does not prepend scheme for abbreviated domains
        self.assertEqual(self.x('google.ca'), ('google.ca', None))

        # Does not prepend scheme for abbreviated domains
        self.assertEqual(self.x('google.ca:8080'), ('google.ca:8080', None))

        # Does not prepend when scheme already exists
        self.assertEqual(self.x('https://google.ca'),
                         ('https://google.ca', None))

        # Does not prepend if None type is not specified in allowed_scheme,
        # because a scheme is required

        y = IS_GENERIC_URL(allowed_schemes=['http', 'blargg'],
            prepend_scheme='http')
        self.assertEqual(y('google.ca'), ('google.ca', 'invalid url!'))


# ##############################################################################


class TestIsHttpUrl(unittest.TestCase):

    x = IS_HTTP_URL()

    def testInvalidUrls(self):
        urlsToCheck = [
            None,
            '',
            'http://invalid' + chr(2) + '.com',
            'htp://invalid_scheme.com',
            'blargg://invalid_scheme.com',
            'http://-123.com',
            'http://abcd-.ca',
            'http://-abc123-.me',
            'http://www.dom&ain.com/',
            'http://www.dom=ain.com/',
            'http://www.benn.ca&',
            'http://%62%65%6E%6E%2E%63%61/path',
            'http://.domain.com',
            'http://.domain.com./path',
            'http://domain..com',
            'http://domain...at..com',
            'http://domain.com..',
            'http://domain.com../path',
            'http://domain.3m',
            'http://domain.-3m',
            'http://domain.3m-',
            'http://domain.-3m-',
            'http://domain.co&m',
            'http://domain.m3456',
            'http://domain.m-3/path#fragment',
            'http://domain.m---k/path?query=value',
            'http://23.32..',
            'http://23..32.56.0',
            'http://38997.222.999',
            'http://23.32.56.99.',
            'http://.23.32.56.99',
            'http://.23.32.56.99.',
            'http://w127.123.0.256:8080',
            'http://23.32.56.99:abcd',
            'http://23.32.56.99:23cd',
            'http://google.com:cd22',
            'http://23.32:1300.56.99',
            'http://www.yahoo:1600.com',
            'path/segment/without/starting/slash',
            'http://www.math.uio.no;param=3',
            '://ABC.com:/%7esmith/home.html',
            ]

        failures = []

        for url in urlsToCheck:
            if self.x(url)[1] == None:
                failures.append('Incorrectly accepted: ' + str(url))

        if len(failures) > 0:
            self.fail(failures)

    def testValidUrls(self):

        urlsToCheck = [
            'http://abc.com:80/~smith/home.html',
            'http://ABC.com/%7Esmith/home.html',
            'http://ABC.com:/%7esmith/home.html',
            'http://www.math.uio.no/faq/compression-faq/part1.html',
            '//google.ca/faq/compression-faq/part1.html',
            '//google.ca/faq;param=3',
            '//google.ca/faq/index.html?query=5',
            '//google.ca/faq/index.html;param=value?query=5',
            '/faq/compression-faq/part1.html',
            '/faq;param=3',
            '/faq/index.html?query=5',
            '/faq/index.html;param=value?query=5',
            'google.com',
            'benn.ca/init/default',
            'benn.ca/init;param=value/default?query=value',
            'http://host-name---with-dashes.me',
            'http://www.host-name---with-dashes.me',
            'http://a.com',
            'http://a.3.com',
            'http://a.bl-ck.com',
            'http://bl-e.b.com',
            'http://host123with456numbers.ca',
            'http://1234567890.com.',
            'http://1234567890.com./path',
            'http://google.com./path',
            'http://domain.xn--0zwm56d',
            'http://127.123.0.256',
            'http://127.123.0.256/document/drawer',
            '127.123.0.256/document/',
            '156.212.123.100',
            'http://www.google.com:180200',
            'http://www.google.com:8080/path',
            'http://www.google.com:8080',
            '//www.google.com:8080',
            'www.google.com:8080',
            'http://127.123.0.256:8080/path',
            '//127.123.0.256:8080',
            '127.123.0.256:8080',
            'http://example.me??query=value?',
            'http://a.com',
            'http://3.com',
            'http://www.benn.ca',
            'http://benn.ca',
            'http://amazon.com/books/',
            'https://amazon.com/movies',
            'hTTp://allcaps.com',
            'http://localhost',
            'HTTPS://localhost.',
            'http://localhost#fragment',
            'http://localhost/hello;param=value',
            'http://localhost/hello;param=value/hi;param2=value2;param3=value3'
                ,
            'http://localhost/hello?query=True',
            'http://www.benn.ca/hello;param=value/hi;param2=value2;param3=value3/index.html?query=3',
            'http://localhost/hello/?query=1500&five=6',
            'http://localhost:8080',
            'http://localhost:8080/',
            'http://localhost:8080/hello',
            'http://localhost:8080/hello%20world/',
            'http://www.a.3.be-nn.5.ca',
            'http://www.amazon.COM',
            ]

        failures = []

        for url in urlsToCheck:
            if self.x(url)[1] != None:
                failures.append('Incorrectly rejected: ' + str(url))

        if len(failures) > 0:
            self.fail(failures)

    def testPrepending(self):
        # prepends scheme for abbreviated domains
        self.assertEqual(self.x('google.ca'), ('http://google.ca', None))

        # prepends scheme for abbreviated domains
        self.assertEqual(self.x('google.ca:8080'),
                         ('http://google.ca:8080', None))

        # does not prepend when scheme already exists
        self.assertEqual(self.x('https://google.ca'),
                         ('https://google.ca', None))

        y = IS_HTTP_URL(prepend_scheme='https', allowed_schemes=[None, 'https'])
        self.assertEqual(y('google.ca'), ('https://google.ca', None))  # prepends https if asked

        z = IS_HTTP_URL(prepend_scheme=None)
        self.assertEqual(z('google.ca:8080'), ('google.ca:8080', None))  # prepending disabled

        try:
            IS_HTTP_URL(prepend_scheme='mailto')
        except Exception, e:
            if str(e)\
                 != "prepend_scheme='mailto' is not in allowed_schemes=[None, 'http', 'https']":
                self.fail('Wrong exception: ' + str(e))
        else:
            self.fail("Got invalid prepend_scheme: 'mailto'")

        # Does not prepend if None type is not specified in allowed_scheme, because a scheme is required

        a = IS_HTTP_URL(allowed_schemes=['http'])
        self.assertEqual(a('google.ca'), ('google.ca', 'invalid url!'))
        self.assertEqual(a('google.ca:80'), ('google.ca:80',
                         'invalid url!'))

class TestUnicode(unittest.TestCase):
    x = IS_URL()
    y = IS_URL(allowed_schemes=['https'], prepend_scheme='https') #excludes the option for abbreviated URLs with no scheme
    z = IS_URL(prepend_scheme=None) # disables prepending the scheme in the return value


    def testUnicodeToAsciiUrl(self):
        self.assertEquals(unicode_to_ascii_authority(u'www.Alliancefran\xe7aise.nu'), 'www.xn--alliancefranaise-npb.nu')
        self.assertEquals(unicode_to_ascii_authority(u'www.benn.ca'), 'www.benn.ca')
        self.assertRaises(UnicodeError, unicode_to_ascii_authority, u'\u4e2d'*1000) #label is too long


    def testValidUrls(self):
        self.assertEquals(self.x(u'www.Alliancefrancaise.nu'), ('http://www.Alliancefrancaise.nu', None))
        self.assertEquals(self.x(u'www.Alliancefran\xe7aise.nu'), ('http://www.xn--alliancefranaise-npb.nu', None))
        self.assertEquals(self.x(u'www.Alliancefran\xe7aise.nu:8080'), ('http://www.xn--alliancefranaise-npb.nu:8080', None))
        self.assertEquals(self.x(u'http://www.Alliancefran\xe7aise.nu'), ('http://www.xn--alliancefranaise-npb.nu', None))
        self.assertEquals(self.x(u'http://www.Alliancefran\xe7aise.nu/parnaise/blue'), ('http://www.xn--alliancefranaise-npb.nu/parnaise/blue', None))
        self.assertEquals(self.x(u'http://www.Alliancefran\xe7aise.nu/parnaise/blue#fragment'), ('http://www.xn--alliancefranaise-npb.nu/parnaise/blue#fragment', None))
        self.assertEquals(self.x(u'http://www.Alliancefran\xe7aise.nu/parnaise/blue?query=value#fragment'), ('http://www.xn--alliancefranaise-npb.nu/parnaise/blue?query=value#fragment', None))
        self.assertEquals(self.x(u'http://www.Alliancefran\xe7aise.nu:8080/parnaise/blue?query=value#fragment'), ('http://www.xn--alliancefranaise-npb.nu:8080/parnaise/blue?query=value#fragment', None))
        self.assertEquals(self.x(u'www.Alliancefran\xe7aise.nu/parnaise/blue?query=value#fragment'), ('http://www.xn--alliancefranaise-npb.nu/parnaise/blue?query=value#fragment', None))
        self.assertEquals(self.x(u'http://\u4e2d\u4fd4.com'), ('http://xn--fiq13b.com', None))
        self.assertEquals(self.x(u'http://\u4e2d\u4fd4.com/\u4e86'), ('http://xn--fiq13b.com/%4e%86', None))
        self.assertEquals(self.x(u'http://\u4e2d\u4fd4.com/\u4e86?query=\u4e86'), ('http://xn--fiq13b.com/%4e%86?query=%4e%86', None))
        self.assertEquals(self.x(u'http://\u4e2d\u4fd4.com/\u4e86?query=\u4e86#fragment'), ('http://xn--fiq13b.com/%4e%86?query=%4e%86#fragment', None))
        self.assertEquals(self.x(u'http://\u4e2d\u4fd4.com?query=\u4e86#fragment'), ('http://xn--fiq13b.com?query=%4e%86#fragment', None))
        self.assertEquals(self.x(u'http://B\xfccher.ch'), ('http://xn--bcher-kva.ch', None))
        self.assertEquals(self.x(u'http://\xe4\xf6\xfc\xdf.com'), ('http://xn--ss-uia6e4a.com', None))
        self.assertEquals(self.x(u'http://visegr\xe1d.com'), ('http://xn--visegrd-mwa.com', None))
        self.assertEquals(self.x(u'http://h\xe1zipatika.com'), ('http://xn--hzipatika-01a.com', None))
        self.assertEquals(self.x(u'http://www.\xe7ukurova.com'), ('http://www.xn--ukurova-txa.com', None))
        self.assertEquals(self.x(u'http://nixier\xf6hre.nixieclock-tube.com'), ('http://xn--nixierhre-57a.nixieclock-tube.com', None))
        self.assertEquals(self.x(u'google.ca.'), ('http://google.ca.', None))

        self.assertEquals(self.y(u'https://google.ca'), ('https://google.ca', None))
        self.assertEquals(self.y(u'https://\u4e2d\u4fd4.com'), ('https://xn--fiq13b.com', None))

        self.assertEquals(self.z(u'google.ca'), ('google.ca', None))


    def testInvalidUrls(self):
        self.assertEquals(self.x(u'://ABC.com'), (u'://ABC.com', 'invalid url!'))
        self.assertEquals(self.x(u'http://\u4e2d\u4fd4.dne'), (u'http://\u4e2d\u4fd4.dne', 'invalid url!'))
        self.assertEquals(self.x(u'https://google.dne'), (u'https://google.dne', 'invalid url!'))
        self.assertEquals(self.x(u'https://google..ca'), (u'https://google..ca', 'invalid url!'))
        self.assertEquals(self.x(u'google..ca'), (u'google..ca', 'invalid url!'))
        self.assertEquals(self.x(u'http://' + u'\u4e2d'*1000 + u'.com'), (u'http://' + u'\u4e2d'*1000 + u'.com', 'invalid url!'))

        self.assertEquals(self.x(u'http://google.com#fragment_\u4e86'), (u'http://google.com#fragment_\u4e86', 'invalid url!'))
        self.assertEquals(self.x(u'http\u4e86://google.com'), (u'http\u4e86://google.com', 'invalid url!'))
        self.assertEquals(self.x(u'http\u4e86://google.com#fragment_\u4e86'), (u'http\u4e86://google.com#fragment_\u4e86', 'invalid url!'))

        self.assertEquals(self.y(u'http://\u4e2d\u4fd4.com/\u4e86'), (u'http://\u4e2d\u4fd4.com/\u4e86', 'invalid url!'))
        #self.assertEquals(self.y(u'google.ca'), (u'google.ca', 'invalid url!'))

        self.assertEquals(self.z(u'invalid.domain..com'), (u'invalid.domain..com', 'invalid url!'))
        self.assertEquals(self.z(u'invalid.\u4e2d\u4fd4.blargg'), (u'invalid.\u4e2d\u4fd4.blargg', 'invalid url!'))

# ##############################################################################

if __name__ == '__main__':
    unittest.main()