11/* 
2-  * Copyright 2012-2023  the original author or authors. 
2+  * Copyright 2012-2025  the original author or authors. 
33 * 
44 * Licensed under the Apache License, Version 2.0 (the "License"); 
55 * you may not use this file except in compliance with the License. 
1919import  io .undertow .Undertow .Builder ;
2020import  io .undertow .servlet .api .DeploymentInfo ;
2121import  jakarta .servlet .Filter ;
22- import  jakarta .servlet .Servlet ;
2322import  jakarta .servlet .ServletContext ;
24- import  jakarta .servlet .http .HttpServletRequest ;
25- import  jakarta .servlet .http .HttpServletResponse ;
2623import  org .apache .catalina .Context ;
2724import  org .apache .catalina .connector .Connector ;
2825import  org .apache .catalina .startup .Tomcat ;
3431import  org .springframework .boot .autoconfigure .AutoConfigurations ;
3532import  org .springframework .boot .autoconfigure .condition .ConditionalOnExpression ;
3633import  org .springframework .boot .test .context .FilteredClassLoader ;
37- import  org .springframework .boot .test .context .assertj .AssertableWebApplicationContext ;
38- import  org .springframework .boot .test .context .runner .ContextConsumer ;
3934import  org .springframework .boot .test .context .runner .WebApplicationContextRunner ;
40- import  org .springframework .boot .testsupport .web .servlet .DirtiesUrlFactories ;
4135import  org .springframework .boot .web .embedded .jetty .JettyServerCustomizer ;
4236import  org .springframework .boot .web .embedded .jetty .JettyServletWebServerFactory ;
4337import  org .springframework .boot .web .embedded .tomcat .TomcatConnectorCustomizer ;
4943import  org .springframework .boot .web .embedded .undertow .UndertowServletWebServerFactory ;
5044import  org .springframework .boot .web .server .WebServerFactoryCustomizer ;
5145import  org .springframework .boot .web .servlet .FilterRegistrationBean ;
52- import  org .springframework .boot .web .servlet .ServletRegistrationBean ;
5346import  org .springframework .boot .web .servlet .context .AnnotationConfigServletWebServerApplicationContext ;
5447import  org .springframework .boot .web .servlet .server .AbstractServletWebServerFactory ;
5548import  org .springframework .boot .web .servlet .server .ConfigurableServletWebServerFactory ;
5649import  org .springframework .boot .web .servlet .server .CookieSameSiteSupplier ;
5750import  org .springframework .boot .web .servlet .server .ServletWebServerFactory ;
58- import  org .springframework .context .ApplicationContext ;
5951import  org .springframework .context .annotation .Bean ;
6052import  org .springframework .context .annotation .Configuration ;
6153import  org .springframework .stereotype .Component ;
6254import  org .springframework .web .filter .ForwardedHeaderFilter ;
63- import  org .springframework .web .servlet .DispatcherServlet ;
64- import  org .springframework .web .servlet .FrameworkServlet ;
6555
6656import  static  org .assertj .core .api .Assertions .assertThat ;
6757import  static  org .mockito .ArgumentMatchers .any ;
7767 * @author Raheela Aslam 
7868 * @author Madhura Bhave 
7969 */ 
80- @ DirtiesUrlFactories 
8170class  ServletWebServerFactoryAutoConfigurationTests  {
8271
8372	private  final  WebApplicationContextRunner  contextRunner  = new  WebApplicationContextRunner (
8473			AnnotationConfigServletWebServerApplicationContext ::new )
85- 		.withConfiguration (AutoConfigurations .of (ServletWebServerFactoryAutoConfiguration .class ,
86- 				DispatcherServletAutoConfiguration .class ))
74+ 		.withConfiguration (AutoConfigurations .of (ServletWebServerFactoryAutoConfiguration .class ))
8775		.withUserConfiguration (WebServerConfiguration .class );
8876
8977	@ Test 
9078	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 ));
13080	}
13181
13282	@ Test 
13383	void  webServerHasNoServletContext () {
134- 		this .contextRunner .withUserConfiguration (EnsureWebServerHasNoServletContext .class ).run (verifyContext ());
84+ 		this .contextRunner .withUserConfiguration (EnsureWebServerHasNoServletContext .class )
85+ 			.run ((context ) -> assertThat (context ).hasNotFailed ());
13586	}
13687
13788	@ 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 ));
14395	}
14496
14597	@ Test 
@@ -424,17 +376,6 @@ void relativeRedirectsShouldNotBeEnabledWhenNotUsingTomcatContainer() {
424376		});
425377	}
426378
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- 
438379	@ Configuration (proxyBeanMethods  = false )
439380	@ ConditionalOnExpression ("true" )
440381	static  class  WebServerConfiguration  {
@@ -446,68 +387,6 @@ ServletWebServerFactory webServerFactory() {
446387
447388	}
448389
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- 
511390	@ Component 
512391	static  class  EnsureWebServerHasNoServletContext  implements  BeanPostProcessor  {
513392
@@ -527,17 +406,6 @@ public Object postProcessAfterInitialization(Object bean, String beanName) {
527406
528407	}
529408
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- 
541409	@ Configuration (proxyBeanMethods  = false )
542410	static  class  TomcatConnectorCustomizerConfiguration  {
543411
0 commit comments