50 lines
2.1 KiB
Python
50 lines
2.1 KiB
Python
"""canonical 商品 URL 的离线解析测试。"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
import sys
|
|
import unittest
|
|
|
|
|
|
CLIENT_ROOT = Path(__file__).resolve().parents[2]
|
|
sys.path.insert(0, str(CLIENT_ROOT / "src"))
|
|
|
|
from cmbuyer_client.pdd.product_url import ProductUrlError, parse_product_url
|
|
|
|
|
|
class ProductUrlTests(unittest.TestCase):
|
|
def test_rebuilds_url_from_goods_id(self) -> None:
|
|
link = parse_product_url("https://mobile.yangkeduo.com/goods.html?goods_id=00123")
|
|
|
|
self.assertEqual(link.goods_id, "00123")
|
|
self.assertEqual(
|
|
link.canonical_url,
|
|
"https://mobile.yangkeduo.com/goods.html?goods_id=00123",
|
|
)
|
|
|
|
def test_rejects_noncanonical_and_ambiguous_urls(self) -> None:
|
|
rejected = (
|
|
"http://mobile.yangkeduo.com/goods.html?goods_id=123",
|
|
"https://other.example/goods.html?goods_id=123",
|
|
"https://mobile.yangkeduo.com/other.html?goods_id=123",
|
|
"https://user@mobile.yangkeduo.com/goods.html?goods_id=123",
|
|
"https://mobile.yangkeduo.com:8443/goods.html?goods_id=123",
|
|
"https://mobile.yangkeduo.com:443/goods.html?goods_id=123",
|
|
"https://mobile.yangkeduo.com/goods.html?goods_id=123#fragment",
|
|
"https://mobile.yangkeduo.com/goods.html",
|
|
"https://mobile.yangkeduo.com/goods.html?goods_id=123&goods_id=456",
|
|
"https://mobile.yangkeduo.com/goods.html?goods_id=123&source=share",
|
|
"https://mobile.yangkeduo.com/goods.html?goods_id=12a",
|
|
"https://mobile.yangkeduo.com/goods.html?goods_id=%EF%BC%91%EF%BC%92%EF%BC%93",
|
|
"https://mobile.yangkeduo.com/goods.html?goods_id=",
|
|
" https://mobile.yangkeduo.com/goods.html?goods_id=123",
|
|
"https://MOBILE.YANGKEDUO.COM/goods.html?goods_id=123",
|
|
"https://mobile.yangkeduo.com/goods.html?goods_id=%31%32%33",
|
|
)
|
|
|
|
for value in rejected:
|
|
with self.subTest(value=value):
|
|
with self.assertRaises(ProductUrlError):
|
|
parse_product_url(value)
|