1
1
/*
2
- * Copyright 2012-2023 the original author or authors.
2
+ * Copyright 2012-2025 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
19
19
import io .undertow .Undertow .Builder ;
20
20
import io .undertow .servlet .api .DeploymentInfo ;
21
21
import jakarta .servlet .Filter ;
22
- import jakarta .servlet .Servlet ;
23
22
import jakarta .servlet .ServletContext ;
24
- import jakarta .servlet .http .HttpServletRequest ;
25
- import jakarta .servlet .http .HttpServletResponse ;
26
23
import org .apache .catalina .Context ;
27
24
import org .apache .catalina .connector .Connector ;
28
25
import org .apache .catalina .startup .Tomcat ;
34
31
import org .springframework .boot .autoconfigure .AutoConfigurations ;
35
32
import org .springframework .boot .autoconfigure .condition .ConditionalOnExpression ;
36
33
import org .springframework .boot .test .context .FilteredClassLoader ;
37
- import org .springframework .boot .test .context .assertj .AssertableWebApplicationContext ;
38
- import org .springframework .boot .test .context .runner .ContextConsumer ;
39
34
import org .springframework .boot .test .context .runner .WebApplicationContextRunner ;
40
- import org .springframework .boot .testsupport .web .servlet .DirtiesUrlFactories ;
41
35
import org .springframework .boot .web .embedded .jetty .JettyServerCustomizer ;
42
36
import org .springframework .boot .web .embedded .jetty .JettyServletWebServerFactory ;
43
37
import org .springframework .boot .web .embedded .tomcat .TomcatConnectorCustomizer ;
49
43
import org .springframework .boot .web .embedded .undertow .UndertowServletWebServerFactory ;
50
44
import org .springframework .boot .web .server .WebServerFactoryCustomizer ;
51
45
import org .springframework .boot .web .servlet .FilterRegistrationBean ;
52
- import org .springframework .boot .web .servlet .ServletRegistrationBean ;
53
46
import org .springframework .boot .web .servlet .context .AnnotationConfigServletWebServerApplicationContext ;
54
47
import org .springframework .boot .web .servlet .server .AbstractServletWebServerFactory ;
55
48
import org .springframework .boot .web .servlet .server .ConfigurableServletWebServerFactory ;
56
49
import org .springframework .boot .web .servlet .server .CookieSameSiteSupplier ;
57
50
import org .springframework .boot .web .servlet .server .ServletWebServerFactory ;
58
- import org .springframework .context .ApplicationContext ;
59
51
import org .springframework .context .annotation .Bean ;
60
52
import org .springframework .context .annotation .Configuration ;
61
53
import org .springframework .stereotype .Component ;
62
54
import org .springframework .web .filter .ForwardedHeaderFilter ;
63
- import org .springframework .web .servlet .DispatcherServlet ;
64
- import org .springframework .web .servlet .FrameworkServlet ;
65
55
66
56
import static org .assertj .core .api .Assertions .assertThat ;
67
57
import static org .mockito .ArgumentMatchers .any ;
77
67
* @author Raheela Aslam
78
68
* @author Madhura Bhave
79
69
*/
80
- @ DirtiesUrlFactories
81
70
class ServletWebServerFactoryAutoConfigurationTests {
82
71
83
72
private final WebApplicationContextRunner contextRunner = new WebApplicationContextRunner (
84
73
AnnotationConfigServletWebServerApplicationContext ::new )
85
- .withConfiguration (AutoConfigurations .of (ServletWebServerFactoryAutoConfiguration .class ,
86
- DispatcherServletAutoConfiguration .class ))
74
+ .withConfiguration (AutoConfigurations .of (ServletWebServerFactoryAutoConfiguration .class ))
87
75
.withUserConfiguration (WebServerConfiguration .class );
88
76
89
77
@ Test
90
78
void createFromConfigClass () {
91
- this .contextRunner .run (verifyContext ());
92
- }
93
-
94
- @ Test
95
- void contextAlreadyHasDispatcherServletWithDefaultName () {
96
- this .contextRunner .withUserConfiguration (DispatcherServletConfiguration .class ).run (verifyContext ());
97
- }
98
-
99
- @ Test
100
- void contextAlreadyHasDispatcherServlet () {
101
- this .contextRunner .withUserConfiguration (SpringServletConfiguration .class ).run ((context ) -> {
102
- verifyContext (context );
103
- assertThat (context .getBeanNamesForType (DispatcherServlet .class )).hasSize (2 );
104
- });
105
- }
106
-
107
- @ Test
108
- void contextAlreadyHasNonDispatcherServlet () {
109
- this .contextRunner .withUserConfiguration (NonSpringServletConfiguration .class ).run ((context ) -> {
110
- verifyContext (context ); // the non default servlet is still registered
111
- assertThat (context ).doesNotHaveBean (DispatcherServlet .class );
112
- });
113
- }
114
-
115
- @ Test
116
- void contextAlreadyHasNonServlet () {
117
- this .contextRunner .withUserConfiguration (NonServletConfiguration .class ).run ((context ) -> {
118
- assertThat (context ).doesNotHaveBean (DispatcherServlet .class );
119
- assertThat (context ).doesNotHaveBean (Servlet .class );
120
- });
121
- }
122
-
123
- @ Test
124
- void contextAlreadyHasDispatcherServletAndRegistration () {
125
- this .contextRunner .withUserConfiguration (DispatcherServletWithRegistrationConfiguration .class )
126
- .run ((context ) -> {
127
- verifyContext (context );
128
- assertThat (context ).hasSingleBean (DispatcherServlet .class );
129
- });
79
+ this .contextRunner .run ((context ) -> assertThat (context ).hasSingleBean (ServletWebServerFactoryCustomizer .class ));
130
80
}
131
81
132
82
@ Test
133
83
void webServerHasNoServletContext () {
134
- this .contextRunner .withUserConfiguration (EnsureWebServerHasNoServletContext .class ).run (verifyContext ());
84
+ this .contextRunner .withUserConfiguration (EnsureWebServerHasNoServletContext .class )
85
+ .run ((context ) -> assertThat (context ).hasNotFailed ());
135
86
}
136
87
137
88
@ Test
138
- void customizeWebServerFactoryThroughCallback () {
139
- this .contextRunner .withUserConfiguration (CallbackEmbeddedServerFactoryCustomizer .class ).run ((context ) -> {
140
- verifyContext (context );
141
- assertThat (context .getBean (MockServletWebServerFactory .class ).getPort ()).isEqualTo (9000 );
142
- });
89
+ void webServerFactoryCustomizerBeansAreCalledToCustomizeWebServerFactory () {
90
+ this .contextRunner
91
+ .withBean (WebServerFactoryCustomizer .class ,
92
+ () -> (WebServerFactoryCustomizer <ConfigurableServletWebServerFactory >) (factory ) -> factory
93
+ .setPort (9000 ))
94
+ .run ((context ) -> assertThat (context .getBean (MockServletWebServerFactory .class ).getPort ()).isEqualTo (9000 ));
143
95
}
144
96
145
97
@ Test
@@ -424,17 +376,6 @@ void relativeRedirectsShouldNotBeEnabledWhenNotUsingTomcatContainer() {
424
376
});
425
377
}
426
378
427
- private ContextConsumer <AssertableWebApplicationContext > verifyContext () {
428
- return this ::verifyContext ;
429
- }
430
-
431
- private void verifyContext (ApplicationContext context ) {
432
- MockServletWebServerFactory factory = context .getBean (MockServletWebServerFactory .class );
433
- Servlet servlet = context .getBean (DispatcherServletAutoConfiguration .DEFAULT_DISPATCHER_SERVLET_BEAN_NAME ,
434
- Servlet .class );
435
- then (factory .getServletContext ()).should ().addServlet ("dispatcherServlet" , servlet );
436
- }
437
-
438
379
@ Configuration (proxyBeanMethods = false )
439
380
@ ConditionalOnExpression ("true" )
440
381
static class WebServerConfiguration {
@@ -446,68 +387,6 @@ ServletWebServerFactory webServerFactory() {
446
387
447
388
}
448
389
449
- @ Configuration (proxyBeanMethods = false )
450
- static class DispatcherServletConfiguration {
451
-
452
- @ Bean
453
- DispatcherServlet dispatcherServlet () {
454
- return new DispatcherServlet ();
455
- }
456
-
457
- }
458
-
459
- @ Configuration (proxyBeanMethods = false )
460
- static class SpringServletConfiguration {
461
-
462
- @ Bean
463
- DispatcherServlet springServlet () {
464
- return new DispatcherServlet ();
465
- }
466
-
467
- }
468
-
469
- @ Configuration (proxyBeanMethods = false )
470
- static class NonSpringServletConfiguration {
471
-
472
- @ Bean
473
- FrameworkServlet dispatcherServlet () {
474
- return new FrameworkServlet () {
475
- @ Override
476
- protected void doService (HttpServletRequest request , HttpServletResponse response ) {
477
- }
478
- };
479
- }
480
-
481
- }
482
-
483
- @ Configuration (proxyBeanMethods = false )
484
- static class NonServletConfiguration {
485
-
486
- @ Bean
487
- String dispatcherServlet () {
488
- return "foo" ;
489
- }
490
-
491
- }
492
-
493
- @ Configuration (proxyBeanMethods = false )
494
- static class DispatcherServletWithRegistrationConfiguration {
495
-
496
- @ Bean (name = DispatcherServletAutoConfiguration .DEFAULT_DISPATCHER_SERVLET_BEAN_NAME )
497
- DispatcherServlet dispatcherServlet () {
498
- return new DispatcherServlet ();
499
- }
500
-
501
- @ Bean (name = DispatcherServletAutoConfiguration .DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME )
502
- ServletRegistrationBean <DispatcherServlet > dispatcherRegistration (DispatcherServlet dispatcherServlet ) {
503
- ServletRegistrationBean <DispatcherServlet > registration = new ServletRegistrationBean <>(dispatcherServlet ,
504
- "/app/*" );
505
- registration .setName (DispatcherServletAutoConfiguration .DEFAULT_DISPATCHER_SERVLET_BEAN_NAME );
506
- return registration ;
507
- }
508
-
509
- }
510
-
511
390
@ Component
512
391
static class EnsureWebServerHasNoServletContext implements BeanPostProcessor {
513
392
@@ -527,17 +406,6 @@ public Object postProcessAfterInitialization(Object bean, String beanName) {
527
406
528
407
}
529
408
530
- @ Component
531
- static class CallbackEmbeddedServerFactoryCustomizer
532
- implements WebServerFactoryCustomizer <ConfigurableServletWebServerFactory > {
533
-
534
- @ Override
535
- public void customize (ConfigurableServletWebServerFactory serverFactory ) {
536
- serverFactory .setPort (9000 );
537
- }
538
-
539
- }
540
-
541
409
@ Configuration (proxyBeanMethods = false )
542
410
static class TomcatConnectorCustomizerConfiguration {
543
411
0 commit comments