forked from micronaut-projects/micronaut-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Method binding with no @Body TCK tests, updated docs and docs example…
…s. (micronaut-projects#9479) * Add http-tck tests for json and form data body and controller with no `@Body` annotation. Update binding docs, and add docs examples for java, groovy, kotlin. closes micronaut-projects#9388
- Loading branch information
Showing
14 changed files
with
614 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
test-suite-groovy/src/test/groovy/io/micronaut/docs/server/binding/Point.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright 2017-2023 original authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.micronaut.docs.server.binding | ||
|
||
import io.micronaut.core.annotation.Introspected | ||
|
||
@Introspected | ||
class Point { | ||
|
||
Integer x | ||
Integer y | ||
|
||
Point(Integer x, Integer y) { | ||
this.x = x | ||
this.y = y | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
test-suite-groovy/src/test/groovy/io/micronaut/docs/server/binding/PointController.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright 2017-2023 original authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.micronaut.docs.server.binding | ||
|
||
import io.micronaut.context.annotation.Requires | ||
import io.micronaut.http.HttpStatus | ||
import io.micronaut.http.MediaType | ||
import io.micronaut.http.annotation.Consumes | ||
import io.micronaut.http.annotation.Controller | ||
import io.micronaut.http.annotation.Post | ||
import io.micronaut.http.annotation.Status | ||
|
||
@Requires(property = 'spec.name', value = 'PointControllerSpec') | ||
// tag::class[] | ||
@Controller("/point") | ||
class PointController { | ||
|
||
@Post(uri = "/no-body-json") | ||
@Status(HttpStatus.CREATED) | ||
Point noBodyJson(Integer x, Integer y) { // (1) | ||
new Point(x,y) | ||
} | ||
|
||
@Consumes(MediaType.APPLICATION_FORM_URLENCODED) | ||
@Post("/no-body-form") | ||
@Status(HttpStatus.CREATED) | ||
Point noBodyForm(Integer x, Integer y) { // (2) | ||
new Point(x,y) | ||
} | ||
} | ||
// end::class[] |
70 changes: 70 additions & 0 deletions
70
...-suite-groovy/src/test/groovy/io/micronaut/docs/server/binding/PointControllerSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Copyright 2017-2023 original authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.micronaut.docs.server.binding | ||
|
||
import io.micronaut.context.ApplicationContext | ||
import io.micronaut.http.HttpHeaders | ||
import io.micronaut.http.HttpRequest | ||
import io.micronaut.http.HttpResponse | ||
import io.micronaut.http.MediaType | ||
import io.micronaut.http.client.HttpClient | ||
import io.micronaut.runtime.server.EmbeddedServer | ||
import spock.lang.AutoCleanup | ||
import spock.lang.Shared | ||
import spock.lang.Specification | ||
|
||
class PointControllerSpec extends Specification { | ||
|
||
@Shared | ||
@AutoCleanup | ||
EmbeddedServer embeddedServer = ApplicationContext.run( | ||
EmbeddedServer, ['spec.name': 'PointControllerSpec']) | ||
@Shared | ||
@AutoCleanup | ||
HttpClient client = embeddedServer.applicationContext.createBean(HttpClient, embeddedServer.getURL()) | ||
|
||
void "test JSON with no @Body endpoint"() { | ||
given: | ||
HttpRequest<String> httpRequest = HttpRequest | ||
.POST("/point/no-body-json", '{"x":10,"y":20}') | ||
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON) | ||
|
||
when: | ||
HttpResponse<Point> response = client.toBlocking().exchange(httpRequest, Point) | ||
|
||
then: | ||
assertResult(response.body.orElse(null)) | ||
} | ||
|
||
void "test Form data with no @Body endpoint"() { | ||
given: | ||
HttpRequest<String> httpRequest = HttpRequest | ||
.POST("/point/no-body-form", 'x=10&y=20') | ||
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED) | ||
|
||
when: | ||
HttpResponse<Point> response = client.toBlocking().exchange(httpRequest, Point) | ||
|
||
then: | ||
assertResult(response.body.orElse(null)) | ||
} | ||
|
||
void assertResult(Point p) { | ||
p | ||
p.x == 10 | ||
p.y == 20 | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
test-suite-kotlin-ksp/src/test/kotlin/io/micronaut/docs/server/binding/Point.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* Copyright 2017-2023 original authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.micronaut.docs.server.binding | ||
|
||
import io.micronaut.core.annotation.Introspected | ||
|
||
@Introspected | ||
data class Point (val x: Int, val y: Int) | ||
|
40 changes: 40 additions & 0 deletions
40
test-suite-kotlin-ksp/src/test/kotlin/io/micronaut/docs/server/binding/PointController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright 2017-2023 original authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.micronaut.docs.server.binding | ||
|
||
import io.micronaut.context.annotation.Requires | ||
import io.micronaut.http.HttpStatus | ||
import io.micronaut.http.MediaType | ||
import io.micronaut.http.annotation.Consumes | ||
import io.micronaut.http.annotation.Controller | ||
import io.micronaut.http.annotation.Post | ||
import io.micronaut.http.annotation.Status | ||
|
||
@Requires(property = "spec.name", value = "PointControllerTest") | ||
// tag::class[] | ||
@Controller("/point") | ||
class PointController { | ||
|
||
@Post(uri = "/no-body-json") | ||
@Status(HttpStatus.CREATED) | ||
fun noBodyJson(x: Int, y: Int) = Point(x,y) // (1) | ||
|
||
@Consumes(MediaType.APPLICATION_FORM_URLENCODED) | ||
@Post("/no-body-form") | ||
@Status(HttpStatus.CREATED) | ||
fun noBodyForm(x: Int, y: Int) = Point(x,y) // (2) | ||
} | ||
// end::class[] |
64 changes: 64 additions & 0 deletions
64
...-suite-kotlin-ksp/src/test/kotlin/io/micronaut/docs/server/binding/PointControllerTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright 2017-2023 original authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.micronaut.docs.server.binding | ||
|
||
import io.kotest.core.spec.style.StringSpec | ||
import io.kotest.matchers.shouldBe | ||
import io.kotest.matchers.shouldNotBe | ||
import io.micronaut.context.ApplicationContext | ||
import io.micronaut.http.HttpHeaders | ||
import io.micronaut.http.HttpRequest | ||
import io.micronaut.http.MediaType | ||
import io.micronaut.http.client.HttpClient | ||
import io.micronaut.runtime.server.EmbeddedServer | ||
import java.util.Map | ||
|
||
class PointControllerTest : StringSpec() { | ||
|
||
val embeddedServer = autoClose( | ||
ApplicationContext.run(EmbeddedServer::class.java, Map.of<String, Any>("spec.name", "PointControllerTest")) | ||
) | ||
|
||
val client = autoClose( | ||
embeddedServer.applicationContext.createBean(HttpClient::class.java, embeddedServer.getURL()) | ||
) | ||
|
||
init { | ||
"test JSON with no @Body endpoint"() { | ||
val httpRequest: HttpRequest<String> = HttpRequest | ||
.POST("/point/no-body-json", "{\"x\":10,\"y\":20}") | ||
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON) | ||
val response = client!!.toBlocking().exchange(httpRequest, Point::class.java) | ||
|
||
assertResult(response.body.orElse(null)) | ||
} | ||
|
||
"test Form data with no @Body endpoint"() { | ||
val httpRequest: HttpRequest<String> = HttpRequest | ||
.POST("/point/no-body-form", "x=10&y=20") | ||
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED) | ||
val response = client!!.toBlocking().exchange(httpRequest, Point::class.java) | ||
|
||
assertResult(response.body.orElse(null)) | ||
} | ||
} | ||
|
||
private fun assertResult(p: Point) { | ||
p shouldNotBe null | ||
p.x shouldBe 10 | ||
p.y shouldBe 20 | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
test-suite-kotlin/src/test/kotlin/io/micronaut/docs/server/binding/Point.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* Copyright 2017-2023 original authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.micronaut.docs.server.binding | ||
|
||
import io.micronaut.core.annotation.Introspected | ||
|
||
@Introspected | ||
data class Point (val x: Int, val y: Int) | ||
|
Oops, something went wrong.