1 package org.jetbrains.ide
3 import com.intellij.testFramework.DisposeModulesRule
4 import com.intellij.testFramework.ProjectRule
5 import com.intellij.testFramework.RuleChain
6 import com.intellij.testFramework.TemporaryDirectory
7 import io.netty.handler.codec.http.HttpResponseStatus
8 import org.assertj.core.api.Assertions.assertThat
9 import org.junit.ClassRule
11 import org.junit.rules.Timeout
12 import java.net.HttpURLConnection
14 import java.util.concurrent.TimeUnit
16 internal abstract class BuiltInServerTestCase {
19 @ClassRule val projectRule = ProjectRule()
22 protected val tempDirManager = TemporaryDirectory()
23 protected val manager = TestManager(projectRule, tempDirManager)
25 private val ruleChain = RuleChain(
27 Timeout(60, TimeUnit.SECONDS),
29 DisposeModulesRule(projectRule))
30 @Rule fun getChain() = ruleChain
32 protected open val urlPathPrefix = ""
34 protected fun doTest(filePath: String? = manager.filePath, additionalCheck: ((connection: HttpURLConnection) -> Unit)? = null) {
35 val serviceUrl = "http://localhost:${BuiltInServerManager.getInstance().port}$urlPathPrefix"
36 var url = serviceUrl + (if (filePath == null) "" else ("/$filePath"))
37 val line = manager.annotation?.line ?: -1
41 val column = manager.annotation?.column ?: -1
46 val expectedStatus = HttpResponseStatus.valueOf(manager.annotation?.status ?: 200)
47 val connection = testUrl(url, expectedStatus)
48 check(serviceUrl, expectedStatus)
49 additionalCheck?.invoke(connection)
52 protected open fun check(serviceUrl: String, expectedStatus: HttpResponseStatus) {
56 internal fun testUrl(url: String, expectedStatus: HttpResponseStatus): HttpURLConnection {
57 val connection = URL(url).openConnection() as HttpURLConnection
58 assertThat(HttpResponseStatus.valueOf(connection.responseCode)).isEqualTo(expectedStatus)