mirror of
https://github.com/apple/pkl.git
synced 2026-04-20 07:21:32 +02:00
Follow HTTP redirects (#328)
- Change HttpClient to follow all redirects except HTTPS to HTTP. - Run language snippet tests with --no-cache and real PackageServer instead of pre-seeded cache. This increases HTTP test coverage and enables testing of package redirects. - Change PackageServer to return 301 for request paths starting with /HTTP301/ and 307 for request paths starting with /HTTP307/. - Update some outdated test package checksums that apparently weren't verified.
This commit is contained in:
@@ -22,6 +22,7 @@ import java.lang.invoke.MethodHandles;
|
||||
import java.lang.invoke.MethodType;
|
||||
import java.net.ConnectException;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient.Redirect;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.net.http.HttpResponse.BodyHandler;
|
||||
@@ -81,6 +82,7 @@ final class JdkHttpClient implements HttpClient {
|
||||
java.net.http.HttpClient.newBuilder()
|
||||
.sslContext(createSslContext(certificateFiles, certificateUris))
|
||||
.connectTimeout(connectTimeout)
|
||||
.followRedirects(Redirect.NORMAL)
|
||||
.build();
|
||||
}
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ examples {
|
||||
import("package://localhost:0/birds@0.5.0#/catalog/Ostritch.pkl")
|
||||
}
|
||||
["importing while specifying checksum"] {
|
||||
import("package://localhost:0/birds@0.5.0::sha256:3f19ab9fcee2f44f93a75a09e531db278c6d2cd25206836c8c2c4071cd7d3118#/catalog/Swallow.pkl")
|
||||
import("package://localhost:0/birds@0.5.0::sha256:bfaf5281613d170a740505cc87561041f4e0cad1f0e6938bf94f7609f9a4673d#/catalog/Swallow.pkl")
|
||||
}
|
||||
["reads"] {
|
||||
read("package://localhost:0/birds@0.5.0#/Bird.pkl")
|
||||
|
||||
@@ -20,6 +20,6 @@ examples {
|
||||
import("package://localhost:0/birds@0.5.0#/allFruit.pkl").fruitFiles
|
||||
}
|
||||
["glob import while specifying checksum"] {
|
||||
import*("package://localhost:0/birds@0.5.0::sha256:3f19ab9fcee2f44f93a75a09e531db278c6d2cd25206836c8c2c4071cd7d3118#/catalog/*.pkl")
|
||||
import*("package://localhost:0/birds@0.5.0::sha256:bfaf5281613d170a740505cc87561041f4e0cad1f0e6938bf94f7609f9a4673d#/catalog/*.pkl")
|
||||
}
|
||||
}
|
||||
|
||||
15
pkl-core/src/test/files/LanguageSnippetTests/input/packages/redirects.pkl
vendored
Normal file
15
pkl-core/src/test/files/LanguageSnippetTests/input/packages/redirects.pkl
vendored
Normal file
@@ -0,0 +1,15 @@
|
||||
amends ".../snippetTest.pkl"
|
||||
|
||||
examples {
|
||||
["permanent redirect is followed"] {
|
||||
import("package://localhost:0/HTTP301/birds@0.5.0#/catalog/Swallow.pkl")
|
||||
}
|
||||
|
||||
["temporary redirect is followed"] {
|
||||
import("package://localhost:0/HTTP307/birds@0.5.0#/catalog/Swallow.pkl")
|
||||
}
|
||||
|
||||
["double redirect is followed"] {
|
||||
import("package://localhost:0/HTTP301/HTTP307/birds@0.5.0#/catalog/Swallow.pkl")
|
||||
}
|
||||
}
|
||||
@@ -118,13 +118,13 @@ examples {
|
||||
}
|
||||
["glob import while specifying checksum"] {
|
||||
new {
|
||||
["package://localhost:0/birds@0.5.0::sha256:3f19ab9fcee2f44f93a75a09e531db278c6d2cd25206836c8c2c4071cd7d3118#/catalog/Ostritch.pkl"] {
|
||||
["package://localhost:0/birds@0.5.0::sha256:bfaf5281613d170a740505cc87561041f4e0cad1f0e6938bf94f7609f9a4673d#/catalog/Ostritch.pkl"] {
|
||||
name = "Ostritch"
|
||||
favoriteFruit {
|
||||
name = "Orange"
|
||||
}
|
||||
}
|
||||
["package://localhost:0/birds@0.5.0::sha256:3f19ab9fcee2f44f93a75a09e531db278c6d2cd25206836c8c2c4071cd7d3118#/catalog/Swallow.pkl"] {
|
||||
["package://localhost:0/birds@0.5.0::sha256:bfaf5281613d170a740505cc87561041f4e0cad1f0e6938bf94f7609f9a4673d#/catalog/Swallow.pkl"] {
|
||||
name = "Swallow"
|
||||
favoriteFruit {
|
||||
name = "Apple"
|
||||
|
||||
26
pkl-core/src/test/files/LanguageSnippetTests/output/packages/redirects.pcf
vendored
Normal file
26
pkl-core/src/test/files/LanguageSnippetTests/output/packages/redirects.pcf
vendored
Normal file
@@ -0,0 +1,26 @@
|
||||
examples {
|
||||
["permanent redirect is followed"] {
|
||||
new {
|
||||
name = "Swallow"
|
||||
favoriteFruit {
|
||||
name = "Apple"
|
||||
}
|
||||
}
|
||||
}
|
||||
["temporary redirect is followed"] {
|
||||
new {
|
||||
name = "Swallow"
|
||||
favoriteFruit {
|
||||
name = "Apple"
|
||||
}
|
||||
}
|
||||
}
|
||||
["double redirect is followed"] {
|
||||
new {
|
||||
name = "Swallow"
|
||||
favoriteFruit {
|
||||
name = "Apple"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,9 +4,12 @@ import org.junit.platform.engine.EngineDiscoveryRequest
|
||||
import org.junit.platform.engine.TestDescriptor
|
||||
import org.junit.platform.engine.UniqueId
|
||||
import org.junit.platform.engine.support.descriptor.EngineDescriptor
|
||||
import org.pkl.commons.test.FileTestUtils
|
||||
import org.pkl.commons.test.InputOutputTestEngine
|
||||
import org.pkl.commons.test.PackageServer
|
||||
import org.pkl.core.http.HttpClient
|
||||
import org.pkl.core.project.Project
|
||||
import org.pkl.core.util.IoUtils
|
||||
import java.io.PrintWriter
|
||||
import java.io.StringWriter
|
||||
import java.nio.file.Files
|
||||
@@ -33,6 +36,8 @@ abstract class AbstractLanguageSnippetTestsEngine : InputOutputTestEngine() {
|
||||
//language=regexp
|
||||
internal val selection: String = ""
|
||||
|
||||
protected val packageServer: PackageServer = PackageServer()
|
||||
|
||||
override val includedTests: List<Regex> = listOf(Regex(".*$selection\\.pkl"))
|
||||
|
||||
override val excludedTests: List<Regex> = listOf(Regex(".*/native/.*"))
|
||||
@@ -41,11 +46,6 @@ abstract class AbstractLanguageSnippetTestsEngine : InputOutputTestEngine() {
|
||||
|
||||
override val isInputFile: (Path) -> Boolean = { it.isRegularFile() }
|
||||
|
||||
protected val cacheDir: Path by lazy {
|
||||
rootProjectDir.resolve("pkl-core/build/packages-cache")
|
||||
.also { PackageServer.populateCacheDir(it) }
|
||||
}
|
||||
|
||||
protected tailrec fun Path.getProjectDir(): Path? =
|
||||
if (Files.exists(this.resolve("PklProject"))) this
|
||||
else parent?.getProjectDir()
|
||||
@@ -58,6 +58,15 @@ abstract class AbstractLanguageSnippetTestsEngine : InputOutputTestEngine() {
|
||||
return expectedOutputDir.resolve(stdoutPath)
|
||||
}
|
||||
|
||||
override fun beforeAll() {
|
||||
// disable SHA verification for packages
|
||||
IoUtils.setTestMode()
|
||||
}
|
||||
|
||||
override fun afterAll() {
|
||||
packageServer.close()
|
||||
}
|
||||
|
||||
protected fun String.stripFilePaths() = replace(snippetsDir.toString(), "/\$snippetsDir")
|
||||
|
||||
protected fun String.stripLineNumbers() = replace(lineNumberRegex) { result ->
|
||||
@@ -92,7 +101,11 @@ class LanguageSnippetTestsEngine : AbstractLanguageSnippetTestsEngine() {
|
||||
"name2" to "value2",
|
||||
"/foo/bar" to "foobar"
|
||||
))
|
||||
.setModuleCacheDir(cacheDir)
|
||||
.setModuleCacheDir(null)
|
||||
.setHttpClient(HttpClient.builder()
|
||||
.setTestPort(packageServer.port)
|
||||
.addCertificates(FileTestUtils.selfSignedCertificate)
|
||||
.buildLazily())
|
||||
}
|
||||
|
||||
override val testClass: KClass<*> = LanguageSnippetTests::class
|
||||
@@ -158,8 +171,7 @@ abstract class AbstractNativeLanguageSnippetTestsEngine : AbstractLanguageSnippe
|
||||
val args = buildList {
|
||||
add(pklExecutablePath.toString())
|
||||
add("eval")
|
||||
add("--cache-dir")
|
||||
add(cacheDir.toString())
|
||||
add("--no-cache")
|
||||
if (inputFile.startsWith(projectsDir)) {
|
||||
val projectDir = inputFile.getProjectDir()
|
||||
if (projectDir != null) {
|
||||
@@ -187,7 +199,11 @@ abstract class AbstractNativeLanguageSnippetTestsEngine : AbstractLanguageSnippe
|
||||
}
|
||||
add("--settings")
|
||||
add("pkl:settings")
|
||||
add("--ca-certificates")
|
||||
add(FileTestUtils.selfSignedCertificate.toString())
|
||||
add("--test-mode")
|
||||
add("--test-port")
|
||||
add(packageServer.port.toString())
|
||||
add(inputFile.toString())
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user