private ServerWebExchange testHandle(String path, MethodParameter returnType, Object returnValue, String responseBody, ViewResolver... resolvers) { Model model = this.bindingContext.getModel(); model.asMap().clear(); model.addAttribute("id", "123"); HandlerResult result = new HandlerResult(new Object(), returnValue, returnType, this.bindingContext); MockServerWebExchange exchange = MockServerWebExchange.from(get(path)); resultHandler(resolvers).handleResult(exchange, result).block(Duration.ofSeconds(5)); assertResponseBody(exchange, responseBody); return exchange; }
private void testDefaultViewName(Object returnValue, MethodParameter returnType) { this.bindingContext.getModel().addAttribute("id", "123"); HandlerResult result = new HandlerResult(new Object(), returnValue, returnType, this.bindingContext); ViewResolutionResultHandler handler = resultHandler(new TestViewResolver("account")); MockServerWebExchange exchange = MockServerWebExchange.from(get("/account")); handler.handleResult(exchange, result).block(Duration.ofMillis(5000)); assertResponseBody(exchange, "account: {id=123}"); exchange = MockServerWebExchange.from(get("/account/")); handler.handleResult(exchange, result).block(Duration.ofMillis(5000)); assertResponseBody(exchange, "account: {id=123}"); exchange = MockServerWebExchange.from(get("/account.123")); handler.handleResult(exchange, result).block(Duration.ofMillis(5000)); assertResponseBody(exchange, "account: {id=123}"); }
@Test public void contentNegotiation() { TestBean value = new TestBean("Joe"); MethodParameter returnType = on(Handler.class).resolveReturnType(TestBean.class); HandlerResult handlerResult = new HandlerResult(new Object(), value, returnType, this.bindingContext); MockServerWebExchange exchange = MockServerWebExchange.from(get("/account").accept(APPLICATION_JSON)); TestView defaultView = new TestView("jsonView", APPLICATION_JSON); resultHandler(Collections.singletonList(defaultView), new TestViewResolver("account")) .handleResult(exchange, handlerResult) .block(Duration.ofSeconds(5)); assertEquals(APPLICATION_JSON, exchange.getResponse().getHeaders().getContentType()); assertResponseBody(exchange, "jsonView: {" + "org.springframework.validation.BindingResult.testBean=" + "org.springframework.validation.BeanPropertyBindingResult: 0 errors, " + "testBean=TestBean[name=Joe]" + "}"); }
@Test // SPR-15291 public void contentNegotiationWithRedirect() { HandlerResult handlerResult = new HandlerResult(new Object(), "redirect:/", on(Handler.class).annotNotPresent(ModelAttribute.class).resolveReturnType(String.class), this.bindingContext); UrlBasedViewResolver viewResolver = new UrlBasedViewResolver(); viewResolver.setApplicationContext(new StaticApplicationContext()); ViewResolutionResultHandler resultHandler = resultHandler(viewResolver); MockServerWebExchange exchange = MockServerWebExchange.from(get("/account").accept(APPLICATION_JSON)); resultHandler.handleResult(exchange, handlerResult).block(Duration.ZERO); MockServerHttpResponse response = exchange.getResponse(); assertEquals(303, response.getStatusCode().value()); assertEquals("/", response.getHeaders().getLocation().toString()); }
@Test public void unresolvedViewName() { String returnValue = "account"; MethodParameter returnType = on(Handler.class).annotPresent(ModelAttribute.class).resolveReturnType(String.class); HandlerResult result = new HandlerResult(new Object(), returnValue, returnType, this.bindingContext); MockServerWebExchange exchange = MockServerWebExchange.from(get("/path")); Mono<Void> mono = resultHandler().handleResult(exchange, result); StepVerifier.create(mono) .expectNextCount(0) .expectErrorMessage("Could not resolve view with name 'path'.") .verify(); }
@Test public void contentNegotiationWith406() { TestBean value = new TestBean("Joe"); MethodParameter returnType = on(Handler.class).resolveReturnType(TestBean.class); HandlerResult handlerResult = new HandlerResult(new Object(), value, returnType, this.bindingContext); MockServerWebExchange exchange = MockServerWebExchange.from(get("/account").accept(APPLICATION_JSON)); ViewResolutionResultHandler resultHandler = resultHandler(new TestViewResolver("account")); Mono<Void> mono = resultHandler.handleResult(exchange, handlerResult); StepVerifier.create(mono) .expectNextCount(0) .expectError(NotAcceptableStatusException.class) .verify(); }