|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
| 3 | +import codecs |
3 | 4 | import sys |
4 | 5 | from typing import TYPE_CHECKING, Any |
5 | 6 | from unittest import mock |
|
21 | 22 | _CZECH = 'Test dekódování znaků českého jazyka' |
22 | 23 | _UKRAINIAN = 'Тест декодування символів української мови' |
23 | 24 | _CHINESE = '中文字符解码测试' |
| 25 | +_XHTML = b'<?xml version="1.0"?><html xmlns="http://www.w3.org/1999/xhtml"><body><a href="/next">Next</a></body></html>' |
24 | 26 |
|
25 | 27 |
|
26 | 28 | async def test_basic(server_url: URL, http_client: HttpClient) -> None: |
@@ -576,21 +578,35 @@ async def test_parse_decodes_page_encoding(texts: list[str], body: bytes, conten |
576 | 578 |
|
577 | 579 |
|
578 | 580 | @pytest.mark.parametrize( |
579 | | - ('content_type', 'expected_type'), |
| 581 | + ('body', 'content_type', 'expected_type'), |
580 | 582 | [ |
581 | | - pytest.param('text/html', 'html', id='html'), |
582 | | - pytest.param('application/xhtml+xml; charset=utf-8', 'html', id='xhtml'), |
583 | | - pytest.param('application/xml', 'xml', id='xml'), |
| 583 | + pytest.param(_XHTML, 'text/html', 'html', id='xhtml-as-html'), |
| 584 | + pytest.param(_XHTML, 'application/xhtml+xml; charset=utf-8', 'html', id='xhtml-as-xhtml'), |
| 585 | + pytest.param(_XHTML, 'application/xml', 'xml', id='xhtml-as-xml'), |
| 586 | + pytest.param(b'{"hello": "world"}', 'text/html', 'json', id='json-as-html'), |
| 587 | + pytest.param(b'{"hello": "world"}', 'text/html; charset=utf-8', 'json', id='json-as-html-charset'), |
| 588 | + pytest.param(b' \n[1, 2]', 'text/html', 'json', id='json-after-whitespace'), |
| 589 | + pytest.param(b'\n{"hello": "world"}', 'text/html; charset=utf-8', 'json', id='json-after-whitespace-charset'), |
| 590 | + pytest.param(b'[<a href="/next">Next</a>]', 'text/html', 'html', id='bracket-not-json'), |
| 591 | + pytest.param(codecs.BOM_UTF8 + b'{"hello": "world"}', 'text/html', 'json', id='json-with-bom'), |
| 592 | + pytest.param(codecs.BOM_UTF8 + _XHTML, 'text/html', 'html', id='xhtml-with-bom'), |
| 593 | + pytest.param(_UKRAINIAN.encode('cp1251'), 'text/html', 'html', id='non-ascii-start'), |
584 | 594 | ], |
585 | 595 | ) |
586 | | -async def test_parse_xml_declaration(content_type: str, expected_type: str) -> None: |
587 | | - """A page starting with an XML declaration is read as HTML unless the response is XML.""" |
588 | | - body = ( |
589 | | - b'<?xml version="1.0" encoding="UTF-8"?>' |
590 | | - b'<html xmlns="http://www.w3.org/1999/xhtml"><body><a href="/next">Next</a></body></html>' |
591 | | - ) |
| 596 | +async def test_parse_selector_type(body: bytes, content_type: str, expected_type: str) -> None: |
| 597 | + """An HTML response starting with an XML declaration is read as HTML, while JSON and XML keep their type.""" |
592 | 598 | response = mock.Mock(headers=HttpHeaders({'Content-Type': content_type}), read=mock.AsyncMock(return_value=body)) |
593 | 599 |
|
594 | 600 | selector = await ParselParser().parse(response) |
595 | 601 |
|
596 | 602 | assert selector.type == expected_type |
| 603 | + |
| 604 | + |
| 605 | +async def test_parse_empty_body() -> None: |
| 606 | + """An empty body is read as an empty HTML document.""" |
| 607 | + response = mock.Mock(headers=HttpHeaders({'Content-Type': 'text/html'}), read=mock.AsyncMock(return_value=b'')) |
| 608 | + |
| 609 | + selector = await ParselParser().parse(response) |
| 610 | + |
| 611 | + assert selector.type == 'html' |
| 612 | + assert selector.css('a').getall() == [] |
0 commit comments