|
| 1 | +package io.sentry.util |
| 2 | + |
| 3 | +import com.google.common.truth.Truth.assertThat |
| 4 | +import io.sentry.JsonObjectReader |
| 5 | +import io.sentry.SentryOptions |
| 6 | +import java.io.StringReader |
| 7 | +import kotlin.test.Test |
| 8 | + |
| 9 | +class GraphqlUtilsTest { |
| 10 | + @Test |
| 11 | + fun `filters document from a GraphQL request body`() { |
| 12 | + val options = SentryOptions().also { it.dataCollection.graphql.setDocument(false) } |
| 13 | + |
| 14 | + val result = GraphqlUtils.filterRequestBody(REQUEST_BODY, options) |
| 15 | + |
| 16 | + JsonObjectReader(StringReader(result)).use { reader -> |
| 17 | + @Suppress("UNCHECKED_CAST") val body = reader.nextObjectOrNull() as Map<String, Any> |
| 18 | + assertThat(body).containsEntry("operationName", "GetUser") |
| 19 | + assertThat(body).containsEntry("variables", mapOf("id" to "123")) |
| 20 | + assertThat(body).doesNotContainKey("query") |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + @Test |
| 25 | + fun `filters variables from a GraphQL request body`() { |
| 26 | + val options = SentryOptions().also { it.dataCollection.graphql.setVariables(false) } |
| 27 | + |
| 28 | + val result = GraphqlUtils.filterRequestBody(REQUEST_BODY, options) |
| 29 | + |
| 30 | + JsonObjectReader(StringReader(result)).use { reader -> |
| 31 | + @Suppress("UNCHECKED_CAST") val body = reader.nextObjectOrNull() as Map<String, Any> |
| 32 | + assertThat(body).containsEntry("operationName", "GetUser") |
| 33 | + assertThat(body).containsEntry("query", "query { viewer { name } }") |
| 34 | + assertThat(body).doesNotContainKey("variables") |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + private companion object { |
| 39 | + const val REQUEST_BODY = |
| 40 | + """{"operationName":"GetUser","variables":{"id":"123"},"query":"query { viewer { name } }"}""" |
| 41 | + } |
| 42 | +} |
0 commit comments