Files
pkl/pkl-commons-test/pkl-commons-test.gradle.kts
translatenix 3f3dfdeb1e Use java.net.http.HttpClient instead of java.net.Http(s)URLConnection (#217)
Moving to java.net.http.HttpClient brings many benefits, including
HTTP/2 support and the ability to make asynchronous requests.

Major additions and changes:
- Introduce a lightweight org.pkl.core.http.HttpClient API.
  This keeps some flexibility and allows to enforce behavior
  such as setting the User-Agent header.
- Provide an implementation that delegates to java.net.http.HttpClient.
- Use HttpClient for all HTTP(s) requests across the codebase.
  This required adding an HttpClient parameter to constructors and
  factory methods of multiple classes, some of which are public APIs.
- Manage CA certificates per HTTP client instead of per JVM.
  This makes it unnecessary to set JVM-wide system/security properties
  and default SSLSocketFactory's.
- Add executor v2 options to the executor SPI
- Add pkl-certs as a new artifact, and remove certs from pkl-commons-cli artifact

Each HTTP client maintains its own connection pool and SSLContext.
For efficiency reasons, It's best to reuse clients whenever feasible.
To avoid memory leaks, clients are not stored in static fields.

HTTP clients are expensive to create. For this reason,
EvaluatorBuilder defaults to a "lazy" client that creates the underlying
java.net.http.HttpClient on the first send (which may never happen).
2024-03-06 10:25:56 -08:00

132 lines
3.7 KiB
Kotlin

import java.security.MessageDigest
plugins {
pklAllProjects
pklKotlinLibrary
}
// note: no need to publish this library
dependencies {
api(libs.junitApi)
api(libs.junitEngine)
api(libs.junitParams)
api(projects.pklCommons) // for convenience
implementation(libs.assertj)
runtimeOnly(projects.pklCerts)
}
/**
* Creates test packages from the `src/test/files/packages` directory.
*
* These packages are used by PackageServer to serve assets when running
* LanguageSnippetTests and PackageResolversTest.
*/
val createTestPackages = tasks.create("createTestPackages")
fun toHex(hash: ByteArray): String {
val hexDigitTable = charArrayOf('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f')
val builder = StringBuilder(hash.size * 2)
for (b in hash) {
builder.append(hexDigitTable[b.toInt() shr 4 and 0xF])
builder.append(hexDigitTable[b.toInt() and 0xF])
}
return builder.toString()
}
fun File.computeChecksum(): String {
val md = MessageDigest.getInstance("SHA-256")
val hash = md.digest(readBytes())
return toHex(hash)
}
tasks.processResources {
dependsOn(createTestPackages)
dependsOn(generateCerts)
}
val mainSourceSet by sourceSets.named("main") {
resources {
srcDir(buildDir.resolve("test-packages/"))
srcDir(buildDir.resolve("keystore/"))
}
}
val sourcesJar = tasks.named("sourcesJar").get()
for (packageDir in file("src/main/files/packages").listFiles()!!) {
if (!packageDir.isDirectory) continue
val destinationDir = buildDir.resolve("test-packages/org/pkl/commons/test/packages/${packageDir.name}")
val metadataJson = packageDir.resolve("${packageDir.name}.json")
val packageContents = packageDir.resolve("package")
val zipFileName = "${packageDir.name}.zip"
val archiveFile = destinationDir.resolve(zipFileName)
tasks.create("zip-${packageDir.name}", Zip::class) {
archiveFileName.set(zipFileName)
from(packageContents)
destinationDirectory.set(destinationDir)
// required so that checksums are reproducible
isPreserveFileTimestamps = false
isReproducibleFileOrder = true
}
val copyTask = tasks.create("copy-${packageDir.name}", Copy::class) {
dependsOn("zip-${packageDir.name}")
from(metadataJson)
into(destinationDir)
val shasumFile = file("$destinationDir/${packageDir.name}.json.sha256")
outputs.file(shasumFile)
doFirst {
expand(mapOf("computedChecksum" to archiveFile.computeChecksum()))
}
doLast {
val outputFile = file("$destinationDir").resolve("${packageDir.name}.json")
shasumFile.writeText(outputFile.computeChecksum())
}
createTestPackages.dependsOn(this)
}
sourcesJar.dependsOn.add(copyTask)
}
val generateKeys by tasks.registering(JavaExec::class) {
val outputFile = file("$buildDir/keystore/localhost.p12")
outputs.file(outputFile)
mainClass.set("sun.security.tools.keytool.Main")
args = listOf(
"-genkeypair",
"-keyalg", "RSA",
"-alias", "integ_tests",
"-keystore", "localhost.p12",
"-storepass", "password",
"-dname", "CN=localhost"
)
workingDir = file("$buildDir/keystore/")
onlyIf { !outputFile.exists() }
doFirst {
workingDir.mkdirs()
}
}
val generateCerts by tasks.registering(JavaExec::class) {
dependsOn("generateKeys")
val outputFile = file("$buildDir/keystore/localhost.pem")
outputs.file(outputFile)
mainClass.set("sun.security.tools.keytool.Main")
args = listOf(
"-exportcert",
"-alias", "integ_tests",
"-storepass", "password",
"-keystore", "localhost.p12",
"-rfc",
"-file", "localhost.pem"
)
workingDir = file("$buildDir/keystore/")
onlyIf { !outputFile.exists() }
doFirst {
workingDir.mkdirs()
}
}