1717package de .codecentric .boot .admin .server .web .client ;
1818
1919import de .codecentric .boot .admin .server .domain .entities .Instance ;
20+ import de .codecentric .boot .admin .server .domain .values .Endpoints ;
2021import de .codecentric .boot .admin .server .domain .values .InstanceId ;
2122import de .codecentric .boot .admin .server .domain .values .Registration ;
2223import de .codecentric .boot .admin .server .web .client .exception .ResolveEndpointException ;
3738import com .github .tomakehurst .wiremock .core .Options ;
3839import com .github .tomakehurst .wiremock .junit .WireMockClassRule ;
3940
41+ import static com .github .tomakehurst .wiremock .client .WireMock .containing ;
4042import static com .github .tomakehurst .wiremock .client .WireMock .equalTo ;
4143import static com .github .tomakehurst .wiremock .client .WireMock .get ;
4244import static com .github .tomakehurst .wiremock .client .WireMock .getRequestedFor ;
4648import static org .assertj .core .api .Assertions .assertThat ;
4749import static org .mockito .Mockito .mock ;
4850import static org .mockito .Mockito .when ;
49- import static org .springframework .http .HttpHeaders .ACCEPT ;
5051import static org .springframework .http .HttpHeaders .AUTHORIZATION ;
5152import static org .springframework .http .HttpHeaders .CONTENT_LENGTH ;
5253import static org .springframework .http .HttpHeaders .CONTENT_TYPE ;
5354import static org .springframework .http .HttpHeaders .EMPTY ;
55+ import static wiremock .org .apache .http .HttpHeaders .ACCEPT ;
5456
5557public class InstanceWebClientTest {
5658 @ ClassRule
@@ -70,10 +72,7 @@ public void should_rewirte_url() {
7072 Mono <ClientResponse > exchange = instanceWebClient .instance (instance ).get ().uri ("health" ).exchange ();
7173
7274 StepVerifier .create (exchange ).expectNextCount (1 ).verifyComplete ();
73- wireMock .verify (1 ,
74- getRequestedFor (urlEqualTo ("/status" )).withHeader (ACCEPT , equalTo (MediaType .APPLICATION_JSON_VALUE ))
75- .withHeader (ACCEPT , equalTo (ActuatorMediaType .V1_JSON ))
76- .withHeader (ACCEPT , equalTo (ActuatorMediaType .V2_JSON )));
75+ wireMock .verify (1 , getRequestedFor (urlEqualTo ("/status" )));
7776 }
7877
7978 @ Test
@@ -104,7 +103,7 @@ public void should_exchange_absolute_url_without_instance() {
104103 }
105104
106105 @ Test
107- public void should_add_headers () {
106+ public void should_add_headers_from_provider () {
108107 Instance instance = Instance .create (InstanceId .of ("id" ))
109108 .register (Registration .create ("test" , wireMock .url ("/status" )).build ());
110109 wireMock .stubFor (get ("/status" ).willReturn (ok ()));
@@ -116,15 +115,65 @@ public void should_add_headers() {
116115 wireMock .verify (1 , getRequestedFor (urlEqualTo ("/status" )).withHeader (AUTHORIZATION , equalTo ("streng:geheim" )));
117116 }
118117
118+ @ Test
119+ public void should_add_default_accept_headers () {
120+ Instance instance = Instance .create (InstanceId .of ("id" ))
121+ .register (Registration .create ("test" , wireMock .url ("/status" )).build ());
122+ wireMock .stubFor (get ("/status" ).willReturn (ok ()));
123+
124+ Mono <ClientResponse > exchange = instanceWebClient .instance (instance ).get ().uri ("health" ).exchange ();
125+
126+ StepVerifier .create (exchange ).expectNextCount (1 ).verifyComplete ();
127+ wireMock .verify (1 ,
128+ getRequestedFor (urlEqualTo ("/status" )).withHeader (ACCEPT , containing (MediaType .APPLICATION_JSON_VALUE ))
129+ .withHeader (ACCEPT , containing (ActuatorMediaType .V1_JSON ))
130+ .withHeader (ACCEPT , containing (ActuatorMediaType .V2_JSON ))
131+ );
132+ }
133+
134+ @ Test
135+ public void should_not_add_default_accept_headers () {
136+ Instance instance = Instance .create (InstanceId .of ("id" ))
137+ .register (Registration .create ("test" , wireMock .url ("/status" )).build ());
138+ wireMock .stubFor (get ("/status" ).willReturn (ok ()));
139+
140+ Mono <ClientResponse > exchange = instanceWebClient .instance (instance )
141+ .get ()
142+ .uri ("health" )
143+ .header (ACCEPT , MediaType .TEXT_XML_VALUE )
144+ .exchange ();
145+
146+ StepVerifier .create (exchange ).expectNextCount (1 ).verifyComplete ();
147+ wireMock .verify (1 ,
148+ getRequestedFor (urlEqualTo ("/status" )).withHeader (ACCEPT , equalTo (MediaType .TEXT_XML_VALUE ))
149+ );
150+ }
151+
152+ @ Test
153+ public void should_add_default_logfile_accept_headers () {
154+ Instance instance = Instance .create (InstanceId .of ("id" ))
155+ .register (Registration .create ("test" , wireMock .url ("/status" )).build ())
156+ .withEndpoints (Endpoints .single ("logfile" , wireMock .url ("/log" )));
157+ wireMock .stubFor (get ("/log" ).willReturn (ok ()));
158+
159+ Mono <ClientResponse > exchange = instanceWebClient .instance (instance ).get ().uri ("logfile" ).exchange ();
160+
161+ StepVerifier .create (exchange ).expectNextCount (1 ).verifyComplete ();
162+ wireMock .verify (1 ,
163+ getRequestedFor (urlEqualTo ("/log" )).withHeader (ACCEPT , containing (MediaType .TEXT_PLAIN_VALUE ))
164+ .withHeader (ACCEPT , containing (MediaType .ALL_VALUE ))
165+ );
166+ }
167+
119168 @ Test
120169 public void should_convert_legacy_endpont () {
121170 Instance instance = Instance .create (InstanceId .of ("id" ))
122171 .register (Registration .create ("test" , wireMock .url ("/status" )).build ());
123172
124173 String responseBody = "{ \" status\" : \" UP\" , \" foo\" : \" bar\" }" ;
125- wireMock .stubFor (get ("/status" ).willReturn (
126- okForContentType ( ActuatorMediaType . V1_JSON , responseBody ). withHeader ( CONTENT_LENGTH ,
127- Integer . toString ( responseBody . length ()) ).withHeader ("X-Custom" , "1234" )));
174+ wireMock .stubFor (get ("/status" ).willReturn (okForContentType ( ActuatorMediaType . V1_JSON , responseBody ). withHeader ( CONTENT_LENGTH ,
175+ Integer . toString ( responseBody . length ())
176+ ).withHeader ("X-Custom" , "1234" )));
128177
129178 Mono <ClientResponse > exchange = instanceWebClient .instance (instance ).get ().uri ("health" ).exchange ();
130179
@@ -183,8 +232,10 @@ public void should_error_on_missing_endpoint() {
183232
184233 @ Test
185234 public void should_error_on_timeout () {
186- InstanceWebClient fastTimeoutClient = new InstanceWebClient (headersProvider , Duration .ofMillis (10 ),
187- Duration .ofMillis (10 ));
235+ InstanceWebClient fastTimeoutClient = new InstanceWebClient (headersProvider ,
236+ Duration .ofMillis (10 ),
237+ Duration .ofMillis (10 )
238+ );
188239
189240 wireMock .stubFor (get ("/foo" ).willReturn (ok ().withFixedDelay (100 )));
190241
0 commit comments