Test a POST with MultiPart using TestApplicationEngine does not success or fail
See original GitHub issueKtor 1.3.2, TestApplicationEngine
When running a test that sends post with multipart it runs forever and never stops.
No problem when server really runs. It’s just the test.
To Reproduce
Server:
fun main(args: Array<String>): Unit = io.ktor.server.netty.EngineMain.main(args)
@Suppress("unused") // Referenced in application.conf
@kotlin.jvm.JvmOverloads
fun Application.module(testing: Boolean = false) {
install(ContentNegotiation) {
}
routing {
post("/") {
val mp = try {
call.receiveMultipart()
} catch (e: Exception) {
call.application.log.error("Getting multipart error"")
null
}
if (mp == null) call.respond(HttpStatusCode.BadRequest, "Null value")
else call.respond(HttpStatusCode.OK,"OK")
}
}
}
Test:
internal class ApplicationKtTest {
private val boundary = "***bbb***"
private val sysTempDir = System.getProperty("java.io.tmpdir") ?: "/tmp"
private val multipart = listOf(PartData.FileItem({ byteArrayOf(1, 2, 3).inputStream().asInput() }, {}, headersOf(
HttpHeaders.ContentDisposition,
ContentDisposition.File
.withParameter(ContentDisposition.Parameters.Name, "file")
.withParameter(ContentDisposition.Parameters.FileName, "test.jpg")
.toString()
)))
@Test
fun testUploadApplication() = testApp {
handlePost("/", boundary, multipart).apply {
println("============= RESPONSE ====================")
println(response.content)
println("=================================")
}
}
}
private fun TestApplicationEngine.handlePost(uri: String,
boundary: String,
multipart: List<PartData.FileItem>,
setup: TestApplicationRequest.() -> Unit = {}
): TestApplicationCall {
return handleRequest(method = HttpMethod.Post, uri = uri) {
addHeader(HttpHeaders.ContentType,
ContentType.MultiPart.FormData.withParameter("boundary", boundary).toString()
)
setBody(boundary, multipart)
setup()
}
}
private fun testApp(callback: TestApplicationEngine.() -> Unit): Unit {
withTestApplication({ module(true) }, callback)
}
The test runs forever and gives no result. if addHeader
and setBody
are commented there is a response.
Expected behavior
The test should print OK
Repository If you just want to clone a test repository.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:6 (1 by maintainers)
Top Results From Across the Web
Testing a Spring Boot Multipart POST Request using Junit ...
POST and GET requests APIs can easily be tested with postman without any error. In the POST request, I send an image file...
Read more >Testing
Ktor provides a special testing engine that doesn't create a web server, doesn't bind to sockets, and doesn't make any real HTTP requests....
Read more >Tool for sending multipart/form-data request with Postman
UPDATE: Newer tutorial available: https://www.youtube.com/watch?v=c07IsbSNqfIIn this video tutorial, I will show you how to debug an upload ...
Read more >Testing file uploads with Postman (multipart/form-data)
I will show you how to debug an upload script and demonstrate it with a tool called Postman that can make requests encoded...
Read more >ktorio/ktor 1.5.2 on GitHub
... feature doesn't change the CIO's timeout (KTOR-2000); Fixed test a POST with MultiPart using TestApplicationEngine does not success or fail (KTOR-345) ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Fixed in 1.5.2
@neelkamath just doing
is enough to make it work.