Last active
November 24, 2019 09:52
-
-
Save createdbypete/ac57c6b59133b184799fadbf32b4b93a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example | |
import io.ktor.application.ApplicationCall | |
import io.ktor.application.call | |
import io.ktor.application.install | |
import io.ktor.features.ContentConverter | |
import io.ktor.features.ContentNegotiation | |
import io.ktor.http.* | |
import io.ktor.request.ApplicationReceiveRequest | |
import io.ktor.request.receive | |
import io.ktor.response.respond | |
import io.ktor.routing.post | |
import io.ktor.routing.routing | |
import io.ktor.server.testing.handleRequest | |
import io.ktor.server.testing.setBody | |
import io.ktor.server.testing.withTestApplication | |
import io.ktor.util.pipeline.PipelineContext | |
import org.junit.Test | |
import kotlin.test.assertEquals | |
import kotlin.test.assertNotNull | |
class FormDataConverter : ContentConverter { | |
override suspend fun convertForReceive(context: PipelineContext<ApplicationReceiveRequest, ApplicationCall>): Any? { | |
TODO("not implemented") | |
} | |
override suspend fun convertForSend( | |
context: PipelineContext<Any, ApplicationCall>, | |
contentType: ContentType, | |
value: Any | |
): Any? { | |
TODO("not implemented") | |
} | |
} | |
class FormDataConverterTest { | |
@Test | |
fun testMap() = withTestApplication { | |
val uc = "\u0422" | |
application.install(ContentNegotiation) { | |
register(ContentType.Application.FormUrlEncoded, FormDataConverter()) | |
} | |
application.routing { | |
post("/") { | |
val map = call.receive<Map<*, *>>() | |
val text = map.entries.joinToString { "${it.key}=${it.value}" } | |
call.respond(text) | |
} | |
} | |
handleRequest(HttpMethod.Post, "/") { | |
addHeader("Accept", "text/plain") | |
addHeader("Content-Type", "application/x-www-form-urlencoded") | |
setBody("""id=1&title=Hello,%20World!&unicode=$uc""") | |
}.response.let { response -> | |
assertEquals(HttpStatusCode.OK, response.status()) | |
assertNotNull(response.content) | |
assertEquals(listOf("""id=1, title=Hello, World!, unicode=$uc"""), response.content!!.lines()) | |
val contentTypeText = assertNotNull(response.headers[HttpHeaders.ContentType]) | |
assertEquals(ContentType.Text.Plain.withCharset(Charsets.UTF_8), ContentType.parse(contentTypeText)) | |
} | |
} | |
@Test | |
fun testEntity() = withTestApplication { | |
val uc = "\u0422" | |
application.install(ContentNegotiation) { | |
register(ContentType.Application.FormUrlEncoded, FormDataConverter()) | |
} | |
application.routing { | |
post("/") { | |
val entity = call.receive<MyEntity>() | |
call.respond(entity.toString()) | |
} | |
} | |
handleRequest(HttpMethod.Post, "/") { | |
addHeader("Content-Type", "application/x-www-form-urlencoded") | |
setBody("""id=777&name=Cargo&children[0][item]=Qube&children[0][quantity]=1&children[1][item]=Sphere&children[1][quantity]=2&children[1][item]=$uc&children[1][quantity]=3""") | |
}.response.let { response -> | |
assertEquals(HttpStatusCode.OK, response.status()) | |
assertNotNull(response.content) | |
assertEquals( | |
listOf("""MyEntity(id=777, name=Cargo, children=[ChildEntity(item=Qube, quantity=1), ChildEntity(item=Sphere, quantity=2), ChildEntity(item=$uc, quantity=3)])"""), | |
response.content!!.lines() | |
) | |
val contentTypeText = assertNotNull(response.headers[HttpHeaders.ContentType]) | |
assertEquals(ContentType.Text.Plain.withCharset(Charsets.UTF_8), ContentType.parse(contentTypeText)) | |
} | |
} | |
} | |
data class MyEntity(val id: Int, val name: String, val children: List<ChildEntity>) | |
data class ChildEntity(val item: String, val quantity: Int) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment