Skip to content

Commit 98a6ad1

Browse files
committed
removed if statement cancelling body handling, added post route test
1 parent ad0a820 commit 98a6ad1

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

src/main/kotlin/com/papsign/ktor/openapigen/route/OpenAPIRoute.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ abstract class OpenAPIRoute<T : OpenAPIRoute<T>>(val ktorRoute: Route, val provi
6363
pass(this, responder, PHandler.handle(params), Unit as B)
6464
}
6565
} else {
66-
if(paramsClass == Unit::class)
6766
getContentTypesMap(bodyClass).forEach { (contentType, parsers) ->
6867
contentType(contentType) {
6968
handle {
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.papsign.ktor.openapigen.routing
2+
3+
import com.papsign.ktor.openapigen.annotations.parameters.HeaderParam
4+
import com.papsign.ktor.openapigen.route.apiRouting
5+
import com.papsign.ktor.openapigen.route.path.normal.post
6+
import com.papsign.ktor.openapigen.route.response.respond
7+
import com.papsign.ktor.openapigen.route.route
8+
import installJackson
9+
import installOpenAPI
10+
import io.ktor.http.HttpHeaders
11+
import io.ktor.http.HttpMethod
12+
import io.ktor.routing.Routing
13+
import io.ktor.server.testing.contentType
14+
import io.ktor.server.testing.handleRequest
15+
import io.ktor.server.testing.setBody
16+
import io.ktor.server.testing.withTestApplication
17+
import org.junit.Assert.assertEquals
18+
import org.junit.Test
19+
import kotlin.test.assertTrue
20+
21+
class RoutingTest {
22+
23+
data class TestHeaderParams(@HeaderParam("test param") val `Test-Header`: Long)
24+
data class TestBodyParams(val xyz: Long)
25+
data class TestResponse(val msg: String)
26+
27+
@Test
28+
fun testPostWithHeaderAndBodyParams() {
29+
val route = "/test"
30+
withTestApplication({
31+
installOpenAPI()
32+
installJackson()
33+
apiRouting {
34+
(this.ktorRoute as Routing).trace { println(it.buildText()) }
35+
route(route) {
36+
post<TestHeaderParams, TestResponse, TestBodyParams> { params, body ->
37+
respond(TestResponse("$params -> $body"))
38+
}
39+
}
40+
}
41+
}) {
42+
handleRequest(HttpMethod.Post, route) {
43+
addHeader(HttpHeaders.ContentType, "application/json")
44+
addHeader(HttpHeaders.Accept, "application/json")
45+
addHeader("Test-Header", "123")
46+
setBody("{\"xyz\":456}")
47+
}.apply {
48+
assertTrue { response.contentType().match("application/json") }
49+
assertEquals(
50+
"{\"msg\":\"${TestHeaderParams(123)} -> ${TestBodyParams(456)}\"}",
51+
response.content
52+
)
53+
}
54+
}
55+
}
56+
}

0 commit comments

Comments
 (0)