|
| 1 | +package io.sentry.spring7.integrationtest |
| 2 | + |
| 3 | +import io.sentry.test.MockSentryServer |
| 4 | +import jakarta.servlet.ServletContainerInitializer |
| 5 | +import java.nio.file.Files |
| 6 | +import java.util.ServiceLoader |
| 7 | +import org.apache.catalina.startup.Tomcat |
| 8 | +import org.junit.Before |
| 9 | +import org.springframework.http.client.ClientHttpResponse |
| 10 | +import org.springframework.web.client.DefaultResponseErrorHandler |
| 11 | +import org.springframework.web.client.RestTemplate |
| 12 | +import org.springframework.web.util.DefaultUriBuilderFactory |
| 13 | + |
| 14 | +/** |
| 15 | + * Drives [AppInitializer]'s app over HTTP and asserts what reaches [server]. |
| 16 | + * |
| 17 | + * Subclasses share one app and one server. Envelopes that arrive late can outlive [server]'s reset, |
| 18 | + * so tests match on values only they produce. |
| 19 | + */ |
| 20 | +abstract class IntegrationTest { |
| 21 | + val server = TomcatApp.server |
| 22 | + val restTemplate = TomcatApp.restTemplate |
| 23 | + |
| 24 | + @Before |
| 25 | + fun resetServer() { |
| 26 | + server.reset() |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +/** |
| 31 | + * The app in an embedded Tomcat, with no Spring Boot on the classpath, so Sentry takes the same |
| 32 | + * path as in a WAR. Tomcat starts once per test JVM. |
| 33 | + */ |
| 34 | +object TomcatApp { |
| 35 | + val server = MockSentryServer() |
| 36 | + |
| 37 | + init { |
| 38 | + System.setProperty("sentry.dsn", server.dsn) |
| 39 | + // The profiler rejects a traces path with a dot anywhere in it, which the module's absolute |
| 40 | + // path can have. |
| 41 | + System.setProperty( |
| 42 | + "sentry.profiling-traces-dir-path", |
| 43 | + "${System.getProperty("java.io.tmpdir")}/sentry-integration-test/profiling-traces", |
| 44 | + ) |
| 45 | + } |
| 46 | + |
| 47 | + private val tomcat = |
| 48 | + Tomcat().apply { |
| 49 | + setBaseDir(Files.createTempDirectory("tomcat").toString()) |
| 50 | + setPort(0) |
| 51 | + // Creates the default connector. |
| 52 | + connector |
| 53 | + val context = addContext("", Files.createTempDirectory("docbase").toString()) |
| 54 | + // As Tomcat does for a WAR, runs each initializer the classpath registers: Spring's starts |
| 55 | + // AppInitializer's app, and Sentry's adds SentrySpringFilter. Only Spring's uses the set. |
| 56 | + ServiceLoader.load(ServletContainerInitializer::class.java).forEach { |
| 57 | + context.addServletContainerInitializer(it, setOf(AppInitializer::class.java)) |
| 58 | + } |
| 59 | + start() |
| 60 | + } |
| 61 | + |
| 62 | + val restTemplate = |
| 63 | + RestTemplate().apply { |
| 64 | + uriTemplateHandler = |
| 65 | + DefaultUriBuilderFactory("http://localhost:${tomcat.connector.localPort}") |
| 66 | + // Returns error responses rather than throwing them, so tests can check a failed request. |
| 67 | + errorHandler = |
| 68 | + object : DefaultResponseErrorHandler() { |
| 69 | + override fun hasError(response: ClientHttpResponse): Boolean = false |
| 70 | + } |
| 71 | + } |
| 72 | +} |
0 commit comments