Skip to content

use HTTP 302 status for UI redirect #1107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ protected Mono<Void> redirectToUi(ServerHttpRequest request, ServerHttpResponse
String contextPath = this.fromCurrentContextPath(request);
String sbUrl = this.buildUrl(contextPath, swaggerUiConfigParameters.getUiRootPath() + springDocConfigProperties.getWebjars().getPrefix() + SWAGGER_UI_URL);
UriComponentsBuilder uriBuilder = getUriComponentsBuilder(sbUrl);
response.setStatusCode(HttpStatus.TEMPORARY_REDIRECT);
response.setStatusCode(HttpStatus.FOUND);
response.getHeaders().setLocation(URI.create(uriBuilder.build().encode().toString()));
return response.setComplete();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@
public class SpringDocApp1RedirecFilterTest extends AbstractSpringDocTest {

@Test
public void shouldRedirectWithConfigUrlIgnoringQueryParams() throws Exception {
public void shouldRedirectWithConfigUrlIgnoringQueryParams() {

WebTestClient.ResponseSpec responseSpec = webTestClient.get().uri("/swagger-ui.html").exchange()
.expectStatus().isTemporaryRedirect();
.expectStatus().isFound();
responseSpec.expectHeader()
.value("Location", Matchers.is("/webjars/swagger-ui/index.html?configUrl=/v3/api-docs/swagger-config&filter=false"));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@
public class SpringDocApp1RedirectConfigUrlTest extends AbstractSpringDocTest {

@Test
public void shouldRedirectWithConfigUrlIgnoringQueryParams() throws Exception {
public void shouldRedirectWithConfigUrlIgnoringQueryParams() {

WebTestClient.ResponseSpec responseSpec = webTestClient.get().uri("/swagger-ui.html").exchange()
.expectStatus().isTemporaryRedirect();
.expectStatus().isFound();
responseSpec.expectHeader()
.value("Location", Matchers.is("/webjars/swagger-ui/index.html?configUrl=/foo/bar"));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
public class SpringDocApp1RedirectDefaultTest extends AbstractSpringDocTest {

@Test
public void shouldRedirectWithDefaultQueryParams() throws Exception {
public void shouldRedirectWithDefaultQueryParams() {
WebTestClient.ResponseSpec responseSpec = webTestClient.get().uri("/swagger-ui.html").exchange()
.expectStatus().isTemporaryRedirect();
.expectStatus().isFound();
responseSpec.expectHeader()
.value("Location", Matchers.is("/webjars/swagger-ui/index.html?configUrl=/v3/api-docs/swagger-config"));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@
public class SpringDocApp1RedirectLayoutTest extends AbstractSpringDocTest {

@Test
public void shouldRedirectWithConfigUrlIgnoringQueryParams() throws Exception {
public void shouldRedirectWithConfigUrlIgnoringQueryParams() {

WebTestClient.ResponseSpec responseSpec = webTestClient.get().uri("/swagger-ui.html").exchange()
.expectStatus().isTemporaryRedirect();
.expectStatus().isFound();
responseSpec.expectHeader()
.value("Location", Matchers.is("/webjars/swagger-ui/index.html?configUrl=/v3/api-docs/swagger-config&layout=BaseLayout"));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@
public class SpringDocApp1RedirectQueryParams1Test extends AbstractSpringDocTest {

@Test
public void shouldRedirectWithQueryParamsWithoutOauth2() throws Exception {
public void shouldRedirectWithQueryParamsWithoutOauth2() {

WebTestClient.ResponseSpec responseSpec = webTestClient.get().uri("/swagger-ui.html").exchange()
.expectStatus().isTemporaryRedirect();
.expectStatus().isFound();
responseSpec.expectHeader()
.value("Location", Matchers.is("/webjars/swagger-ui/index.html?url=/v3/api-docs"));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@
public class SpringDocApp1RedirectQueryParams2Test extends AbstractSpringDocTest {

@Test
public void shouldRedirectWithQueryParams() throws Exception {
public void shouldRedirectWithQueryParams() {

WebTestClient.ResponseSpec responseSpec = webTestClient.get().uri("/swagger-ui.html").exchange()
.expectStatus().isTemporaryRedirect();
.expectStatus().isFound();
responseSpec.expectHeader()
.value("Location", Matchers.is("/webjars/swagger-ui/index.html?oauth2RedirectUrl=/webjars/swagger-ui/oauth2-redirect.html&url=/v3/api-docs"));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@
public class SpringDocApp1RedirectWithConfigTest extends AbstractSpringDocTest {

@Test
public void shouldRedirectWithConfiguredParams() throws Exception {
public void shouldRedirectWithConfiguredParams() {
WebTestClient.ResponseSpec responseSpec = webTestClient.get().uri("/swagger-ui.html").exchange()
.expectStatus().isTemporaryRedirect();
.expectStatus().isFound();

responseSpec.expectHeader()
.value("Location", Matchers.is("/webjars/swagger-ui/index.html?configUrl=/baf/batz/swagger-config"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
public class SpringDocApp1Test extends AbstractSpringDocTest {

@Test
public void shouldDisplaySwaggerUiPage() throws Exception {
public void shouldDisplaySwaggerUiPage() {
webTestClient.get().uri("/swagger-ui.html").exchange()
.expectStatus().isTemporaryRedirect();
.expectStatus().isFound();
}

@SpringBootApplication
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import org.springframework.http.HttpStatus;
import org.springframework.test.web.reactive.server.EntityExchangeResult;

import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.assertj.core.api.Assertions.assertThat;
import static org.skyscreamer.jsonassert.JSONAssert.assertEquals;


Expand All @@ -42,22 +42,21 @@ class SpringDocApp13Test extends AbstractSpringDocActuatorTest {
static class SpringDocTestApp {}

@Test
void testIndex() throws Exception {

void testIndex() {
EntityExchangeResult<byte[]> getResult = webTestClient.get().uri("/application/webjars/swagger-ui/index.html")
.exchange()
.expectStatus().isOk()
.expectBody().returnResult();

assertThat(getResult.getResponseBody()).isNotNull();
String result = new String(getResult.getResponseBody());
assertTrue(result.contains("Swagger UI"));
assertThat(result).contains("Swagger UI");
}

@Test
public void testIndexActuator() {
HttpStatus httpStatusMono = webClient.get().uri("/application/swaggerui")
.exchangeToMono( clientResponse -> Mono.just(clientResponse.statusCode())).block();
assertTrue(httpStatusMono.equals(HttpStatus.TEMPORARY_REDIRECT));
assertThat(httpStatusMono).isEqualTo(HttpStatus.FOUND);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import org.springframework.http.HttpStatus;
import org.springframework.test.web.reactive.server.EntityExchangeResult;

import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.assertj.core.api.Assertions.assertThat;
import static org.skyscreamer.jsonassert.JSONAssert.assertEquals;


Expand All @@ -41,21 +41,21 @@ static class SpringDocTestApp {}


@Test
void testIndex() throws Exception {
void testIndex() {
EntityExchangeResult<byte[]> getResult = webTestClient.get().uri("/application/webjars/swagger-ui/index.html")
.exchange()
.expectStatus().isOk()
.expectBody().returnResult();

assertThat(getResult.getResponseBody()).isNotNull();
String contentAsString = new String(getResult.getResponseBody());
assertTrue(contentAsString.contains("Swagger UI"));
assertThat(contentAsString).contains("Swagger UI");
}

@Test
public void testIndexActuator() {
HttpStatus httpStatusMono = webClient.get().uri("/application/swaggerui")
.exchangeToMono( clientResponse -> Mono.just(clientResponse.statusCode())).block();
assertTrue(httpStatusMono.equals(HttpStatus.TEMPORARY_REDIRECT));
assertThat(httpStatusMono).isEqualTo(HttpStatus.FOUND);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import org.springframework.http.HttpStatus;
import org.springframework.test.web.reactive.server.EntityExchangeResult;

import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.assertj.core.api.Assertions.assertThat;
import static org.skyscreamer.jsonassert.JSONAssert.assertEquals;


Expand All @@ -49,15 +49,16 @@ void testIndex() {
.exchange()
.expectStatus().isOk()
.expectBody().returnResult();
assertThat(getResult.getResponseBody()).isNotNull();
String contentAsString = new String(getResult.getResponseBody());
assertTrue(contentAsString.contains("Swagger UI"));
assertThat(contentAsString).contains("Swagger UI");
}

@Test
public void testIndexActuator() {
HttpStatus httpStatusMono = webClient.get().uri("/test/application/swaggerui")
.exchangeToMono( clientResponse -> Mono.just(clientResponse.statusCode())).block();
assertTrue(httpStatusMono.equals(HttpStatus.TEMPORARY_REDIRECT));
assertThat(httpStatusMono).isEqualTo(HttpStatus.FOUND);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import org.springframework.http.HttpStatus;
import org.springframework.test.web.reactive.server.EntityExchangeResult;

import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.assertj.core.api.Assertions.assertThat;
import static org.skyscreamer.jsonassert.JSONAssert.assertEquals;


Expand All @@ -43,20 +43,21 @@ class SpringDocApp16Test extends AbstractSpringDocActuatorTest {
static class SpringDocTestApp {}

@Test
void testIndex() throws Exception {
void testIndex() {
EntityExchangeResult<byte[]> getResult = webTestClient.get().uri("/application/webjars/swagger-ui/index.html")
.exchange()
.expectStatus().isOk()
.expectBody().returnResult();
assertThat(getResult.getResponseBody()).isNotNull();
String contentAsString = new String(getResult.getResponseBody());
assertTrue(contentAsString.contains("Swagger UI"));
assertThat(contentAsString).contains("Swagger UI");
}

@Test
public void testIndexActuator() {
HttpStatus httpStatusMono = webClient.get().uri("/test/application/swaggerui")
.exchangeToMono( clientResponse -> Mono.just(clientResponse.statusCode())).block();
assertTrue(httpStatusMono.equals(HttpStatus.TEMPORARY_REDIRECT));
assertThat(httpStatusMono).isEqualTo(HttpStatus.FOUND);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import org.springframework.http.HttpStatus;
import org.springframework.web.reactive.function.client.WebClient;

import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.assertj.core.api.Assertions.assertThat;
import static org.skyscreamer.jsonassert.JSONAssert.assertEquals;


Expand Down Expand Up @@ -62,11 +62,11 @@ void init(){
public void testIndexActuator() throws Exception {
HttpStatus httpStatusMono = webClient.get().uri("/test/documentation/swagger-ui.html")
.exchangeToMono(clientResponse -> Mono.just(clientResponse.statusCode())).block();
assertTrue(httpStatusMono.equals(HttpStatus.TEMPORARY_REDIRECT));
assertThat(httpStatusMono).isEqualTo(HttpStatus.FOUND);

httpStatusMono = webClient.get().uri("/test/documentation/webjars-pref/swagger-ui/index.html")
.exchangeToMono(clientResponse -> Mono.just(clientResponse.statusCode())).block();
assertTrue(httpStatusMono.equals(HttpStatus.OK));
assertThat(httpStatusMono).isEqualTo(HttpStatus.OK);

String contentAsString = webClient.get().uri("/test/documentation/v3/api-docs/swagger-config").retrieve()
.bodyToMono(String.class).block();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import org.springframework.http.HttpStatus;
import org.springframework.web.reactive.function.client.WebClient;

import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.assertj.core.api.Assertions.assertThat;
import static org.skyscreamer.jsonassert.JSONAssert.assertEquals;


Expand All @@ -58,11 +58,11 @@ void init(){
public void testIndex() throws Exception {
HttpStatus httpStatusMono = webClient.get().uri("/")
.exchangeToMono(clientResponse -> Mono.just(clientResponse.statusCode())).block();
assertTrue(httpStatusMono.equals(HttpStatus.TEMPORARY_REDIRECT));
assertThat(httpStatusMono).isEqualTo(HttpStatus.FOUND);

httpStatusMono = webClient.get().uri("/webjars/swagger-ui/index.html")
.exchangeToMono(clientResponse -> Mono.just(clientResponse.statusCode())).block();
assertTrue(httpStatusMono.equals(HttpStatus.OK));
assertThat(httpStatusMono).isEqualTo(HttpStatus.OK);

String contentAsString = webClient.get().uri("/v3/api-docs/swagger-config").retrieve()
.bodyToMono(String.class).block();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import javax.annotation.PostConstruct;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import reactor.core.publisher.Mono;
import test.org.springdoc.ui.AbstractCommonTest;
Expand All @@ -33,6 +32,7 @@
import org.springframework.http.HttpStatus;
import org.springframework.web.reactive.function.client.WebClient;

import static org.assertj.core.api.Assertions.assertThat;
import static org.skyscreamer.jsonassert.JSONAssert.assertEquals;


Expand All @@ -58,11 +58,11 @@ void init(){
public void testIndex() throws Exception {
HttpStatus httpStatusMono = webClient.get().uri("/test/swagger-ui.html")
.exchangeToMono(clientResponse -> Mono.just(clientResponse.statusCode())).block();
Assertions.assertTrue(httpStatusMono.equals(HttpStatus.TEMPORARY_REDIRECT));
assertThat(httpStatusMono).isEqualTo(HttpStatus.FOUND);

httpStatusMono = webClient.get().uri("/webjars/swagger-ui/index.html")
.exchangeToMono(clientResponse -> Mono.just(clientResponse.statusCode())).block();
Assertions.assertTrue(httpStatusMono.equals(HttpStatus.OK));
assertThat(httpStatusMono).isEqualTo(HttpStatus.OK);

String contentAsString = webClient.get().uri("/test/v3/api-docs/swagger-config").retrieve()
.bodyToMono(String.class).block();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@
public class SpringDocApp3RedirectDefaultTest extends AbstractSpringDocTest {

@Test
public void shouldRedirectWithDefaultQueryParams() throws Exception {
public void shouldRedirectWithDefaultQueryParams() {
WebTestClient.ResponseSpec responseSpec = webTestClient.get().uri("/documentation/swagger-ui.html").exchange()
.expectStatus().isTemporaryRedirect();
.expectStatus().isFound();
responseSpec.expectHeader()
.value("Location", Matchers.is("/documentation/webjars/swagger-ui/index.html?configUrl=/documentation/v3/api-docs/swagger-config"));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@
public class SpringDocApp3RedirectWithPrefixTest extends AbstractSpringDocTest {

@Test
public void shouldRedirectWithPrefix() throws Exception {
public void shouldRedirectWithPrefix() {
WebTestClient.ResponseSpec responseSpec = webTestClient.get().uri("/documentation/swagger-ui.html").exchange()
.expectStatus().isTemporaryRedirect();
.expectStatus().isFound();
responseSpec.expectHeader()
.value("Location", Matchers.is("/documentation/webjars-pref/swagger-ui/index.html?configUrl=/documentation/v3/api-docs/swagger-config"));
webTestClient.get().uri("/documentation/webjars-pref/swagger-ui/index.html").exchange()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
public class SpringDocApp3Test extends AbstractSpringDocTest {

@Test
public void shouldDisplaySwaggerUiPage() throws Exception {
public void shouldDisplaySwaggerUiPage() {
webTestClient.get().uri("/documentation/swagger-ui.html").exchange()
.expectStatus().isTemporaryRedirect();
.expectStatus().isFound();
webTestClient.get().uri("/documentation/webjars/swagger-ui/index.html").exchange()
.expectStatus().isOk();
}
Expand Down