srcset_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package sanitizer
  4. import "testing"
  5. func TestParseSrcSetAttributeWithRelativeURLs(t *testing.T) {
  6. input := `example-320w.jpg, example-480w.jpg 1.5x, example-640,w.jpg 2x, example-640w.jpg 640w`
  7. candidates := ParseSrcSetAttribute(input)
  8. if len(candidates) != 4 {
  9. t.Error(`Incorrect number of candidates`)
  10. }
  11. if candidates.String() != `example-320w.jpg, example-480w.jpg 1.5x, example-640,w.jpg 2x, example-640w.jpg 640w` {
  12. t.Errorf(`Unexpected string output`)
  13. }
  14. }
  15. func TestParseSrcSetAttributeWithAbsoluteURLs(t *testing.T) {
  16. input := `http://example.org/example-320w.jpg 320w, http://example.org/example-480w.jpg 1.5x`
  17. candidates := ParseSrcSetAttribute(input)
  18. if len(candidates) != 2 {
  19. t.Error(`Incorrect number of candidates`)
  20. }
  21. if candidates.String() != `http://example.org/example-320w.jpg 320w, http://example.org/example-480w.jpg 1.5x` {
  22. t.Errorf(`Unexpected string output`)
  23. }
  24. }
  25. func TestParseSrcSetAttributeWithOneCandidate(t *testing.T) {
  26. input := `http://example.org/example-320w.jpg`
  27. candidates := ParseSrcSetAttribute(input)
  28. if len(candidates) != 1 {
  29. t.Error(`Incorrect number of candidates`)
  30. }
  31. if candidates.String() != `http://example.org/example-320w.jpg` {
  32. t.Errorf(`Unexpected string output`)
  33. }
  34. }
  35. func TestParseSrcSetAttributeWithCommaURL(t *testing.T) {
  36. input := `http://example.org/example,a:b/d.jpg , example-480w.jpg 1.5x`
  37. candidates := ParseSrcSetAttribute(input)
  38. if len(candidates) != 2 {
  39. t.Error(`Incorrect number of candidates`)
  40. }
  41. if candidates.String() != `http://example.org/example,a:b/d.jpg, example-480w.jpg 1.5x` {
  42. t.Errorf(`Unexpected string output`)
  43. }
  44. }
  45. func TestParseSrcSetAttributeWithIncorrectDescriptor(t *testing.T) {
  46. input := `http://example.org/example-320w.jpg test`
  47. candidates := ParseSrcSetAttribute(input)
  48. if len(candidates) != 0 {
  49. t.Error(`Incorrect number of candidates`)
  50. }
  51. if candidates.String() != `` {
  52. t.Errorf(`Unexpected string output`)
  53. }
  54. }
  55. func TestParseSrcSetAttributeWithTooManyDescriptors(t *testing.T) {
  56. input := `http://example.org/example-320w.jpg 10w 1x`
  57. candidates := ParseSrcSetAttribute(input)
  58. if len(candidates) != 0 {
  59. t.Error(`Incorrect number of candidates`)
  60. }
  61. if candidates.String() != `` {
  62. t.Errorf(`Unexpected string output`)
  63. }
  64. }