From a9c7767e9ad1897f0f0797452138f1730a51c362 Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Tue, 14 Jan 2020 17:08:19 +0100 Subject: [PATCH] The error-message is reanderd instead of the text, in case of a 404 * Added a unit-test, that defines the expected behavior * Fixed the behavior in HtmlController accordingly --- .../java/de/juplo/demo/HtmlController.java | 7 ++++++- .../de/juplo/demo/HtmlControllerTest.java | 20 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/juplo/demo/HtmlController.java b/src/main/java/de/juplo/demo/HtmlController.java index 5696718..d26c8c5 100644 --- a/src/main/java/de/juplo/demo/HtmlController.java +++ b/src/main/java/de/juplo/demo/HtmlController.java @@ -5,6 +5,7 @@ import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; +import reactor.core.publisher.Mono; /** @@ -26,7 +27,11 @@ public class HtmlController @GetMapping("/") public String fetch(Model model, @RequestParam String path) { - model.addAttribute("text", service.getRemoteText(path)); + model.addAttribute( + "text", + service + .getRemoteText(path) + .onErrorResume(t -> Mono.just(t.getMessage()))); return "home"; } } diff --git a/src/test/java/de/juplo/demo/HtmlControllerTest.java b/src/test/java/de/juplo/demo/HtmlControllerTest.java index 1cf7274..aede077 100644 --- a/src/test/java/de/juplo/demo/HtmlControllerTest.java +++ b/src/test/java/de/juplo/demo/HtmlControllerTest.java @@ -13,6 +13,7 @@ import static org.mockito.Mockito.when; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.ui.Model; +import org.springframework.web.reactive.function.client.WebClientResponseException; import reactor.core.publisher.Mono; import reactor.test.StepVerifier; @@ -56,4 +57,23 @@ public class HtmlControllerTest .expectComplete() .verify(); } + + @Test + @DisplayName("Data not found on remote-server") + void testNotFoud() + { + Mono mono = Mono.error(WebClientResponseException.create(404, "", null, null, null)); + when(service.getRemoteText("foo")).thenReturn(mono); + + String result = controller.fetch(model, "foo"); + + assertThat(result).isEqualTo("home"); + ArgumentCaptor> captor = ArgumentCaptor.forClass(Mono.class); + verify(model).addAttribute(eq("text"), captor.capture()); + StepVerifier + .create(captor.getValue()) + .expectNext("404 ") + .expectComplete() + .verify(); + } } -- 2.20.1