|
| 1 | +#include "../uri_utils.hpp" |
| 2 | + |
| 3 | +#include <gtest/gtest.h> |
| 4 | + |
| 5 | +namespace just_audio_windows { |
| 6 | +namespace test { |
| 7 | + |
| 8 | +// ── EncodeSpacesInUri ──────────────────────────────────────────────────────── |
| 9 | + |
| 10 | +// A plain URL with no spaces should pass through unchanged. |
| 11 | +TEST(EncodeSpacesInUri, NoSpaces_ReturnsUnchanged) { |
| 12 | + EXPECT_EQ(EncodeSpacesInUri("https://example.com/audio.mp3"), |
| 13 | + "https://example.com/audio.mp3"); |
| 14 | +} |
| 15 | + |
| 16 | +// An empty string should stay empty. |
| 17 | +TEST(EncodeSpacesInUri, EmptyString_ReturnsEmpty) { |
| 18 | + EXPECT_EQ(EncodeSpacesInUri(""), ""); |
| 19 | +} |
| 20 | + |
| 21 | +// Literal spaces in a local file path must be encoded as %20. |
| 22 | +// This is the core regression for https://github.com/bdlukaa/just_audio_windows/issues/26. |
| 23 | +TEST(EncodeSpacesInUri, LiteralSpacesInFilePath_EncodedAsPercent20) { |
| 24 | + EXPECT_EQ( |
| 25 | + EncodeSpacesInUri("file:///C:/Users/My Files/song.mp3"), |
| 26 | + "file:///C:/Users/My%20Files/song.mp3"); |
| 27 | +} |
| 28 | + |
| 29 | +// Multiple consecutive spaces must each be individually encoded. |
| 30 | +TEST(EncodeSpacesInUri, MultipleSpaces_AllEncoded) { |
| 31 | + EXPECT_EQ( |
| 32 | + EncodeSpacesInUri( |
| 33 | + "C:/Users/HP/Downloads/Ve Kamleya Rocky Aur Rani.mp3"), |
| 34 | + "C:/Users/HP/Downloads/Ve%20Kamleya%20Rocky%20Aur%20Rani.mp3"); |
| 35 | +} |
| 36 | + |
| 37 | +// Already percent-encoded spaces (%20) must NOT be double-encoded. |
| 38 | +// This ensures a properly-encoded file:// URI like those produced by |
| 39 | +// Dart's Uri.file() is left intact. |
| 40 | +TEST(EncodeSpacesInUri, AlreadyEncodedSpaces_NotDoubleEncoded) { |
| 41 | + EXPECT_EQ( |
| 42 | + EncodeSpacesInUri("file:///C:/Users/My%20Files/song.mp3"), |
| 43 | + "file:///C:/Users/My%20Files/song.mp3"); |
| 44 | +} |
| 45 | + |
| 46 | +// Percent-encoded multi-byte UTF-8 sequences (e.g. U+2019 RIGHT SINGLE |
| 47 | +// QUOTATION MARK) must pass through unchanged so that Windows::Foundation::Uri |
| 48 | +// can decode them natively. |
| 49 | +// This is the core regression for the original apostrophe bug. |
| 50 | +TEST(EncodeSpacesInUri, MultiBytePercentEncoded_Unchanged) { |
| 51 | + EXPECT_EQ( |
| 52 | + EncodeSpacesInUri( |
| 53 | + "https://example.com/speech?text=I%E2%80%99d%20like%20a%20coffee"), |
| 54 | + "https://example.com/speech?text=I%E2%80%99d%20like%20a%20coffee"); |
| 55 | +} |
| 56 | + |
| 57 | +// A URL with both unencoded spaces and percent-encoded multi-byte chars: |
| 58 | +// spaces must be encoded while the percent-sequences stay intact. |
| 59 | +TEST(EncodeSpacesInUri, MixedLiteralSpacesAndPercentEncoded) { |
| 60 | + EXPECT_EQ( |
| 61 | + EncodeSpacesInUri( |
| 62 | + "https://example.com/speech?text=I%E2%80%99d like a coffee"), |
| 63 | + "https://example.com/speech?text=I%E2%80%99d%20like%20a%20coffee"); |
| 64 | +} |
| 65 | + |
| 66 | +// A URL with only query-string spaces should have them encoded. |
| 67 | +TEST(EncodeSpacesInUri, SpaceInQueryString) { |
| 68 | + EXPECT_EQ( |
| 69 | + EncodeSpacesInUri("https://example.com/tts?text=hello world"), |
| 70 | + "https://example.com/tts?text=hello%20world"); |
| 71 | +} |
| 72 | + |
| 73 | +} // namespace test |
| 74 | +} // namespace just_audio_windows |
0 commit comments