/
/
/
1"""Tests for image URL normalization."""
2
3from __future__ import annotations
4
5from typing import cast
6
7import pytest
8
9from music_assistant.providers.neteasecloudmusic import NeteaseCloudMusicProvider
10
11
12def _normalize(url: str) -> str:
13 # _normalize_image_url does not touch instance state
14 return NeteaseCloudMusicProvider._normalize_image_url(
15 cast("NeteaseCloudMusicProvider", None), url
16 )
17
18
19@pytest.mark.parametrize(
20 ("url", "expected"),
21 [
22 # http links on the NCM image CDN are upgraded to https
23 ("http://p1.music.126.net/abc.jpg", "https://p1.music.126.net/abc.jpg"),
24 ("http://music.126.net/abc.jpg", "https://music.126.net/abc.jpg"),
25 # the CDN hostname must be the actual host, not just a substring anywhere
26 ("http://evil.com/music.126.net/abc.jpg", "http://evil.com/music.126.net/abc.jpg"),
27 ("http://evil-music.126.net/abc.jpg", "http://evil-music.126.net/abc.jpg"),
28 ("http://music.126.net.evil.com/abc.jpg", "http://music.126.net.evil.com/abc.jpg"),
29 # https and non-NCM urls stay untouched
30 ("https://p1.music.126.net/abc.jpg", "https://p1.music.126.net/abc.jpg"),
31 ("http://example.com/abc.jpg", "http://example.com/abc.jpg"),
32 ],
33)
34def test_normalize_image_url(url: str, expected: str) -> None:
35 """Only real *.music.126.net hosts are upgraded to https."""
36 assert _normalize(url) == expected
37