Skip to content

Commit 8173442

Browse files
committed
Add consistent tests for WebFlux and WebMvc to verify correct WebJar mappings
1 parent 2551a6f commit 8173442

File tree

11 files changed

+461
-2
lines changed

11 files changed

+461
-2
lines changed

springdoc-openapi-starter-webflux-ui/src/test/java/test/org/springdoc/ui/app34/SpringDocApp34Test.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,14 @@
3333
import static org.junit.jupiter.api.Assertions.assertFalse;
3434
import static org.junit.jupiter.api.Assertions.assertTrue;
3535

36-
@TestPropertySource(properties = "springdoc.swagger-ui.disable-swagger-default-url=true")
36+
@TestPropertySource(properties = {
37+
"springdoc.swagger-ui.disable-swagger-default-url=true",
38+
"springdoc.swagger-ui.path=/documentation/swagger-ui.html"
39+
})
3740
public class SpringDocApp34Test extends AbstractSpringDocTest {
3841

3942
@Test
40-
void transformed_index_with_oauth() throws Exception {
43+
void testWebJarResourceTransformed() {
4144
EntityExchangeResult<byte[]> getResult = webTestClient.get().uri("/webjars/swagger-ui/swagger-initializer.js")
4245
.exchange()
4346
.expectStatus().isOk()
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
*
3+
* *
4+
* * *
5+
* * * * Copyright 2019-2020 the original author or authors.
6+
* * * *
7+
* * * * Licensed under the Apache License, Version 2.0 (the "License");
8+
* * * * you may not use this file except in compliance with the License.
9+
* * * * You may obtain a copy of the License at
10+
* * * *
11+
* * * * https://www.apache.org/licenses/LICENSE-2.0
12+
* * * *
13+
* * * * Unless required by applicable law or agreed to in writing, software
14+
* * * * distributed under the License is distributed on an "AS IS" BASIS,
15+
* * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* * * * See the License for the specific language governing permissions and
17+
* * * * limitations under the License.
18+
* * *
19+
* *
20+
*
21+
*
22+
*/
23+
24+
package test.org.springdoc.ui.app35;
25+
26+
import jakarta.validation.Valid;
27+
import jakarta.validation.constraints.Size;
28+
29+
import org.springframework.web.bind.annotation.GetMapping;
30+
import org.springframework.web.bind.annotation.RequestParam;
31+
import org.springframework.web.bind.annotation.RestController;
32+
33+
@RestController
34+
public class HelloController {
35+
36+
@GetMapping(value = "/persons")
37+
public void persons(@Valid @RequestParam @Size(min = 4, max = 6) String name) {
38+
39+
}
40+
41+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
*
3+
* *
4+
* * *
5+
* * * * Copyright 2019-2020 the original author or authors.
6+
* * * *
7+
* * * * Licensed under the Apache License, Version 2.0 (the "License");
8+
* * * * you may not use this file except in compliance with the License.
9+
* * * * You may obtain a copy of the License at
10+
* * * *
11+
* * * * https://www.apache.org/licenses/LICENSE-2.0
12+
* * * *
13+
* * * * Unless required by applicable law or agreed to in writing, software
14+
* * * * distributed under the License is distributed on an "AS IS" BASIS,
15+
* * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* * * * See the License for the specific language governing permissions and
17+
* * * * limitations under the License.
18+
* * *
19+
* *
20+
*
21+
*
22+
*/
23+
24+
package test.org.springdoc.ui.app35;
25+
26+
import org.junit.jupiter.api.Test;
27+
import org.springframework.http.CacheControl;
28+
import test.org.springdoc.ui.AbstractSpringDocTest;
29+
import org.springframework.boot.autoconfigure.SpringBootApplication;
30+
import org.springframework.test.context.TestPropertySource;
31+
import org.springframework.test.web.reactive.server.EntityExchangeResult;
32+
33+
import static org.junit.jupiter.api.Assertions.assertFalse;
34+
import static org.junit.jupiter.api.Assertions.assertTrue;
35+
36+
@TestPropertySource(properties = {
37+
"spring.webflux.webjars-path-pattern=/webjars-pref/**",
38+
"springdoc.swagger-ui.disable-swagger-default-url=true",
39+
"springdoc.swagger-ui.path=/documentation/swagger-ui.html"
40+
})
41+
public class SpringDocApp35Test extends AbstractSpringDocTest {
42+
43+
@Test
44+
void testWebJarPrefix() {
45+
webTestClient.get().uri("/webjars/swagger-ui/swagger-initializer.js")
46+
.exchange()
47+
.expectStatus().isNotFound();
48+
49+
EntityExchangeResult<byte[]> getResult = webTestClient.get().uri("/webjars-pref/swagger-ui/swagger-initializer.js")
50+
.exchange()
51+
.expectStatus().isOk()
52+
.expectHeader().cacheControl(CacheControl.noStore())
53+
.expectBody().returnResult();
54+
55+
var responseContent = new String(getResult.getResponseBody());
56+
assertFalse(responseContent.contains("https://petstore.swagger.io/v2/swagger.json"));
57+
assertTrue(responseContent.contains("/v3/api-docs"));
58+
}
59+
60+
@SpringBootApplication
61+
static class SpringDocTestApp {}
62+
63+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
*
3+
* *
4+
* * *
5+
* * * * Copyright 2019-2020 the original author or authors.
6+
* * * *
7+
* * * * Licensed under the Apache License, Version 2.0 (the "License");
8+
* * * * you may not use this file except in compliance with the License.
9+
* * * * You may obtain a copy of the License at
10+
* * * *
11+
* * * * https://www.apache.org/licenses/LICENSE-2.0
12+
* * * *
13+
* * * * Unless required by applicable law or agreed to in writing, software
14+
* * * * distributed under the License is distributed on an "AS IS" BASIS,
15+
* * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* * * * See the License for the specific language governing permissions and
17+
* * * * limitations under the License.
18+
* * *
19+
* *
20+
*
21+
*
22+
*/
23+
24+
package test.org.springdoc.ui.app36;
25+
26+
import jakarta.validation.Valid;
27+
import jakarta.validation.constraints.Size;
28+
29+
import org.springframework.web.bind.annotation.GetMapping;
30+
import org.springframework.web.bind.annotation.RequestParam;
31+
import org.springframework.web.bind.annotation.RestController;
32+
33+
@RestController
34+
public class HelloController {
35+
36+
@GetMapping(value = "/persons")
37+
public void persons(@Valid @RequestParam @Size(min = 4, max = 6) String name) {
38+
39+
}
40+
41+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
*
3+
* *
4+
* * *
5+
* * * * Copyright 2019-2020 the original author or authors.
6+
* * * *
7+
* * * * Licensed under the Apache License, Version 2.0 (the "License");
8+
* * * * you may not use this file except in compliance with the License.
9+
* * * * You may obtain a copy of the License at
10+
* * * *
11+
* * * * https://www.apache.org/licenses/LICENSE-2.0
12+
* * * *
13+
* * * * Unless required by applicable law or agreed to in writing, software
14+
* * * * distributed under the License is distributed on an "AS IS" BASIS,
15+
* * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* * * * See the License for the specific language governing permissions and
17+
* * * * limitations under the License.
18+
* * *
19+
* *
20+
*
21+
*
22+
*/
23+
24+
package test.org.springdoc.ui.app36;
25+
26+
import org.junit.jupiter.api.Test;
27+
import test.org.springdoc.ui.AbstractSpringDocTest;
28+
29+
import org.springframework.boot.autoconfigure.SpringBootApplication;
30+
import org.springframework.test.context.TestPropertySource;
31+
32+
@TestPropertySource(properties = {
33+
"spring.webflux.webjars-path-pattern=/webjars-pref/**",
34+
"spring.web.resources.add-mappings=false",
35+
"springdoc.swagger-ui.path=/documentation/swagger-ui.html"
36+
})
37+
public class SpringDocApp36Test extends AbstractSpringDocTest {
38+
39+
@Test
40+
void testWebJarMappingDisabled() {
41+
webTestClient.get().uri("/webjars/swagger-ui/swagger-initializer.js")
42+
.exchange()
43+
.expectStatus().isNotFound();
44+
45+
webTestClient.get().uri("/webjars-pref/swagger-ui/swagger-initializer.js")
46+
.exchange()
47+
.expectStatus().isNotFound();
48+
}
49+
50+
@SpringBootApplication
51+
static class SpringDocTestApp {}
52+
53+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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.app36;
20+
21+
22+
import jakarta.validation.Valid;
23+
import jakarta.validation.constraints.Size;
24+
import org.springframework.web.bind.annotation.GetMapping;
25+
import org.springframework.web.bind.annotation.RequestParam;
26+
import org.springframework.web.bind.annotation.RestController;
27+
28+
@RestController
29+
public class HelloController {
30+
31+
@GetMapping(value = "/persons")
32+
public void persons(@Valid @RequestParam @Size(min = 4, max = 6) String name) {
33+
34+
}
35+
36+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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.app36;
20+
21+
import org.junit.jupiter.api.Test;
22+
import org.springframework.boot.autoconfigure.SpringBootApplication;
23+
import org.springframework.test.context.TestPropertySource;
24+
import test.org.springdoc.ui.AbstractSpringDocTest;
25+
26+
import static org.hamcrest.Matchers.containsString;
27+
import static org.hamcrest.Matchers.not;
28+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
29+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
30+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
31+
32+
@TestPropertySource(properties = {
33+
"springdoc.swagger-ui.disable-swagger-default-url=true",
34+
"springdoc.swagger-ui.path=/documentation/swagger-ui.html"
35+
})
36+
public class SpringDocApp36Test extends AbstractSpringDocTest {
37+
38+
@Test
39+
void testWebJarResourceTransformed() throws Exception {
40+
mockMvc.perform(get("/webjars/swagger-ui/swagger-initializer.js"))
41+
.andExpect(status().isOk())
42+
.andExpect(content().string(not(containsString("https://petstore.swagger.io/v2/swagger.json"))))
43+
.andExpect(content().string(containsString("/v3/api-docs")));
44+
}
45+
46+
@SpringBootApplication
47+
static class SpringDocTestApp {}
48+
49+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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.app37;
20+
21+
22+
import jakarta.validation.Valid;
23+
import jakarta.validation.constraints.Size;
24+
import org.springframework.web.bind.annotation.GetMapping;
25+
import org.springframework.web.bind.annotation.RequestParam;
26+
import org.springframework.web.bind.annotation.RestController;
27+
28+
@RestController
29+
public class HelloController {
30+
31+
@GetMapping(value = "/persons")
32+
public void persons(@Valid @RequestParam @Size(min = 4, max = 6) String name) {
33+
34+
}
35+
36+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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.app37;
20+
21+
import org.junit.jupiter.api.Test;
22+
import org.springframework.boot.autoconfigure.SpringBootApplication;
23+
import org.springframework.test.context.TestPropertySource;
24+
import test.org.springdoc.ui.AbstractSpringDocTest;
25+
26+
import static org.hamcrest.Matchers.containsString;
27+
import static org.hamcrest.Matchers.not;
28+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
29+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
30+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
31+
32+
@TestPropertySource(properties = {
33+
"spring.mvc.webjars-path-pattern=/webjars-pref/**",
34+
"springdoc.swagger-ui.disable-swagger-default-url=true",
35+
"springdoc.swagger-ui.path=/documentation/swagger-ui.html"
36+
})
37+
public class SpringDocApp37Test extends AbstractSpringDocTest {
38+
39+
@Test
40+
void testWebJarPrefix() throws Exception {
41+
mockMvc.perform(get("/webjars/swagger-ui/swagger-initializer.js"))
42+
.andExpect(status().isNotFound());
43+
44+
mockMvc.perform(get("/webjars-pref/swagger-ui/swagger-initializer.js"))
45+
.andExpect(status().isOk())
46+
.andExpect(content().string(not(containsString("https://petstore.swagger.io/v2/swagger.json"))))
47+
.andExpect(content().string(containsString("/v3/api-docs")));
48+
}
49+
50+
@SpringBootApplication
51+
static class SpringDocTestApp {}
52+
53+
}

0 commit comments

Comments
 (0)