-
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 Rethrow UnsupportedMediaTypeException if content type heade…
…r is not present or is invalid
- Loading branch information
Gleb Nazarov
authored and
Gleb Nazarov
committed
Oct 7, 2024
1 parent
58d1714
commit 5735df8
Showing
3 changed files
with
75 additions
and
11 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
55 changes: 55 additions & 0 deletions
55
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,55 @@ | ||
/* | ||
* 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.http.* | ||
import io.ktor.server.application.* | ||
import io.ktor.server.engine.multiPartData | ||
import io.ktor.server.plugins.UnsupportedMediaTypeException | ||
import io.ktor.server.request.* | ||
import io.ktor.util.pipeline.* | ||
import io.ktor.utils.io.* | ||
import io.mockk.* | ||
import kotlinx.coroutines.* | ||
import kotlinx.coroutines.test.TestScope | ||
import kotlinx.coroutines.test.runTest | ||
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() { | ||
// Setup | ||
every { mockContext.call.request } returns mockRequest | ||
every { mockRequest.header(HttpHeaders.ContentType) } returns null | ||
|
||
// Act & Assert | ||
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) | ||
} | ||
} | ||
} | ||
} |