Skip to content

Commit

Permalink
Verify sending Content-Type and custom object body via POST (#1897)
Browse files Browse the repository at this point in the history
* Verify sending Content-Type and custom object body via POST

    Close #997, #1127

* Verify string can be sent with json feature

	Close #1265

* Fix exception message for #997

* Remove obsolete check
  • Loading branch information
e5l authored Aug 7, 2020
1 parent c3377f3 commit 451595f
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class CIOHttpsTest : TestWithKtor() {

@Test
@Ignore
fun external() = testWithEngine(CIO) {
fun testExternal() = testWithEngine(CIO) {
test { client ->
client.get<HttpStatement>("https://kotlinlang.org").execute { response ->
assertEquals(HttpStatusCode.OK, response.status)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ class HttpSend(
// default send scenario
scope.requestPipeline.intercept(HttpRequestPipeline.Send) { content ->
check(content is OutgoingContent) {
"Fail to send body. Content has type: ${content::class}, but OutgoingContent expected."
"""
|Fail to serialize body. Content has type: ${content::class}, but OutgoingContent expected.
|If you expect serialized body, please check that you have installed the corresponding feature(like `Json`) and set `Content-Type` header."""
.trimMargin()
}
context.body = content

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ data class GithubProfile(
class KotlinxSerializerTest : ClientLoader() {
@Test
fun testRegisterCustom() {
// TODO: unmute when JS IR serialization is fixed https://github.com/Kotlin/kotlinx.serialization/issues/751
if (isKotlinJsIr) return

val serializer = KotlinxSerializer()

val user = User(1, "vasya")
Expand All @@ -44,9 +41,6 @@ class KotlinxSerializerTest : ClientLoader() {

@Test
fun testRegisterCustomList() {
// TODO: unmute when JS IR serialization is fixed https://github.com/Kotlin/kotlinx.serialization/issues/751
if (isKotlinJsIr) return

val serializer = KotlinxSerializer()

val user = User(2, "petya")
Expand Down Expand Up @@ -119,10 +113,6 @@ class KotlinxSerializerTest : ClientLoader() {

@Test
fun testMultipleListSerializersWithClient() = clientTests {

// TODO: unmute when JS IR serialization is fixed https://github.com/Kotlin/kotlinx.serialization/issues/751
if (isKotlinJsIr) return@clientTests

val testSerializer = KotlinxSerializer()

config {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
* Copyright 2014-2019 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

@file:Suppress("NO_EXPLICIT_RETURN_TYPE_IN_API_MODE_WARNING")

package io.ktor.client.tests.utils

import io.ktor.client.*
Expand All @@ -12,16 +14,24 @@ import io.ktor.utils.io.core.*
import kotlinx.coroutines.*

/**
* Local test server url.
* Web url for tests.
*/
public const val TEST_SERVER: String = "http://127.0.0.1:8080"

/**
* Websocket server url for tests.
*/
public const val TEST_WEBSOCKET_SERVER: String = "ws://127.0.0.1:8080"

/**
* Proxy server url for tests.
*/
const val TEST_SERVER: String = "http://127.0.0.1:8080"
const val TEST_WEBSOCKET_SERVER: String = "ws://127.0.0.1:8080"
const val HTTP_PROXY_SERVER: String = "http://127.0.0.1:8082"
public const val HTTP_PROXY_SERVER: String = "http://127.0.0.1:8082"

/**
* Perform test with selected client [engine].
*/
fun testWithEngine(
public fun testWithEngine(
engine: HttpClientEngine,
block: suspend TestClientBuilder<*>.() -> Unit
) = testWithClient(HttpClient(engine), block)
Expand Down Expand Up @@ -49,7 +59,7 @@ private fun testWithClient(
/**
* Perform test with selected client engine [factory].
*/
fun <T : HttpClientEngineConfig> testWithEngine(
public fun <T : HttpClientEngineConfig> testWithEngine(
factory: HttpClientEngineFactory<T>,
loader: ClientLoader? = null,
block: suspend TestClientBuilder<T>.() -> Unit
Expand Down Expand Up @@ -95,22 +105,22 @@ private suspend fun concurrency(level: Int, block: suspend (Int) -> Unit) {

@InternalAPI
@Suppress("KDocMissingDocumentation")
class TestClientBuilder<T : HttpClientEngineConfig>(
var config: HttpClientConfig<T>.() -> Unit = {},
var test: suspend TestInfo.(client: HttpClient) -> Unit = {},
var repeatCount: Int = 1,
public class TestClientBuilder<T : HttpClientEngineConfig>(
public var config: HttpClientConfig<T>.() -> Unit = {},
public var test: suspend TestInfo.(client: HttpClient) -> Unit = {},
public var repeatCount: Int = 1,
var dumpAfterDelay: Long = -1,
var concurrency: Int = 1
)

@InternalAPI
@Suppress("KDocMissingDocumentation")
fun <T : HttpClientEngineConfig> TestClientBuilder<T>.config(block: HttpClientConfig<T>.() -> Unit): Unit {
public fun <T : HttpClientEngineConfig> TestClientBuilder<T>.config(block: HttpClientConfig<T>.() -> Unit) {
config = block
}

@InternalAPI
@Suppress("KDocMissingDocumentation")
fun TestClientBuilder<*>.test(block: suspend TestInfo.(client: HttpClient) -> Unit): Unit {
public fun TestClientBuilder<*>.test(block: suspend TestInfo.(client: HttpClient) -> Unit) {
test = block
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ class MockedTests {
}

@Test
// TODO: unmute when JS IR serialization is fixed https://github.com/Kotlin/kotlinx.serialization/issues/751
fun testWithLongJson() = if (isKotlinJsIr) Unit else testWithEngine(MockEngine) {
fun testWithLongJson() = testWithEngine(MockEngine) {
config {
install(JsonFeature) {
serializer = KotlinxSerializer()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright 2014-2020 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

package io.ktor.client.tests.features

import io.ktor.client.features.*
import io.ktor.client.features.json.*
import io.ktor.client.request.*
import io.ktor.client.tests.utils.*
import io.ktor.http.*
import kotlinx.serialization.Serializable
import kotlin.test.*

@Serializable
class MyCustomObject(val message: String)

class SerializationTest : ClientLoader() {
@Test
fun testSendCustomObject() = clientTests {
config {
install(JsonFeature)
}

test { client ->
assertFailsWith<ClientRequestException> {
client.post {
url("https://google.com")
header(HttpHeaders.ContentType, ContentType.Application.Json)
body = MyCustomObject(message = "Hello World")
}
}
}
}

@Test
fun testSendStringWithSerialization() = clientTests {
config {
install(JsonFeature)
}

test { client ->
assertFailsWith<ClientRequestException> {
client.post {
url("https://google.com")
header(HttpHeaders.ContentType, ContentType.Application.Json)
body = "Hello, world"
}
}
}
}

@Test
fun testSendObjectWithoutContentType() = clientTests {
config {
install(JsonFeature)
}

test { client ->
assertFailsWith<IllegalStateException> {
client.post("$TEST_SERVER/json/object") {
body = MyCustomObject("Foo")
}
}
}
}
}

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit 451595f

Please sign in to comment.