Skip to content

Commit a6f2ff6

Browse files
authored
rework symlink test (apache#445) (apache#451)
* rework symlink test * add test that uses temp file symbolic link (still not fully working) * Update FileAndResourceDirectivesSymlinkSpec.scala * Update FileAndResourceDirectivesSymlinkSpec.scala
1 parent 5692814 commit a6f2ff6

File tree

3 files changed

+73
-11
lines changed

3 files changed

+73
-11
lines changed

http-tests/src/test/resources/dirWithLink/linked-dir

Lines changed: 0 additions & 1 deletion
This file was deleted.

http-tests/src/test/scala/org/apache/pekko/http/scaladsl/server/directives/FileAndResourceDirectivesSpec.scala

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -212,16 +212,6 @@ class FileAndResourceDirectivesSpec extends RoutingSpec with Inspectors with Ins
212212
headers should contain(`Last-Modified`(DateTime(lastModified)))
213213
}
214214
}
215-
"not follow symbolic links to find a file" in {
216-
EventFilter.warning(pattern = ".* points to a location that is not part of .*", occurrences = 1).intercept {
217-
Get("linked-dir/empty.pdf") ~> _getFromDirectory("dirWithLink") ~> check {
218-
handled shouldBe false
219-
/* TODO: resurrect following links under an option
220-
responseAs[String] shouldEqual "123"
221-
mediaType shouldEqual `application/pdf`*/
222-
}
223-
}
224-
}
225215
}
226216

227217
"getFromResource" should {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* license agreements; and to You under the Apache License, version 2.0:
4+
*
5+
* https://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* This file is part of the Apache Pekko project, which was derived from Akka.
8+
*/
9+
10+
/*
11+
* Copyright (C) 2009-2022 Lightbend Inc. <https://www.lightbend.com>
12+
*/
13+
14+
package org.apache.pekko.http.scaladsl.server
15+
package directives
16+
17+
import java.io.File
18+
import java.nio.file.{ Files, Paths }
19+
20+
import scala.concurrent.duration._
21+
22+
import org.apache.pekko
23+
import pekko.http.scaladsl.testkit.RouteTestTimeout
24+
import pekko.testkit._
25+
26+
import org.scalatest.{ BeforeAndAfterAll, Inside, Inspectors }
27+
28+
class FileAndResourceDirectivesSymlinkSpec extends RoutingSpec
29+
with Inspectors with Inside with BeforeAndAfterAll {
30+
31+
// need to serve from the src directory, when sbt copies the resource directory over to the
32+
// target directory it will resolve symlinks in the process
33+
val testRoot = new File("http-tests/src/test/resources")
34+
require(testRoot.exists(), s"testRoot was not found at ${testRoot.getAbsolutePath}")
35+
36+
val tempDir = Files.createTempDirectory("pekko-http-symlink-test")
37+
tempDir.toFile.deleteOnExit()
38+
val dirWithLink = new File(tempDir.toFile, "dirWithLink")
39+
dirWithLink.mkdir()
40+
val symlink = Files.createSymbolicLink(
41+
Paths.get(dirWithLink.getAbsolutePath, "linked-dir"),
42+
new File(testRoot, "subDirectory").toPath.toAbsolutePath)
43+
44+
override def afterAll(): Unit = {
45+
super.afterAll()
46+
Files.deleteIfExists(symlink)
47+
Files.deleteIfExists(dirWithLink.toPath)
48+
Files.deleteIfExists(tempDir)
49+
}
50+
51+
// operations touch files, can be randomly hit by slowness
52+
implicit val routeTestTimeout: RouteTestTimeout = RouteTestTimeout(3.seconds.dilated)
53+
54+
override def testConfigSource = super.testConfigSource ++ """
55+
pekko.http.routing.range-coalescing-threshold = 1
56+
"""
57+
58+
"getFromDirectory" should {
59+
def _getFromDirectory() = getFromDirectory(dirWithLink.getCanonicalPath)
60+
61+
"not follow symbolic links to find a file" in {
62+
Files.isSymbolicLink(symlink) shouldBe true
63+
EventFilter.warning(pattern = ".* points to a location that is not part of .*", occurrences = 1).intercept {
64+
Get("linked-dir/empty.pdf") ~> _getFromDirectory() ~> check {
65+
handled shouldBe false
66+
/* TODO: resurrect following links under an option
67+
responseAs[String] shouldEqual "123"
68+
mediaType shouldEqual `application/pdf`*/
69+
}
70+
}
71+
}
72+
}
73+
}

0 commit comments

Comments
 (0)