Skip to content

Commit ef7cc81

Browse files
author
wiiitek
committed
simplify unit tests for springdoc ui behind a proxy
1 parent 582f3d2 commit ef7cc81

File tree

6 files changed

+86
-165
lines changed

6 files changed

+86
-165
lines changed

springdoc-openapi-ui/src/test/java/test/org/springdoc/ui/app31/SpringDocApp31Test.java

Lines changed: 0 additions & 47 deletions
This file was deleted.

springdoc-openapi-ui/src/test/java/test/org/springdoc/ui/app31/SpringDocBehindProxyTest.java

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,21 @@
1919
package test.org.springdoc.ui.app31;
2020

2121
import org.junit.jupiter.api.Test;
22+
import test.org.springdoc.ui.AbstractSpringDocTest;
23+
2224
import org.springframework.boot.autoconfigure.SpringBootApplication;
2325
import org.springframework.test.context.TestPropertySource;
24-
import org.springframework.test.web.servlet.MvcResult;
25-
import test.org.springdoc.ui.AbstractSpringDocTest;
2626

2727
import static org.hamcrest.CoreMatchers.equalTo;
28-
import static org.junit.jupiter.api.Assertions.assertEquals;
29-
import static org.junit.jupiter.api.Assertions.assertTrue;
28+
import static org.hamcrest.Matchers.containsString;
3029
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
30+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
3131
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
3232
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
3333

34-
@TestPropertySource(properties = {"server.forward-headers-strategy=framework"})
34+
@TestPropertySource(properties = {
35+
"server.forward-headers-strategy=framework"
36+
})
3537
public class SpringDocBehindProxyTest extends AbstractSpringDocTest {
3638

3739
private static final String X_FORWARD_PREFIX = "/path/prefix";
@@ -47,13 +49,12 @@ public void shouldServeSwaggerUIAtDefaultPath() throws Exception {
4749

4850
@Test
4951
public void shouldReturnCorrectInitializerJS() throws Exception {
50-
MvcResult mvcResult = mockMvc.perform(get("/swagger-ui/swagger-initializer.js")
52+
mockMvc.perform(get("/swagger-ui/swagger-initializer.js")
5153
.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\","));
54+
.andExpect(status().isOk())
55+
.andExpect(content().string(
56+
containsString("\"configUrl\" : \"/path/prefix/v3/api-docs/swagger-config\",")
57+
));
5758
}
5859

5960
@Test
@@ -63,15 +64,21 @@ public void shouldCalculateOauthRedirectBehindProxy() throws Exception {
6364
.header("X-Forwarded-Host", "proxy-host")
6465
.header("X-Forwarded-Prefix", X_FORWARD_PREFIX))
6566
.andExpect(status().isOk())
66-
.andExpect(jsonPath("oauth2RedirectUrl", equalTo("https://proxy-host/path/prefix/swagger-ui/oauth2-redirect.html")));
67+
.andExpect(jsonPath("oauth2RedirectUrl",
68+
equalTo("https://proxy-host/path/prefix/swagger-ui/oauth2-redirect.html")
69+
));
6770
}
6871

6972
@Test
7073
public void shouldCalculateUrlsBehindProxy() throws Exception {
7174
mockMvc.perform(get("/v3/api-docs/swagger-config")
72-
.header("X-Forwarded-Prefix", X_FORWARD_PREFIX))
75+
.header("X-Forwarded-Prefix", X_FORWARD_PREFIX))
7376
.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")));
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+
));
7683
}
77-
}
84+
}
Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,55 +19,59 @@
1919
package test.org.springdoc.ui.app31;
2020

2121
import org.junit.jupiter.api.Test;
22+
import test.org.springdoc.ui.AbstractSpringDocTest;
23+
2224
import org.springframework.boot.autoconfigure.SpringBootApplication;
2325
import org.springframework.test.context.TestPropertySource;
24-
import org.springframework.test.web.servlet.MvcResult;
25-
import test.org.springdoc.ui.AbstractSpringDocTest;
2626

2727
import static org.hamcrest.CoreMatchers.equalTo;
28-
import static org.junit.jupiter.api.Assertions.assertTrue;
28+
import static org.hamcrest.Matchers.containsString;
2929
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
30-
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
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;
3134

