Skip to content

Commit c18ac9b

Browse files
committed
Merge branch 'wiiitek-behind-proxy-2.x' into 2.x
2 parents 425eff2 + b97cb76 commit c18ac9b

File tree

7 files changed

+494
-0
lines changed

7 files changed

+494
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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.app32;
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.context.annotation.Import;
26+
import org.springframework.test.context.TestPropertySource;
27+
28+
import static org.assertj.core.api.Assertions.assertThat;
29+
30+
@TestPropertySource(properties = {
31+
"server.forward-headers-strategy=framework"
32+
})
33+
@Import(SpringDocConfig.class)
34+
public class SpringDocBehindProxyTest 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 shouldServeSwaggerUIAtDefaultPath() {
43+
webTestClient.get().uri("/webjars/swagger-ui/index.html").exchange()
44+
.expectStatus().isOk();
45+
}
46+
47+
@Test
48+
public void shouldReturnCorrectInitializerJS() throws Exception {
49+
webTestClient
50+
.get().uri("/webjars/swagger-ui/swagger-initializer.js")
51+
.header("X-Forwarded-Prefix", X_FORWARD_PREFIX)
52+
.exchange()
53+
.expectStatus().isOk()
54+
.expectBody(String.class)
55+
.consumeWith(response ->
56+
assertThat(response.getResponseBody())
57+
.contains("\"configUrl\" : \"/path/prefix/v3/api-docs/swagger-config\",")
58+
);
59+
}
60+
61+
@Test
62+
public void shouldCalculateOauthRedirectBehindProxy() throws Exception {
63+
webTestClient
64+
.get().uri("/v3/api-docs/swagger-config")
65+
.header("X-Forwarded-Proto", "https")
66+
.header("X-Forwarded-Host", "proxy-host")
67+
.header("X-Forwarded-Prefix", X_FORWARD_PREFIX)
68+
.exchange()
69+
.expectStatus().isOk().expectBody()
70+
.jsonPath("$.oauth2RedirectUrl").isEqualTo("https://proxy-host/path/prefix/webjars/swagger-ui/oauth2-redirect.html");
71+
}
72+
73+
@Test
74+
public void shouldCalculateUrlsBehindProxy() throws Exception {
75+
webTestClient
76+
.get().uri("/v3/api-docs/swagger-config")
77+
.header("X-Forwarded-Prefix", X_FORWARD_PREFIX)
78+
.exchange()
79+
.expectStatus().isOk().expectBody()
80+
.jsonPath("$.url")
81+
.isEqualTo("/path/prefix/v3/api-docs")
82+
.jsonPath("$.configUrl")
83+
.isEqualTo("/path/prefix/v3/api-docs/swagger-config");
84+
}
85+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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.app32;
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.context.annotation.Import;
26+
import org.springframework.test.context.TestPropertySource;
27+
28+
import static org.assertj.core.api.Assertions.assertThat;
29+
30+
@TestPropertySource(properties = {
31+
"server.forward-headers-strategy=framework",
32+
"springdoc.swagger-ui.path=/foo/documentation/swagger.html"
33+
})
34+
@Import(SpringDocConfig.class)
35+
public class SpringDocBehindProxyWithCustomUIPathTest extends AbstractSpringDocTest {
36+
37+
private static final String X_FORWARD_PREFIX = "/path/prefix";
38+
39+
@SpringBootApplication
40+
static class SpringDocTestApp {}
41+
42+
@Test
43+
public void shouldRedirectSwaggerUIFromCustomPath() {
44+
webTestClient
45+
.get().uri("/foo/documentation/swagger.html")
46+
.header("X-Forwarded-Prefix", X_FORWARD_PREFIX)
47+
.exchange()
48+
.expectStatus().isFound()
49+
.expectHeader().location("/path/prefix/foo/documentation/webjars/swagger-ui/index.html");
50+
}
51+
52+
@Test
53+
public void shouldReturnCorrectInitializerJS() {
54+
webTestClient
55+
.get().uri("/foo/documentation/webjars/swagger-ui/swagger-initializer.js")
56+
.header("X-Forwarded-Prefix", X_FORWARD_PREFIX)
57+
.exchange()
58+
.expectStatus().isOk()
59+
.expectBody(String.class)
60+
.consumeWith(response ->
61+
assertThat(response.getResponseBody())
62+
.contains("\"configUrl\" : \"/path/prefix/v3/api-docs/swagger-config\",")
63+
);
64+
}
65+
66+
@Test
67+
public void shouldCalculateUrlsBehindProxy() throws Exception {
68+
webTestClient
69+
.get().uri("/v3/api-docs/swagger-config")
70+
.header("X-Forwarded-Prefix", X_FORWARD_PREFIX)
71+
.exchange()
72+
.expectStatus().isOk()
73+
.expectBody()
74+
.jsonPath("$.url")
75+
.isEqualTo("/path/prefix/v3/api-docs")
76+
.jsonPath("$.configUrl")
77+
.isEqualTo("/path/prefix/v3/api-docs/swagger-config");
78+
}
79+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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.app32;
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.context.annotation.Import;
26+
import org.springframework.test.context.TestPropertySource;
27+
28+
import static org.assertj.core.api.Assertions.assertThat;
29+
30+
@TestPropertySource(properties = {
31+
"server.forward-headers-strategy=framework",
32+
"springdoc.swagger-ui.path=/foo/documentation/swagger.html",
33+
"springdoc.api-docs.path=/bar/openapi/v3"
34+
})
35+
@Import(SpringDocConfig.class)
36+
public class SpringDocBehindProxyWithCustomUIPathWithApiDocsTest extends AbstractSpringDocTest {
37+
38+
private static final String X_FORWARD_PREFIX = "/path/prefix";
39+
40+
@SpringBootApplication
41+
static class SpringDocTestApp {}
42+
43+
@Test
44+
public void shouldRedirectSwaggerUIFromCustomPath() {
45+
webTestClient
46+
.get().uri("/foo/documentation/swagger.html")
47+
.header("X-Forwarded-Prefix", X_FORWARD_PREFIX)
48+
.exchange()
49+
.expectStatus().isFound()
50+
.expectHeader().location("/path/prefix/foo/documentation/webjars/swagger-ui/index.html");
51+
}
52+
53+
@Test
54+
public void shouldReturnCorrectInitializerJS() {
55+
webTestClient
56+
.get().uri("/foo/documentation/webjars/swagger-ui/swagger-initializer.js")
57+
.header("X-Forwarded-Prefix", X_FORWARD_PREFIX)
58+
.exchange()
59+
.expectStatus().isOk()
60+
.expectBody(String.class)
61+
.consumeWith(response ->
62+
assertThat(response.getResponseBody())
63+
.contains("\"configUrl\" : \"/path/prefix/bar/openapi/v3/swagger-config\",")
64+
);
65+
}
66+
67+
@Test
68+
public void shouldCalculateUrlsBehindProxy() {
69+
webTestClient
70+
.get().uri("/bar/openapi/v3/swagger-config")
71+
.header("X-Forwarded-Prefix", X_FORWARD_PREFIX)
72+
.exchange()
73+
.expectStatus().isOk()
74+
.expectBody()
75+
.jsonPath("$.url")
76+
.isEqualTo("/path/prefix/bar/openapi/v3")
77+
.jsonPath("$.configUrl")
78+
.isEqualTo("/path/prefix/bar/openapi/v3/swagger-config");
79+
}
80+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package test.org.springdoc.ui.app32;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.web.server.adapter.ForwardedHeaderTransformer;
6+
7+
@Configuration
8+
public class SpringDocConfig {
9+
10+
@Bean
11+
ForwardedHeaderTransformer forwardedHeaderTransformer() {
12+
return new ForwardedHeaderTransformer();
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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.app32;
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.hamcrest.CoreMatchers.equalTo;
28+
import static org.hamcrest.Matchers.containsString;
29+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
30+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
31+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
32+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
33+
34+
@TestPropertySource(properties = {
35+
"server.forward-headers-strategy=framework"
36+
})
37+
public class SpringDocBehindProxyTest extends AbstractSpringDocTest {
38+
39+
private static final String X_FORWARD_PREFIX = "/path/prefix";
40+
41+
@SpringBootApplication
42+
static class SpringDocTestApp {}
43+
44+
@Test
45+
public void shouldServeSwaggerUIAtDefaultPath() throws Exception {
46+
mockMvc.perform(get("/swagger-ui/index.html"))
47+
.andExpect(status().isOk());
48+
}
49+
50+
@Test
51+
public void shouldReturnCorrectInitializerJS() throws Exception {
52+
mockMvc.perform(get("/swagger-ui/swagger-initializer.js")
53+
.header("X-Forwarded-Prefix", X_FORWARD_PREFIX))
54+
.andExpect(status().isOk())
55+
.andExpect(content().string(
56+
containsString("\"configUrl\" : \"/path/prefix/v3/api-docs/swagger-config\",")
57+
));
58+
}
59+
60+
@Test
61+
public void shouldCalculateOauthRedirectBehindProxy() throws Exception {
62+
mockMvc.perform(get("/v3/api-docs/swagger-config")
63+
.header("X-Forwarded-Proto", "https")
64+
.header("X-Forwarded-Host", "proxy-host")
65+
.header("X-Forwarded-Prefix", X_FORWARD_PREFIX))
66+
.andExpect(status().isOk())
67+
.andExpect(jsonPath("oauth2RedirectUrl",
68+
equalTo("https://proxy-host/path/prefix/swagger-ui/oauth2-redirect.html")
69+
));
70+
}
71+
72+
@Test
73+
public void shouldCalculateUrlsBehindProxy() throws Exception {
74+
mockMvc.perform(get("/v3/api-docs/swagger-config")
75+
.header("X-Forwarded-Prefix", X_FORWARD_PREFIX))
76+
.andExpect(status().isOk())
77+
.andExpect(jsonPath("url",
78+
equalTo("/path/prefix/v3/api-docs")
79+
))
80+
.andExpect(jsonPath("configUrl",
81+
equalTo("/path/prefix/v3/api-docs/swagger-config")
82+
));
83+
}
84+
}

0 commit comments

Comments
 (0)