2323import org .junit .jupiter .api .Assertions ;
2424import org .junit .jupiter .api .Test ;
2525import org .mockito .MockedStatic ;
26+ import org .springframework .core .env .MapPropertySource ;
27+ import org .springframework .core .env .StandardEnvironment ;
2628import org .springframework .util .ResourceUtils ;
2729import reactor .netty .resources .ConnectionProvider ;
2830
3234import java .security .cert .CertificateFactory ;
3335import java .time .Duration ;
3436import java .util .ArrayList ;
37+ import java .util .Arrays ;
3538import java .util .Collections ;
39+ import java .util .List ;
3640
3741import static org .mockito .ArgumentMatchers .anyString ;
3842import static org .mockito .Mockito .doThrow ;
@@ -171,7 +175,7 @@ public void httpClientPropertiesTest() {
171175 httpClientProperties .setReaderIdleTime (1 );
172176 httpClientProperties .setReadTimeout (1 );
173177 httpClientProperties .setResponseTimeout (1L );
174- httpClientProperties .setStrategy ("strategy " );
178+ httpClientProperties .setStrategy ("netty " );
175179 httpClientProperties .setWiretap (true );
176180 httpClientProperties .setWriterIdleTime (1 );
177181 httpClientProperties .setWriteTimeout (1 );
@@ -182,9 +186,66 @@ public void httpClientPropertiesTest() {
182186 Assertions .assertEquals (httpClientProperties .getReaderIdleTime (), 1 );
183187 Assertions .assertEquals (httpClientProperties .getReadTimeout (), 1 );
184188 Assertions .assertEquals (httpClientProperties .getResponseTimeout (), Duration .ofMillis (1L ));
185- Assertions .assertEquals (httpClientProperties .getStrategy (), "strategy " );
189+ Assertions .assertEquals (httpClientProperties .getStrategy (), "netty " );
186190 Assertions .assertTrue (httpClientProperties .isWiretap ());
187191 Assertions .assertEquals (httpClientProperties .getWriterIdleTime (), 1 );
188192 Assertions .assertEquals (httpClientProperties .getWriteTimeout (), 1 );
189193 }
194+
195+ @ Test
196+ public void validateStrategyRejectsUnsupportedValueTest () {
197+ List <String > unsupported = Arrays .asList ("web-client" , "web_client" , "" , " netty" , "netty " , "Netty " , "httpclient" );
198+ for (String value : unsupported ) {
199+ HttpClientProperties httpClientProperties = new HttpClientProperties ();
200+ httpClientProperties .setStrategy (value );
201+ IllegalStateException exception = Assertions .assertThrows (IllegalStateException .class , httpClientProperties ::validateStrategy );
202+ Assertions .assertTrue (exception .getMessage ().contains ("shenyu.httpclient.strategy" ), exception .getMessage ());
203+ Assertions .assertTrue (exception .getMessage ().contains ("netty" ), exception .getMessage ());
204+ Assertions .assertTrue (exception .getMessage ().contains ("webClient" ), exception .getMessage ());
205+ }
206+ }
207+
208+ @ Test
209+ public void validateStrategyAcceptsSupportedValueTest () {
210+ List <String > supported = Arrays .asList ("netty" , "webClient" , "NETTY" , "WEBCLIENT" , "webclient" , "WebClient" );
211+ for (String value : supported ) {
212+ HttpClientProperties httpClientProperties = new HttpClientProperties ();
213+ httpClientProperties .setStrategy (value );
214+ Assertions .assertDoesNotThrow (httpClientProperties ::validateStrategy );
215+ }
216+ }
217+
218+ @ Test
219+ public void validateStrategyAcceptsAbsentValueTest () {
220+ HttpClientProperties httpClientProperties = new HttpClientProperties ();
221+ Assertions .assertNull (httpClientProperties .getStrategy ());
222+ Assertions .assertDoesNotThrow (httpClientProperties ::validateStrategy );
223+ }
224+
225+ @ Test
226+ public void afterPropertiesSetRejectsUnsupportedValueTest () {
227+ HttpClientProperties httpClientProperties = new HttpClientProperties ();
228+ httpClientProperties .setStrategy ("web-client" );
229+ Assertions .assertThrows (IllegalStateException .class , httpClientProperties ::afterPropertiesSet );
230+ }
231+
232+ @ Test
233+ public void validateStrategyRejectsNullValuedConfiguredPropertyTest () {
234+ HttpClientProperties httpClientProperties = new HttpClientProperties ();
235+ StandardEnvironment environment = new StandardEnvironment ();
236+ environment .getPropertySources ().addFirst (new MapPropertySource ("nullValuedStrategy" ,
237+ Collections .singletonMap (HttpClientProperties .STRATEGY_PROPERTY , null )));
238+ httpClientProperties .setEnvironment (environment );
239+ IllegalStateException exception = Assertions .assertThrows (IllegalStateException .class , httpClientProperties ::validateStrategy );
240+ Assertions .assertTrue (exception .getMessage ().contains ("shenyu.httpclient.strategy" ), exception .getMessage ());
241+ Assertions .assertTrue (exception .getMessage ().contains ("netty" ), exception .getMessage ());
242+ Assertions .assertTrue (exception .getMessage ().contains ("webClient" ), exception .getMessage ());
243+ }
244+
245+ @ Test
246+ public void validateStrategyAcceptsAbsentPropertyWithEnvironmentTest () {
247+ HttpClientProperties httpClientProperties = new HttpClientProperties ();
248+ httpClientProperties .setEnvironment (new StandardEnvironment ());
249+ Assertions .assertDoesNotThrow (httpClientProperties ::validateStrategy );
250+ }
190251}
0 commit comments