Hi! Is this the way to set content type in tests (...
# ktor
d
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
"This is the way"
d
🙈
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
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
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
@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
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
@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
@hfhbd thanks, updated the visibility