From 5c1ab7ecd5b05c1df208567d707338616fc293e5 Mon Sep 17 00:00:00 2001 From: rstoyanchev Date: Fri, 6 Sep 2024 17:02:33 +0200 Subject: [PATCH] Polishing contribution Closes gh-33498 --- .../result/view/DefaultRenderingBuilder.java | 6 ++++-- .../result/view/DefaultRenderingBuilderTests.java | 8 +++++--- .../view/ViewResolutionResultHandlerTests.java | 15 +++++++++++++++ 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/DefaultRenderingBuilder.java b/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/DefaultRenderingBuilder.java index 4d452cd12794..9f7402fc26d3 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/DefaultRenderingBuilder.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/DefaultRenderingBuilder.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -84,10 +84,12 @@ private Model initModel() { @Override public DefaultRenderingBuilder status(HttpStatusCode status) { - this.status = status; if (this.view instanceof RedirectView) { ((RedirectView) this.view).setStatusCode(status); } + else { + this.status = status; + } return this; } diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/result/view/DefaultRenderingBuilderTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/result/view/DefaultRenderingBuilderTests.java index f96ab31a00ca..7b97e8eab6b8 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/result/view/DefaultRenderingBuilderTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/result/view/DefaultRenderingBuilderTests.java @@ -23,6 +23,7 @@ import org.junit.jupiter.api.Test; import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; import static org.assertj.core.api.Assertions.assertThat; @@ -126,13 +127,14 @@ void redirectWithPropagateQuery() { assertThat(((RedirectView) view).isPropagateQuery()).isTrue(); } - @Test + @Test // gh-33498 void redirectWithCustomStatus() { - Rendering rendering = Rendering.redirectTo("foo").status(HttpStatus.MOVED_PERMANENTLY).build(); + HttpStatus status = HttpStatus.MOVED_PERMANENTLY; + Rendering rendering = Rendering.redirectTo("foo").status(status).build(); Object view = rendering.view(); assertThat(view.getClass()).isEqualTo(RedirectView.class); - assertThat(((RedirectView) view).statusCode()).isEqualTo(HttpStatus.MOVED_PERMANENTLY); + assertThat(((RedirectView) view).getStatusCode()).isEqualTo(status); } diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/result/view/ViewResolutionResultHandlerTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/result/view/ViewResolutionResultHandlerTests.java index f08d538121e8..f30575d66998 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/result/view/ViewResolutionResultHandlerTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/result/view/ViewResolutionResultHandlerTests.java @@ -16,6 +16,7 @@ package org.springframework.web.reactive.result.view; +import java.net.URI; import java.nio.ByteBuffer; import java.time.Duration; import java.util.Arrays; @@ -196,6 +197,20 @@ void handleReturnValueTypes() { assertThat(exchange.getResponse().getHeaders().getFirst("h")).isEqualTo("h1"); } + @Test // gh-33498 + void handleRedirect() { + HttpStatus status = HttpStatus.MOVED_PERMANENTLY; + Rendering returnValue = Rendering.redirectTo("foo").status(status).build(); + MethodParameter returnType = on(Handler.class).resolveReturnType(Rendering.class); + HandlerResult result = new HandlerResult(new Object(), returnValue, returnType, this.bindingContext); + + MockServerWebExchange exchange = MockServerWebExchange.from(get("/path")); + resultHandler(new UrlBasedViewResolver()).handleResult(exchange, result).block(Duration.ofSeconds(5)); + + assertThat(exchange.getResponse().getStatusCode()).isEqualTo(status); + assertThat(exchange.getResponse().getHeaders().getLocation()).isEqualTo(URI.create("foo")); + } + @Test void handleWithMultipleResolvers() { testHandle("/account",