Skip to content

Commit 118eaf4

Browse files
author
bnasslahsen
committed
Added tests. fixes #320
1 parent f0bfec8 commit 118eaf4

10 files changed

+148
-17
lines changed

springdoc-openapi-ui/src/main/java/org/springdoc/ui/SwaggerWelcome.java

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
1616
import org.springframework.web.util.UriComponentsBuilder;
1717

18+
import javax.annotation.PostConstruct;
1819
import javax.servlet.http.HttpServletRequest;
1920
import java.util.Map;
2021

@@ -39,10 +40,22 @@ class SwaggerWelcome {
3940
@Autowired
4041
private SwaggerUiConfigProperties swaggerUiConfig;
4142

43+
private String uiRootPath;
44+
45+
@PostConstruct
46+
public void calculateUiRootPath() {
47+
StringBuilder sbUrl = new StringBuilder();
48+
if (StringUtils.isNotBlank(mvcServletPath))
49+
sbUrl.append(mvcServletPath);
50+
if (swaggerPath.contains(DEFAULT_PATH_SEPARATOR))
51+
sbUrl.append(swaggerPath.substring(0, swaggerPath.lastIndexOf(DEFAULT_PATH_SEPARATOR)));
52+
this.uiRootPath=sbUrl.toString();
53+
}
54+
4255
@Operation(hidden = true)
4356
@GetMapping(SWAGGER_UI_PATH)
4457
public String redirectToUi(HttpServletRequest request) {
45-
StringBuilder sbUrl = new StringBuilder(REDIRECT_URL_PREFIX).append(this.calculateUiRootPath());
58+
StringBuilder sbUrl = new StringBuilder(REDIRECT_URL_PREFIX).append(this.uiRootPath);
4659
sbUrl.append(SWAGGER_UI_URL);
4760
buildConfigUrl(request);
4861
UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromUriString(sbUrl.toString());
@@ -72,26 +85,14 @@ private void buildConfigUrl(HttpServletRequest request) {
7285
String url = buildUrl(request, apiDocsUrl);
7386
String swaggerConfigUrl = url + DEFAULT_PATH_SEPARATOR + SWAGGGER_CONFIG_FILE;
7487
swaggerUiConfig.setConfigUrl(swaggerConfigUrl);
75-
7688
if (SwaggerUiConfigProperties.getSwaggerUrls().isEmpty())
7789
swaggerUiConfig.setUrl(url);
7890
else
7991
SwaggerUiConfigProperties.addUrl(url);
80-
8192
}
8293
if (!swaggerUiConfig.isValidUrl(swaggerUiConfig.getOauth2RedirectUrl())) {
83-
swaggerUiConfig.setOauth2RedirectUrl(ServletUriComponentsBuilder.fromCurrentContextPath().path(this.calculateUiRootPath().append(swaggerUiConfig.getOauth2RedirectUrl()).toString()).build().toString());
94+
swaggerUiConfig.setOauth2RedirectUrl(ServletUriComponentsBuilder.fromCurrentContextPath().path(this.uiRootPath).path(swaggerUiConfig.getOauth2RedirectUrl()).build().toString());
8495
}
8596
}
8697

87-
private StringBuilder calculateUiRootPath() {
88-
String uiRootPath = "";
89-
if (swaggerPath.contains("/"))
90-
uiRootPath = swaggerPath.substring(0, swaggerPath.lastIndexOf('/'));
91-
StringBuilder sbUrl = new StringBuilder();
92-
if (StringUtils.isNotBlank(mvcServletPath))
93-
sbUrl.append(mvcServletPath);
94-
sbUrl.append(uiRootPath);
95-
return sbUrl;
96-
}
9798
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package test.org.springdoc.ui.app1;
2+
3+
import org.junit.Test;
4+
import org.springframework.boot.test.context.SpringBootTest;
5+
import org.springframework.test.web.servlet.MvcResult;
6+
import test.org.springdoc.ui.AbstractSpringDocTest;
7+
8+
import static org.hamcrest.CoreMatchers.equalTo;
9+
import static org.junit.Assert.assertTrue;
10+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
11+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
12+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
13+
14+
@SpringBootTest(properties = {
15+
"springdoc.swagger-ui.path=/test/swagger.html"
16+
})
17+
public class SpringDocConfigPathsTest extends AbstractSpringDocTest {
18+
19+
@Test
20+
public void should_display_swaggerui_page() throws Exception {
21+
mockMvc.perform(get("/context-path/servlet-path/test/swagger.html").contextPath("/context-path").servletPath("/servlet-path")).andExpect(status().isFound()).andReturn();
22+
MvcResult mvcResult = mockMvc.perform(get("/context-path/servlet-path/test/swagger-ui/index.html").contextPath("/context-path").servletPath("/servlet-path")).andExpect(status().isOk()).andReturn();
23+
String contentAsString = mvcResult.getResponse().getContentAsString();
24+
assertTrue(contentAsString.contains("Swagger UI"));
25+
}
26+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"springdoc.swagger-ui.configUrl=/foo/bar",
1414
"springdoc.swagger-ui.url=/batz" // ignored since configUrl is configured
1515
})
16-
public class SpringDocApp1RedirectConfigUrlTest extends AbstractSpringDocTest {
16+
public class SpringDocRedirectConfigUrlTest extends AbstractSpringDocTest {
1717

1818
@Test
1919
public void shouldRedirectWithConfigUrlIgnoringQueryParams() throws Exception {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
99
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
1010

11-
public class SpringDocApp1RedirectDefaultTest extends AbstractSpringDocTest {
11+
public class SpringDocRedirectDefaultTest extends AbstractSpringDocTest {
1212

1313
@Test
1414
public void shouldRedirectWithDefaultQueryParams() throws Exception {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"springdoc.swagger-ui.validatorUrl=/foo/validate",
1616
"springdoc.api-docs.path=/baf/batz"
1717
})
18-
public class SpringDocApp1RedirectWithConfigTest extends AbstractSpringDocTest {
18+
public class SpringDocRedirectWithConfigTest extends AbstractSpringDocTest {
1919

2020
@Test
2121
public void shouldRedirectWithConfiguredParams() throws Exception {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package test.org.springdoc.ui.app5;
2+
3+
import org.junit.Test;
4+
import org.springframework.boot.test.context.SpringBootTest;
5+
import test.org.springdoc.ui.AbstractSpringDocTest;
6+
7+
import static org.hamcrest.CoreMatchers.equalTo;
8+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
9+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
10+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
11+
12+
@SpringBootTest(properties ="server.servlet.context-path=/context-path")
13+
public class SpringDocOauthContextPathTest extends AbstractSpringDocTest {
14+
15+
@Test
16+
public void oauth2_redirect_url_calculated_with_context_path() throws Exception {
17+
mockMvc.perform(get("/context-path/v3/api-docs/swagger-config").contextPath("/context-path"))
18+
.andExpect(status().isOk())
19+
.andExpect(jsonPath("oauth2RedirectUrl", equalTo("http://localhost/context-path/swagger-ui/oauth2-redirect.html")));
20+
}
21+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package test.org.springdoc.ui.app5;
2+
3+
import org.junit.Test;
4+
import test.org.springdoc.ui.AbstractSpringDocTest;
5+
6+
import static org.hamcrest.CoreMatchers.equalTo;
7+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
8+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
9+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
10+
11+
public class SpringDocOauthPathsTest extends AbstractSpringDocTest {
12+
13+
@Test
14+
public void oauth2_redirect_url_calculated() throws Exception {
15+
mockMvc.perform(get("/v3/api-docs/swagger-config"))
16+
.andExpect(status().isOk())
17+
.andExpect(jsonPath("oauth2RedirectUrl", equalTo("http://localhost/swagger-ui/oauth2-redirect.html")));
18+
}
19+
20+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package test.org.springdoc.ui.app5;
2+
3+
import org.junit.Test;
4+
import org.springframework.boot.test.context.SpringBootTest;
5+
import test.org.springdoc.ui.AbstractSpringDocTest;
6+
7+
import static org.hamcrest.CoreMatchers.equalTo;
8+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
9+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
10+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
11+
12+
@SpringBootTest(properties = "springdoc.swagger-ui.oauth2RedirectUrl=http://localhost:8080/toto/test/swagger-ui/oauth2-redirect.html")
13+
public class SpringDocOauthPathsWithPropertyTest extends AbstractSpringDocTest {
14+
15+
@Test
16+
public void oauth2_redirect_url_calculated() throws Exception {
17+
mockMvc.perform(get("/v3/api-docs/swagger-config"))
18+
.andExpect(status().isOk())
19+
.andExpect(jsonPath("oauth2RedirectUrl", equalTo("http://localhost:8080/toto/test/swagger-ui/oauth2-redirect.html")));
20+
}
21+
22+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package test.org.springdoc.ui.app5;
2+
3+
import org.junit.Test;
4+
import org.springframework.boot.test.context.SpringBootTest;
5+
import test.org.springdoc.ui.AbstractSpringDocTest;
6+
7+
import static org.hamcrest.CoreMatchers.equalTo;
8+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
9+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
10+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
11+
12+
@SpringBootTest(properties = {
13+
"springdoc.swagger-ui.path=/test/swagger.html",
14+
"spring.mvc.servlet.path=/servlet-path"
15+
})
16+
public class SpringDocOauthServletPathsTest extends AbstractSpringDocTest {
17+
18+
@Test
19+
public void should_display_oauth2_redirect_page() throws Exception {
20+
mockMvc.perform(get("/context-path/servlet-path/test/swagger-ui/oauth2-redirect.html").contextPath("/context-path").servletPath("/servlet-path")).andExpect(status().isOk()).andReturn();
21+
}
22+
23+
@Test
24+
public void oauth2_redirect_url_calculated_with_context_path_and_servlet_path() throws Exception {
25+
mockMvc.perform(get("/context-path/servlet-path/v3/api-docs/swagger-config").contextPath("/context-path").servletPath("/servlet-path"))
26+
.andExpect(status().isOk())
27+
.andExpect(jsonPath("oauth2RedirectUrl", equalTo("http://localhost/context-path/servlet-path/test/swagger-ui/oauth2-redirect.html")));
28+
}
29+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package test.org.springdoc.ui.app5;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class SpringDocTestApp {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(SpringDocTestApp.class, args);
11+
}
12+
}

0 commit comments

Comments
 (0)