https://kotlinlang.org logo
#ktor
Title
# ktor
d

dave08

04/19/2021, 3:34 PM
Hi! Is this the way to set content type in tests (I thought there would be a better way?):
Copy code
private fun ContentType.full() = "$contentType/$contentSubtype"

handleRequest(
        <http://HttpMethod.Post|HttpMethod.Post>, "/some-url") {
            addHeader(HttpHeaders.Accept, ContentType.Text.Plain.full())
        }
h

hfhbd

04/19/2021, 3:37 PM
"This is the way"
d

dave08

04/19/2021, 3:37 PM
🙈
They should really have some kind of
full()
function there, or even better, specialized header functions for testing that accept ContentType.
I guess more stuff in my utils package to add...
r

Rustam Siniukov

04/19/2021, 4:01 PM
Why do you need
full
function?
ContentType
has proper
toString
function. But we agree that testing API can be better and we are working on it. Please create sub tickets with your concerns or just write a comment here https://youtrack.jetbrains.com/issue/KTOR-1649
d

dave08

04/19/2021, 4:03 PM
I looked for toString first... but AFAIK it doesn't give that... I think it gives what a regular data class would give, also, its not an obvious place to look for the valid complete content type string...
h

hfhbd

04/19/2021, 4:21 PM
@Rustam Siniukov the ticket is private. But you are right, ContentType.Text.Plain.toString() results into "text/plain" and could be used with
addHeader(HttpHeaders.Accept, ContentType.Text.Plain.toString())
. Maybe it should accept
Any
like the client
header
api and call the
toString()
in
addHeader
👍 1
d

dave08

04/20/2021, 9:02 AM
The
HeaderValueWithParameters
that ContentType inherits from has:
Copy code
override fun toString(): String = when {
        parameters.isEmpty() -> content
        else -> {
            val size = content.length + parameters.sumBy { it.name.length + it.value.length + 3 }
            StringBuilder(size).apply {
                append(content)
                for (index in 0 until parameters.size) {
                    val (name, value) = parameters[index]
                    append("; ")
                    append(name)
                    append("=")
                    value.escapeIfNeededTo(this)
                }
            }.toString()
        }
    }
And ContentType itself doesn't implement
toString()
... so how could that work @hfhbd?
protected val content: String
in it does contain the full type, but it's protected...
h

hfhbd

04/20/2021, 9:13 AM
@dave08 But its super class overrides
toString()
, so
ContentType.Text.Plain.toString())
results into
text/plain
.
? I'm missing something?
You probably tried it out, and I just looked at the source code... I'm just a bit confused where that would come from...
Ok, I finally spotted it:
parameters.isEmpty() -> content
🤯... I think this isn't so clear. It would be much better to add a property for this. But, you're right.
r

Rustam Siniukov

04/20/2021, 12:27 PM
@hfhbd thanks, updated the visibility
4 Views