@@ -826,7 +826,7 @@ public void exchangeParameterizedType() throws Exception {
826
826
}
827
827
828
828
@ Test // SPR-15066
829
- public void requestInterceptorCanAddExistingHeaderValue () throws Exception {
829
+ public void requestInterceptorCanAddExistingHeaderValueWithoutBody () throws Exception {
830
830
ClientHttpRequestInterceptor interceptor = (request , body , execution ) -> {
831
831
request .getHeaders ().add ("MyHeader" , "MyInterceptorValue" );
832
832
return execution .execute (request , body );
@@ -851,4 +851,35 @@ public void requestInterceptorCanAddExistingHeaderValue() throws Exception {
851
851
verify (response ).close ();
852
852
}
853
853
854
+ @ Test // SPR-15066
855
+ public void requestInterceptorCanAddExistingHeaderValueWithBody () throws Exception {
856
+ ClientHttpRequestInterceptor interceptor = (request , body , execution ) -> {
857
+ request .getHeaders ().add ("MyHeader" , "MyInterceptorValue" );
858
+ return execution .execute (request , body );
859
+ };
860
+ template .setInterceptors (Collections .singletonList (interceptor ));
861
+
862
+ MediaType contentType = MediaType .TEXT_PLAIN ;
863
+ given (converter .canWrite (String .class , contentType )).willReturn (true );
864
+ given (requestFactory .createRequest (new URI ("http://example.com" ), HttpMethod .POST )).willReturn (request );
865
+ String helloWorld = "Hello World" ;
866
+ HttpHeaders requestHeaders = new HttpHeaders ();
867
+ given (request .getHeaders ()).willReturn (requestHeaders );
868
+ converter .write (helloWorld , contentType , request );
869
+ given (request .execute ()).willReturn (response );
870
+ given (errorHandler .hasError (response )).willReturn (false );
871
+ HttpStatus status = HttpStatus .OK ;
872
+ given (response .getStatusCode ()).willReturn (status );
873
+ given (response .getStatusText ()).willReturn (status .getReasonPhrase ());
874
+
875
+ HttpHeaders entityHeaders = new HttpHeaders ();
876
+ entityHeaders .setContentType (contentType );
877
+ entityHeaders .add ("MyHeader" , "MyEntityValue" );
878
+ HttpEntity <String > entity = new HttpEntity <>(helloWorld , entityHeaders );
879
+ template .exchange ("http://example.com" , HttpMethod .POST , entity , Void .class );
880
+ assertThat (requestHeaders .get ("MyHeader" ), contains ("MyEntityValue" , "MyInterceptorValue" ));
881
+
882
+ verify (response ).close ();
883
+ }
884
+
854
885
}
0 commit comments