|
| 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.junit.jupiter.api.Assertions.assertNotNull; |
| 28 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 29 | + |
| 30 | +@TestPropertySource(properties = { "server.forward-headers-strategy=framework" }) |
| 31 | +public class SpringDocBehindProxyTest extends AbstractSpringDocTest { |
| 32 | + |
| 33 | + private static final String X_FORWARD_PREFIX = "/path/prefix"; |
| 34 | + |
| 35 | + @SpringBootApplication |
| 36 | + static class SpringDocTestApp {} |
| 37 | + |
| 38 | + @Test |
| 39 | + public void shouldServeSwaggerUIAtDefaultPath() { |
| 40 | + webTestClient.get().uri("/webjars/swagger-ui/index.html").exchange() |
| 41 | + .expectStatus().isOk(); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + public void shouldReturnCorrectInitializerJS() throws Exception { |
| 46 | + webTestClient |
| 47 | + .get().uri("/webjars/swagger-ui/swagger-initializer.js") |
| 48 | + .header("X-Forwarded-Prefix", X_FORWARD_PREFIX) |
| 49 | + .exchange() |
| 50 | + .expectStatus().isOk() |
| 51 | + .expectBody(String.class) |
| 52 | + .consumeWith(response -> { |
| 53 | + String actualContent = response.getResponseBody(); |
| 54 | + assertNotNull(actualContent); |
| 55 | + assertTrue(actualContent.contains("window.ui")); |
| 56 | + assertTrue(actualContent.contains("\"configUrl\" : \"/v3/api-docs/swagger-config\",")); |
| 57 | + // TODO: what should be returned |
| 58 | + //assertTrue(actualContent.contains("\"configUrl\" : \"/path/prefix/v3/api-docs/swagger-config\",")); |
| 59 | + } |
| 60 | + ); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void shouldCalculateOauthRedirectBehindProxy() throws Exception { |
| 65 | + webTestClient |
| 66 | + .get().uri("/v3/api-docs/swagger-config") |
| 67 | + .header("X-Forwarded-Proto", "https") |
| 68 | + .header("X-Forwarded-Host", "proxy-host") |
| 69 | + .header("X-Forwarded-Prefix", X_FORWARD_PREFIX) |
| 70 | + .exchange() |
| 71 | + .expectStatus().isOk().expectBody() |
| 72 | + .jsonPath("$.oauth2RedirectUrl").isEqualTo("https://proxy-host/path/prefix/swagger-ui/oauth2-redirect.html"); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void shouldCalculateUrlsBehindProxy() throws Exception { |
| 77 | + webTestClient |
| 78 | + .get().uri("/v3/api-docs/swagger-config") |
| 79 | + .header("X-Forwarded-Prefix", X_FORWARD_PREFIX) |
| 80 | + .exchange() |
| 81 | + .expectStatus().isOk().expectBody() |
| 82 | + .jsonPath("$.configUrl").isEqualTo("/path/prefix/v3/api-docs/swagger-config") |
| 83 | + .jsonPath("$.url").isEqualTo("/path/prefix/v3/api-docs"); |
| 84 | + } |
| 85 | +} |
0 commit comments