Skip to content

Commit 37ae78e

Browse files
committed
Add Tomacat 11 Smoke Test
Closes gh-42730
1 parent 5321d46 commit 37ae78e

File tree

9 files changed

+429
-0
lines changed

9 files changed

+429
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
plugins {
2+
id "java"
3+
}
4+
5+
description = "Spring Boot Tomcat 11 smoke test"
6+
7+
configurations.all {
8+
resolutionStrategy.eachDependency {
9+
if (it.requested.group == 'org.apache.tomcat' || it.requested.group == 'org.apache.tomcat.embed') {
10+
it.useVersion '11.0.0'
11+
}
12+
}
13+
}
14+
15+
dependencies {
16+
implementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter"))
17+
implementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-tomcat"))
18+
implementation("org.springframework:spring-webmvc")
19+
20+
testImplementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-test"))
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2012-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package smoketest.tomcat;
18+
19+
import jakarta.servlet.ServletContextEvent;
20+
import jakarta.servlet.ServletContextListener;
21+
import org.apache.commons.logging.Log;
22+
import org.apache.commons.logging.LogFactory;
23+
24+
import org.springframework.boot.SpringApplication;
25+
import org.springframework.boot.autoconfigure.SpringBootApplication;
26+
import org.springframework.context.annotation.Bean;
27+
28+
@SpringBootApplication
29+
public class SampleTomcat11Application {
30+
31+
private static final Log logger = LogFactory.getLog(SampleTomcat11Application.class);
32+
33+
@Bean
34+
protected ServletContextListener listener() {
35+
return new ServletContextListener() {
36+
37+
@Override
38+
public void contextInitialized(ServletContextEvent sce) {
39+
logger.info("ServletContext initialized");
40+
}
41+
42+
@Override
43+
public void contextDestroyed(ServletContextEvent sce) {
44+
logger.info("ServletContext destroyed");
45+
}
46+
47+
};
48+
}
49+
50+
public static void main(String[] args) {
51+
SpringApplication.run(SampleTomcat11Application.class, args);
52+
}
53+
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2012-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package smoketest.tomcat.service;
18+
19+
import org.springframework.beans.factory.annotation.Value;
20+
import org.springframework.stereotype.Component;
21+
22+
@Component
23+
public class HelloWorldService {
24+
25+
@Value("${test.name:World}")
26+
private String name;
27+
28+
public String getHelloMessage() {
29+
return "Hello " + this.name;
30+
}
31+
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2012-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package smoketest.tomcat.service;
18+
19+
import smoketest.tomcat.util.RandomStringUtil;
20+
21+
import org.springframework.beans.factory.annotation.Value;
22+
import org.springframework.stereotype.Component;
23+
24+
@Component
25+
public class HttpHeaderService {
26+
27+
@Value("${server.tomcat.max-http-response-header-size}")
28+
private int maxHttpResponseHeaderSize;
29+
30+
/**
31+
* Generates random data. The data is:
32+
* <ol>
33+
* <li>is longer than configured
34+
* <code>server.tomcat.max-http-response-header-size</code></li>
35+
* <li>is url encoded by base 64 encode the random value</li>
36+
* </ol>
37+
* @return a base64 encoded string of random bytes
38+
*/
39+
public String getHeaderValue() {
40+
return RandomStringUtil.getRandomBase64EncodedString(this.maxHttpResponseHeaderSize + 1);
41+
}
42+
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2012-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package smoketest.tomcat.util;
18+
19+
import java.util.Base64;
20+
import java.util.Random;
21+
22+
public final class RandomStringUtil {
23+
24+
private RandomStringUtil() {
25+
}
26+
27+
public static String getRandomBase64EncodedString(int length) {
28+
byte[] responseHeader = new byte[length];
29+
new Random().nextBytes(responseHeader);
30+
return Base64.getEncoder().encodeToString(responseHeader);
31+
}
32+
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2012-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package smoketest.tomcat.web;
18+
19+
import jakarta.servlet.http.HttpServletResponse;
20+
import smoketest.tomcat.service.HelloWorldService;
21+
import smoketest.tomcat.service.HttpHeaderService;
22+
23+
import org.springframework.stereotype.Controller;
24+
import org.springframework.web.bind.annotation.GetMapping;
25+
import org.springframework.web.bind.annotation.ResponseBody;
26+
27+
@Controller
28+
public class SampleController {
29+
30+
private final HelloWorldService helloWorldService;
31+
32+
private final HttpHeaderService httpHeaderService;
33+
34+
public SampleController(HelloWorldService helloWorldService, HttpHeaderService httpHeaderService) {
35+
this.helloWorldService = helloWorldService;
36+
this.httpHeaderService = httpHeaderService;
37+
}
38+
39+
@GetMapping("/")
40+
@ResponseBody
41+
public String helloWorld() {
42+
return this.helloWorldService.getHelloMessage();
43+
}
44+
45+
@GetMapping("/max-http-response-header")
46+
@ResponseBody
47+
public String maxHttpResponseHeader(HttpServletResponse response) {
48+
String headerValue = this.httpHeaderService.getHeaderValue();
49+
response.addHeader("x-max-header", headerValue);
50+
return this.helloWorldService.getHelloMessage();
51+
}
52+
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
server.compression.enabled: true
2+
server.compression.min-response-size: 1
3+
server.max-http-request-header-size=1000
4+
server.tomcat.connection-timeout=5s
5+
server.tomcat.max-http-response-header-size=1000
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2012-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package smoketest.tomcat;
18+
19+
import org.junit.jupiter.api.Test;
20+
import smoketest.tomcat.service.HelloWorldService;
21+
import smoketest.tomcat.web.SampleController;
22+
23+
import org.springframework.beans.factory.annotation.Autowired;
24+
import org.springframework.boot.SpringApplication;
25+
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
26+
import org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration;
27+
import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration;
28+
import org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration;
29+
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
30+
import org.springframework.boot.test.context.SpringBootTest;
31+
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
32+
import org.springframework.boot.test.web.client.TestRestTemplate;
33+
import org.springframework.context.annotation.ComponentScan;
34+
import org.springframework.context.annotation.Configuration;
35+
import org.springframework.context.annotation.Import;
36+
import org.springframework.http.HttpStatus;
37+
import org.springframework.http.ResponseEntity;
38+
39+
import static org.assertj.core.api.Assertions.assertThat;
40+
41+
/**
42+
* Basic integration tests for demo application.
43+
*
44+
* @author Dave Syer
45+
*/
46+
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
47+
class NonAutoConfigurationSampleTomcatApplicationTests {
48+
49+
@Autowired
50+
private TestRestTemplate restTemplate;
51+
52+
@Test
53+
void testHome() {
54+
ResponseEntity<String> entity = this.restTemplate.getForEntity("/", String.class);
55+
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
56+
assertThat(entity.getBody()).isEqualTo("Hello World");
57+
}
58+
59+
@Configuration(proxyBeanMethods = false)
60+
@Import({ ServletWebServerFactoryAutoConfiguration.class, DispatcherServletAutoConfiguration.class,
61+
WebMvcAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class,
62+
PropertyPlaceholderAutoConfiguration.class })
63+
@ComponentScan(basePackageClasses = { SampleController.class, HelloWorldService.class })
64+
public static class NonAutoConfigurationSampleTomcatApplication {
65+
66+
public static void main(String[] args) {
67+
SpringApplication.run(SampleTomcat11Application.class, args);
68+
}
69+
70+
}
71+
72+
}

0 commit comments

Comments
 (0)