summaryrefslogtreecommitdiff
path: root/app/src/androidTest/java/de/test/antennapod/util/URLCheckerTest.java
blob: 7f26ff6120300244637d14e237dc5234a668ad06 (plain)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package de.test.antennapod.util;

import androidx.test.filters.SmallTest;
import de.danoeh.antennapod.core.util.URLChecker;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

/**
 * Test class for URLChecker
 */
@SmallTest
public class URLCheckerTest {

    @Test
    public void testCorrectURLHttp() {
        final String in = "http://example.com";
        final String out = URLChecker.prepareURL(in);
        assertEquals(in, out);
    }

    @Test
    public void testCorrectURLHttps() {
        final String in = "https://example.com";
        final String out = URLChecker.prepareURL(in);
        assertEquals(in, out);
    }

    @Test
    public void testMissingProtocol() {
        final String in = "example.com";
        final String out = URLChecker.prepareURL(in);
        assertEquals("http://example.com", out);
    }

    @Test
    public void testFeedProtocol() {
        final String in = "feed://example.com";
        final String out = URLChecker.prepareURL(in);
        assertEquals("http://example.com", out);
    }

    @Test
    public void testPcastProtocolNoScheme() {
        final String in = "pcast://example.com";
        final String out = URLChecker.prepareURL(in);
        assertEquals("http://example.com", out);
    }

    @Test
    public void testItpcProtocol() {
        final String in = "itpc://example.com";
        final String out = URLChecker.prepareURL(in);
        assertEquals("http://example.com", out);
    }

    @Test
    public void testItpcProtocolWithScheme() {
        final String in = "itpc://https://example.com";
        final String out = URLChecker.prepareURL(in);
        assertEquals("https://example.com", out);
    }

    @Test
    public void testWhiteSpaceUrlShouldNotAppend() {
        final String in = "\n http://example.com \t";
        final String out = URLChecker.prepareURL(in);
        assertEquals("http://example.com", out);
    }

    @Test
    public void testWhiteSpaceShouldAppend() {
        final String in = "\n example.com \t";
        final String out = URLChecker.prepareURL(in);
        assertEquals("http://example.com", out);
    }

    @Test
    public void testAntennaPodSubscribeProtocolNoScheme() throws Exception {
        final String in = "antennapod-subscribe://example.com";
        final String out = URLChecker.prepareURL(in);
        assertEquals("http://example.com", out);
    }

    @Test
    public void testPcastProtocolWithScheme() {
        final String in = "pcast://https://example.com";
        final String out = URLChecker.prepareURL(in);
        assertEquals("https://example.com", out);
    }

    @Test
    public void testAntennaPodSubscribeProtocolWithScheme() throws Exception {
        final String in = "antennapod-subscribe://https://example.com";
        final String out = URLChecker.prepareURL(in);
        assertEquals("https://example.com", out);
    }

    @Test
    public void testProtocolRelativeUrlIsAbsolute() throws Exception {
        final String in = "https://example.com";
        final String inBase = "http://examplebase.com";
        final String out = URLChecker.prepareURL(in, inBase);
        assertEquals(in, out);
    }

    @Test
    public void testProtocolRelativeUrlIsRelativeHttps() throws Exception {
        final String in = "//example.com";
        final String inBase = "https://examplebase.com";
        final String out = URLChecker.prepareURL(in, inBase);
        assertEquals("https://example.com", out);
    }

    @Test
    public void testProtocolRelativeUrlIsHttpsWithAPSubscribeProtocol() throws Exception {
        final String in = "//example.com";
        final String inBase = "antennapod-subscribe://https://examplebase.com";
        final String out = URLChecker.prepareURL(in, inBase);
        assertEquals("https://example.com", out);
    }

    @Test
    public void testProtocolRelativeUrlBaseUrlNull() throws Exception {
        final String in = "example.com";
        final String out = URLChecker.prepareURL(in, null);
        assertEquals("http://example.com", out);
    }

    @Test
    public void testUrlEqualsSame() {
        assertTrue(URLChecker.urlEquals("https://www.example.com/test", "https://www.example.com/test"));
        assertTrue(URLChecker.urlEquals("https://www.example.com/test", "https://www.example.com/test/"));
        assertTrue(URLChecker.urlEquals("https://www.example.com/test", "https://www.example.com//test"));
        assertTrue(URLChecker.urlEquals("https://www.example.com", "https://www.example.com/"));
        assertTrue(URLChecker.urlEquals("https://www.example.com", "http://www.example.com"));
        assertTrue(URLChecker.urlEquals("http://www.example.com/", "https://www.example.com/"));
        assertTrue(URLChecker.urlEquals("https://www.example.com/?id=42", "https://www.example.com/?id=42"));
        assertTrue(URLChecker.urlEquals("https://example.com/podcast%20test", "https://example.com/podcast test"));
        assertTrue(URLChecker.urlEquals("https://example.com/?a=podcast%20test", "https://example.com/?a=podcast test"));
        assertTrue(URLChecker.urlEquals("https://example.com/?", "https://example.com/"));
        assertTrue(URLChecker.urlEquals("https://example.com/?", "https://example.com"));
        assertTrue(URLChecker.urlEquals("https://Example.com", "https://example.com"));
        assertTrue(URLChecker.urlEquals("https://example.com/test", "https://example.com/Test"));
    }

    @Test
    public void testUrlEqualsDifferent() {
        assertFalse(URLChecker.urlEquals("https://www.example.com/test", "https://www.example2.com/test"));
        assertFalse(URLChecker.urlEquals("https://www.example.com/test", "https://www.example.de/test"));
        assertFalse(URLChecker.urlEquals("https://example.com/", "https://otherpodcast.example.com/"));
        assertFalse(URLChecker.urlEquals("https://www.example.com/?id=42&a=b", "https://www.example.com/?id=43&a=b"));
        assertFalse(URLChecker.urlEquals("https://example.com/podcast%25test", "https://example.com/podcast test"));
    }
}