fix HeavyBuiltInWebServerTest
[idea/community.git] / platform / built-in-server / testSrc / BuiltInWebServerTest.kt
1 package org.jetbrains.ide
2
3 import com.google.common.net.UrlEscapers
4 import com.intellij.openapi.application.runWriteAction
5 import com.intellij.openapi.module.EmptyModuleType
6 import com.intellij.openapi.module.ModuleManager
7 import com.intellij.openapi.project.Project
8 import com.intellij.openapi.roots.ModuleRootModificationUtil
9 import com.intellij.openapi.util.io.FileUtil
10 import com.intellij.openapi.util.text.StringUtil
11 import com.intellij.openapi.vfs.LocalFileSystem
12 import com.intellij.testFramework.*
13 import com.intellij.util.*
14 import io.netty.handler.codec.http.HttpResponseStatus
15 import org.assertj.core.api.Assertions.assertThat
16 import org.junit.ClassRule
17 import org.junit.Rule
18 import org.junit.Test
19
20 internal class BuiltInWebServerTest : BuiltInServerTestCase() {
21   override val urlPathPrefix: String
22     get() = "/${projectRule.project.name}"
23
24   @Test
25   @TestManager.TestDescriptor(filePath = "foo/index.html", doNotCreate = true, status = 200)
26   fun `get only dir without end slash`() {
27     testIndex("foo")
28   }
29
30   @Test
31   @TestManager.TestDescriptor(filePath = "foo/index.html", doNotCreate = true, status = 200)
32   fun `get only dir with end slash`() {
33     testIndex("foo/")
34   }
35
36   @Test
37   @TestManager.TestDescriptor(filePath = "foo/index.html", doNotCreate = true, status = 200)
38   fun `get index file and then dir`() {
39     testIndex("foo/index.html", "foo")
40   }
41
42   private fun testIndex(vararg paths: String) {
43     val project = projectRule.project
44     val newPath = tempDirManager.newPath()
45     newPath.writeChild(manager.filePath!!, "hello")
46     newPath.refreshVfs()
47
48     createModule(newPath.systemIndependentPath, project)
49
50     for (path in paths) {
51       doTest(path) {
52         assertThat(it.inputStream.reader().readText()).isEqualTo("hello")
53       }
54     }
55   }
56 }
57
58 private fun createModule(systemIndependentPath: String, project: Project) {
59   runInEdtAndWait {
60     runWriteAction {
61       val module = ModuleManager.getInstance(project).newModule("$systemIndependentPath/test.iml", EmptyModuleType.EMPTY_MODULE)
62       ModuleRootModificationUtil.addContentRoot(module, systemIndependentPath)
63     }
64   }
65 }
66
67 internal class HeavyBuiltInWebServerTest {
68   companion object {
69     @JvmField
70     @ClassRule val appRule = ProjectRule()
71   }
72
73   @Rule
74   @JvmField
75   val tempDirManager = TemporaryDirectory()
76
77   @Test
78   fun `path outside of project`() {
79     val projectDir = tempDirManager.newPath().resolve("foo/bar")
80     val projectDirPath = projectDir.systemIndependentPath
81     createHeavyProject("$projectDirPath/test.ipr").use { project ->
82       projectDir.createDirectories()
83       LocalFileSystem.getInstance().refreshAndFindFileByPath(projectDirPath)
84       createModule(projectDirPath, project)
85
86       val path = tempDirManager.newPath("doNotExposeMe.txt").write("doNotExposeMe").systemIndependentPath
87       val relativePath = FileUtil.getRelativePath(project.basePath!!, path, '/')
88       val webPath = StringUtil.replace(UrlEscapers.urlPathSegmentEscaper().escape("${project.name}/$relativePath"), "%2F", "/")
89       testUrl("http://localhost:${BuiltInServerManager.getInstance().port}/$webPath", HttpResponseStatus.NOT_FOUND)
90     }
91   }
92
93   @Test
94   fun `file in hidden folder`() {
95     val projectDir = tempDirManager.newPath().resolve("foo/bar")
96     val projectDirPath = projectDir.systemIndependentPath
97     createHeavyProject("$projectDirPath/test.ipr").use { project ->
98       projectDir.createDirectories()
99       LocalFileSystem.getInstance().refreshAndFindFileByPath(projectDirPath)
100       createModule(projectDirPath, project)
101
102       val dir = projectDir.resolve(".coverage")
103       dir.createDirectories()
104       val path = dir.resolve("foo").write("exposeMe").systemIndependentPath
105       val relativePath = FileUtil.getRelativePath(project.basePath!!, path, '/')
106       val webPath = StringUtil.replace(UrlEscapers.urlPathSegmentEscaper().escape("${project.name}/$relativePath"), "%2F", "/")
107       testUrl("http://localhost:${BuiltInServerManager.getInstance().port}/$webPath", HttpResponseStatus.OK)
108     }
109   }
110 }