Repository navigation
test(servlet): Test Servlet 3.1 and 4.0 runtimes #6625
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: chore/servlet-api-compatibility-stack
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,12 +35,66 @@ dependencies { | |
| testImplementation(projects.sentryTestSupport) | ||
| testImplementation(kotlin(Config.kotlinStdLib)) | ||
| testImplementation(libs.awaitility.kotlin) | ||
| testImplementation(libs.google.truth) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: 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) | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tomcat 9 suite skips Kotlin testsMedium Severity The 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 { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
|
||
| 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" | ||
| } | ||
| } |


There was a problem hiding this comment.
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.