Skip to content

Commit ac1b135

Browse files
author
wiiitek
committed
additional tests for springdoc to document behaviour behind the proxy
1 parent f4a5c6e commit ac1b135

File tree

4 files changed

+247
-0
lines changed

4 files changed

+247
-0
lines changed
Lines changed: 77 additions & 0 deletions
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.app31;
20+
21+
import org.junit.jupiter.api.Test;
22+
import org.springframework.boot.autoconfigure.SpringBootApplication;
23+
import org.springframework.test.context.TestPropertySource;
24+
import org.springframework.test.web.servlet.MvcResult;
25+
import test.org.springdoc.ui.AbstractSpringDocTest;
26+
27+
import static org.hamcrest.CoreMatchers.equalTo;
28+
import static org.junit.jupiter.api.Assertions.assertEquals;
29+
import static org.junit.jupiter.api.Assertions.assertTrue;
30+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
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 = {"server.forward-headers-strategy=framework"})
35+
public class SpringDocBehindProxyTest 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 shouldServeSwaggerUIAtDefaultPath() throws Exception {
44+
mockMvc.perform(get("/swagger-ui/index.html"))
45+
.andExpect(status().isOk());
46+
}
47+
48+
@Test
49+
public void shouldReturnCorrectInitializerJS() throws Exception {
50+
MvcResult mvcResult = mockMvc.perform(get("/swagger-ui/swagger-initializer.js")
51+
.header("X-Forwarded-Prefix", X_FORWARD_PREFIX))
52+
.andExpect(status().isOk()).andReturn();
53+
String actualContent = mvcResult.getResponse().getContentAsString();
54+
55+
assertTrue(actualContent.contains("window.ui"));
56+
assertTrue(actualContent.contains("\"configUrl\" : \"/path/prefix/v3/api-docs/swagger-config\","));
57+
}
58+
59+
@Test
60+
public void shouldCalculateOauthRedirectBehindProxy() throws Exception {
61+
mockMvc.perform(get("/v3/api-docs/swagger-config")
62+
.header("X-Forwarded-Proto", "https")
63+
.header("X-Forwarded-Host", "proxy-host")
64+
.header("X-Forwarded-Prefix", X_FORWARD_PREFIX))
65+
.andExpect(status().isOk())
66+
.andExpect(jsonPath("oauth2RedirectUrl", equalTo("https://proxy-host/path/prefix/swagger-ui/oauth2-redirect.html")));
67+
}
68+
69+
@Test
70+
public void shouldCalculateUrlsBehindProxy() throws Exception {
71+
mockMvc.perform(get("/v3/api-docs/swagger-config")
72+
.header("X-Forwarded-Prefix", X_FORWARD_PREFIX))
73+
.andExpect(status().isOk())
74+
.andExpect(jsonPath("configUrl", equalTo("/path/prefix/v3/api-docs/swagger-config")))
75+
.andExpect(jsonPath("url", equalTo("/path/prefix/v3/api-docs")));
76+
}
77+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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 org.springframework.boot.autoconfigure.SpringBootApplication;
23+
import org.springframework.test.context.TestPropertySource;
24+
import org.springframework.test.web.servlet.MvcResult;
25+
import test.org.springdoc.ui.AbstractSpringDocTest;
26+
27+
import static org.hamcrest.CoreMatchers.equalTo;
28+
import static org.junit.jupiter.api.Assertions.assertTrue;
29+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
30+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
31+
32+
@TestPropertySource(properties = {
33+
"server.forward-headers-strategy=framework",
34+
"springdoc.api-docs.path=/docs/v3/openapi",
35+
"springdoc.swagger-ui.path=/documentation/swagger.html"
36+
})
37+
public class SpringDocBehindProxyWithCustomSpringAndUIPathsTest extends AbstractSpringDocTest {
38+
39+
private static final String X_FORWARD_PREFIX = "/path/prefix";
40+
41+
private static final String EXTERNAL_SWAGGER_CONFIG_URL = "/path/prefix/docs/v3/openapi/swagger-config";
42+
private static final String EXTERNAL_OPENAPI_JSON_URL = "/path/prefix/docs/v3/openapi";
43+
44+
@SpringBootApplication
45+
static class SpringDocTestApp {}
46+
47+
@Test
48+
public void shouldServeOpenapiJsonUnderCustomPath() throws Exception {
49+
mockMvc.perform(get("/docs/v3/openapi")
50+
.header("X-Forwarded-Prefix", X_FORWARD_PREFIX))
51+
.andExpect(status().isOk());
52+
}
53+
54+
@Test
55+
public void shouldCalculateUrlsBehindProxy() throws Exception {
56+
mockMvc.perform(get("/docs/v3/openapi/swagger-config")
57+
.header("X-Forwarded-Prefix", X_FORWARD_PREFIX))
58+
.andExpect(status().isOk())
59+
.andExpect(jsonPath("configUrl", equalTo(EXTERNAL_SWAGGER_CONFIG_URL)))
60+
.andExpect(jsonPath("url", equalTo(EXTERNAL_OPENAPI_JSON_URL)));
61+
}
62+
63+
@Test
64+
public void shouldReturnCorrectInitializerJS() throws Exception {
65+
MvcResult mvcResult = mockMvc.perform(get("/documentation/swagger-ui/swagger-initializer.js")
66+
.header("X-Forwarded-Prefix", X_FORWARD_PREFIX))
67+
.andExpect(status().isOk()).andReturn();
68+
String actualContent = mvcResult.getResponse().getContentAsString();
69+
70+
assertTrue(actualContent.contains("window.ui"));
71+
assertTrue(actualContent.contains("\"configUrl\" : \"" + EXTERNAL_SWAGGER_CONFIG_URL +"\","));
72+
}
73+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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 org.springframework.boot.autoconfigure.SpringBootApplication;
23+
import org.springframework.test.context.TestPropertySource;
24+
import org.springframework.test.web.servlet.MvcResult;
25+
import test.org.springdoc.ui.AbstractSpringDocTest;
26+
27+
import static org.hamcrest.CoreMatchers.equalTo;
28+
import static org.junit.jupiter.api.Assertions.assertTrue;
29+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
30+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
31+
32+
@TestPropertySource(properties = {
33+
"server.forward-headers-strategy=framework",
34+
"springdoc.swagger-ui.path=/documentation/swagger.html"
35+
})
36+
public class SpringDocBehindProxyWithCustomUIPathPathTest extends AbstractSpringDocTest {
37+
38+
private static final String X_FORWARD_PREFIX = "/path/prefix";
39+
40+
private static final String EXTERNAL_SWAGGER_CONFIG_URL = "/path/prefix/v3/api-docs/swagger-config";
41+
private static final String EXTERNAL_OPENAPI_JSON_URL = "/path/prefix/v3/api-docs";
42+
43+
@SpringBootApplication
44+
static class SpringDocTestApp {}
45+
46+
@Test
47+
public void shouldRedirectSwaggerUIFromCustomPath() throws Exception {
48+
mockMvc.perform(get("/documentation/swagger.html")
49+
.header("X-Forwarded-Prefix", X_FORWARD_PREFIX))
50+
.andExpect(status().isFound())
51+
.andExpect(header().string("Location", "/path/prefix/documentation/swagger-ui/index.html"));
52+
}
53+
54+
@Test
55+
public void shouldCalculateUrlsBehindProxy() throws Exception {
56+
mockMvc.perform(get("/v3/api-docs/swagger-config")
57+
.header("X-Forwarded-Prefix", X_FORWARD_PREFIX))
58+
.andExpect(status().isOk())
59+
.andExpect(jsonPath("configUrl", equalTo(EXTERNAL_SWAGGER_CONFIG_URL)))
60+
.andExpect(jsonPath("url", equalTo(EXTERNAL_OPENAPI_JSON_URL)));
61+
}
62+
63+
@Test
64+
public void shouldReturnCorrectInitializerJS() throws Exception {
65+
MvcResult mvcResult = mockMvc.perform(get("/documentation/swagger-ui/swagger-initializer.js")
66+
.header("X-Forwarded-Prefix", X_FORWARD_PREFIX))
67+
.andExpect(status().isOk()).andReturn();
68+
String actualContent = mvcResult.getResponse().getContentAsString();
69+
70+
assertTrue(actualContent.contains("window.ui"));
71+
assertTrue(actualContent.contains("\"configUrl\" : \"" + EXTERNAL_SWAGGER_CONFIG_URL + "\","));
72+
}
73+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
window.onload = function() {
2+
//<editor-fold desc="Changeable Configuration Block">
3+
4+
// the following lines will be replaced by docker/configurator, when it runs in a docker-container
5+
window.ui = SwaggerUIBundle({
6+
url: "https://petstore.swagger.io/v2/swagger.json",
7+
dom_id: '#swagger-ui',
8+
deepLinking: true,
9+
presets: [
10+
SwaggerUIBundle.presets.apis,
11+
SwaggerUIStandalonePreset
12+
],
13+
plugins: [
14+
SwaggerUIBundle.plugins.DownloadUrl
15+
],
16+
layout: "StandaloneLayout" ,
17+
18+
"configUrl" : "/path/prefix/v3/api-docs/swagger-config",
19+
"validatorUrl" : ""
20+
21+
});
22+
23+
//</editor-fold>
24+
};

0 commit comments

Comments
 (0)