<commit_before>package io.ktor.server.plugins.contentnegotiation

import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.*
import io.ktor.serialization.*
import io.ktor.server.application.*
import io.ktor.server.request.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import io.ktor.server.testing.*
import io.ktor.util.reflect.*
import io.ktor.utils.io.*
import io.ktor.utils.io.charsets.*
import kotlin.test.*

/*
 * Copyright 2014-2022 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
 */

class RequestConverterTest {

    @Test
    fun testIgnoreType() = testApplication {
        var used = false

        install(ContentNegotiation) {
            ignoreType<NonSerializableClass>()
            register(
                ContentType.Application.Json,
                object : ContentConverter {
                    override suspend fun deserialize(
                        charset: Charset,
                        typeInfo: TypeInfo,
                        content: ByteReadChannel
                    ): Any? {
                        used = true
                        return null
                    }
                }
            )
        }

        routing {
            post("/foo") {
                val result: String = try {
                    call.receive<NonSerializableClass>()
                    "OK"
                } catch (cause: Throwable) {
                    cause.message ?: cause.toString()
                }
                call.respondText(result)
            }
            post("/bar") {
                val result: String = try {
                    call.receive<SerializableClass>()
                    "OK"
                } catch (cause: Throwable) {
                    cause.message ?: cause.toString()
                }
                call.respondText(result)
            }
        }

        val responseFoo = client.post("/foo") {
            contentType(ContentType.Application.Json)
        }

        assertEquals(
            "Cannot transform this request's content to io.ktor.server.plugins.contentnegotiation.NonSerializableClass",
            responseFoo.bodyAsText()
        )
        assertFalse(used)

        val responseBar = client.post("/bar") {
            contentType(ContentType.Application.Json)
        }

        assertEquals(
            "No suitable converter found for TypeInfo(" +
                "type=class io.ktor.server.plugins.contentnegotiation.SerializableClass, " +
                "reifiedType=class io.ktor.server.plugins.contentnegotiation.SerializableClass, " +
                "kotlinType=io.ktor.server.plugins.contentnegotiation.SerializableClass" +
                ")",
            responseBar.bodyAsText()
        )
        assertTrue(used)
    }
}

internal class NonSerializableClass

internal class SerializableClass<commit_msg>KTOR-4770 Fix exception message (#3148)<commit_after>/*
 * Copyright 2014-2022 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
 */

package io.ktor.server.plugins.contentnegotiation

import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.*
import io.ktor.serialization.*
import io.ktor.server.application.*
import io.ktor.server.request.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import io.ktor.server.testing.*
import io.ktor.util.reflect.*
import io.ktor.utils.io.*
import io.ktor.utils.io.charsets.*
import kotlin.test.*

class RequestConverterTest {

    @Test
    fun testIgnoreType() = testApplication {
        var used = false

        install(ContentNegotiation) {
            ignoreType<NonSerializableClass>()
            register(
                ContentType.Application.Json,
                object : ContentConverter {
                    override suspend fun deserialize(
                        charset: Charset,
                        typeInfo: TypeInfo,
                        content: ByteReadChannel
                    ): Any? {
                        used = true
                        return null
                    }
                }
            )
        }

        routing {
            post("/foo") {
                val result: String = try {
                    call.receive<NonSerializableClass>()
                    "OK"
                } catch (cause: Throwable) {
                    cause.message ?: cause.toString()
                }
                call.respondText(result)
            }
            post("/bar") {
                val result: String = try {
                    call.receive<SerializableClass>()
                    "OK"
                } catch (cause: Throwable) {
                    cause.message ?: cause.toString()
                }
                call.respondText(result)
            }
        }

        val responseFoo = client.post("/foo") {
            contentType(ContentType.Application.Json)
        }

        assertEquals(
            "Cannot transform this request's content to io.ktor.server.plugins.contentnegotiation.NonSerializableClass",
            responseFoo.bodyAsText()
        )
        assertFalse(used)

        val responseBar = client.post("/bar") {
            contentType(ContentType.Application.Json)
        }

        assertTrue(responseBar.bodyAsText().startsWith("No suitable converter found for TypeInfo"))
        assertTrue(used)
    }
}

internal class NonSerializableClass

internal class SerializableClass
{'fraction_non_alphanumeric': 0.09219088937093275, 'fraction_numerical': 0.005061460592913955, 'mean_word_length': 1.5675174013921114, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1924692', 'n_tokens_mistral': 1432, 'n_tokens_neox': 1355, 'n_words': 281}