3235
@TestPropertySource(properties = {
3336
"server.forward-headers-strategy=framework",
34-
"springdoc.swagger-ui.path=/documentation/swagger.html"
37+
"springdoc.swagger-ui.path=/foo/documentation/swagger.html"
3538
})
36-
public class SpringDocBehindProxyWithCustomUIPathPathTest extends AbstractSpringDocTest {
39+
public class SpringDocBehindProxyWithCustomUIPathTest extends AbstractSpringDocTest {
3740

3841
private static final String X_FORWARD_PREFIX = "/path/prefix";
3942

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-
4343
@SpringBootApplication
4444
static class SpringDocTestApp {}
4545

4646
@Test
4747
public void shouldRedirectSwaggerUIFromCustomPath() throws Exception {
48-
mockMvc.perform(get("/documentation/swagger.html")
48+
mockMvc.perform(get("/foo/documentation/swagger.html")
4949
.header("X-Forwarded-Prefix", X_FORWARD_PREFIX))
5050
.andExpect(status().isFound())
51-
.andExpect(header().string("Location", "/path/prefix/documentation/swagger-ui/index.html"));
51+
.andExpect(header().string("Location", "/path/prefix/foo/documentation/swagger-ui/index.html"));
5252
}
5353

5454
@Test
55-
public void shouldCalculateUrlsBehindProxy() throws Exception {
56-
mockMvc.perform(get("/v3/api-docs/swagger-config")
55+
public void shouldReturnCorrectInitializerJS() throws Exception {
56+
mockMvc.perform(get("/foo/documentation/swagger-ui/swagger-initializer.js")
5757
.header("X-Forwarded-Prefix", X_FORWARD_PREFIX))
5858
.andExpect(status().isOk())
59-
.andExpect(jsonPath("configUrl", equalTo(EXTERNAL_SWAGGER_CONFIG_URL)))
60-
.andExpect(jsonPath("url", equalTo(EXTERNAL_OPENAPI_JSON_URL)));
59+
.andExpect(content().string(
60+
containsString("\"configUrl\" : \"/path/prefix/v3/api-docs/swagger-config\",")
61+
));
6162
}
6263

6364
@Test
64-
public void shouldReturnCorrectInitializerJS() throws Exception {
65-
MvcResult mvcResult = mockMvc.perform(get("/documentation/swagger-ui/swagger-initializer.js")
65+
public void shouldCalculateUrlsBehindProxy() throws Exception {
66+
mockMvc.perform(get("/v3/api-docs/swagger-config")
6667
.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 + "\","));
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+
));
7275
}
73-
}
76+
77+
}
Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,55 +19,57 @@
1919
package test.org.springdoc.ui.app31;
2020

2121
import org.junit.jupiter.api.Test;
22+
import test.org.springdoc.ui.AbstractSpringDocTest;
23+
2224
import org.springframework.boot.autoconfigure.SpringBootApplication;
2325
import org.springframework.test.context.TestPropertySource;
24-
import org.springframework.test.web.servlet.MvcResult;
25-
import test.org.springdoc.ui.AbstractSpringDocTest;
2626

2727
import static org.hamcrest.CoreMatchers.equalTo;
28-
import static org.junit.jupiter.api.Assertions.assertTrue;
28+
import static org.hamcrest.Matchers.containsString;
2929
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
30-
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
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;
3133

3234
@TestPropertySource(properties = {
3335
"server.forward-headers-strategy=framework",
34-
"springdoc.api-docs.path=/docs/v3/openapi",
35-
"springdoc.swagger-ui.path=/documentation/swagger.html"
36+
"springdoc.swagger-ui.path=/foo/documentation/swagger.html",
37+
"springdoc.api-docs.path=/bar/openapi/v3"
3638
})
37-
public class SpringDocBehindProxyWithCustomSpringAndUIPathsTest extends AbstractSpringDocTest {
39+
public class SpringDocBehindProxyWithCustomUIPathWithApiDocsTest extends AbstractSpringDocTest {
3840

3941
private static final String X_FORWARD_PREFIX = "/path/prefix";
4042

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-
4443
@SpringBootApplication
4544
static class SpringDocTestApp {}
4645

4746
@Test
4847
public void shouldServeOpenapiJsonUnderCustomPath() throws Exception {
49-
mockMvc.perform(get("/docs/v3/openapi")
48+
mockMvc.perform(get("/bar/openapi/v3")
5049
.header("X-Forwarded-Prefix", X_FORWARD_PREFIX))
5150
.andExpect(status().isOk());
5251
}
5352

5453
@Test
55-
public void shouldCalculateUrlsBehindProxy() throws Exception {
56-
mockMvc.perform(get("/docs/v3/openapi/swagger-config")
54+
public void shouldReturnCorrectInitializerJS() throws Exception {
55+
mockMvc.perform(get("/foo/documentation/swagger-ui/swagger-initializer.js")
5756
.header("X-Forwarded-Prefix", X_FORWARD_PREFIX))
5857
.andExpect(status().isOk())
59-
.andExpect(jsonPath("configUrl", equalTo(EXTERNAL_SWAGGER_CONFIG_URL)))
60-
.andExpect(jsonPath("url", equalTo(EXTERNAL_OPENAPI_JSON_URL)));
58+
.andExpect(content().string(
59+
containsString("\"configUrl\" : \"/path/prefix/bar/openapi/v3/swagger-config\",")
60+
));
6161
}
6262

6363
@Test
64-
public void shouldReturnCorrectInitializerJS() throws Exception {
65-
MvcResult mvcResult = mockMvc.perform(get("/documentation/swagger-ui/swagger-initializer.js")
64+
public void shouldCalculateUrlsBehindProxy() throws Exception {
65+
mockMvc.perform(get("/bar/openapi/v3/swagger-config")
6666
.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 +"\","));
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+
));
7274
}
73-
}
75+
}

springdoc-openapi-webflux-ui/src/test/java/test/org/springdoc/ui/app31/SpringDocApp31Test.java

Lines changed: 0 additions & 43 deletions
This file was deleted.

springdoc-openapi-webflux-ui/src/test/java/test/org/springdoc/ui/app31/SpringDocBehindProxyTest.java

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@
2424
import org.springframework.boot.autoconfigure.SpringBootApplication;
2525
import org.springframework.test.context.TestPropertySource;
2626

27-
import static org.junit.jupiter.api.Assertions.assertNotNull;
28-
import static org.junit.jupiter.api.Assertions.assertTrue;
27+
import static org.assertj.core.api.Assertions.assertThat;
2928

30-
@TestPropertySource(properties = { "server.forward-headers-strategy=framework" })
29+
@TestPropertySource(properties = {
30+
"server.forward-headers-strategy=framework"
31+
})
3132
public class SpringDocBehindProxyTest extends AbstractSpringDocTest {
3233

3334
private static final String X_FORWARD_PREFIX = "/path/prefix";
@@ -49,14 +50,9 @@ public void shouldReturnCorrectInitializerJS() throws Exception {
4950
.exchange()
5051
.expectStatus().isOk()
5152
.expectBody(String.class)
52-
.consumeWith(response -> {
53-
String actualContent = response.getResponseBody();
54-
assertNotNull(actualContent);
55-
assertTrue(actualContent.contains("window.ui"));
56-
assertTrue(actualContent.contains("\"configUrl\" : \"/v3/api-docs/swagger-config\","));
57-
// TODO: what should be returned
58-
//assertTrue(actualContent.contains("\"configUrl\" : \"/path/prefix/v3/api-docs/swagger-config\","));
59-
}
53+
.consumeWith(response ->
54+
assertThat(response.getResponseBody())
55+
.contains("\"configUrl\" : \"/path/prefix/v3/api-docs/swagger-config\",")
6056
);
6157
}
6258

@@ -79,7 +75,9 @@ public void shouldCalculateUrlsBehindProxy() throws Exception {
7975
.header("X-Forwarded-Prefix", X_FORWARD_PREFIX)
8076
.exchange()
8177
.expectStatus().isOk().expectBody()
82-
.jsonPath("$.configUrl").isEqualTo("/path/prefix/v3/api-docs/swagger-config")
83-
.jsonPath("$.url").isEqualTo("/path/prefix/v3/api-docs");
78+
.jsonPath("$.url")
79+
.isEqualTo("/path/prefix/v3/api-docs")
80+
.jsonPath("$.configUrl")
81+
.isEqualTo("/path/prefix/v3/api-docs/swagger-config");
8482
}
8583
}

0 commit comments

Comments
 (0)