1+ package com .baeldung .webclient ;
2+
3+ import com .baeldung .webclient .status .WebClientStatusCodeHandler ;
4+ import com .github .tomakehurst .wiremock .WireMockServer ;
5+ import org .junit .After ;
6+ import org .junit .Before ;
7+ import org .junit .Test ;
8+ import org .junit .runner .RunWith ;
9+ import org .springframework .test .context .junit4 .SpringRunner ;
10+ import reactor .core .publisher .Mono ;
11+
12+ import static com .github .tomakehurst .wiremock .client .WireMock .aResponse ;
13+ import static com .github .tomakehurst .wiremock .client .WireMock .configureFor ;
14+ import static com .github .tomakehurst .wiremock .client .WireMock .post ;
15+ import static com .github .tomakehurst .wiremock .client .WireMock .stubFor ;
16+ import static com .github .tomakehurst .wiremock .client .WireMock .urlEqualTo ;
17+ import static com .github .tomakehurst .wiremock .core .WireMockConfiguration .wireMockConfig ;
18+ import static java .lang .String .format ;
19+ import static org .assertj .core .api .Assertions .assertThatThrownBy ;
20+ import static org .assertj .core .api .AssertionsForClassTypes .assertThat ;
21+
22+ @ RunWith (SpringRunner .class )
23+ public class WebClientStatusCodeHandlerIntegrationTest {
24+ private String baseUrl ;
25+ private WireMockServer wireMockServer ;
26+
27+ @ Before
28+ public void setUp () {
29+ wireMockServer = new WireMockServer (wireMockConfig ().dynamicPort ());
30+ wireMockServer .start ();
31+ configureFor ("localhost" , wireMockServer .port ());
32+ baseUrl = format ("http://localhost:%s" , wireMockServer .port ());
33+ }
34+
35+ @ After
36+ public void tearDown () {
37+ wireMockServer .stop ();
38+ }
39+
40+ @ Test
41+ public void whenResponseIs2XX_thenBothStatusHandlerAndExchangeFilterReturnEqualResponses () {
42+ stubPostResponse ("/success" , 200 , "success" );
43+
44+ Mono <String > responseStatusHandler = WebClientStatusCodeHandler
45+ .getResponseBodyUsingOnStatus (baseUrl + "/success" );
46+
47+ Mono <String > responseExchangeFilter = WebClientStatusCodeHandler
48+ .getResponseBodyUsingExchangeFilterFunction (baseUrl + "/success" );
49+
50+ assertThat (responseStatusHandler .block ())
51+ .isEqualTo (responseExchangeFilter .block ())
52+ .isEqualTo ("success" );
53+ }
54+
55+ @ Test
56+ public void whenResponseIs500_thenBothStatusHandlerAndExchangeFilterReturnEqualResponses () {
57+ stubPostResponse ("/server-error" , 500 , "Internal Server Error" );
58+
59+ Mono <String > responseStatusHandler = WebClientStatusCodeHandler
60+ .getResponseBodyUsingOnStatus (baseUrl + "/server-error" );
61+
62+ Mono <String > responseExchangeFilter = WebClientStatusCodeHandler
63+ .getResponseBodyUsingExchangeFilterFunction (baseUrl + "/server-error" );
64+
65+ assertThatThrownBy (responseStatusHandler ::block )
66+ .isInstanceOf (Exception .class )
67+ .hasMessageContaining ("Internal Server Error" );
68+
69+ assertThatThrownBy (responseExchangeFilter ::block )
70+ .isInstanceOf (Exception .class )
71+ .hasMessageContaining ("Internal Server Error" );
72+ }
73+
74+ @ Test
75+ public void whenResponseIs400_thenBothStatusHandlerAndExchangeFilterReturnEqualResponses () {
76+ stubPostResponse ("/client-error" , 400 , "Bad Request" );
77+
78+ Mono <String > responseStatusHandler = WebClientStatusCodeHandler
79+ .getResponseBodyUsingOnStatus (baseUrl + "/client-error" );
80+
81+ Mono <String > responseExchangeFilter = WebClientStatusCodeHandler
82+ .getResponseBodyUsingExchangeFilterFunction (baseUrl + "/client-error" );
83+
84+ assertThatThrownBy (responseStatusHandler ::block )
85+ .isInstanceOf (Exception .class )
86+ .hasMessageContaining ("Bad Request" );
87+
88+ assertThatThrownBy (responseExchangeFilter ::block )
89+ .isInstanceOf (Exception .class )
90+ .hasMessageContaining ("Bad Request" );
91+ }
92+
93+ private static void stubPostResponse (String url , int statusCode , String response ) {
94+ stubFor (post (urlEqualTo (url )).willReturn (aResponse ()
95+ .withStatus (statusCode )
96+ .withBody (response )));
97+ }
98+ }
0 commit comments