Polishing contribution

Closes gh-33498
This commit is contained in:
rstoyanchev
2024-09-06 17:02:33 +02:00
parent 7655329463
commit 5c1ab7ecd5
3 changed files with 24 additions and 5 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,10 +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,13 +127,14 @@ class DefaultRenderingBuilderTests {
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);
}

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;
@@ -196,6 +197,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",