Skip to content

Commit d5be3b9

Browse files
author
wiiitek
committed
Add tests for Springdoc behind a proxy
1 parent 425eff2 commit d5be3b9

File tree

6 files changed

+474
-0
lines changed

6 files changed

+474
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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.assertj.core.api.Assertions.assertThat;
28+
29+
@TestPropertySource(properties = {
30+
"server.forward-headers-strategy=framework"
31+
})
32+
public class SpringDocBehindProxyTest extends AbstractSpringDocTest {
33+
34+
private static final String X_FORWARD_PREFIX = "/path/prefix";
35+
36+
@SpringBootApplication
37+
static class SpringDocTestApp {}
38+
39+
@Test
40+
public void shouldServeSwaggerUIAtDefaultPath() {
41+
webTestClient.get().uri("/webjars/swagger-ui/index.html").exchange()
42+
.expectStatus().isOk();
43+
}
44+
45+
@Test
46+
public void shouldReturnCorrectInitializerJS() throws Exception {
47+
webTestClient
48+
.get().uri("/webjars/swagger-ui/swagger-initializer.js")
49+
.header("X-Forwarded-Prefix", X_FORWARD_PREFIX)
50+
.exchange()
51+
.expectStatus().isOk()
52+
.expectBody(String.class)
53+
.consumeWith(response ->
54+
assertThat(response.getResponseBody())
55+
.contains("\"configUrl\" : \"/path/prefix/v3/api-docs/swagger-config\",")
56+
);
57+
}
58+
59+
@Test
60+
public void shouldCalculateOauthRedirectBehindProxy() throws Exception {
61+
webTestClient
62+
.get().uri("/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+
.exchange()
67+
.expectStatus().isOk().expectBody()
68+
.jsonPath("$.oauth2RedirectUrl").isEqualTo("https://proxy-host/path/prefix/swagger-ui/oauth2-redirect.html");
69+
}
70+
71+
@Test
72+
public void shouldCalculateUrlsBehindProxy() throws Exception {
73+
webTestClient
74+
.get().uri("/v3/api-docs/swagger-config")
75+
.header("X-Forwarded-Prefix", X_FORWARD_PREFIX)
76+
.exchange()
77+
.expectStatus().isOk().expectBody()
78+
.jsonPath("$.url")
79+
.isEqualTo("/path/prefix/v3/api-docs")
80+
.jsonPath("$.configUrl")
81+
.isEqualTo("/path/prefix/v3/api-docs/swagger-config");
82+
}
83+
}
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.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+
})
33+
public class SpringDocBehindProxyWithCustomUIPathTest extends AbstractSpringDocTest {
34+
35+
private static final String X_FORWARD_PREFIX = "/path/prefix";
36+
37+
@SpringBootApplication
38+
static class SpringDocTestApp {}
39+
40+
@Test
41+
public void shouldRedirectSwaggerUIFromCustomPath() {
42+
webTestClient
43+
.get().uri("/foo/documentation/swagger.html")
44+
.header("X-Forwarded-Prefix", X_FORWARD_PREFIX)
45+
.exchange()
46+
.expectStatus().isFound()
47+
.expectHeader().location("/path/prefix/foo/documentation/swagger-ui/index.html");
48+
}
49+
50+
@Test
51+
public void shouldReturnCorrectInitializerJS() {
52+
webTestClient
53+
.get().uri("/foo/documentation/swagger-ui/swagger-initializer.js")
54+
.header("X-Forwarded-Prefix", X_FORWARD_PREFIX)
55+
.exchange()
56+
.expectStatus().isOk()
57+
.expectBody(String.class)
58+
.consumeWith(response ->
59+
assertThat(response.getResponseBody())
60+
.contains("\"configUrl\" : \\\"/path/prefix/v3/api-docs/swagger-config\\\",")
61+
);
62+
}
63+
64+
@Test
65+
public void shouldCalculateUrlsBehindProxy() throws Exception {
66+
webTestClient
67+
.get().uri("/v3/api-docs/swagger-config")
68+
.header("X-Forwarded-Prefix", X_FORWARD_PREFIX)
69+
.exchange()
70+
.expectStatus().isOk()
71+
.expectBody()
72+
.jsonPath("$.url")
73+
.isEqualTo("/path/prefix/v3/api-docs")
74+
.jsonPath("$.configUrl")
75+
.isEqualTo("/path/prefix/v3/api-docs/swagger-config");
76+
}
77+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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.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+
}
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)