Skip to content

Commit 3a6fa4f

Browse files
adinauerclaude
andcommitted
test(graphql): Cover GraphqlUtils request body filtering
Add focused coverage for parsing a single GraphQL request object and independently removing document and variable content while preserving operation metadata and allowed fields. Refs #5666 Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 9f37039 commit 3a6fa4f

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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

Comments
 (0)