Skip to content

Commit

Permalink
Use custom host header to workaround Autobahn external port checking
Browse files Browse the repository at this point in the history
  • Loading branch information
joffrey-bion committed Aug 24, 2023
1 parent 349094e commit 0063847
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package org.hildan.krossbow.websocket.test.autobahn

import kotlinx.coroutines.withTimeoutOrNull
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json
import org.hildan.krossbow.websocket.WebSocketClient
import org.hildan.krossbow.websocket.WebSocketConnection
import org.hildan.krossbow.websocket.test.connectWithTimeout
import org.hildan.krossbow.websocket.test.expectCloseFrame
import org.hildan.krossbow.websocket.test.expectNoMoreFrames
import org.hildan.krossbow.websocket.test.expectTextFrame
import kotlin.test.fail
import kotlin.time.Duration.Companion.seconds

internal class AutobahnClientTester(
private val wsClient: WebSocketClient,
Expand All @@ -19,8 +21,14 @@ internal class AutobahnClientTester(
) {
private val testServerUrl = config.websocketTestServerUrl

suspend fun connectForAutobahnTestCase(case: String): WebSocketConnection =
wsClient.connectWithTimeout("$testServerUrl/runCase?casetuple=$case&agent=$agentUnderTest")
suspend fun connectForAutobahnTestCase(case: String): WebSocketConnection {
val url = "$testServerUrl/runCase?casetuple=$case&agent=$agentUnderTest"
val hostWithoutPort = url.substringAfter("://").substringBefore("/").substringBefore(":")
return withTimeoutOrNull(8.seconds) {
// Host header to fix external port strict checking with Autobahn
wsClient.connect(url, headers = mapOf("Host" to "$hostWithoutPort:9001"))
} ?: fail("Timed out after ${8.seconds} while connecting to $url")
}

suspend fun getCaseCount(): Int = callAndGetJson("getCaseCount")

Expand Down
6 changes: 6 additions & 0 deletions buildSrc/src/main/kotlin/krossbow-autobahn-tests.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ tasks.withType<org.jetbrains.kotlin.gradle.targets.jvm.tasks.KotlinJvmTest> {
// https://github.com/crossbario/autobahn-testsuite/issues/119
maxParallelForks = 1

// Autobahn's port checking fails when containerized because the external port present in the Host header is not
// the same as the port that the server actually listens to (from inside the container).
// We work around this by overriding the Host header during the handshake, but the JDK11 HttpClient doesn't allow
// that unless we whitelist the Host header with this JVM property.
jvmArgs("-Djdk.httpclient.allowRestrictedHeaders=host")

doFirst {
val autobahnContainer = getAutobahnTestServerContainerInfo()
environment("AUTOBAHN_SERVER_HOST", autobahnContainer.host)
Expand Down

0 comments on commit 0063847

Please sign in to comment.