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