-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
KTOR-7470 receiveMultipart throw UnsupportedMediaTypeException (#4339)
- Loading branch information
Showing
9 changed files
with
127 additions
and
14 deletions.
There are no files selected for viewing
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
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
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
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
11 changes: 11 additions & 0 deletions
11
ktor-http/ktor-http-cio/common/src/io/ktor/http/cio/internals/Errors.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/* | ||
* Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package io.ktor.http.cio.internals | ||
|
||
import io.ktor.utils.io.* | ||
import kotlinx.io.IOException | ||
|
||
@InternalAPI | ||
public class UnsupportedMediaTypeExceptionCIO(message: String) : IOException(message) |
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
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
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
78 changes: 78 additions & 0 deletions
78
ktor-server/ktor-server-core/jvm/test/io/ktor/tests/server/engine/MultiPartDataTest.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* Copyright 2014-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package io.ktor.tests.server.engine | ||
|
||
import io.ktor.client.request.* | ||
import io.ktor.http.* | ||
import io.ktor.server.application.* | ||
import io.ktor.server.engine.* | ||
import io.ktor.server.plugins.* | ||
import io.ktor.server.request.* | ||
import io.ktor.server.response.* | ||
import io.ktor.server.routing.* | ||
import io.ktor.server.testing.* | ||
import io.ktor.util.pipeline.* | ||
import io.ktor.utils.io.* | ||
import io.mockk.* | ||
import kotlinx.coroutines.* | ||
import kotlinx.coroutines.test.* | ||
import kotlin.test.* | ||
|
||
class MultiPartDataTest { | ||
private val mockContext = mockk<PipelineContext<*, PipelineCall>>(relaxed = true) | ||
private val mockRequest = mockk<PipelineRequest>(relaxed = true) | ||
private val testScope = TestScope() | ||
|
||
@Test | ||
fun givenRequest_whenNoContentTypeHeaderPresent_thenUnsupportedMediaTypeException() { | ||
// Given | ||
every { mockContext.call.request } returns mockRequest | ||
every { mockRequest.header(HttpHeaders.ContentType) } returns null | ||
|
||
// When & Then | ||
assertFailsWith<UnsupportedMediaTypeException> { | ||
runBlocking { mockContext.multiPartData(ByteReadChannel("sample data")) } | ||
} | ||
} | ||
|
||
@Test | ||
fun givenWrongContentType_whenProcessMultiPart_thenUnsupportedMediaTypeException() { | ||
// Given | ||
val rc = ByteReadChannel("sample data") | ||
val contentType = "test/plain; boundary=test" | ||
val contentLength = "123" | ||
every { mockContext.call.request } returns mockRequest | ||
every { mockContext.call.attributes.getOrNull<Long>(any()) } returns 0L | ||
every { mockRequest.header(HttpHeaders.ContentType) } returns contentType | ||
every { mockRequest.header(HttpHeaders.ContentLength) } returns contentLength | ||
|
||
// When & Then | ||
testScope.runTest { | ||
assertFailsWith<UnsupportedMediaTypeException> { | ||
mockContext.multiPartData(rc) | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
fun testUnsupportedMediaTypeStatusCode() = testApplication { | ||
routing { | ||
post { | ||
call.receiveMultipart() | ||
call.respond(HttpStatusCode.OK) | ||
} | ||
} | ||
|
||
client.post { | ||
accept(ContentType.Text.Plain) | ||
}.apply { | ||
assertEquals(HttpStatusCode.UnsupportedMediaType, status) | ||
} | ||
|
||
client.post {}.apply { | ||
assertEquals(HttpStatusCode.UnsupportedMediaType, status) | ||
} | ||
} | ||
} |