|
| 1 | +package com.android.swingmusic.auth.presentation.util |
| 2 | + |
| 3 | +import org.junit.Assert.assertEquals |
| 4 | +import org.junit.Assert.assertFalse |
| 5 | +import org.junit.Assert.assertNull |
| 6 | +import org.junit.Assert.assertTrue |
| 7 | +import org.junit.Test |
| 8 | + |
| 9 | +class AuthUtilsTest { |
| 10 | + |
| 11 | + // normalizeUrl |
| 12 | + @Test |
| 13 | + fun `normalizeUrl returns null for null input`() { |
| 14 | + assertNull(AuthUtils.normalizeUrl(null)) |
| 15 | + } |
| 16 | + |
| 17 | + @Test |
| 18 | + fun `normalizeUrl returns null for blank input`() { |
| 19 | + assertNull(AuthUtils.normalizeUrl(" \t \n ")) |
| 20 | + } |
| 21 | + |
| 22 | + @Test |
| 23 | + fun `normalizeUrl trims whitespace without scheme`() { |
| 24 | + assertEquals("https://example.com", AuthUtils.normalizeUrl(" example.com ")) |
| 25 | + } |
| 26 | + |
| 27 | + @Test |
| 28 | + fun `normalizeUrl trims whitespace with scheme`() { |
| 29 | + assertEquals("https://example.com", AuthUtils.normalizeUrl(" https://example.com ")) |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + fun `normalizeUrl prepends https when scheme is missing`() { |
| 34 | + assertEquals("https://example.com", AuthUtils.normalizeUrl("example.com")) |
| 35 | + assertEquals("https://sub.domain.com", AuthUtils.normalizeUrl("sub.domain.com")) |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + fun `normalizeUrl handles ports and IPs when missing scheme`() { |
| 40 | + assertEquals("https://example.com:8080", AuthUtils.normalizeUrl("example.com:8080")) |
| 41 | + assertEquals("https://192.168.1.1", AuthUtils.normalizeUrl("192.168.1.1")) |
| 42 | + assertEquals("https://192.168.1.1:8443", AuthUtils.normalizeUrl("192.168.1.1:8443")) |
| 43 | + assertEquals("https://localhost:3000", AuthUtils.normalizeUrl("localhost:3000")) |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + fun `normalizeUrl leaves http and https unchanged`() { |
| 48 | + assertEquals("http://example.com", AuthUtils.normalizeUrl("http://example.com")) |
| 49 | + assertEquals("https://example.com", AuthUtils.normalizeUrl("https://example.com")) |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + fun `normalizeUrl leaves other schemes unchanged`() { |
| 54 | + assertEquals("ftp://example.com", AuthUtils.normalizeUrl("ftp://example.com")) |
| 55 | + assertEquals("custom+scheme://host/path", AuthUtils.normalizeUrl("custom+scheme://host/path")) |
| 56 | + } |
| 57 | + |
| 58 | + // validInputUrl |
| 59 | + @Test |
| 60 | + fun `validInputUrl accepts http, https, and ftp`() { |
| 61 | + assertTrue(AuthUtils.validInputUrl("http://example.com")) |
| 62 | + assertTrue(AuthUtils.validInputUrl("https://example.com")) |
| 63 | + assertTrue(AuthUtils.validInputUrl("ftp://example.com")) |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + fun `validInputUrl accepts domains with ports`() { |
| 68 | + assertTrue(AuthUtils.validInputUrl("https://example.com:8080")) |
| 69 | + assertTrue(AuthUtils.validInputUrl("http://sub.example.co.uk:3000/path")) |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + fun `validInputUrl accepts localhost and IPv4 with optional ports`() { |
| 74 | + assertTrue(AuthUtils.validInputUrl("http://localhost")) |
| 75 | + assertTrue(AuthUtils.validInputUrl("http://localhost:3000/health")) |
| 76 | + assertTrue(AuthUtils.validInputUrl("https://192.168.1.1")) |
| 77 | + assertTrue(AuthUtils.validInputUrl("https://192.168.1.1:8443/api")) |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + fun `validInputUrl rejects malformed IPv4`() { |
| 82 | + assertFalse(AuthUtils.validInputUrl("https://999.1.1.1")) |
| 83 | + assertFalse(AuthUtils.validInputUrl("http://256.256.256.256")) |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + fun `validInputUrl rejects missing scheme`() { |
| 88 | + assertFalse(AuthUtils.validInputUrl("example.com")) |
| 89 | + assertFalse(AuthUtils.validInputUrl("www.example.com")) |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + fun `validInputUrl rejects blank or null`() { |
| 94 | + assertFalse(AuthUtils.validInputUrl(null)) |
| 95 | + assertFalse(AuthUtils.validInputUrl(" ")) |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + fun `validInputUrl accepts typical paths and query`() { |
| 100 | + assertTrue(AuthUtils.validInputUrl("https://example.com/path?query=1#frag")) |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + fun `validInputUrl rejects obvious invalid urls and custom schemes`() { |
| 105 | + assertFalse(AuthUtils.validInputUrl("https:///example.com")) |
| 106 | + assertFalse(AuthUtils.validInputUrl("https://")) |
| 107 | + assertFalse(AuthUtils.validInputUrl("not a url")) |
| 108 | + assertFalse(AuthUtils.validInputUrl("custom+scheme://host/path")) |
| 109 | + } |
| 110 | +} |
0 commit comments