Revision control
Copy as Markdown
package org.mozilla.tv.firefox.utils
import androidx.test.core.app.ApplicationProvider
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test
import org.junit.runner.RunWith
import org.mozilla.tv.firefox.helpers.FirefoxRobolectricTestRunner
@RunWith(FirefoxRobolectricTestRunner::class)
class UrlUtilsTest {
@Test
fun isValidSearchQueryUrl() {
assertTrue(UrlUtils.isValidSearchQueryUrl("http-test-site.com/search/?q=%s"))
assertTrue(UrlUtils.isValidSearchQueryUrl("example.com/search/?q=%s"))
assertTrue(UrlUtils.isValidSearchQueryUrl(" example.com/search/?q=%s "))
assertFalse(UrlUtils.isValidSearchQueryUrl(" example.com/search/?q= "))
}
@Test
@Throws(Exception::class)
fun urlsMatchExceptForTrailingSlash() {
assertTrue(UrlUtils.urlsMatchExceptForTrailingSlash("http://www.mozilla.org", "http://www.mozilla.org"))
assertTrue(UrlUtils.urlsMatchExceptForTrailingSlash("http://www.mozilla.org/", "http://www.mozilla.org"))
assertTrue(UrlUtils.urlsMatchExceptForTrailingSlash("http://www.mozilla.org", "http://www.mozilla.org/"))
assertFalse(UrlUtils.urlsMatchExceptForTrailingSlash("http://mozilla.org", "http://www.mozilla.org"))
assertFalse(UrlUtils.urlsMatchExceptForTrailingSlash("http://www.mozilla.org/", "http://mozilla.org"))
assertFalse(UrlUtils.urlsMatchExceptForTrailingSlash("http://www.mozilla.org", "https://www.mozilla.org"))
assertFalse(UrlUtils.urlsMatchExceptForTrailingSlash("https://www.mozilla.org", "http://www.mozilla.org"))
// Same length of domain, but otherwise different:
assertFalse(UrlUtils.urlsMatchExceptForTrailingSlash("http://www.allizom.org", "http://www.mozilla.org"))
assertFalse(UrlUtils.urlsMatchExceptForTrailingSlash("http://www.allizom.org/", "http://www.mozilla.org"))
assertFalse(UrlUtils.urlsMatchExceptForTrailingSlash("http://www.allizom.org", "http://www.mozilla.org/"))
// Check upper/lower case is OK:
assertTrue(UrlUtils.urlsMatchExceptForTrailingSlash("http://www.MOZILLA.org", "http://www.mozilla.org"))
assertTrue(UrlUtils.urlsMatchExceptForTrailingSlash("http://www.MOZILLA.org/", "http://www.mozilla.org"))
assertTrue(UrlUtils.urlsMatchExceptForTrailingSlash("http://www.MOZILLA.org", "http://www.mozilla.org/"))
}
@Test
fun isPermittedResourceProtocol() {
assertFalse(UrlUtils.isPermittedResourceProtocol(""))
assertFalse(UrlUtils.isPermittedResourceProtocol(null))
assertTrue(UrlUtils.isPermittedResourceProtocol("http"))
assertTrue(UrlUtils.isPermittedResourceProtocol("https"))
assertTrue(UrlUtils.isPermittedResourceProtocol("data"))
assertTrue(UrlUtils.isPermittedResourceProtocol("file"))
assertFalse(UrlUtils.isPermittedResourceProtocol("nielsenwebid"))
}
@Test
fun isPermittedProtocol() {
assertFalse(UrlUtils.isSupportedProtocol(""))
assertFalse(UrlUtils.isSupportedProtocol(null))
assertTrue(UrlUtils.isSupportedProtocol("http"))
assertTrue(UrlUtils.isSupportedProtocol("https"))
assertTrue(UrlUtils.isSupportedProtocol("error"))
assertTrue(UrlUtils.isSupportedProtocol("data"))
assertFalse(UrlUtils.isSupportedProtocol("market"))
}
@Test
fun testIsUrl() {
assertTrue(UrlUtils.isUrl("mozilla.org"))
assertFalse(UrlUtils.isUrl("Hello World"))
assertFalse(UrlUtils.isUrl("Mozilla"))
}
@Test
fun testNormalize() {
assertEquals("https://www.mozilla.org/en-US/internet-health/", UrlUtils.normalize("https://www.mozilla.org/en-US/internet-health/"))
}
@Test
fun testIsSearchQuery() {
assertTrue(UrlUtils.isSearchQuery("hello world"))
assertFalse(UrlUtils.isSearchQuery("mozilla.org"))
assertFalse(UrlUtils.isSearchQuery("mozilla"))
}
@Test
fun testCreateSearchUrl() {
assertCreatedUrlContainsBase("how can mirrors be real if our eyes arent real?",
}
private fun assertCreatedUrlContainsBase(searchTerm: String, baseUrl: String) {
val searchString = UrlUtils.createSearchUrl(ApplicationProvider.getApplicationContext(), searchTerm)
// We need to remove the search code for this comparison since it could change based on the locale
assertTrue("\"$searchString\" does not contain \"$baseUrl\"",
searchString.replaceFirst("(client=)(.*)(&)".toRegex(), "").contains(baseUrl))
}
@Test
fun `WHEN input has no user info THEN should return unchanged`() {
listOf(
"",
"öäü102ß",
"user@mozilla.org",
"https://user:password@www.uri-contains-%-percentage-marks-that-are-not-associated-with-numbers.org%",
"://user:password@i/have/no/scheme.org"
).forEach {
val transformed = UrlUtils.stripUserInfo(it)
assertEquals(it, transformed)
}
}
@Test
fun `WHEN null is passed THEN should return blank`() {
assertEquals("", UrlUtils.stripUserInfo(null))
}
@Test
fun `WHEN user info is included THEN should return url without user info`() {
fun String.stripped(): String {
return UrlUtils.stripUserInfo(this)
}
}
@Test
fun isInternalErrorURL() {
assertTrue(UrlUtils.isInternalErrorURL("data:text/html;charset=utf-8;base64,"))
assertFalse(UrlUtils.isInternalErrorURL("www.mozilla.org"))
assertFalse(UrlUtils.isInternalErrorURL("error:-8"))
assertFalse(UrlUtils.isInternalErrorURL("hello world"))
}
@Test
fun isHttpOrHttpsUrl() {
assertFalse(UrlUtils.isHttpOrHttps(null))
assertFalse(UrlUtils.isHttpOrHttps(""))
assertFalse(UrlUtils.isHttpOrHttps(" "))
assertFalse(UrlUtils.isHttpOrHttps("mozilla.org"))
}
@Test
fun testStripCommonSubdomains() {
assertEquals("mozilla.org", UrlUtils.stripCommonSubdomains("mozilla.org"))
assertEquals("mozilla.org", UrlUtils.stripCommonSubdomains("www.mozilla.org"))
assertEquals("mozilla.org", UrlUtils.stripCommonSubdomains("m.mozilla.org"))
assertEquals("mozilla.org", UrlUtils.stripCommonSubdomains("mobile.mozilla.org"))
assertEquals("random.mozilla.org", UrlUtils.stripCommonSubdomains("random.mozilla.org"))
assertEquals(null, UrlUtils.stripCommonSubdomains(null))
}
@Test
fun `GIVEN input is not 'home' WHEN input is transformed THEN it should be unchanged`() {
listOf(
"error:-8",
"hello world",
"mozilla.org",
"www.mozilla.org",
"m.mozilla.org",
"mobile.mozilla.org"
).forEach {
val itTransformed = UrlUtils.toUrlBarDisplay(it)
assertEquals(it, itTransformed)
}
}
@Test
fun `GIVEN input is 'home' WHEN input is transformed THEN it should return blank`() {
assertEquals("", UrlUtils.toUrlBarDisplay(URLs.APP_URL_HOME))
}
}