|
| 1 | +package io.sentry.react |
| 2 | + |
| 3 | +import android.app.Activity |
| 4 | +import com.facebook.react.bridge.JavaOnlyMap |
| 5 | +import com.facebook.react.common.JavascriptException |
| 6 | +import io.sentry.Breadcrumb |
| 7 | +import io.sentry.ILogger |
| 8 | +import io.sentry.android.core.SentryAndroidOptions |
| 9 | +import org.junit.Assert.assertEquals |
| 10 | +import org.junit.Assert.assertFalse |
| 11 | +import org.junit.Assert.assertNull |
| 12 | +import org.junit.Assert.assertTrue |
| 13 | +import org.junit.Before |
| 14 | +import org.junit.Test |
| 15 | +import org.junit.runner.RunWith |
| 16 | +import org.junit.runners.JUnit4 |
| 17 | +import org.mockito.Mockito.mock |
| 18 | +import org.mockito.MockitoAnnotations |
| 19 | + |
| 20 | +@RunWith(JUnit4::class) |
| 21 | +class RNSentryStartTest { |
| 22 | + private lateinit var logger: ILogger |
| 23 | + |
| 24 | + private lateinit var activity: Activity |
| 25 | + |
| 26 | + @Before |
| 27 | + fun setUp() { |
| 28 | + MockitoAnnotations.openMocks(this) |
| 29 | + logger = mock(ILogger::class.java) |
| 30 | + activity = mock(Activity::class.java) |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + fun `when the spotlight option is enabled, the spotlight SentryAndroidOption is set to true and the default url is used`() { |
| 35 | + val options = |
| 36 | + JavaOnlyMap.of( |
| 37 | + "spotlight", |
| 38 | + true, |
| 39 | + "defaultSidecarUrl", |
| 40 | + "http://localhost:8969/teststream", |
| 41 | + ) |
| 42 | + val actualOptions = SentryAndroidOptions() |
| 43 | + RNSentryStart.getSentryAndroidOptions(actualOptions, options, activity, logger) |
| 44 | + assert(actualOptions.isEnableSpotlight) |
| 45 | + assertEquals("http://localhost:8969/teststream", actualOptions.spotlightConnectionUrl) |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + fun `when the spotlight url is passed, the spotlight is enabled for the given url`() { |
| 50 | + val options = JavaOnlyMap.of("spotlight", "http://localhost:8969/teststream") |
| 51 | + val actualOptions = SentryAndroidOptions() |
| 52 | + RNSentryStart.getSentryAndroidOptions(actualOptions, options, activity, logger) |
| 53 | + assert(actualOptions.isEnableSpotlight) |
| 54 | + assertEquals("http://localhost:8969/teststream", actualOptions.spotlightConnectionUrl) |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + fun `when the spotlight option is disabled, the spotlight SentryAndroidOption is set to false`() { |
| 59 | + val options = JavaOnlyMap.of("spotlight", false) |
| 60 | + val actualOptions = SentryAndroidOptions() |
| 61 | + RNSentryStart.getSentryAndroidOptions(actualOptions, options, activity, logger) |
| 62 | + assertFalse(actualOptions.isEnableSpotlight) |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + fun `the JavascriptException is added to the ignoredExceptionsForType list on initialisation`() { |
| 67 | + val actualOptions = SentryAndroidOptions() |
| 68 | + RNSentryStart.getSentryAndroidOptions(actualOptions, JavaOnlyMap.of(), activity, logger) |
| 69 | + assertTrue(actualOptions.ignoredExceptionsForType.contains(JavascriptException::class.java)) |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + fun `beforeBreadcrumb callback filters out Sentry DSN requests breadcrumbs`() { |
| 74 | + val options = SentryAndroidOptions() |
| 75 | + val rnOptions = |
| 76 | + JavaOnlyMap.of( |
| 77 | + "dsn", |
| 78 | + "https://[email protected]/1234567", |
| 79 | + "devServerUrl", |
| 80 | + "http://localhost:8081", |
| 81 | + ) |
| 82 | + RNSentryStart.getSentryAndroidOptions(options, rnOptions, activity, logger) |
| 83 | + |
| 84 | + val breadcrumb = |
| 85 | + Breadcrumb().apply { |
| 86 | + type = "http" |
| 87 | + setData("url", "https://def.ingest.sentry.io/1234567") |
| 88 | + } |
| 89 | + |
| 90 | + val result = options.beforeBreadcrumb?.execute(breadcrumb, mock()) |
| 91 | + |
| 92 | + assertNull("Breadcrumb should be filtered out", result) |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + fun `beforeBreadcrumb callback filters out dev server breadcrumbs`() { |
| 97 | + val mockDevServerUrl = "http://localhost:8081" |
| 98 | + val options = SentryAndroidOptions() |
| 99 | + val rnOptions = |
| 100 | + JavaOnlyMap.of( |
| 101 | + "dsn", |
| 102 | + "https://[email protected]/1234567", |
| 103 | + "devServerUrl", |
| 104 | + mockDevServerUrl, |
| 105 | + ) |
| 106 | + RNSentryStart.getSentryAndroidOptions(options, rnOptions, activity, logger) |
| 107 | + |
| 108 | + val breadcrumb = |
| 109 | + Breadcrumb().apply { |
| 110 | + type = "http" |
| 111 | + setData("url", mockDevServerUrl) |
| 112 | + } |
| 113 | + |
| 114 | + val result = options.beforeBreadcrumb?.execute(breadcrumb, mock()) |
| 115 | + |
| 116 | + assertNull("Breadcrumb should be filtered out", result) |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + fun `beforeBreadcrumb callback does not filter out non dev server or dsn breadcrumbs`() { |
| 121 | + val options = SentryAndroidOptions() |
| 122 | + val rnOptions = |
| 123 | + JavaOnlyMap.of( |
| 124 | + "dsn", |
| 125 | + "https://[email protected]/1234567", |
| 126 | + "devServerUrl", |
| 127 | + "http://localhost:8081", |
| 128 | + ) |
| 129 | + RNSentryStart.getSentryAndroidOptions(options, rnOptions, activity, logger) |
| 130 | + |
| 131 | + val breadcrumb = |
| 132 | + Breadcrumb().apply { |
| 133 | + type = "http" |
| 134 | + setData("url", "http://testurl.com/service") |
| 135 | + } |
| 136 | + |
| 137 | + val result = options.beforeBreadcrumb?.execute(breadcrumb, mock()) |
| 138 | + |
| 139 | + assertEquals(breadcrumb, result) |
| 140 | + } |
| 141 | + |
| 142 | + @Test |
| 143 | + fun `the breadcrumb is not filtered out when the dev server url and dsn are not passed`() { |
| 144 | + val options = SentryAndroidOptions() |
| 145 | + RNSentryStart.getSentryAndroidOptions(options, JavaOnlyMap(), activity, logger) |
| 146 | + |
| 147 | + val breadcrumb = |
| 148 | + Breadcrumb().apply { |
| 149 | + type = "http" |
| 150 | + setData("url", "http://testurl.com/service") |
| 151 | + } |
| 152 | + |
| 153 | + val result = options.beforeBreadcrumb?.execute(breadcrumb, mock()) |
| 154 | + |
| 155 | + assertEquals(breadcrumb, result) |
| 156 | + } |
| 157 | + |
| 158 | + @Test |
| 159 | + fun `the breadcrumb is not filtered out when the dev server url is not passed and the dsn does not match`() { |
| 160 | + val options = SentryAndroidOptions() |
| 161 | + val rnOptions = JavaOnlyMap.of( "dsn", "https://[email protected]/1234567") |
| 162 | + RNSentryStart.getSentryAndroidOptions(options, rnOptions, activity, logger) |
| 163 | + |
| 164 | + val breadcrumb = |
| 165 | + Breadcrumb().apply { |
| 166 | + type = "http" |
| 167 | + setData("url", "http://testurl.com/service") |
| 168 | + } |
| 169 | + |
| 170 | + val result = options.beforeBreadcrumb?.execute(breadcrumb, mock()) |
| 171 | + |
| 172 | + assertEquals(breadcrumb, result) |
| 173 | + } |
| 174 | + |
| 175 | + @Test |
| 176 | + fun `the breadcrumb is not filtered out when the dev server url does not match and the dsn is not passed`() { |
| 177 | + val options = SentryAndroidOptions() |
| 178 | + val rnOptions = JavaOnlyMap.of("devServerUrl", "http://localhost:8081") |
| 179 | + RNSentryStart.getSentryAndroidOptions(options, rnOptions, activity, logger) |
| 180 | + |
| 181 | + val breadcrumb = |
| 182 | + Breadcrumb().apply { |
| 183 | + type = "http" |
| 184 | + setData("url", "http://testurl.com/service") |
| 185 | + } |
| 186 | + |
| 187 | + val result = options.beforeBreadcrumb?.execute(breadcrumb, mock()) |
| 188 | + |
| 189 | + assertEquals(breadcrumb, result) |
| 190 | + } |
| 191 | +} |
0 commit comments