Missing path-parameter renders a page without remote-data (no 400)
authorKai Moritz <kai@juplo.de>
Tue, 14 Jan 2020 17:06:41 +0000 (18:06 +0100)
committerKai Moritz <kai@juplo.de>
Thu, 16 Jan 2020 10:17:48 +0000 (11:17 +0100)
* Added a test to MappingIT, that defines the expected behavior
* Fixed the behavior in HtmlController accordingly

src/main/java/de/juplo/demo/HtmlController.java
src/test/java/de/juplo/demo/HtmlControllerIT.java

index d26c8c5..0c5f276 100644 (file)
@@ -25,13 +25,15 @@ public class HtmlController
 
 
   @GetMapping("/")
-  public String fetch(Model model, @RequestParam String path)
+  public String fetch(Model model, @RequestParam(required = false) String path)
   {
     model.addAttribute(
         "text",
-        service
-            .getRemoteText(path)
-            .onErrorResume(t -> Mono.just(t.getMessage())));
+        path == null
+            ? ""
+            : service
+                  .getRemoteText(path)
+                  .onErrorResume(t -> Mono.just(t.getMessage())));
     return "home";
   }
 }
index bcf037c..b6bd98f 100644 (file)
@@ -50,4 +50,29 @@ public class HtmlControllerIT
         });
     verify(service).getRemoteText("foo");
   }
+
+  @Test
+  @DisplayName("Mapping for HtmlController: /")
+  void testUriWithoutParameter()
+  {
+    webClient
+        .get()
+        .uri("/")
+        .exchange()
+        .expectStatus().isOk()
+        .expectBody(String.class).value(rendered ->
+        {
+          Document doc = Jsoup.parse(rendered);
+          assertThat(
+              doc
+                  .select("html > body > main > div > div > div > pre")
+                  .isEmpty())
+              .isFalse();
+          assertThat(
+              doc
+                  .select("html > body > main > div > div > div > pre")
+                  .text())
+              .isEmpty();
+        });
+  }
 }