File: test_utils_url.py

package info (click to toggle)
python-scrapy 2.14.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,316 kB
  • sloc: python: 55,421; xml: 199; makefile: 25; sh: 7
file content (468 lines) | stat: -rw-r--r-- 18,112 bytes parent folder | download
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
import warnings
from importlib import import_module

import pytest

from scrapy.linkextractors import IGNORED_EXTENSIONS
from scrapy.spiders import Spider
from scrapy.utils.url import (  # type: ignore[attr-defined]
    _is_filesystem_path,
    _public_w3lib_objects,
    add_http_if_no_scheme,
    guess_scheme,
    strip_url,
    url_has_any_extension,
    url_is_from_any_domain,
    url_is_from_spider,
)


def test_url_is_from_any_domain():
    url = "http://www.wheele-bin-art.co.uk/get/product/123"
    assert url_is_from_any_domain(url, ["wheele-bin-art.co.uk"])
    assert not url_is_from_any_domain(url, ["art.co.uk"])

    url = "http://wheele-bin-art.co.uk/get/product/123"
    assert url_is_from_any_domain(url, ["wheele-bin-art.co.uk"])
    assert not url_is_from_any_domain(url, ["art.co.uk"])

    url = "http://www.Wheele-Bin-Art.co.uk/get/product/123"
    assert url_is_from_any_domain(url, ["wheele-bin-art.CO.UK"])
    assert url_is_from_any_domain(url, ["WHEELE-BIN-ART.CO.UK"])

    url = "http://192.169.0.15:8080/mypage.html"
    assert url_is_from_any_domain(url, ["192.169.0.15:8080"])
    assert not url_is_from_any_domain(url, ["192.169.0.15"])

    url = (
        "javascript:%20document.orderform_2581_1190810811.mode.value=%27add%27;%20"
        "javascript:%20document.orderform_2581_1190810811.submit%28%29"
    )
    assert not url_is_from_any_domain(url, ["testdomain.com"])
    assert not url_is_from_any_domain(url + ".testdomain.com", ["testdomain.com"])


def test_url_is_from_spider():
    class MySpider(Spider):
        name = "example.com"

    assert url_is_from_spider("http://www.example.com/some/page.html", MySpider)
    assert url_is_from_spider("http://sub.example.com/some/page.html", MySpider)
    assert not url_is_from_spider("http://www.example.org/some/page.html", MySpider)
    assert not url_is_from_spider("http://www.example.net/some/page.html", MySpider)


def test_url_is_from_spider_class_attributes():
    class MySpider(Spider):
        name = "example.com"

    assert url_is_from_spider("http://www.example.com/some/page.html", MySpider)
    assert url_is_from_spider("http://sub.example.com/some/page.html", MySpider)
    assert not url_is_from_spider("http://www.example.org/some/page.html", MySpider)
    assert not url_is_from_spider("http://www.example.net/some/page.html", MySpider)


def test_url_is_from_spider_with_allowed_domains():
    class MySpider(Spider):
        name = "example.com"
        allowed_domains = ["example.org", "example.net"]

    assert url_is_from_spider("http://www.example.com/some/page.html", MySpider)
    assert url_is_from_spider("http://sub.example.com/some/page.html", MySpider)
    assert url_is_from_spider("http://example.com/some/page.html", MySpider)
    assert url_is_from_spider("http://www.example.org/some/page.html", MySpider)
    assert url_is_from_spider("http://www.example.net/some/page.html", MySpider)
    assert not url_is_from_spider("http://www.example.us/some/page.html", MySpider)

    class MySpider2(Spider):
        name = "example.com"
        allowed_domains = {"example.com", "example.net"}

    assert url_is_from_spider("http://www.example.com/some/page.html", MySpider2)

    class MySpider3(Spider):
        name = "example.com"
        allowed_domains = ("example.com", "example.net")

    assert url_is_from_spider("http://www.example.com/some/page.html", MySpider3)


@pytest.mark.parametrize(
    ("url", "expected"),
    [
        ("http://www.example.com/archive.tar.gz", True),
        ("http://www.example.com/page.doc", True),
        ("http://www.example.com/page.pdf", True),
        ("http://www.example.com/page.htm", False),
        ("http://www.example.com/", False),
        ("http://www.example.com/page.doc.html", False),
    ],
)
def test_url_has_any_extension(url: str, expected: bool) -> None:
    deny_extensions = {"." + e for e in IGNORED_EXTENSIONS}
    assert url_has_any_extension(url, deny_extensions) is expected


