Skip to content

Commit

Permalink
KTOR-6911 Fix UnsupportedContentTypeException in SSE on OkHttp (#4020)
Browse files Browse the repository at this point in the history
  • Loading branch information
marychatte committed Apr 12, 2024
1 parent 7c76fa7 commit 5f27f30
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@ internal fun Application.serverSentEvents() {
}
)
}

post {
call.respondSseEvents(
flow {
emit(SseEvent("Hello"))
}
)

}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,8 @@ internal fun OutgoingContent.convertToOkHttpBody(callContext: CoroutineContext):
StreamRequestBody(contentLength) { GlobalScope.writer(callContext) { writeTo(channel) }.channel }
}
is OutgoingContent.NoContent -> ByteArray(0).toRequestBody(null, 0, 0)
else -> throw UnsupportedContentTypeException(this)
is OutgoingContent.ContentWrapper -> delegate().convertToOkHttpBody(callContext)
is OutgoingContent.ProtocolUpgrade -> throw UnsupportedContentTypeException(this)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,4 +376,22 @@ class ServerSentEventsTest : ClientLoader(timeoutSeconds = 120) {
}
}
}

@Test
fun testPostRequest() = clientTests {
config {
install(SSE)
}

test { client ->
client.sse({
url("$TEST_SERVER/sse")
method = HttpMethod.Post
}) {
incoming.single().apply {
assertEquals("Hello", data)
}
}
}
}
}

0 comments on commit 5f27f30

Please sign in to comment.