|
| 1 | +package io.sentry |
| 2 | + |
| 3 | +import com.google.common.truth.Truth.assertThat |
| 4 | +import kotlin.test.Test |
| 5 | + |
| 6 | +class DataCollectionResolverTest { |
| 7 | + @Test |
| 8 | + fun `one resolver is reused per options instance`() { |
| 9 | + val options = SentryOptions() |
| 10 | + |
| 11 | + assertThat(options.dataCollectionResolver).isSameInstanceAs(options.dataCollectionResolver) |
| 12 | + } |
| 13 | + |
| 14 | + @Test |
| 15 | + fun `each options instance owns its resolver`() { |
| 16 | + val first = SentryOptions() |
| 17 | + val second = SentryOptions() |
| 18 | + |
| 19 | + assertThat(first.dataCollectionResolver).isNotSameInstanceAs(second.dataCollectionResolver) |
| 20 | + } |
| 21 | + |
| 22 | + @Test |
| 23 | + fun `data collection configured reflects namespace explicitness`() { |
| 24 | + val options = SentryOptions() |
| 25 | + |
| 26 | + assertThat(options.dataCollectionResolver.isDataCollectionConfigured).isFalse() |
| 27 | + |
| 28 | + options.dataCollection.queryParams = KeyValueCollectionBehavior.denyList() |
| 29 | + |
| 30 | + assertThat(options.dataCollectionResolver.isDataCollectionConfigured).isTrue() |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + fun `user info falls back to sendDefaultPii when unset`() { |
| 35 | + val options = SentryOptions() |
| 36 | + |
| 37 | + assertThat(options.dataCollectionResolver.isUserInfo).isFalse() |
| 38 | + |
| 39 | + options.isSendDefaultPii = true |
| 40 | + |
| 41 | + assertThat(options.dataCollectionResolver.isUserInfo).isTrue() |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + fun `user info override takes precedence over sendDefaultPii`() { |
| 46 | + val options = SentryOptions().apply { isSendDefaultPii = true } |
| 47 | + |
| 48 | + options.dataCollection.setUserInfo(false) |
| 49 | + |
| 50 | + assertThat(options.dataCollectionResolver.isUserInfo).isFalse() |
| 51 | + |
| 52 | + options.isSendDefaultPii = false |
| 53 | + options.dataCollection.setUserInfo(true) |
| 54 | + |
| 55 | + assertThat(options.dataCollectionResolver.isUserInfo).isTrue() |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + fun `omitted booleans use data collection defaults once namespace is explicit`() { |
| 60 | + val options = SentryOptions().apply { isSendDefaultPii = false } |
| 61 | + |
| 62 | + options.dataCollection.cookies = KeyValueCollectionBehavior.off() |
| 63 | + |
| 64 | + assertThat(options.dataCollectionResolver.isUserInfo).isTrue() |
| 65 | + assertThat(options.dataCollectionResolver.isDatabaseQueryData).isTrue() |
| 66 | + assertThat(options.dataCollectionResolver.isGraphqlDocument).isTrue() |
| 67 | + assertThat(options.dataCollectionResolver.isGraphqlVariables).isTrue() |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + fun `database query data falls back to sendDefaultPii and override takes precedence`() { |
| 72 | + val options = SentryOptions().apply { isSendDefaultPii = true } |
| 73 | + |
| 74 | + assertThat(options.dataCollectionResolver.isDatabaseQueryData).isTrue() |
| 75 | + |
| 76 | + options.dataCollection.setDatabaseQueryData(false) |
| 77 | + |
| 78 | + assertThat(options.dataCollectionResolver.isDatabaseQueryData).isFalse() |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + fun `GraphQL document falls back to sendDefaultPii and override takes precedence`() { |
| 83 | + val options = SentryOptions().apply { isSendDefaultPii = true } |
| 84 | + |
| 85 | + assertThat(options.dataCollectionResolver.isGraphqlDocument).isTrue() |
| 86 | + |
| 87 | + options.dataCollection.graphql.setDocument(false) |
| 88 | + |
| 89 | + assertThat(options.dataCollectionResolver.isGraphqlDocument).isFalse() |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + fun `cookies are off when unset and sendDefaultPii is false`() { |
| 94 | + val options = SentryOptions() |
| 95 | + |
| 96 | + assertThat(options.dataCollectionResolver.cookies).isEqualTo(KeyValueCollectionBehavior.off()) |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + fun `cookies use default deny list when unset and sendDefaultPii is true`() { |
| 101 | + val options = SentryOptions().apply { isSendDefaultPii = true } |
| 102 | + |
| 103 | + assertThat(options.dataCollectionResolver.cookies) |
| 104 | + .isEqualTo(KeyValueCollectionBehavior.denyList()) |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + fun `cookies use default deny list when namespace is explicit`() { |
| 109 | + val options = SentryOptions().apply { isSendDefaultPii = false } |
| 110 | + |
| 111 | + options.dataCollection.setUserInfo(false) |
| 112 | + |
| 113 | + assertThat(options.dataCollectionResolver.cookies) |
| 114 | + .isEqualTo(KeyValueCollectionBehavior.denyList()) |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + fun `cookies override takes precedence over sendDefaultPii`() { |
| 119 | + val options = SentryOptions().apply { isSendDefaultPii = false } |
| 120 | + val behavior = KeyValueCollectionBehavior.allowList("language", "theme") |
| 121 | + |
| 122 | + options.dataCollection.cookies = behavior |
| 123 | + |
| 124 | + assertThat(options.dataCollectionResolver.cookies).isEqualTo(behavior) |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + fun `query params use default deny list when unset`() { |
| 129 | + val options = SentryOptions() |
| 130 | + |
| 131 | + assertThat(options.dataCollectionResolver.queryParams) |
| 132 | + .isEqualTo(KeyValueCollectionBehavior.denyList()) |
| 133 | + } |
| 134 | + |
| 135 | + @Test |
| 136 | + fun `query params override takes precedence`() { |
| 137 | + val options = SentryOptions() |
| 138 | + val behavior = KeyValueCollectionBehavior.allowList("language", "theme") |
| 139 | + |
| 140 | + options.dataCollection.queryParams = behavior |
| 141 | + |
| 142 | + assertThat(options.dataCollectionResolver.queryParams).isEqualTo(behavior) |
| 143 | + } |
| 144 | + |
| 145 | + @Test |
| 146 | + fun `HTTP request headers use default deny list when unset`() { |
| 147 | + val options = SentryOptions() |
| 148 | + |
| 149 | + assertThat(options.dataCollectionResolver.httpRequestHeaders) |
| 150 | + .isEqualTo(KeyValueCollectionBehavior.denyList()) |
| 151 | + } |
| 152 | + |
| 153 | + @Test |
| 154 | + fun `HTTP request headers override takes precedence`() { |
| 155 | + val options = SentryOptions() |
| 156 | + val behavior = KeyValueCollectionBehavior.allowList("content-type") |
| 157 | + |
| 158 | + options.dataCollection.httpHeaders.request = behavior |
| 159 | + |
| 160 | + assertThat(options.dataCollectionResolver.httpRequestHeaders).isEqualTo(behavior) |
| 161 | + } |
| 162 | + |
| 163 | + @Test |
| 164 | + fun `HTTP response headers use default deny list when unset`() { |
| 165 | + val options = SentryOptions() |
| 166 | + |
| 167 | + assertThat(options.dataCollectionResolver.httpResponseHeaders) |
| 168 | + .isEqualTo(KeyValueCollectionBehavior.denyList()) |
| 169 | + } |
| 170 | + |
| 171 | + @Test |
| 172 | + fun `HTTP response headers override takes precedence`() { |
| 173 | + val options = SentryOptions() |
| 174 | + val behavior = KeyValueCollectionBehavior.off() |
| 175 | + |
| 176 | + options.dataCollection.httpHeaders.response = behavior |
| 177 | + |
| 178 | + assertThat(options.dataCollectionResolver.httpResponseHeaders).isEqualTo(behavior) |
| 179 | + } |
| 180 | + |
| 181 | + @Test |
| 182 | + fun `HTTP bodies preserve direction-specific legacy fallbacks when data collection is absent`() { |
| 183 | + val options = SentryOptions() |
| 184 | + |
| 185 | + assertThat(options.dataCollectionResolver.isIncomingRequestBody).isFalse() |
| 186 | + assertThat(options.dataCollectionResolver.isOutgoingRequestBody).isTrue() |
| 187 | + assertThat(options.dataCollectionResolver.isIncomingResponseBody).isTrue() |
| 188 | + assertThat(options.dataCollectionResolver.isOutgoingResponseBody).isFalse() |
| 189 | + |
| 190 | + options.isSendDefaultPii = true |
| 191 | + |
| 192 | + assertThat(options.dataCollectionResolver.isIncomingRequestBody).isTrue() |
| 193 | + assertThat(options.dataCollectionResolver.isOutgoingRequestBody).isTrue() |
| 194 | + assertThat(options.dataCollectionResolver.isIncomingResponseBody).isTrue() |
| 195 | + assertThat(options.dataCollectionResolver.isOutgoingResponseBody).isTrue() |
| 196 | + } |
| 197 | + |
| 198 | + @Test |
| 199 | + fun `explicit empty data collection enables every HTTP body direction`() { |
| 200 | + val options = SentryOptions().apply { dataCollection = DataCollection() } |
| 201 | + |
| 202 | + assertThat(options.dataCollectionResolver.isIncomingRequestBody).isTrue() |
| 203 | + assertThat(options.dataCollectionResolver.isOutgoingRequestBody).isTrue() |
| 204 | + assertThat(options.dataCollectionResolver.isIncomingResponseBody).isTrue() |
| 205 | + assertThat(options.dataCollectionResolver.isOutgoingResponseBody).isTrue() |
| 206 | + } |
| 207 | + |
| 208 | + @Test |
| 209 | + fun `explicit HTTP body set controls every direction`() { |
| 210 | + val options = SentryOptions().apply { isSendDefaultPii = true } |
| 211 | + |
| 212 | + options.dataCollection.httpBodies = |
| 213 | + setOf(HttpBodyType.INCOMING_REQUEST, HttpBodyType.OUTGOING_RESPONSE) |
| 214 | + |
| 215 | + assertThat(options.dataCollectionResolver.isIncomingRequestBody).isTrue() |
| 216 | + assertThat(options.dataCollectionResolver.isOutgoingRequestBody).isFalse() |
| 217 | + assertThat(options.dataCollectionResolver.isIncomingResponseBody).isFalse() |
| 218 | + assertThat(options.dataCollectionResolver.isOutgoingResponseBody).isTrue() |
| 219 | + } |
| 220 | + |
| 221 | + @Test |
| 222 | + fun `GraphQL variables fall back to sendDefaultPii and override takes precedence`() { |
| 223 | + val options = SentryOptions().apply { isSendDefaultPii = true } |
| 224 | + |
| 225 | + assertThat(options.dataCollectionResolver.isGraphqlVariables).isTrue() |
| 226 | + |
| 227 | + options.dataCollection.graphql.setVariables(false) |
| 228 | + |
| 229 | + assertThat(options.dataCollectionResolver.isGraphqlVariables).isFalse() |
| 230 | + } |
| 231 | +} |
0 commit comments