Merge branch '6.1.x'

This commit is contained in:
rstoyanchev
2024-09-09 11:56:21 +01:00
4 changed files with 85 additions and 47 deletions

View File

@@ -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,7 +84,12 @@ class DefaultRenderingBuilder implements Rendering.RedirectBuilder {
@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;
}

View File

@@ -23,6 +23,7 @@ import java.util.Map;
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,6 +127,16 @@ class DefaultRenderingBuilderTests {
assertThat(((RedirectView) view).isPropagateQuery()).isTrue();
}
@Test // gh-33498
void redirectWithCustomStatus() {
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).getStatusCode()).isEqualTo(status);
}
private static class Foo {}

View File

@@ -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;
@@ -202,6 +203,20 @@ class ViewResolutionResultHandlerTests {
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",