Skip to content

Commit

Permalink
Enable native tests; Add proxy test with json
Browse files Browse the repository at this point in the history
  • Loading branch information
e5l committed Jan 10, 2020
1 parent b7a12cf commit 4620e9b
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 18 deletions.
31 changes: 21 additions & 10 deletions ktor-client/ktor-client-tests/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,30 @@ kotlin.sourceSets {
}
}

// if (!ideaActive) {
// listOf("linuxX64Test", "mingwX64Test", "macosX64Test").map { getByName(it) }.forEach {
// it.dependencies {
// api(project(":ktor-client:ktor-client-curl"))
// }
// }
//
// listOf("iosX64Test", "iosArm64Test", "iosArm64Test", "macosX64Test").map { getByName(it) }.forEach {
// it.dependencies {
if (!ideaActive) {
listOf("linuxX64Test", "mingwX64Test", "macosX64Test").map { getByName(it) }.forEach {
it.dependencies {
api(project(":ktor-client:ktor-client-curl"))
}
}

listOf("iosX64Test", "iosArm64Test", "iosArm64Test", "macosX64Test").map { getByName(it) }.forEach {
it.dependencies {
api(project(":ktor-client:ktor-client-ios"))
}
}
} else {
posixTest {
dependencies {
api(project(":ktor-client:ktor-client-curl"))
}
}
// darwinTest {
// dependencies {
// api(project(":ktor-client:ktor-client-ios"))
// }
// }
// }
}
}

val startTestServer = task<KtorTestServer>("startTestServer") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@
package io.ktor.client.tests

import io.ktor.client.engine.*
import io.ktor.client.features.json.*
import io.ktor.client.request.*
import io.ktor.client.tests.utils.*
import kotlin.collections.get
import kotlinx.serialization.*
import kotlin.test.*

@Serializable
data class ProxyResponse(val status: String)

class ProxyTest : ClientLoader() {

@Test
Expand All @@ -25,4 +29,22 @@ class ProxyTest : ClientLoader() {
assertEquals("proxy", response)
}
}

@Test
fun testProxyWithSerialization() = clientTests(listOf("Js")) {
config {
engine {
proxy = ProxyBuilder.http(HTTP_PROXY_SERVER)
}

install(JsonFeature)
}

test { client ->
val response = client.get<ProxyResponse>("http://google.com")
val expected = ProxyResponse("OK")

assertEquals(expected, response)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ internal class TestTcpServer(val port: Int, handler: suspend (Socket) -> Unit) :

try {
socket.use { handler(it) }
} catch (_ : Throwable) {
} catch (_: Throwable) {
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ suspend fun proxyHandler(socket: Socket) {
val output = socket.openWriteChannel()

val statusLine = input.readUTF8Line()
val response = if (statusLine == "GET http://google.com/ HTTP/1.1") {
buildResponse(HttpStatusCode.OK)
} else {
buildResponse(HttpStatusCode.BadRequest)

val response = when (statusLine) {
"GET http://google.com/ HTTP/1.1" -> buildResponse(HttpStatusCode.OK)
"GET http://google.com/json HTTP/1.1" -> buildResponse(HttpStatusCode.OK, "{'status': 'ok'}")
else -> buildResponse(HttpStatusCode.BadRequest)
}

output.writeStringUtf8(response)
Expand All @@ -27,9 +28,9 @@ suspend fun proxyHandler(socket: Socket) {
}
}

private fun buildResponse(status: HttpStatusCode) = buildString {
private fun buildResponse(status: HttpStatusCode, body: String = "proxy") = buildString {
append("HTTP/1.1 ${status.value} ${status.description}\r\n")
append("Connection: close\r\n")
append("\r\n")
append("proxy")
append(body)
}

0 comments on commit 4620e9b

Please sign in to comment.