Skip to content

Commit 83dfd9f

Browse files
runningcodeclaude
andcommitted
test: Add tests for custom DSN string parsing
Cover edge cases specific to the manual indexOf/substring parser: null input, missing scheme separator, no slash after host, multiple path segments, port with path, multiple double slashes, query string with port, empty secret key, and a realistic Sentry DSN with org id. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 0246373 commit 83dfd9f

1 file changed

Lines changed: 67 additions & 0 deletions

File tree

sentry/src/test/java/io/sentry/DsnTest.kt

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,4 +145,71 @@ class DsnTest {
145145
val dsn = Dsn("http://key@localhost:9000/456")
146146
assertNull(dsn.orgId)
147147
}
148+
149+
@Test
150+
fun `when dsn is null, throws exception`() {
151+
assertFailsWith<IllegalArgumentException> { Dsn(null) }
152+
}
153+
154+
@Test
155+
fun `when dsn has no scheme separator, throws exception`() {
156+
assertFailsWith<IllegalArgumentException> { Dsn("httpspublicKey@host/id") }
157+
}
158+
159+
@Test
160+
fun `when dsn has no slash after host, throws exception`() {
161+
assertFailsWith<IllegalArgumentException> { Dsn("https://key@host") }
162+
}
163+
164+
@Test
165+
fun `dsn parsed with multiple path segments`() {
166+
val dsn = Dsn("https://key@host/path/to/sentry/id")
167+
168+
assertEquals("https://host/path/to/sentry/api/id", dsn.sentryUri.toURL().toString())
169+
assertEquals("key", dsn.publicKey)
170+
assertEquals("/path/to/sentry/", dsn.path)
171+
assertEquals("id", dsn.projectId)
172+
}
173+
174+
@Test
175+
fun `dsn parsed with port and path`() {
176+
val dsn = Dsn("http://key:secret@host:8080/path/id")
177+
178+
assertEquals("http://host:8080/path/api/id", dsn.sentryUri.toURL().toString())
179+
assertEquals("key", dsn.publicKey)
180+
assertEquals("secret", dsn.secretKey)
181+
assertEquals("/path/", dsn.path)
182+
assertEquals("id", dsn.projectId)
183+
}
184+
185+
@Test
186+
fun `dsn with multiple double slashes in path is normalized`() {
187+
val dsn = Dsn("http://key@host//path//id")
188+
assertEquals("http://host/path/api/id", dsn.sentryUri.toURL().toString())
189+
}
190+
191+
@Test
192+
fun `dsn with query string and port`() {
193+
val dsn = Dsn("https://key@host:443/id?foo=bar&baz=1")
194+
195+
assertEquals("https://host:443/api/id", dsn.sentryUri.toURL().toString())
196+
assertEquals("id", dsn.projectId)
197+
}
198+
199+
@Test
200+
fun `dsn with empty secret key after colon`() {
201+
val dsn = Dsn("https://publicKey:@host/id")
202+
203+
assertEquals("publicKey", dsn.publicKey)
204+
assertEquals("", dsn.secretKey)
205+
}
206+
207+
@Test
208+
fun `dsn with numeric project id`() {
209+
val dsn = Dsn("https://key@o123.ingest.sentry.io/1234567")
210+
211+
assertEquals("1234567", dsn.projectId)
212+
assertEquals("123", dsn.orgId)
213+
assertEquals("https://o123.ingest.sentry.io/api/1234567", dsn.sentryUri.toURL().toString())
214+
}
148215
}

0 commit comments

Comments
 (0)