From 12efc629a3ee4062e4398acdb0b53fdec018059d Mon Sep 17 00:00:00 2001 From: Bhavya Agrawal <72398995+Bhavya-official@users.noreply.github.com> Date: Wed, 16 Mar 2022 18:23:35 +0530 Subject: [PATCH] [CORRECTION] Add Test Support for @RequestMapping NoPath, OnlySlashPath, MissingSlashLeadingPath. (#692) --- .../support/SpringMvcContractTests.java | 45 ++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/support/SpringMvcContractTests.java b/spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/support/SpringMvcContractTests.java index 9637d5a6..44f85862 100644 --- a/spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/support/SpringMvcContractTests.java +++ b/spring-cloud-openfeign-core/src/test/java/org/springframework/cloud/openfeign/support/SpringMvcContractTests.java @@ -78,7 +78,9 @@ import static org.junit.jupiter.api.Assumptions.assumeTrue; * @author Olga Maciaszek-Sharma * @author Szymon Linowski * @author Sam Kruglov - */ + * @author Bhavya Agrawal + **/ + class SpringMvcContractTests { private static final Class EXECUTABLE_TYPE; @@ -171,6 +173,41 @@ class SpringMvcContractTests { assertThat(data.indexToName().get(0).iterator().next()).isEqualTo("id"); } + @Test + void testProcessAnnotations_SimpleNoPath() throws Exception { + Method method = TestTemplate_Simple.class.getDeclaredMethod("getTest"); + MethodMetadata data = contract.parseAndValidateMetadata(method.getDeclaringClass(), method); + + assertThat(data.template().url()).isEqualTo("/"); + assertThat(data.template().method()).isEqualTo("GET"); + assertThat(data.template().headers().get("Accept").iterator().next()) + .isEqualTo(MediaType.APPLICATION_JSON_VALUE); + } + + @Test + void testProcessAnnotations_SimplePathIsOnlyASlash() throws Exception { + Method method = TestTemplate_Simple.class.getDeclaredMethod("getSlashPath", String.class); + MethodMetadata data = contract + .parseAndValidateMetadata(method.getDeclaringClass(), method); + + assertThat(data.template().url()).isEqualTo("/?id=" + "{id}"); + assertThat(data.template().method()).isEqualTo("GET"); + assertThat(data.template().headers().get("Accept").iterator().next()) + .isEqualTo(MediaType.APPLICATION_JSON_VALUE); + } + + @Test + void testProcessAnnotations_MissingLeadingSlashInPath() throws Exception { + Method method = TestTemplate_Simple.class.getDeclaredMethod("getTestNoLeadingSlash", String.class); + MethodMetadata data = contract + .parseAndValidateMetadata(method.getDeclaringClass(), method); + + assertThat(data.template().url()).isEqualTo("/test?name=" + "{name}"); + assertThat(data.template().method()).isEqualTo("GET"); + assertThat(data.template().headers().get("Accept").iterator().next()) + .isEqualTo(MediaType.APPLICATION_JSON_VALUE); + } + @Test void testProcessAnnotations_SimpleGetMapping() throws Exception { Method method = TestTemplate_Simple.class.getDeclaredMethod("getMappingTest", String.class); @@ -616,6 +653,12 @@ class SpringMvcContractTests { @PostMapping(produces = MediaType.APPLICATION_JSON_VALUE) TestObject postMappingTest(@RequestBody TestObject object); + @GetMapping(value = "/", produces = MediaType.APPLICATION_JSON_VALUE) + ResponseEntity getSlashPath(@RequestParam("id") String id); + + @GetMapping(path = "test", produces = MediaType.APPLICATION_JSON_VALUE) + ResponseEntity getTestNoLeadingSlash(@RequestParam("name") String name); + } @RequestMapping("/prepend/{classId}")