Skip to content

Commit

Permalink
Polishing contribution
Browse files Browse the repository at this point in the history
Closes gh-33498
  • Loading branch information
rstoyanchev committed Sep 9, 2024
1 parent 7655329 commit 5c1ab7e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit 5c1ab7e

Please sign in to comment.