@pytest.mark.parametrize(
    ("url", "expected"),
    [
        ("www.example.com", "http://www.example.com"),
        ("example.com", "http://example.com"),
        ("www.example.com/some/page.html", "http://www.example.com/some/page.html"),
        ("www.example.com:80", "http://www.example.com:80"),
        ("www.example.com/some/page#frag", "http://www.example.com/some/page#frag"),
        ("www.example.com/do?a=1&b=2&c=3", "http://www.example.com/do?a=1&b=2&c=3"),
        (
            "username:password@www.example.com",
            "http://username:password@www.example.com",
        ),
        (
            "username:password@www.example.com:80/some/page/do?a=1&b=2&c=3#frag",
            "http://username:password@www.example.com:80/some/page/do?a=1&b=2&c=3#frag",
        ),
        ("http://www.example.com", "http://www.example.com"),
        ("http://example.com", "http://example.com"),
        (
            "http://www.example.com/some/page.html",
            "http://www.example.com/some/page.html",
        ),
        ("http://www.example.com:80", "http://www.example.com:80"),
        (
            "http://www.example.com/some/page#frag",
            "http://www.example.com/some/page#frag",
        ),
        (
            "http://www.example.com/do?a=1&b=2&c=3",
            "http://www.example.com/do?a=1&b=2&c=3",
        ),
        (
            "http://username:password@www.example.com",
            "http://username:password@www.example.com",
        ),
        (
            "http://username:password@www.example.com:80/some/page/do?a=1&b=2&c=3#frag",
            "http://username:password@www.example.com:80/some/page/do?a=1&b=2&c=3#frag",
        ),
        ("//www.example.com", "http://www.example.com"),
        ("//example.com", "http://example.com"),
        ("//www.example.com/some/page.html", "http://www.example.com/some/page.html"),
        ("//www.example.com:80", "http://www.example.com:80"),
        ("//www.example.com/some/page#frag", "http://www.example.com/some/page#frag"),
        ("//www.example.com/do?a=1&b=2&c=3", "http://www.example.com/do?a=1&b=2&c=3"),
        (
            "//username:password@www.example.com",
            "http://username:password@www.example.com",
        ),
        (
            "//username:password@www.example.com:80/some/page/do?a=1&b=2&c=3#frag",
            "http://username:password@www.example.com:80/some/page/do?a=1&b=2&c=3#frag",
        ),
        ("https://www.example.com", "https://www.example.com"),
        ("ftp://www.example.com", "ftp://www.example.com"),
    ],
)
def test_add_http_if_no_scheme(url: str, expected: str) -> None:
    assert add_http_if_no_scheme(url) == expected


@pytest.mark.parametrize(
    ("url", "expected"),
    [
        ("/index", "file://"),
        ("/index.html", "file://"),
        ("./index.html", "file://"),
        ("../index.html", "file://"),
        ("../../index.html", "file://"),
        ("./data/index.html", "file://"),
        (".hidden/data/index.html", "file://"),
        ("/home/user/www/index.html", "file://"),
        ("//home/user/www/index.html", "file://"),
        ("file:///home/user/www/index.html", "file://"),
        ("index.html", "http://"),
        ("example.com", "http://"),
        ("www.example.com", "http://"),
        ("www.example.com/index.html", "http://"),
        ("http://example.com", "http://"),
        ("http://example.com/index.html", "http://"),
        ("localhost", "http://"),
        ("localhost/index.html", "http://"),
        # some corner cases (default to http://)
        ("/", "http://"),
        (".../test", "http://"),
    ],
)
def test_guess_scheme(url: str, expected: str):
    assert guess_scheme(url).startswith(expected)


@pytest.mark.parametrize(
    ("url", "expected", "reason"),
    [
        (
            r"C:\absolute\path\to\a\file.html",
            "file://",
            "Windows filepath are not supported for scrapy shell",
        ),
    ],
)
def test_guess_scheme_skipped(url: str, expected: str, reason: str):
    pytest.skip(reason)


