Skip to content

Commit 5c1ab7e

Browse files
committed
Polishing contribution
Closes gh-33498
1 parent 7655329 commit 5c1ab7e

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

spring-webflux/src/main/java/org/springframework/web/reactive/result/view/DefaultRenderingBuilder.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -84,10 +84,12 @@ private Model initModel() {
8484

8585
@Override
8686
public DefaultRenderingBuilder status(HttpStatusCode status) {
87-
this.status = status;
8887
if (this.view instanceof RedirectView) {
8988
((RedirectView) this.view).setStatusCode(status);
9089
}
90+
else {
91+
this.status = status;
92+
}
9193
return this;
9294
}
9395

spring-webflux/src/test/java/org/springframework/web/reactive/result/view/DefaultRenderingBuilderTests.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.junit.jupiter.api.Test;
2424

2525
import org.springframework.http.HttpHeaders;
26+
import org.springframework.http.HttpStatus;
2627

2728
import static org.assertj.core.api.Assertions.assertThat;
2829

@@ -126,13 +127,14 @@ void redirectWithPropagateQuery() {
126127
assertThat(((RedirectView) view).isPropagateQuery()).isTrue();
127128
}
128129

129-
@Test
130+
@Test // gh-33498
130131
void redirectWithCustomStatus() {
131-
Rendering rendering = Rendering.redirectTo("foo").status(HttpStatus.MOVED_PERMANENTLY).build();
132+
HttpStatus status = HttpStatus.MOVED_PERMANENTLY;
133+
Rendering rendering = Rendering.redirectTo("foo").status(status).build();
132134

133135
Object view = rendering.view();
134136
assertThat(view.getClass()).isEqualTo(RedirectView.class);
135-
assertThat(((RedirectView) view).statusCode()).isEqualTo(HttpStatus.MOVED_PERMANENTLY);
137+
assertThat(((RedirectView) view).getStatusCode()).isEqualTo(status);
136138
}
137139

138140

spring-webflux/src/test/java/org/springframework/web/reactive/result/view/ViewResolutionResultHandlerTests.java

+15
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.web.reactive.result.view;
1818

19+
import java.net.URI;
1920
import java.nio.ByteBuffer;
2021
import java.time.Duration;
2122
import java.util.Arrays;
@@ -196,6 +197,20 @@ void handleReturnValueTypes() {
196197
assertThat(exchange.getResponse().getHeaders().getFirst("h")).isEqualTo("h1");
197198
}
198199

200+
@Test // gh-33498
201+
void handleRedirect() {
202+
HttpStatus status = HttpStatus.MOVED_PERMANENTLY;
203+
Rendering returnValue = Rendering.redirectTo("foo").status(status).build();
204+
MethodParameter returnType = on(Handler.class).resolveReturnType(Rendering.class);
205+
HandlerResult result = new HandlerResult(new Object(), returnValue, returnType, this.bindingContext);
206+
207+
MockServerWebExchange exchange = MockServerWebExchange.from(get("/path"));
208+
resultHandler(new UrlBasedViewResolver()).handleResult(exchange, result).block(Duration.ofSeconds(5));
209+
210+
assertThat(exchange.getResponse().getStatusCode()).isEqualTo(status);
211+
assertThat(exchange.getResponse().getHeaders().getLocation()).isEqualTo(URI.create("foo"));
212+
}
213+
199214
@Test
200215
void handleWithMultipleResolvers() {
201216
testHandle("/account",

0 commit comments

Comments
 (0)