mirror of
https://github.com/D4Vinci/Scrapling.git
synced 2026-09-14 20:07:02 +08:00
5edd203076
Solves #440
24 lines
1.1 KiB
Python
24 lines
1.1 KiB
Python
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from scrapling.engines.toolbelt.custom import Response
|
|
from scrapling.spiders.cache import ResponseCacheManager
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_cache_preserves_shadow_wrappers(tmp_path: Path) -> None:
|
|
content = '<!DOCTYPE html><html><body><p id="host"><shadow-root><!--kept--><section>FIRST<div id="nested"><shadow-root><p>SECOND</p></shadow-root></div>LAST</section></shadow-root></p></body></html>'
|
|
response = Response("https://shadow.test/", content, 200, "OK", {}, {}, {})
|
|
assert response.css("#host > shadow-root > section")
|
|
cache = ResponseCacheManager(tmp_path)
|
|
await cache.put(b"shadow", response)
|
|
restored = await cache.get(b"shadow")
|
|
assert restored is not None
|
|
assert restored.body == response.body == content.encode()
|
|
assert restored.css("#host > shadow-root > section")
|
|
assert restored.css("#nested > shadow-root > p::text").get() == "SECOND"
|
|
assert restored.css("#host")[0].get_all_text(separator="|", strip=True) == "FIRST|SECOND|LAST"
|
|
assert not restored.xpath("//comment()")
|
|
assert "<!--kept-->" in restored.body.decode()
|