question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Test a POST with MultiPart using TestApplicationEngine does not success or fail

See original GitHub issue

Ktor 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:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
e5lcommented, Apr 5, 2021

Fixed in 1.5.2

1reaction
razvncommented, Jul 12, 2020

@neelkamath just doing

call.receiveMultipart().forEachPart { it.dispose }

is enough to make it work.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found