Skip to content

Commit

Permalink
Add test for url details apply methods
Browse files Browse the repository at this point in the history
  • Loading branch information
adinauer committed Jan 30, 2023
1 parent 46cff12 commit 0c58acc
Showing 1 changed file with 83 additions and 0 deletions.
83 changes: 83 additions & 0 deletions sentry/src/test/java/io/sentry/UrlDetailsTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package io.sentry

import io.sentry.protocol.Request
import io.sentry.util.UrlUtils
import org.mockito.kotlin.mock
import org.mockito.kotlin.verify
import org.mockito.kotlin.verifyNoMoreInteractions
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNull

class UrlDetailsTest {

@Test
fun `does not crash on null span`() {
val urlDetails = UrlUtils.UrlDetails("https://sentry.io/api", "q=1", "top")
urlDetails.applyToSpan(null)
}

@Test
fun `applies query and fragment to span`() {
val urlDetails = UrlUtils.UrlDetails("https://sentry.io/api", "q=1", "top")
val span = mock<ISpan>()
urlDetails.applyToSpan(span)

verify(span).setData("http.query", "q=1")
verify(span).setData("http.fragment", "top")
}

@Test
fun `applies query to span`() {
val urlDetails = UrlUtils.UrlDetails("https://sentry.io/api", "q=1", null)
val span = mock<ISpan>()
urlDetails.applyToSpan(span)

verify(span).setData("http.query", "q=1")
verifyNoMoreInteractions(span)
}

@Test
fun `applies fragment to span`() {
val urlDetails = UrlUtils.UrlDetails("https://sentry.io/api", null, "top")
val span = mock<ISpan>()
urlDetails.applyToSpan(span)

verify(span).setData("http.fragment", "top")
verifyNoMoreInteractions(span)
}

@Test
fun `does not crash on null request`() {
val urlDetails = UrlUtils.UrlDetails("https://sentry.io/api", "q=1", "top")
urlDetails.applyToRequest(null)
}

@Test
fun `applies details to request`() {
val urlDetails = UrlUtils.UrlDetails("https://sentry.io/api", "q=1", "top")
val request = Request()
urlDetails.applyToRequest(request)

assertEquals("https://sentry.io/api", request.url)
assertEquals("q=1", request.queryString)
assertEquals("top", request.fragment)
}

@Test
fun `applies details without fragment and url to request`() {
val urlDetails = UrlUtils.UrlDetails("https://sentry.io/api", null, null)
val request = Request()
urlDetails.applyToRequest(request)

assertEquals("https://sentry.io/api", request.url)
assertNull(request.queryString)
assertNull(request.fragment)
}

@Test
fun `returns fallback for null URL`() {
val urlDetails = UrlUtils.UrlDetails(null, null, null)
assertEquals("unknown", urlDetails.urlOrFallback)
}
}

0 comments on commit 0c58acc

Please sign in to comment.