aboutsummaryrefslogtreecommitdiff
path: root/internal/reader/sanitizer/srcset_test.go
blob: e15013732177b9d2eda752cc6e0859ffb4ea8454 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package sanitizer

import "testing"

func TestParseSrcSetAttributeWithRelativeURLs(t *testing.T) {
	input := `example-320w.jpg, example-480w.jpg 1.5x,   example-640,w.jpg 2x, example-640w.jpg 640w`
	candidates := ParseSrcSetAttribute(input)

	if len(candidates) != 4 {
		t.Error(`Incorrect number of candidates`)
	}

	if candidates.String() != `example-320w.jpg, example-480w.jpg 1.5x, example-640,w.jpg 2x, example-640w.jpg 640w` {
		t.Errorf(`Unexpected string output`)
	}
}

func TestParseSrcSetAttributeWithAbsoluteURLs(t *testing.T) {
	input := `http://example.org/example-320w.jpg 320w, http://example.org/example-480w.jpg 1.5x`
	candidates := ParseSrcSetAttribute(input)

	if len(candidates) != 2 {
		t.Error(`Incorrect number of candidates`)
	}

	if candidates.String() != `http://example.org/example-320w.jpg 320w, http://example.org/example-480w.jpg 1.5x` {
		t.Errorf(`Unexpected string output`)
	}
}

func TestParseSrcSetAttributeWithOneCandidate(t *testing.T) {
	input := `http://example.org/example-320w.jpg`
	candidates := ParseSrcSetAttribute(input)

	if len(candidates) != 1 {
		t.Error(`Incorrect number of candidates`)
	}

	if candidates.String() != `http://example.org/example-320w.jpg` {
		t.Errorf(`Unexpected string output`)
	}
}

func TestParseSrcSetAttributeWithCommaURL(t *testing.T) {
	input := `http://example.org/example,a:b/d.jpg , example-480w.jpg 1.5x`
	candidates := ParseSrcSetAttribute(input)

	if len(candidates) != 2 {
		t.Error(`Incorrect number of candidates`)
	}

	if candidates.String() != `http://example.org/example,a:b/d.jpg, example-480w.jpg 1.5x` {
		t.Errorf(`Unexpected string output`)
	}
}

func TestParseSrcSetAttributeWithIncorrectDescriptor(t *testing.T) {
	input := `http://example.org/example-320w.jpg test`
	candidates := ParseSrcSetAttribute(input)

	if len(candidates) != 0 {
		t.Error(`Incorrect number of candidates`)
	}

	if candidates.String() != `` {
		t.Errorf(`Unexpected string output`)
	}
}

func TestParseSrcSetAttributeWithTooManyDescriptors(t *testing.T) {
	input := `http://example.org/example-320w.jpg 10w 1x`
	candidates := ParseSrcSetAttribute(input)

	if len(candidates) != 0 {
		t.Error(`Incorrect number of candidates`)
	}

	if candidates.String() != `` {
		t.Errorf(`Unexpected string output`)
	}
}