Skip to content

Commit

Permalink
fix codestyle, run apiDump, add test
Browse files Browse the repository at this point in the history
  • Loading branch information
marychatte committed Nov 18, 2024
1 parent 4e022e4 commit 5324ecd
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 8 deletions.
4 changes: 4 additions & 0 deletions ktor-http/ktor-http-cio/api/ktor-http-cio.api
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ public final class io/ktor/http/cio/Response : io/ktor/http/cio/HttpMessage {
public final fun getVersion ()Ljava/lang/CharSequence;
}

public final class io/ktor/http/cio/UnsupportedMediaTypeExceptionCIO : java/io/IOException {
public fun <init> (Ljava/lang/String;)V
}

public final class io/ktor/http/cio/internals/CharsKt {
public static final fun parseDecLong (Ljava/lang/CharSequence;)J
}
Expand Down
8 changes: 6 additions & 2 deletions ktor-http/ktor-http-cio/jvm/src/io/ktor/http/cio/Multipart.kt
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@ public fun CoroutineScope.parseMultipart(
headers: HttpHeadersMap,
maxPartSize: Long = Long.MAX_VALUE
): ReceiveChannel<MultipartEvent> {
val contentType = headers["Content-Type"] ?: throw UnsupportedMediaTypeExceptionCIO("Failed to parse multipart: no Content-Type header")
val contentType = headers["Content-Type"] ?: throw UnsupportedMediaTypeExceptionCIO(
"Failed to parse multipart: no Content-Type header"
)
val contentLength = headers["Content-Length"]?.parseDecLong()

return parseMultipart(input, contentType, contentLength, maxPartSize)
Expand All @@ -154,7 +156,9 @@ public fun CoroutineScope.parseMultipart(
maxPartSize: Long = Long.MAX_VALUE,
): ReceiveChannel<MultipartEvent> {
if (!contentType.startsWith("multipart/", ignoreCase = true)) {
throw UnsupportedMediaTypeExceptionCIO("Failed to parse multipart: Content-Type should be multipart/* but it is $contentType")
throw UnsupportedMediaTypeExceptionCIO(
"Failed to parse multipart: Content-Type should be multipart/* but it is $contentType"
)
}
val boundaryByteBuffer = parseBoundaryInternal(contentType)
val boundaryBytes = ByteString(boundaryByteBuffer)
Expand Down
2 changes: 1 addition & 1 deletion ktor-server/ktor-server-core/api/ktor-server-core.klib.api
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,7 @@ final class io.ktor.server.plugins/PayloadTooLargeException : io.ktor.server.plu
}

final class io.ktor.server.plugins/UnsupportedMediaTypeException : io.ktor.server.plugins/ContentTransformationException, kotlinx.coroutines/CopyableThrowable<io.ktor.server.plugins/UnsupportedMediaTypeException> { // io.ktor.server.plugins/UnsupportedMediaTypeException|null[0]
constructor <init>(io.ktor.http/ContentType) // io.ktor.server.plugins/UnsupportedMediaTypeException.<init>|<init>(io.ktor.http.ContentType){}[0]
constructor <init>(io.ktor.http/ContentType?) // io.ktor.server.plugins/UnsupportedMediaTypeException.<init>|<init>(io.ktor.http.ContentType?){}[0]

final fun createCopy(): io.ktor.server.plugins/UnsupportedMediaTypeException // io.ktor.server.plugins/UnsupportedMediaTypeException.createCopy|createCopy(){}[0]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ public class UnsupportedMediaTypeException(
) : ContentTransformationException(
contentType?.let { "Content type $it is not supported" }
?: "Content-Type header is required"
), CopyableThrowable<UnsupportedMediaTypeException> {
),
CopyableThrowable<UnsupportedMediaTypeException> {

override fun createCopy(): UnsupportedMediaTypeException = UnsupportedMediaTypeException(contentType).also {
it.initCauseBridge(this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@

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.multiPartData
import io.ktor.server.plugins.UnsupportedMediaTypeException
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.TestScope
import kotlinx.coroutines.test.runTest
import kotlinx.coroutines.test.*
import kotlin.test.*

class MultiPartDataTest {
Expand Down Expand Up @@ -52,4 +55,24 @@ class MultiPartDataTest {
}
}
}

@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)
}
}
}

0 comments on commit 5324ecd

Please sign in to comment.