Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,8 @@ gummy-bears-api21 = { module = "com.toasttab.android:gummy-bears-api-21", versio
java8-signature = { module = "org.codehaus.mojo.signature:java18", version.ref = "java8Signature" }

# tomcat libraries
tomcat85-embed-core = { module = "org.apache.tomcat.embed:tomcat-embed-core", version = "8.5.100" }
tomcat9-embed-core = { module = "org.apache.tomcat.embed:tomcat-embed-core", version = "9.0.108" }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should probably extract 9.0.108 to a variable so that all the 9.x versions are updated together.

tomcat-catalina = { module = "org.apache.tomcat:tomcat-catalina", version = "9.0.108" }
tomcat-embed-jasper = { module = "org.apache.tomcat.embed:tomcat-embed-jasper", version = "9.0.108" }
tomcat-catalina-jakarta = { module = "org.apache.tomcat:tomcat-catalina", version = "11.0.22" }
Expand Down
54 changes: 54 additions & 0 deletions sentry-servlet/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,66 @@ dependencies {
testImplementation(projects.sentryTestSupport)
testImplementation(kotlin(Config.kotlinStdLib))
testImplementation(libs.awaitility.kotlin)
testImplementation(libs.google.truth)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this used?

testImplementation(libs.kotlin.test.junit)
testImplementation(libs.mockito.kotlin)
testImplementation(libs.springboot.starter.test)
testImplementation(libs.springboot.starter.web)
}

testing {
suites {
register<JvmTestSuite>("integrationTest") {
useJUnit()
dependencies {
implementation(project())
implementation(projects.sentryTestSupport)
implementation(libs.google.truth)
implementation(libs.kotlin.test.junit)
implementation(libs.tomcat85.embed.core)
}
targets.all {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

another option that would mean we don't have to compile the test twice is to adjust this to have a single test suite but with two targets. the two targets are tomcat 8.5 and tomcat 9.

here's what my agent thinks would work:

register<JvmTestSuite>("integrationTest") {
      useJUnit()
      dependencies {
        implementation(project())
        implementation(projects.sentryTestSupport)
        implementation(libs.google.truth)
        implementation(libs.kotlin.test.junit)
        implementation(libs.tomcat85.embed.core)
      }
      targets.named("integrationTest") {
        testTask.configure { /* Tomcat 8.5 system properties */ }
      }
      targets.register("integrationTestTomcat9") {
        testTask.configure {
          classpath = sourceSets["integrationTest"].output + tomcat9RuntimeClasspath
          /* Tomcat 9 system properties */
        }
      }
    }

Its mostly an opinion though. i'll approve this anyways.

testTask.configure {
description = "Runs the servlet integration test on Tomcat 8.5 and Servlet 3.1"
systemProperty("io.sentry.test.tomcat-version-prefix", "8.5.")
systemProperty("io.sentry.test.servlet-major-version", "3")
systemProperty("io.sentry.test.servlet-minor-version", "1")
}
}
}

register<JvmTestSuite>("integrationTestTomcat9") {
useJUnit()
dependencies {
implementation(project())
implementation(projects.sentryTestSupport)
implementation(libs.google.truth)
implementation(libs.kotlin.test.junit)
implementation(libs.tomcat9.embed.core)
}
sources {
java.setSrcDirs(sourceSets["integrationTest"].allJava.srcDirs)
resources.setSrcDirs(sourceSets["integrationTest"].resources.srcDirs)
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tomcat 9 suite skips Kotlin tests

Medium Severity

The integrationTestTomcat9 suite copies integrationTest's allJava.srcDirs, which is src/integrationTest/java, not the Kotlin tests in src/integrationTest/kotlin. Kotlin still compiles src/integrationTestTomcat9/kotlin, so that suite produces no classes and Gradle skips the task as NO-SOURCE. Tomcat 9 / Servlet 4.0 never actually runs.

Fix in Cursor Fix in Web

Triggered by project rule: PR Review Guidelines for Cursor Bugbot

Reviewed by Cursor Bugbot for commit bc1635c. Configure here.

targets.all {
testTask.configure {
description = "Runs the servlet integration test on Tomcat 9 and Servlet 4"
systemProperty("io.sentry.test.tomcat-version-prefix", "9.0.")
systemProperty("io.sentry.test.servlet-major-version", "4")
systemProperty("io.sentry.test.servlet-minor-version", "0")
}
}
}
}
}

tasks.check {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't think we run integration tests as part of the check task for other modules. we usually run them separately

dependsOn(
testing.suites.named("integrationTest"),
testing.suites.named("integrationTestTomcat9"),
)
}

tasks.withType<JavaCompile>().configureEach {
options.errorprone {
check("NullAway", net.ltgt.gradle.errorprone.CheckSeverity.ERROR)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package io.sentry.servlet

import com.google.common.truth.Truth.assertThat
import io.sentry.Sentry
import io.sentry.test.MockSentryServer
import io.sentry.test.event
import java.net.HttpURLConnection
import java.net.URI
import java.nio.file.Files
import java.util.ServiceLoader
import javax.servlet.ServletContainerInitializer
import javax.servlet.ServletContext
import javax.servlet.http.HttpServlet
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
import kotlin.test.AfterTest
import kotlin.test.BeforeTest
import kotlin.test.Test
import org.apache.catalina.startup.Tomcat
import org.apache.catalina.util.ServerInfo

class SentryServletIntegrationTest {
private val servletMajorVersion =
System.getProperty("io.sentry.test.servlet-major-version", "3").toInt()
private val servletMinorVersion =
System.getProperty("io.sentry.test.servlet-minor-version", "1").toInt()
private val tomcatVersionPrefix =
System.getProperty("io.sentry.test.tomcat-version-prefix", "8.5.")

private var server: MockSentryServer? = null
private var servletContext: ServletContext? = null
private var tomcat: Tomcat? = null

@BeforeTest
fun setUp() {
val mockServer = MockSentryServer()
server = mockServer
Sentry.init { it.dsn = mockServer.dsn }

tomcat =
Tomcat().apply {
setBaseDir(Files.createTempDirectory("tomcat").toString())
setPort(0)
connector.setProperty("address", "127.0.0.1")

val context = addContext("", Files.createTempDirectory("servlet-docbase").toString())
context.effectiveMajorVersion = servletMajorVersion
context.effectiveMinorVersion = servletMinorVersion
context.parentClassLoader = javaClass.classLoader
Tomcat.addServlet(context, "capture", CaptureServlet())
context.addServletMappingDecoded("/capture", "capture")
val initializer =
ServiceLoader.load(ServletContainerInitializer::class.java)
.filterIsInstance<SentryServletContainerInitializer>()
.single()
context.addServletContainerInitializer(initializer, emptySet())

start()
servletContext = context.servletContext
}
}

@AfterTest
fun tearDown() {
Sentry.close()
tomcat?.stop()
tomcat?.destroy()
server?.close()
}

@Test
fun `captures request data on supported servlet container`() {
assertThat(ServerInfo.getServerNumber()).startsWith(tomcatVersionPrefix)
assertThat(servletContext!!.effectiveMajorVersion).isEqualTo(servletMajorVersion)
assertThat(servletContext!!.effectiveMinorVersion).isEqualTo(servletMinorVersion)

val port = tomcat!!.connector.localPort
val connection =
URI("http://127.0.0.1:$port/capture?customer=123").toURL().openConnection()
as HttpURLConnection

try {
assertThat(connection.responseCode).isEqualTo(HttpServletResponse.SC_OK)
assertThat(connection.inputStream.bufferedReader().use { it.readText().trim() })
.isEqualTo("captured")
} finally {
connection.disconnect()
}

val event = server!!.awaitEnvelope { it.event()?.message?.formatted == MESSAGE }.event()!!

assertThat(event.request?.method).isEqualTo("GET")
assertThat(event.request?.url).isEqualTo("http://127.0.0.1:$port/capture")
assertThat(event.request?.queryString).isEqualTo("customer=123")
}

private class CaptureServlet : HttpServlet() {
override fun doGet(request: HttpServletRequest, response: HttpServletResponse) {
Sentry.captureMessage(MESSAGE)
response.writer.print("captured")
}
}

private companion object {
const val MESSAGE = "captured from servlet integration test"
}
}
Loading