class TestStripUrl:
    @pytest.mark.parametrize(
        "url",
        [
            "http://www.example.com/index.html",
            "http://www.example.com/index.html?somekey=somevalue",
        ],
    )
    def test_noop(self, url: str) -> None:
        assert strip_url(url) == url

    def test_fragments(self):
        assert (
            strip_url(
                "http://www.example.com/index.html?somekey=somevalue#section",
                strip_fragment=False,
            )
            == "http://www.example.com/index.html?somekey=somevalue#section"
        )

    @pytest.mark.parametrize(
        ("url", "origin", "expected"),
        [
            ("http://www.example.com/", False, "http://www.example.com/"),
            ("http://www.example.com", False, "http://www.example.com"),
            ("http://www.example.com", True, "http://www.example.com/"),
        ],
    )
    def test_path(self, url: str, origin: bool, expected: str) -> None:
        assert strip_url(url, origin_only=origin) == expected

    @pytest.mark.parametrize(
        ("url", "expected"),
        [
            (
                "http://username@www.example.com/index.html?somekey=somevalue#section",
                "http://www.example.com/index.html?somekey=somevalue",
            ),
            (
                "https://username:@www.example.com/index.html?somekey=somevalue#section",
                "https://www.example.com/index.html?somekey=somevalue",
            ),
            (
                "ftp://username:password@www.example.com/index.html?somekey=somevalue#section",
                "ftp://www.example.com/index.html?somekey=somevalue",
            ),
            # user: "username@", password: none
            (
                "http://username%40@www.example.com/index.html?somekey=somevalue#section",
                "http://www.example.com/index.html?somekey=somevalue",
            ),
            # user: "username:pass", password: ""
            (
                "https://username%3Apass:@www.example.com/index.html?somekey=somevalue#section",
                "https://www.example.com/index.html?somekey=somevalue",
            ),
            # user: "me", password: "user@domain.com"
            (
                "ftp://me:user%40domain.com@www.example.com/index.html?somekey=somevalue#section",
                "ftp://www.example.com/index.html?somekey=somevalue",
            ),
        ],
    )
    def test_credentials(self, url: str, expected: str) -> None:
        assert strip_url(url, strip_credentials=True) == expected

    @pytest.mark.parametrize(
        ("url", "expected"),
        [
            (
                "http://username:password@www.example.com:80/index.html?somekey=somevalue#section",
                "http://www.example.com/index.html?somekey=somevalue",
            ),
            (
                "http://username:password@www.example.com:8080/index.html#section",
                "http://www.example.com:8080/index.html",
            ),
            (
                "http://username:password@www.example.com:443/index.html?somekey=somevalue&someotherkey=sov#section",
                "http://www.example.com:443/index.html?somekey=somevalue&someotherkey=sov",
            ),
            (
                "https://username:password@www.example.com:443/index.html",
                "https://www.example.com/index.html",
            ),
            (
                "https://username:password@www.example.com:442/index.html",
                "https://www.example.com:442/index.html",
            ),
            (
                "https://username:password@www.example.com:80/index.html",
                "https://www.example.com:80/index.html",
            ),
            (
                "ftp://username:password@www.example.com:21/file.txt",
                "ftp://www.example.com/file.txt",
            ),
            (
                "ftp://username:password@www.example.com:221/file.txt",
                "ftp://www.example.com:221/file.txt",
            ),
        ],
    )
    def test_default_ports_creds_off(self, url: str, expected: str) -> None:
        assert strip_url(url) == expected

    @pytest.mark.parametrize(
        ("url", "expected"),
        [
            (
                "http://username:password@www.example.com:80/index.html",
                "http://username:password@www.example.com/index.html",
            ),
            (
                "http://username:password@www.example.com:8080/index.html",
                "http://username:password@www.example.com:8080/index.html",
            ),
            (
                "http://username:password@www.example.com:443/index.html",
                "http://username:password@www.example.com:443/index.html",
            ),
            (
                "https://username:password@www.example.com:443/index.html",
                "https://username:password@www.example.com/index.html",
            ),
            (
                "https://username:password@www.example.com:442/index.html",
                "https://username:password@www.example.com:442/index.html",
            ),
            (
                "https://username:password@www.example.com:80/index.html",
                "https://username:password@www.example.com:80/index.html",
            ),
            (
                "ftp://username:password@www.example.com:21/file.txt",
                "ftp://username:password@www.example.com/file.txt",
            ),
            (
                "ftp://username:password@www.example.com:221/file.txt",
                "ftp://username:password@www.example.com:221/file.txt",
            ),
        ],
    )
    def test_default_ports(self, url: str, expected: str) -> None:
        assert (
            strip_url(url, strip_default_port=True, strip_credentials=False) == expected
        )

    @pytest.mark.parametrize(
        ("url", "expected"),
        [
            (
                "http://username:password@www.example.com:80/index.html?somekey=somevalue&someotherkey=sov#section",
                "http://username:password@www.example.com:80/index.html?somekey=somevalue&someotherkey=sov",
            ),
            (
                "http://username:password@www.example.com:8080/index.html?somekey=somevalue&someotherkey=sov#section",
                "http://username:password@www.example.com:8080/index.html?somekey=somevalue&someotherkey=sov",
            ),
            (
                "http://username:password@www.example.com:443/index.html",
                "http://username:password@www.example.com:443/index.html",
            ),
            (
                "https://username:password@www.example.com:443/index.html",
                "https://username:password@www.example.com:443/index.html",
            ),
            (
                "https://username:password@www.example.com:442/index.html",
                "https://username:password@www.example.com:442/index.html",
            ),
            (
                "https://username:password@www.example.com:80/index.html",
                "https://username:password@www.example.com:80/index.html",
            ),
            (
                "ftp://username:password@www.example.com:21/file.txt",
                "ftp://username:password@www.example.com:21/file.txt",
            ),
            (
                "ftp://username:password@www.example.com:221/file.txt",
                "ftp://username:password@www.example.com:221/file.txt",
            ),
        ],
    )
    def test_default_ports_keep(self, url: str, expected: str) -> None:
        assert (
            strip_url(url, strip_default_port=False, strip_credentials=False)
            == expected
        )

    @pytest.mark.parametrize(
        ("url", "expected"),
        [
            (
                "http://username:password@www.example.com/index.html",
                "http://www.example.com/",
            ),
            (
                "http://username:password@www.example.com:80/foo/bar?query=value#somefrag",
                "http://www.example.com/",
            ),
            (
                "http://username:password@www.example.com:8008/foo/bar?query=value#somefrag",
                "http://www.example.com:8008/",
            ),
            (
                "https://username:password@www.example.com:443/index.html",
                "https://www.example.com/",
            ),
        ],
    )
    def test_origin_only(self, url: str, expected: str) -> None:
        assert strip_url(url, origin_only=True) == expected


