|
| 1 | +/* |
| 2 | + * |
| 3 | + * * Copyright 2019-2020 the original author or authors. |
| 4 | + * * |
| 5 | + * * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * * you may not use this file except in compliance with the License. |
| 7 | + * * You may obtain a copy of the License at |
| 8 | + * * |
| 9 | + * * https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * * |
| 11 | + * * Unless required by applicable law or agreed to in writing, software |
| 12 | + * * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * * See the License for the specific language governing permissions and |
| 15 | + * * limitations under the License. |
| 16 | + * |
| 17 | + */ |
| 18 | + |
| 19 | +package test.org.springdoc.ui.app31; |
| 20 | + |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | +import test.org.springdoc.ui.AbstractSpringDocTest; |
| 23 | + |
| 24 | +import org.springframework.boot.autoconfigure.SpringBootApplication; |
| 25 | +import org.springframework.test.context.TestPropertySource; |
| 26 | + |
| 27 | +import static org.assertj.core.api.Assertions.assertThat; |
| 28 | + |
| 29 | +@TestPropertySource(properties = { |
| 30 | + "server.forward-headers-strategy=framework", |
| 31 | + "springdoc.swagger-ui.path=/foo/documentation/swagger.html", |
| 32 | + "springdoc.api-docs.path=/bar/openapi/v3" |
| 33 | +}) |
| 34 | +public class SpringDocBehindProxyWithCustomUIPathWithApiDocsTest extends AbstractSpringDocTest { |
| 35 | + |
| 36 | + private static final String X_FORWARD_PREFIX = "/path/prefix"; |
| 37 | + |
| 38 | + @SpringBootApplication |
| 39 | + static class SpringDocTestApp {} |
| 40 | + |
| 41 | + @Test |
| 42 | + public void shouldRedirectSwaggerUIFromCustomPath() { |
| 43 | + webTestClient |
| 44 | + .get().uri("/foo/documentation/swagger.html") |
| 45 | + .header("X-Forwarded-Prefix", X_FORWARD_PREFIX) |
| 46 | + .exchange() |
| 47 | + .expectStatus().isFound() |
| 48 | + .expectHeader().location("/path/prefix/foo/documentation/swagger-ui/index.html"); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void shouldReturnCorrectInitializerJS() { |
| 53 | + webTestClient |
| 54 | + .get().uri("/foo/documentation/swagger-ui/swagger-initializer.js") |
| 55 | + .header("X-Forwarded-Prefix", X_FORWARD_PREFIX) |
| 56 | + .exchange() |
| 57 | + .expectStatus().isOk() |
| 58 | + .expectBody(String.class) |
| 59 | + .consumeWith(response -> |
| 60 | + assertThat(response.getResponseBody()) |
| 61 | + .contains("\"configUrl\" : \\\"/path/prefix/v3/api-docs/swagger-config\\\",") |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void shouldCalculateUrlsBehindProxy() { |
| 67 | + webTestClient |
| 68 | + .get().uri("/bar/openapi/v3/swagger-config") |
| 69 | + .header("X-Forwarded-Prefix", X_FORWARD_PREFIX) |
| 70 | + .exchange() |
| 71 | + .expectStatus().isOk() |
| 72 | + .expectBody() |
| 73 | + .jsonPath("$.url") |
| 74 | + .isEqualTo("/path/prefix/bar/openapi/v3") |
| 75 | + .jsonPath("$.configUrl") |
| 76 | + .isEqualTo("/path/prefix/bar/openapi/v3/swagger-config"); |
| 77 | + } |
| 78 | +} |
0 commit comments