Browse Source

Improve unit tests in url package

Frédéric Guillot 6 months ago
parent
commit
d5ff4191b6
1 changed files with 44 additions and 92 deletions
  1. 44
    92
      url/url_test.go

+ 44
- 92
url/url_test.go View File

@@ -6,116 +6,68 @@ package url // import "miniflux.app/url"
6 6
 
7 7
 import "testing"
8 8
 
9
-func TestGetAbsoluteURLWithAbsolutePath(t *testing.T) {
10
-	expected := `https://example.org/path/file.ext`
11
-	input := `/path/file.ext`
12
-	output, err := AbsoluteURL("https://example.org/folder/", input)
13
-
14
-	if err != nil {
15
-		t.Error(err)
16
-	}
17
-
18
-	if expected != output {
19
-		t.Errorf(`Unexpected output, got "%s" instead of "%s"`, output, expected)
9
+func TestAbsoluteURL(t *testing.T) {
10
+	scenarios := [][]string{
11
+		[]string{"https://example.org/path/file.ext", "https://example.org/folder/", "/path/file.ext"},
12
+		[]string{"https://example.org/folder/path/file.ext", "https://example.org/folder/", "path/file.ext"},
13
+		[]string{"https://example.org/path/file.ext", "https://example.org/folder", "path/file.ext"},
14
+		[]string{"https://example.org/path/file.ext", "https://example.org/folder/", "https://example.org/path/file.ext"},
15
+		[]string{"https://static.example.org/path/file.ext", "https://www.example.org/", "//static.example.org/path/file.ext"},
20 16
 	}
21
-}
22 17
 
23
-func TestGetAbsoluteURLWithRelativePath(t *testing.T) {
24
-	expected := `https://example.org/folder/path/file.ext`
25
-	input := `path/file.ext`
26
-	output, err := AbsoluteURL("https://example.org/folder/", input)
18
+	for _, scenario := range scenarios {
19
+		actual, err := AbsoluteURL(scenario[1], scenario[2])
27 20
 
28
-	if err != nil {
29
-		t.Error(err)
30
-	}
21
+		if err != nil {
22
+			t.Errorf(`Got error for (%q, %q): %v`, scenario[1], scenario[2], err)
23
+		}
31 24
 
32
-	if expected != output {
33
-		t.Errorf(`Unexpected output, got "%s" instead of "%s"`, output, expected)
25
+		if actual != scenario[0] {
26
+			t.Errorf(`Unexpected result, got %q instead of %q for (%q, %q)`, actual, scenario[0], scenario[1], scenario[2])
27
+		}
34 28
 	}
35 29
 }
36 30
 
37
-func TestGetAbsoluteURLWithRelativePaths(t *testing.T) {
38
-	expected := `https://example.org/path/file.ext`
39
-	input := `path/file.ext`
40
-	output, err := AbsoluteURL("https://example.org/folder", input)
41
-
42
-	if err != nil {
43
-		t.Error(err)
31
+func TestRootURL(t *testing.T) {
32
+	scenarios := map[string]string{
33
+		"https://example.org/path/file.ext":  "https://example.org/",
34
+		"//static.example.org/path/file.ext": "https://static.example.org/",
35
+		"https://example|org/path/file.ext":  "https://example|org/path/file.ext",
44 36
 	}
45 37
 
46
-	if expected != output {
47
-		t.Errorf(`Unexpected output, got "%s" instead of "%s"`, output, expected)
48
-	}
49
-}
50
-
51
-func TestWhenInputIsAlreadyAbsolute(t *testing.T) {
52
-	expected := `https://example.org/path/file.ext`
53
-	input := `https://example.org/path/file.ext`
54
-	output, err := AbsoluteURL("https://example.org/folder/", input)
55
-
56
-	if err != nil {
57
-		t.Error(err)
58
-	}
59
-
60
-	if expected != output {
61
-		t.Errorf(`Unexpected output, got "%s" instead of "%s"`, output, expected)
62
-	}
63
-}
64
-
65
-func TestGetAbsoluteURLWithProtocolRelative(t *testing.T) {
66
-	expected := `https://static.example.org/path/file.ext`
67
-	input := `//static.example.org/path/file.ext`
68
-	output, err := AbsoluteURL("https://www.example.org/", input)
69
-
70
-	if err != nil {
71
-		t.Error(err)
72
-	}
73
-
74
-	if expected != output {
75
-		t.Errorf(`Unexpected output, got "%s" instead of "%s"`, output, expected)
76
-	}
77
-}
78
-
79
-func TestGetRootURL(t *testing.T) {
80
-	expected := `https://example.org/`
81
-	input := `https://example.org/path/file.ext`
82
-	output := RootURL(input)
83
-
84
-	if expected != output {
85
-		t.Errorf(`Unexpected output, got "%s" instead of "%s"`, output, expected)
86
-	}
87
-}
88
-
89
-func TestGetRootURLWithProtocolRelativePath(t *testing.T) {
90
-	expected := `https://static.example.org/`
91
-	input := `//static.example.org/path/file.ext`
92
-	output := RootURL(input)
93
-
94
-	if expected != output {
95
-		t.Errorf(`Unexpected output, got "%s" instead of "%s"`, output, expected)
38
+	for input, expected := range scenarios {
39
+		actual := RootURL(input)
40
+		if actual != expected {
41
+			t.Errorf(`Unexpected result, got %q instead of %q`, actual, expected)
42
+		}
96 43
 	}
97 44
 }
98 45
 
99 46
 func TestIsHTTPS(t *testing.T) {
100
-	if !IsHTTPS("https://example.org/") {
101
-		t.Error("Unable to recognize HTTPS URL")
47
+	scenarios := map[string]bool{
48
+		"https://example.org/": true,
49
+		"http://example.org/":  false,
50
+		"https://example|org/": false,
102 51
 	}
103 52
 
104
-	if IsHTTPS("http://example.org/") {
105
-		t.Error("Unable to recognize HTTP URL")
106
-	}
107
-
108
-	if IsHTTPS("") {
109
-		t.Error("Unable to recognize malformed URL")
53
+	for input, expected := range scenarios {
54
+		actual := IsHTTPS(input)
55
+		if actual != expected {
56
+			t.Errorf(`Unexpected result, got %v instead of %v`, actual, expected)
57
+		}
110 58
 	}
111 59
 }
112 60
 
113
-func TestGetDomain(t *testing.T) {
114
-	expected := `static.example.org`
115
-	input := `http://static.example.org/`
116
-	output := Domain(input)
61
+func TestDomain(t *testing.T) {
62
+	scenarios := map[string]string{
63
+		"https://static.example.org/": "static.example.org",
64
+		"https://example|org/":        "https://example|org/",
65
+	}
117 66
 
118
-	if expected != output {
119
-		t.Errorf(`Unexpected output, got "%s" instead of "%s"`, output, expected)
67
+	for input, expected := range scenarios {
68
+		actual := Domain(input)
69
+		if actual != expected {
70
+			t.Errorf(`Unexpected result, got %q instead of %q`, actual, expected)
71
+		}
120 72
 	}
121 73
 }

Loading…
Cancel
Save