You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
3.0KB

  1. package extractor
  2. import (
  3. "reflect"
  4. "testing"
  5. )
  6. func TestExtractStreams(t *testing.T) {
  7. html := `
  8. <script>
  9. var streamsrc = 'https://example.com/live/stream.mp3';
  10. var streamhash="https://cdn.example.net/relay.m3u8";
  11. var audioUrl="https://example.com/audio.ogg";
  12. </script>
  13. <a href="https://streams.example.org/radio.aac?user=test">listen</a>
  14. <source src="//players.example.eu/ambient.ogg" type="audio/ogg" />
  15. <audio src="https://stream.example.com/live"></audio>
  16. <audio data-src="https://pod.example.com/episode.opus"></audio>
  17. <div data-value="https://example.com/secret.pls"></div>
  18. `
  19. streams := ExtractStreams(html)
  20. if len(streams) != 7 {
  21. t.Fatalf("wanted 7 streams, got %d: %v", len(streams), streams)
  22. }
  23. found := false
  24. for _, s := range streams {
  25. if s == "https://stream.example.com/live" {
  26. found = true
  27. break
  28. }
  29. }
  30. if !found {
  31. t.Fatalf("expected audio tag stream to be present: %v", streams)
  32. }
  33. }
  34. func TestExtractPlaylistLinks(t *testing.T) {
  35. html := `
  36. <a href="https://example.com/stream.m3u">m3u</a>
  37. <a href="https://example.com/playlist.pls">pls</a>
  38. <a href="https://example.com/radio.xspf">xspf</a>
  39. <a href="https://example.com/data.json">json</a>
  40. `
  41. links := ExtractPlaylistLinks(html)
  42. if len(links) != 4 {
  43. t.Fatalf("wanted 4 playlist links, got %d: %v", len(links), links)
  44. }
  45. }
  46. func TestExtractEmbedURLs(t *testing.T) {
  47. html := `<iframe src="//example.com/embed"></iframe><iframe src="https://example.org/player"></iframe>`
  48. urls := ExtractEmbedURLs(html)
  49. want := []string{"https://example.com/embed", "https://example.org/player"}
  50. if !reflect.DeepEqual(urls, want) {
  51. t.Fatalf("wanted iframe URLs %v, got %v", want, urls)
  52. }
  53. }
  54. func TestExtractScriptURLs(t *testing.T) {
  55. html := `<script src="/js/app.js"></script><script src="https://example.org/player.js"></script>`
  56. urls := ExtractScriptURLs(html)
  57. want := []string{"/js/app.js", "https://example.org/player.js"}
  58. if !reflect.DeepEqual(urls, want) {
  59. t.Fatalf("wanted script URLs %v, got %v", want, urls)
  60. }
  61. }
  62. func TestParsePlaylist(t *testing.T) {
  63. m3u := "#EXTM3U\nhttps://example.com/live.mp3\n"
  64. pls := "[playlist]\nFile1=https://example.com/stream.aac\n"
  65. xspf := "<playlist><location>https://example.com/hls.m3u8</location></playlist>"
  66. if len(ParsePlaylist(m3u, "audio/x-mpegurl", "https://example.com/playlist.m3u")) != 1 {
  67. t.Fatal("expected m3u playlist to yield 1 stream")
  68. }
  69. if len(ParsePlaylist(pls, "audio/x-scpls", "https://example.com/playlist.pls")) != 1 {
  70. t.Fatal("expected pls playlist to yield 1 stream")
  71. }
  72. if len(ParsePlaylist(xspf, "application/xspf+xml", "https://example.com/playlist.xspf")) != 1 {
  73. t.Fatal("expected xspf playlist to yield 1 stream")
  74. }
  75. relative := "stream/live.mp3\n"
  76. resolved := ParsePlaylist(relative, "audio/x-mpegurl", "https://example.com/radio/list.m3u")
  77. if len(resolved) != 1 || resolved[0] != "https://example.com/radio/stream/live.mp3" {
  78. t.Fatalf("expected relative URL to resolve against base: %v", resolved)
  79. }
  80. }