mirror of
https://github.com/apple/pkl.git
synced 2026-04-22 16:28:34 +02:00
Fix line endings (#513)
* Adjust gitattributes file (mark some files binary, mark bat files as using crlf endings) * Fix remaining line endings issues
This commit is contained in:
@@ -1,34 +1,34 @@
|
||||
package org.pkl.core.http
|
||||
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.junit.jupiter.api.assertDoesNotThrow
|
||||
import org.junit.jupiter.api.assertThrows
|
||||
import java.net.URI
|
||||
import java.net.http.HttpRequest
|
||||
import java.net.http.HttpResponse
|
||||
|
||||
class DummyHttpClientTest {
|
||||
@Test
|
||||
fun `refuses to send messages`() {
|
||||
val client = HttpClient.dummyClient()
|
||||
val request = HttpRequest.newBuilder(URI("https://example.com")).build()
|
||||
|
||||
assertThrows<AssertionError> {
|
||||
client.send(request, HttpResponse.BodyHandlers.discarding())
|
||||
}
|
||||
|
||||
assertThrows<AssertionError> {
|
||||
client.send(request, HttpResponse.BodyHandlers.discarding())
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `can be closed`() {
|
||||
val client = HttpClient.dummyClient()
|
||||
|
||||
assertDoesNotThrow {
|
||||
client.close()
|
||||
client.close()
|
||||
}
|
||||
}
|
||||
}
|
||||
package org.pkl.core.http
|
||||
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.junit.jupiter.api.assertDoesNotThrow
|
||||
import org.junit.jupiter.api.assertThrows
|
||||
import java.net.URI
|
||||
import java.net.http.HttpRequest
|
||||
import java.net.http.HttpResponse
|
||||
|
||||
class DummyHttpClientTest {
|
||||
@Test
|
||||
fun `refuses to send messages`() {
|
||||
val client = HttpClient.dummyClient()
|
||||
val request = HttpRequest.newBuilder(URI("https://example.com")).build()
|
||||
|
||||
assertThrows<AssertionError> {
|
||||
client.send(request, HttpResponse.BodyHandlers.discarding())
|
||||
}
|
||||
|
||||
assertThrows<AssertionError> {
|
||||
client.send(request, HttpResponse.BodyHandlers.discarding())
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `can be closed`() {
|
||||
val client = HttpClient.dummyClient()
|
||||
|
||||
assertDoesNotThrow {
|
||||
client.close()
|
||||
client.close()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,149 +1,149 @@
|
||||
package org.pkl.core.http
|
||||
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.junit.jupiter.api.assertDoesNotThrow
|
||||
import org.junit.jupiter.api.assertThrows
|
||||
import org.junit.jupiter.api.io.TempDir
|
||||
import org.pkl.commons.test.FileTestUtils
|
||||
import org.pkl.core.Release
|
||||
import java.net.URI
|
||||
import java.net.http.HttpRequest
|
||||
import java.net.http.HttpResponse
|
||||
import java.nio.file.Path
|
||||
import java.time.Duration
|
||||
import kotlin.io.path.copyTo
|
||||
import kotlin.io.path.createDirectories
|
||||
import kotlin.io.path.createFile
|
||||
|
||||
class HttpClientTest {
|
||||
@Test
|
||||
fun `can build default client`() {
|
||||
val client = assertDoesNotThrow {
|
||||
HttpClient.builder().build()
|
||||
}
|
||||
|
||||
assertThat(client).isInstanceOf(RequestRewritingClient::class.java)
|
||||
client as RequestRewritingClient
|
||||
|
||||
val release = Release.current()
|
||||
assertThat(client.userAgent).isEqualTo("Pkl/${release.version()} (${release.os()}; ${release.flavor()})")
|
||||
assertThat(client.requestTimeout).isEqualTo(Duration.ofSeconds(60))
|
||||
|
||||
assertThat(client.delegate).isInstanceOf(JdkHttpClient::class.java)
|
||||
val delegate = client.delegate as JdkHttpClient
|
||||
|
||||
assertThat(delegate.underlying.connectTimeout()).hasValue(Duration.ofSeconds(60))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `can build custom client`() {
|
||||
val client = HttpClient.builder()
|
||||
.setUserAgent("Agent 1")
|
||||
.setRequestTimeout(Duration.ofHours(86))
|
||||
.setConnectTimeout(Duration.ofMinutes(42))
|
||||
.build() as RequestRewritingClient
|
||||
|
||||
assertThat(client.userAgent).isEqualTo("Agent 1")
|
||||
assertThat(client.requestTimeout).isEqualTo(Duration.ofHours(86))
|
||||
|
||||
val delegate = client.delegate as JdkHttpClient
|
||||
assertThat(delegate.underlying.connectTimeout()).hasValue(Duration.ofMinutes(42))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `can load certificates from file system`() {
|
||||
assertDoesNotThrow {
|
||||
HttpClient.builder().addCertificates(FileTestUtils.selfSignedCertificate).build()
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `certificate file located on file system cannot be empty`(@TempDir tempDir: Path) {
|
||||
val file = tempDir.resolve("certs.pem").createFile()
|
||||
|
||||
val e = assertThrows<HttpClientInitException> {
|
||||
HttpClient.builder().addCertificates(file).build()
|
||||
}
|
||||
|
||||
assertThat(e).hasMessageContaining("empty")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `can load certificates from class path`() {
|
||||
assertDoesNotThrow {
|
||||
HttpClient.builder().addCertificates(javaClass.getResource("/org/pkl/certs/PklCARoots.pem")!!.toURI()).build()
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `only allows loading jar and file certificate URIs`() {
|
||||
assertThrows<HttpClientInitException> {
|
||||
HttpClient.builder().addCertificates(URI("https://example.com"))
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `certificate file located on class path cannot be empty`() {
|
||||
val uri = javaClass.getResource("emptyCerts.pem")!!.toURI()
|
||||
|
||||
val e = assertThrows<HttpClientInitException> {
|
||||
HttpClient.builder().addCertificates(uri).build()
|
||||
}
|
||||
|
||||
assertThat(e).hasMessageContaining("empty")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `can load built-in certificates`() {
|
||||
assertDoesNotThrow {
|
||||
HttpClient.builder().addBuiltInCertificates().build()
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `can load certificates from Pkl user home cacerts directory`(@TempDir tempDir: Path) {
|
||||
val certsDir = tempDir.resolve(".pkl")
|
||||
.resolve("cacerts")
|
||||
.createDirectories()
|
||||
.also { dir ->
|
||||
FileTestUtils.selfSignedCertificate.copyTo(dir.resolve("certs.pem"))
|
||||
}
|
||||
|
||||
assertDoesNotThrow {
|
||||
HttpClientBuilder(certsDir).addDefaultCliCertificates().build()
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `loading certificates from cacerts directory falls back to built-in certificates`(@TempDir certsDir: Path) {
|
||||
assertDoesNotThrow {
|
||||
HttpClientBuilder(certsDir).addDefaultCliCertificates().build()
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `can be closed multiple times`() {
|
||||
val client = HttpClient.builder().build()
|
||||
|
||||
assertDoesNotThrow {
|
||||
client.close()
|
||||
client.close()
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `refuses to send messages once closed`() {
|
||||
val client = HttpClient.builder().build()
|
||||
val request = HttpRequest.newBuilder(URI("https://example.com")).build()
|
||||
|
||||
client.close()
|
||||
|
||||
assertThrows<IllegalStateException> {
|
||||
client.send(request, HttpResponse.BodyHandlers.discarding())
|
||||
}
|
||||
assertThrows<IllegalStateException> {
|
||||
client.send(request, HttpResponse.BodyHandlers.discarding())
|
||||
}
|
||||
}
|
||||
}
|
||||
package org.pkl.core.http
|
||||
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.junit.jupiter.api.assertDoesNotThrow
|
||||
import org.junit.jupiter.api.assertThrows
|
||||
import org.junit.jupiter.api.io.TempDir
|
||||
import org.pkl.commons.test.FileTestUtils
|
||||
import org.pkl.core.Release
|
||||
import java.net.URI
|
||||
import java.net.http.HttpRequest
|
||||
import java.net.http.HttpResponse
|
||||
import java.nio.file.Path
|
||||
import java.time.Duration
|
||||
import kotlin.io.path.copyTo
|
||||
import kotlin.io.path.createDirectories
|
||||
import kotlin.io.path.createFile
|
||||
|
||||
class HttpClientTest {
|
||||
@Test
|
||||
fun `can build default client`() {
|
||||
val client = assertDoesNotThrow {
|
||||
HttpClient.builder().build()
|
||||
}
|
||||
|
||||
assertThat(client).isInstanceOf(RequestRewritingClient::class.java)
|
||||
client as RequestRewritingClient
|
||||
|
||||
val release = Release.current()
|
||||
assertThat(client.userAgent).isEqualTo("Pkl/${release.version()} (${release.os()}; ${release.flavor()})")
|
||||
assertThat(client.requestTimeout).isEqualTo(Duration.ofSeconds(60))
|
||||
|
||||
assertThat(client.delegate).isInstanceOf(JdkHttpClient::class.java)
|
||||
val delegate = client.delegate as JdkHttpClient
|
||||
|
||||
assertThat(delegate.underlying.connectTimeout()).hasValue(Duration.ofSeconds(60))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `can build custom client`() {
|
||||
val client = HttpClient.builder()
|
||||
.setUserAgent("Agent 1")
|
||||
.setRequestTimeout(Duration.ofHours(86))
|
||||
.setConnectTimeout(Duration.ofMinutes(42))
|
||||
.build() as RequestRewritingClient
|
||||
|
||||
assertThat(client.userAgent).isEqualTo("Agent 1")
|
||||
assertThat(client.requestTimeout).isEqualTo(Duration.ofHours(86))
|
||||
|
||||
val delegate = client.delegate as JdkHttpClient
|
||||
assertThat(delegate.underlying.connectTimeout()).hasValue(Duration.ofMinutes(42))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `can load certificates from file system`() {
|
||||
assertDoesNotThrow {
|
||||
HttpClient.builder().addCertificates(FileTestUtils.selfSignedCertificate).build()
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `certificate file located on file system cannot be empty`(@TempDir tempDir: Path) {
|
||||
val file = tempDir.resolve("certs.pem").createFile()
|
||||
|
||||
val e = assertThrows<HttpClientInitException> {
|
||||
HttpClient.builder().addCertificates(file).build()
|
||||
}
|
||||
|
||||
assertThat(e).hasMessageContaining("empty")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `can load certificates from class path`() {
|
||||
assertDoesNotThrow {
|
||||
HttpClient.builder().addCertificates(javaClass.getResource("/org/pkl/certs/PklCARoots.pem")!!.toURI()).build()
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `only allows loading jar and file certificate URIs`() {
|
||||
assertThrows<HttpClientInitException> {
|
||||
HttpClient.builder().addCertificates(URI("https://example.com"))
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `certificate file located on class path cannot be empty`() {
|
||||
val uri = javaClass.getResource("emptyCerts.pem")!!.toURI()
|
||||
|
||||
val e = assertThrows<HttpClientInitException> {
|
||||
HttpClient.builder().addCertificates(uri).build()
|
||||
}
|
||||
|
||||
assertThat(e).hasMessageContaining("empty")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `can load built-in certificates`() {
|
||||
assertDoesNotThrow {
|
||||
HttpClient.builder().addBuiltInCertificates().build()
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `can load certificates from Pkl user home cacerts directory`(@TempDir tempDir: Path) {
|
||||
val certsDir = tempDir.resolve(".pkl")
|
||||
.resolve("cacerts")
|
||||
.createDirectories()
|
||||
.also { dir ->
|
||||
FileTestUtils.selfSignedCertificate.copyTo(dir.resolve("certs.pem"))
|
||||
}
|
||||
|
||||
assertDoesNotThrow {
|
||||
HttpClientBuilder(certsDir).addDefaultCliCertificates().build()
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `loading certificates from cacerts directory falls back to built-in certificates`(@TempDir certsDir: Path) {
|
||||
assertDoesNotThrow {
|
||||
HttpClientBuilder(certsDir).addDefaultCliCertificates().build()
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `can be closed multiple times`() {
|
||||
val client = HttpClient.builder().build()
|
||||
|
||||
assertDoesNotThrow {
|
||||
client.close()
|
||||
client.close()
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `refuses to send messages once closed`() {
|
||||
val client = HttpClient.builder().build()
|
||||
val request = HttpRequest.newBuilder(URI("https://example.com")).build()
|
||||
|
||||
client.close()
|
||||
|
||||
assertThrows<IllegalStateException> {
|
||||
client.send(request, HttpResponse.BodyHandlers.discarding())
|
||||
}
|
||||
assertThrows<IllegalStateException> {
|
||||
client.send(request, HttpResponse.BodyHandlers.discarding())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,34 +1,34 @@
|
||||
package org.pkl.core.http
|
||||
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.junit.jupiter.api.assertDoesNotThrow
|
||||
import org.junit.jupiter.api.assertThrows
|
||||
import java.net.URI
|
||||
import java.net.http.HttpRequest
|
||||
import java.net.http.HttpResponse.BodyHandlers
|
||||
|
||||
class LazyHttpClientTest {
|
||||
@Test
|
||||
fun `builds underlying client on first send`() {
|
||||
val client = HttpClient.builder()
|
||||
.addCertificates(javaClass.getResource("brokenCerts.pem")!!.toURI())
|
||||
.buildLazily()
|
||||
val request = HttpRequest.newBuilder(URI("https://example.com")).build()
|
||||
|
||||
assertThrows<HttpClientInitException> {
|
||||
client.send(request, BodyHandlers.discarding())
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `does not build underlying client unnecessarily`() {
|
||||
val client = HttpClient.builder()
|
||||
.addCertificates(javaClass.getResource("brokenCerts.pem")!!.toURI())
|
||||
.buildLazily()
|
||||
|
||||
assertDoesNotThrow {
|
||||
client.close()
|
||||
client.close()
|
||||
}
|
||||
}
|
||||
}
|
||||
package org.pkl.core.http
|
||||
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.junit.jupiter.api.assertDoesNotThrow
|
||||
import org.junit.jupiter.api.assertThrows
|
||||
import java.net.URI
|
||||
import java.net.http.HttpRequest
|
||||
import java.net.http.HttpResponse.BodyHandlers
|
||||
|
||||
class LazyHttpClientTest {
|
||||
@Test
|
||||
fun `builds underlying client on first send`() {
|
||||
val client = HttpClient.builder()
|
||||
.addCertificates(javaClass.getResource("brokenCerts.pem")!!.toURI())
|
||||
.buildLazily()
|
||||
val request = HttpRequest.newBuilder(URI("https://example.com")).build()
|
||||
|
||||
assertThrows<HttpClientInitException> {
|
||||
client.send(request, BodyHandlers.discarding())
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `does not build underlying client unnecessarily`() {
|
||||
val client = HttpClient.builder()
|
||||
.addCertificates(javaClass.getResource("brokenCerts.pem")!!.toURI())
|
||||
.buildLazily()
|
||||
|
||||
assertDoesNotThrow {
|
||||
client.close()
|
||||
client.close()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,127 +1,127 @@
|
||||
package org.pkl.core.http
|
||||
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
import org.assertj.core.api.Assertions.assertThatList
|
||||
import org.junit.jupiter.api.Test
|
||||
import java.net.URI
|
||||
import java.net.http.HttpRequest
|
||||
import java.net.http.HttpRequest.BodyPublishers
|
||||
import java.net.http.HttpResponse.BodyHandlers
|
||||
import java.time.Duration
|
||||
import java.net.http.HttpClient as JdkHttpClient
|
||||
|
||||
class RequestRewritingClientTest {
|
||||
private val captured = RequestCapturingClient()
|
||||
private val client = RequestRewritingClient("Pkl", Duration.ofSeconds(42), -1, captured)
|
||||
private val exampleUri = URI("https://example.com/foo/bar.html")
|
||||
private val exampleRequest = HttpRequest.newBuilder(exampleUri).build()
|
||||
|
||||
@Test
|
||||
fun `fills in missing User-Agent header`() {
|
||||
client.send(exampleRequest, BodyHandlers.discarding())
|
||||
|
||||
assertThatList(captured.request.headers().allValues("User-Agent")).containsOnly("Pkl")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `overrides existing User-Agent headers`() {
|
||||
val request = HttpRequest.newBuilder(exampleUri)
|
||||
.header("User-Agent", "Agent 1")
|
||||
.header("User-Agent", "Agent 2")
|
||||
.build()
|
||||
|
||||
client.send(request, BodyHandlers.discarding())
|
||||
|
||||
assertThatList(captured.request.headers().allValues("User-Agent")).containsOnly("Pkl")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `fills in missing request timeout`() {
|
||||
client.send(exampleRequest, BodyHandlers.discarding())
|
||||
|
||||
assertThat(captured.request.timeout()).hasValue(Duration.ofSeconds(42))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `leaves existing request timeout intact`() {
|
||||
val request = HttpRequest.newBuilder(exampleUri)
|
||||
.timeout(Duration.ofMinutes(33))
|
||||
.build()
|
||||
|
||||
client.send(request, BodyHandlers.discarding())
|
||||
|
||||
assertThat(captured.request.timeout()).hasValue(Duration.ofMinutes(33))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `fills in missing HTTP version`() {
|
||||
client.send(exampleRequest, BodyHandlers.discarding())
|
||||
|
||||
assertThat(captured.request.version()).hasValue(JdkHttpClient.Version.HTTP_2)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `leaves existing HTTP version intact`() {
|
||||
val request = HttpRequest.newBuilder(exampleUri)
|
||||
.version(JdkHttpClient.Version.HTTP_1_1)
|
||||
.build()
|
||||
|
||||
client.send(request, BodyHandlers.discarding())
|
||||
|
||||
assertThat(captured.request.version()).hasValue(JdkHttpClient.Version.HTTP_1_1)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `leaves default method intact`() {
|
||||
val request = HttpRequest.newBuilder(exampleUri).build()
|
||||
|
||||
client.send(request, BodyHandlers.discarding())
|
||||
|
||||
assertThat(captured.request.method()).isEqualTo("GET")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `leaves explicit method intact`() {
|
||||
val request = HttpRequest.newBuilder(exampleUri)
|
||||
.DELETE()
|
||||
.build()
|
||||
|
||||
client.send(request, BodyHandlers.discarding())
|
||||
|
||||
assertThat(captured.request.method()).isEqualTo("DELETE")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `leaves body publisher intact`() {
|
||||
val publisher = BodyPublishers.ofString("body")
|
||||
val request = HttpRequest.newBuilder(exampleUri)
|
||||
.PUT(publisher)
|
||||
.build()
|
||||
|
||||
client.send(request, BodyHandlers.discarding())
|
||||
|
||||
assertThat(captured.request.bodyPublisher().get()).isSameAs(publisher)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `rewrites port 0 if test port is set`() {
|
||||
val captured = RequestCapturingClient()
|
||||
val client = RequestRewritingClient("Pkl", Duration.ofSeconds(42), 5000, captured)
|
||||
val request = HttpRequest.newBuilder(URI("https://example.com:0")).build()
|
||||
|
||||
client.send(request, BodyHandlers.discarding())
|
||||
|
||||
assertThat(captured.request.uri().port).isEqualTo(5000)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `leaves port 0 intact if no test port is set`() {
|
||||
val request = HttpRequest.newBuilder(URI("https://example.com:0")).build()
|
||||
|
||||
client.send(request, BodyHandlers.discarding())
|
||||
|
||||
assertThat(captured.request.uri().port).isEqualTo(0)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
package org.pkl.core.http
|
||||
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
import org.assertj.core.api.Assertions.assertThatList
|
||||
import org.junit.jupiter.api.Test
|
||||
import java.net.URI
|
||||
import java.net.http.HttpRequest
|
||||
import java.net.http.HttpRequest.BodyPublishers
|
||||
import java.net.http.HttpResponse.BodyHandlers
|
||||
import java.time.Duration
|
||||
import java.net.http.HttpClient as JdkHttpClient
|
||||
|
||||
class RequestRewritingClientTest {
|
||||
private val captured = RequestCapturingClient()
|
||||
private val client = RequestRewritingClient("Pkl", Duration.ofSeconds(42), -1, captured)
|
||||
private val exampleUri = URI("https://example.com/foo/bar.html")
|
||||
private val exampleRequest = HttpRequest.newBuilder(exampleUri).build()
|
||||
|
||||
@Test
|
||||
fun `fills in missing User-Agent header`() {
|
||||
client.send(exampleRequest, BodyHandlers.discarding())
|
||||
|
||||
assertThatList(captured.request.headers().allValues("User-Agent")).containsOnly("Pkl")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `overrides existing User-Agent headers`() {
|
||||
val request = HttpRequest.newBuilder(exampleUri)
|
||||
.header("User-Agent", "Agent 1")
|
||||
.header("User-Agent", "Agent 2")
|
||||
.build()
|
||||
|
||||
client.send(request, BodyHandlers.discarding())
|
||||
|
||||
assertThatList(captured.request.headers().allValues("User-Agent")).containsOnly("Pkl")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `fills in missing request timeout`() {
|
||||
client.send(exampleRequest, BodyHandlers.discarding())
|
||||
|
||||
assertThat(captured.request.timeout()).hasValue(Duration.ofSeconds(42))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `leaves existing request timeout intact`() {
|
||||
val request = HttpRequest.newBuilder(exampleUri)
|
||||
.timeout(Duration.ofMinutes(33))
|
||||
.build()
|
||||
|
||||
client.send(request, BodyHandlers.discarding())
|
||||
|
||||
assertThat(captured.request.timeout()).hasValue(Duration.ofMinutes(33))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `fills in missing HTTP version`() {
|
||||
client.send(exampleRequest, BodyHandlers.discarding())
|
||||
|
||||
assertThat(captured.request.version()).hasValue(JdkHttpClient.Version.HTTP_2)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `leaves existing HTTP version intact`() {
|
||||
val request = HttpRequest.newBuilder(exampleUri)
|
||||
.version(JdkHttpClient.Version.HTTP_1_1)
|
||||
.build()
|
||||
|
||||
client.send(request, BodyHandlers.discarding())
|
||||
|
||||
assertThat(captured.request.version()).hasValue(JdkHttpClient.Version.HTTP_1_1)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `leaves default method intact`() {
|
||||
val request = HttpRequest.newBuilder(exampleUri).build()
|
||||
|
||||
client.send(request, BodyHandlers.discarding())
|
||||
|
||||
assertThat(captured.request.method()).isEqualTo("GET")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `leaves explicit method intact`() {
|
||||
val request = HttpRequest.newBuilder(exampleUri)
|
||||
.DELETE()
|
||||
.build()
|
||||
|
||||
client.send(request, BodyHandlers.discarding())
|
||||
|
||||
assertThat(captured.request.method()).isEqualTo("DELETE")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `leaves body publisher intact`() {
|
||||
val publisher = BodyPublishers.ofString("body")
|
||||
val request = HttpRequest.newBuilder(exampleUri)
|
||||
.PUT(publisher)
|
||||
.build()
|
||||
|
||||
client.send(request, BodyHandlers.discarding())
|
||||
|
||||
assertThat(captured.request.bodyPublisher().get()).isSameAs(publisher)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `rewrites port 0 if test port is set`() {
|
||||
val captured = RequestCapturingClient()
|
||||
val client = RequestRewritingClient("Pkl", Duration.ofSeconds(42), 5000, captured)
|
||||
val request = HttpRequest.newBuilder(URI("https://example.com:0")).build()
|
||||
|
||||
client.send(request, BodyHandlers.discarding())
|
||||
|
||||
assertThat(captured.request.uri().port).isEqualTo(5000)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `leaves port 0 intact if no test port is set`() {
|
||||
val request = HttpRequest.newBuilder(URI("https://example.com:0")).build()
|
||||
|
||||
client.send(request, BodyHandlers.discarding())
|
||||
|
||||
assertThat(captured.request.uri().port).isEqualTo(0)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,48 +1,48 @@
|
||||
package org.pkl.core.util
|
||||
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
import org.junit.jupiter.api.Test
|
||||
import java.io.IOException
|
||||
import java.lang.Error
|
||||
|
||||
class ExceptionsTest {
|
||||
@Test
|
||||
fun `get root cause of simple exception`() {
|
||||
val e = IOException("io")
|
||||
assertThat(Exceptions.getRootCause(e)).isSameAs(e)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `get root cause of nested exception`() {
|
||||
val e = IOException("io")
|
||||
val e2 = RuntimeException("runtime")
|
||||
val e3 = Error("error")
|
||||
e.initCause(e2)
|
||||
e2.initCause(e3)
|
||||
assertThat(Exceptions.getRootCause(e)).isSameAs(e3)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `get root reason`() {
|
||||
val e = IOException("io")
|
||||
val e2 = RuntimeException("the root reason")
|
||||
e.initCause(e2)
|
||||
assertThat(Exceptions.getRootReason(e)).isEqualTo("the root reason")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `get root reason if null`() {
|
||||
val e = IOException("io")
|
||||
val e2 = RuntimeException(null as String?)
|
||||
e.initCause(e2)
|
||||
assertThat(Exceptions.getRootReason(e)).isEqualTo("(unknown reason)")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `get root reason if empty`() {
|
||||
val e = IOException("io")
|
||||
val e2 = RuntimeException("")
|
||||
e.initCause(e2)
|
||||
assertThat(Exceptions.getRootReason(e)).isEqualTo("(unknown reason)")
|
||||
}
|
||||
}
|
||||
package org.pkl.core.util
|
||||
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
import org.junit.jupiter.api.Test
|
||||
import java.io.IOException
|
||||
import java.lang.Error
|
||||
|
||||
class ExceptionsTest {
|
||||
@Test
|
||||
fun `get root cause of simple exception`() {
|
||||
val e = IOException("io")
|
||||
assertThat(Exceptions.getRootCause(e)).isSameAs(e)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `get root cause of nested exception`() {
|
||||
val e = IOException("io")
|
||||
val e2 = RuntimeException("runtime")
|
||||
val e3 = Error("error")
|
||||
e.initCause(e2)
|
||||
e2.initCause(e3)
|
||||
assertThat(Exceptions.getRootCause(e)).isSameAs(e3)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `get root reason`() {
|
||||
val e = IOException("io")
|
||||
val e2 = RuntimeException("the root reason")
|
||||
e.initCause(e2)
|
||||
assertThat(Exceptions.getRootReason(e)).isEqualTo("the root reason")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `get root reason if null`() {
|
||||
val e = IOException("io")
|
||||
val e2 = RuntimeException(null as String?)
|
||||
e.initCause(e2)
|
||||
assertThat(Exceptions.getRootReason(e)).isEqualTo("(unknown reason)")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `get root reason if empty`() {
|
||||
val e = IOException("io")
|
||||
val e2 = RuntimeException("")
|
||||
e.initCause(e2)
|
||||
assertThat(Exceptions.getRootReason(e)).isEqualTo("(unknown reason)")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,59 +1,59 @@
|
||||
package org.pkl.core.util
|
||||
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.junit.jupiter.api.assertDoesNotThrow
|
||||
import org.junit.jupiter.api.assertThrows
|
||||
import org.pkl.commons.test.FakeHttpResponse
|
||||
import java.io.IOException
|
||||
import java.net.URI
|
||||
|
||||
class HttpUtilsTest {
|
||||
@Test
|
||||
fun isHttpUrl() {
|
||||
assertThat(HttpUtils.isHttpUrl(URI("http://example.com"))).isTrue
|
||||
assertThat(HttpUtils.isHttpUrl(URI("https://example.com"))).isTrue
|
||||
assertThat(HttpUtils.isHttpUrl(URI("HtTpS://example.com"))).isTrue
|
||||
assertThat(HttpUtils.isHttpUrl(URI("file://example.com"))).isFalse
|
||||
|
||||
assertThat(HttpUtils.isHttpUrl(URI("http://example.com").toURL())).isTrue
|
||||
assertThat(HttpUtils.isHttpUrl(URI("https://example.com").toURL())).isTrue
|
||||
assertThat(HttpUtils.isHttpUrl(URI("HtTpS://example.com").toURL())).isTrue
|
||||
assertThat(HttpUtils.isHttpUrl(URI("file://example.com").toURL())).isFalse
|
||||
}
|
||||
|
||||
@Test
|
||||
fun checkHasStatusCode200() {
|
||||
val response = FakeHttpResponse.withoutBody {
|
||||
statusCode = 200
|
||||
}
|
||||
assertDoesNotThrow {
|
||||
HttpUtils.checkHasStatusCode200(response)
|
||||
}
|
||||
|
||||
val response2 = FakeHttpResponse.withoutBody {
|
||||
statusCode = 404
|
||||
}
|
||||
assertThrows<IOException> {
|
||||
HttpUtils.checkHasStatusCode200(response2)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun setPort() {
|
||||
assertThrows<IllegalArgumentException> {
|
||||
HttpUtils.setPort(URI("https://example.com"), -1)
|
||||
}
|
||||
assertThrows<IllegalArgumentException> {
|
||||
HttpUtils.setPort(URI("https://example.com"), 65536)
|
||||
}
|
||||
assertThat(HttpUtils.setPort(URI("http://example.com"), 123))
|
||||
.isEqualTo(URI("http://example.com:123"))
|
||||
assertThat(HttpUtils.setPort(URI("http://example.com:456"), 123))
|
||||
.isEqualTo(URI("http://example.com:123"))
|
||||
assertThat(HttpUtils.setPort(URI("https://example.com/foo/bar.baz?query=1#fragment"), 123))
|
||||
.isEqualTo(URI("https://example.com:123/foo/bar.baz?query=1#fragment"))
|
||||
assertThat(HttpUtils.setPort(URI("https://example.com:456/foo/bar.baz?query=1#fragment"), 123))
|
||||
.isEqualTo(URI("https://example.com:123/foo/bar.baz?query=1#fragment"))
|
||||
}
|
||||
}
|
||||
package org.pkl.core.util
|
||||
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.junit.jupiter.api.assertDoesNotThrow
|
||||
import org.junit.jupiter.api.assertThrows
|
||||
import org.pkl.commons.test.FakeHttpResponse
|
||||
import java.io.IOException
|
||||
import java.net.URI
|
||||
|
||||
class HttpUtilsTest {
|
||||
@Test
|
||||
fun isHttpUrl() {
|
||||
assertThat(HttpUtils.isHttpUrl(URI("http://example.com"))).isTrue
|
||||
assertThat(HttpUtils.isHttpUrl(URI("https://example.com"))).isTrue
|
||||
assertThat(HttpUtils.isHttpUrl(URI("HtTpS://example.com"))).isTrue
|
||||
assertThat(HttpUtils.isHttpUrl(URI("file://example.com"))).isFalse
|
||||
|
||||
assertThat(HttpUtils.isHttpUrl(URI("http://example.com").toURL())).isTrue
|
||||
assertThat(HttpUtils.isHttpUrl(URI("https://example.com").toURL())).isTrue
|
||||
assertThat(HttpUtils.isHttpUrl(URI("HtTpS://example.com").toURL())).isTrue
|
||||
assertThat(HttpUtils.isHttpUrl(URI("file://example.com").toURL())).isFalse
|
||||
}
|
||||
|
||||
@Test
|
||||
fun checkHasStatusCode200() {
|
||||
val response = FakeHttpResponse.withoutBody {
|
||||
statusCode = 200
|
||||
}
|
||||
assertDoesNotThrow {
|
||||
HttpUtils.checkHasStatusCode200(response)
|
||||
}
|
||||
|
||||
val response2 = FakeHttpResponse.withoutBody {
|
||||
statusCode = 404
|
||||
}
|
||||
assertThrows<IOException> {
|
||||
HttpUtils.checkHasStatusCode200(response2)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun setPort() {
|
||||
assertThrows<IllegalArgumentException> {
|
||||
HttpUtils.setPort(URI("https://example.com"), -1)
|
||||
}
|
||||
assertThrows<IllegalArgumentException> {
|
||||
HttpUtils.setPort(URI("https://example.com"), 65536)
|
||||
}
|
||||
assertThat(HttpUtils.setPort(URI("http://example.com"), 123))
|
||||
.isEqualTo(URI("http://example.com:123"))
|
||||
assertThat(HttpUtils.setPort(URI("http://example.com:456"), 123))
|
||||
.isEqualTo(URI("http://example.com:123"))
|
||||
assertThat(HttpUtils.setPort(URI("https://example.com/foo/bar.baz?query=1#fragment"), 123))
|
||||
.isEqualTo(URI("https://example.com:123/foo/bar.baz?query=1#fragment"))
|
||||
assertThat(HttpUtils.setPort(URI("https://example.com:456/foo/bar.baz?query=1#fragment"), 123))
|
||||
.isEqualTo(URI("https://example.com:123/foo/bar.baz?query=1#fragment"))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user