Skip to content

Commit 6b7a8e1

Browse files
committed
Merge branch 'wiiitek-behind-proxy-1.x'
2 parents f4a5c6e + 1d0cb3a commit 6b7a8e1

File tree

7 files changed

+494
-0
lines changed

7 files changed

+494
-0
lines changed
Lines changed: 84 additions & 0 deletions
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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.header;
32+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
33+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
34+
35+
@TestPropertySource(properties = {
36+
"server.forward-headers-strategy=framework",
37+
"springdoc.swagger-ui.path=/foo/documentation/swagger.html"
38+
})
39+
public class SpringDocBehindProxyWithCustomUIPathTest extends AbstractSpringDocTest {
40+
41+
private static final String X_FORWARD_PREFIX = "/path/prefix";
42+
43+
@SpringBootApplication
44+
static class SpringDocTestApp {}
45+
46+
@Test
47+
public void shouldRedirectSwaggerUIFromCustomPath() throws Exception {
48+
mockMvc.perform(get("/foo/documentation/swagger.html")
49+
.header("X-Forwarded-Prefix", X_FORWARD_PREFIX))
50+
.andExpect(status().isFound())
51+
.andExpect(header().string("Location", "/path/prefix/foo/documentation/swagger-ui/index.html"));
52+
}
53+
54+
@Test
55+
public void shouldReturnCorrectInitializerJS() throws Exception {
56+
mockMvc.perform(get("/foo/documentation/swagger-ui/swagger-initializer.js")
57+
.header("X-Forwarded-Prefix", X_FORWARD_PREFIX))
58+
.andExpect(status().isOk())
59+
.andExpect(content().string(
60+
containsString("\"configUrl\" : \"/path/prefix/v3/api-docs/swagger-config\",")
61+
));
62+
}
63+
64+
@Test
65+
public void shouldCalculateUrlsBehindProxy() throws Exception {
66+
mockMvc.perform(get("/v3/api-docs/swagger-config")
67+
.header("X-Forwarded-Prefix", X_FORWARD_PREFIX))
68+
.andExpect(status().isOk())
69+
.andExpect(jsonPath("url",
70+
equalTo("/path/prefix/v3/api-docs")
71+
))
72+
.andExpect(jsonPath("configUrl",
73+
equalTo("/path/prefix/v3/api-docs/swagger-config")
74+
));
75+
}
76+
77+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
"springdoc.swagger-ui.path=/foo/documentation/swagger.html",
37+
"springdoc.api-docs.path=/bar/openapi/v3"
38+
})
39+
public class SpringDocBehindProxyWithCustomUIPathWithApiDocsTest extends AbstractSpringDocTest {
40+
41+
private static final String X_FORWARD_PREFIX = "/path/prefix";
42+
43+
@SpringBootApplication
44+
static class SpringDocTestApp {}
45+
46+
@Test
47+
public void shouldServeOpenapiJsonUnderCustomPath() throws Exception {
48+
mockMvc.perform(get("/bar/openapi/v3")
49+
.header("X-Forwarded-Prefix", X_FORWARD_PREFIX))
50+
.andExpect(status().isOk());
51+
}
52+
53+
@Test
54+
public void shouldReturnCorrectInitializerJS() throws Exception {
55+
mockMvc.perform(get("/foo/documentation/swagger-ui/swagger-initializer.js")
56+
.header("X-Forwarded-Prefix", X_FORWARD_PREFIX))
57+
.andExpect(status().isOk())
58+
.andExpect(content().string(
59+
containsString("\"configUrl\" : \"/path/prefix/bar/openapi/v3/swagger-config\",")
60+
));
61+
}
62+
63+
@Test
64+
public void shouldCalculateUrlsBehindProxy() throws Exception {
65+
mockMvc.perform(get("/bar/openapi/v3/swagger-config")
66+
.header("X-Forwarded-Prefix", X_FORWARD_PREFIX))
67+
.andExpect(status().isOk())
68+
.andExpect(jsonPath("url",
69+
equalTo("/path/prefix/bar/openapi/v3")
70+
))
71+
.andExpect(jsonPath("configUrl",
72+
equalTo("/path/prefix/bar/openapi/v3/swagger-config")
73+
));
74+
}
75+
}
Lines changed: 85 additions & 0 deletions
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+
}

0 commit comments

Comments
 (0)