@pytest.mark.parametrize(
    ("path", "expected"),
    [
        # https://en.wikipedia.org/wiki/Path_(computing)#Representations_of_paths_by_operating_system_and_shell
        # Unix-like OS, Microsoft Windows / cmd.exe
        ("/home/user/docs/Letter.txt", True),
        ("./inthisdir", True),
        ("../../greatgrandparent", True),
        ("~/.rcinfo", True),
        (r"C:\user\docs\Letter.txt", True),
        ("/user/docs/Letter.txt", True),
        (r"C:\Letter.txt", True),
        (r"\\Server01\user\docs\Letter.txt", True),
        (r"\\?\UNC\Server01\user\docs\Letter.txt", True),
        (r"\\?\C:\user\docs\Letter.txt", True),
        (r"C:\user\docs\somefile.ext:alternate_stream_name", True),
        (r"https://example.com", False),
    ],
)
def test__is_filesystem_path(path: str, expected: bool) -> None:
    assert _is_filesystem_path(path) == expected


@pytest.mark.parametrize(
    "obj_name",
    [
        "_unquotepath",
        "_safe_chars",
        "parse_url",
        *_public_w3lib_objects,
    ],
)
def test_deprecated_imports_from_w3lib(obj_name: str) -> None:
    with warnings.catch_warnings(record=True) as warns:
        obj_type = "attribute" if obj_name == "_safe_chars" else "function"
        message = f"The scrapy.utils.url.{obj_name} {obj_type} is deprecated, use w3lib.url.{obj_name} instead."

        getattr(import_module("scrapy.utils.url"), obj_name)

        assert isinstance(warns[0].message, Warning)
        assert message in warns[0].message.args