@ -51,31 +51,27 @@ import org.springframework.web.util.DefaultUriBuilderFactory;
@@ -51,31 +51,27 @@ import org.springframework.web.util.DefaultUriBuilderFactory;
import static org.assertj.core.api.Assertions.assertThat ;
/ * *
* Integration tests for { @link HttpServiceProxyFactory HTTP Service proxy } using
* { @link RestTemplate } and { @link MockWebServer } .
* Integration tests for { @link HttpServiceProxyFactory HTTP Service proxy }
* with { @link RestTemplateAdapter } connecting to { @link MockWebServer } .
*
* @author Olga Maciaszek - Sharma
* /
class RestTemplateHttpServiceProxy Tests {
class RestTemplateAdapter Tests {
private MockWebServer server ;
private TestService testService ;
private Service service ;
@BeforeEach
void setUp ( ) {
this . server = new MockWebServer ( ) ;
prepareResponse ( ) ;
this . testService = initTestService ( ) ;
}
private TestService initTestService ( ) {
RestTemplate restTemplate = new RestTemplate ( ) ;
restTemplate . setUriTemplateHandler ( new DefaultUriBuilderFactory ( this . server . url ( "/" ) . toString ( ) ) ) ;
return HttpServiceProxyFactory . builder ( )
. exchangeAdapter ( RestTemplateAdapter . forTemplate ( restTemplate ) )
. build ( )
. createClient ( TestService . class ) ;
RestTemplateAdapter adapter = RestTemplateAdapter . create ( restTemplate ) ;
this . service = HttpServiceProxyFactory . builder ( ) . exchangeAdapter ( adapter ) . build ( ) . createClient ( Service . class ) ;
}
@SuppressWarnings ( "ConstantConditions" )
@ -86,32 +82,33 @@ class RestTemplateHttpServiceProxyTests {
@@ -86,32 +82,33 @@ class RestTemplateHttpServiceProxyTests {
}
}
@Test
void getR eques t ( ) throws InterruptedException {
String response = testService . getRequest ( ) ;
void gr eeting ( ) throws InterruptedException {
String response = this . service . getGreeting ( ) ;
RecordedRequest request = this . server . takeRequest ( ) ;
assertThat ( response ) . isEqualTo ( "Hello Spring!" ) ;
assertThat ( request . getMethod ( ) ) . isEqualTo ( "GET" ) ;
assertThat ( request . getPath ( ) ) . isEqualTo ( "/test " ) ;
assertThat ( request . getPath ( ) ) . isEqualTo ( "/greeting " ) ;
}
@Test
void getRequestWithPathVariable ( ) throws InterruptedException {
ResponseEntity < String > response = testService . getRequestWithPathVariable ( "456" ) ;
void greetingById ( ) throws InterruptedException {
ResponseEntity < String > response = this . service . getGreetingById ( "456" ) ;
RecordedRequest request = this . server . takeRequest ( ) ;
assertThat ( response . getStatusCode ( ) ) . isEqualTo ( HttpStatus . OK ) ;
assertThat ( response . getBody ( ) ) . isEqualTo ( "Hello Spring!" ) ;
assertThat ( request . getMethod ( ) ) . isEqualTo ( "GET" ) ;
assertThat ( request . getPath ( ) ) . isEqualTo ( "/test /456" ) ;
assertThat ( request . getPath ( ) ) . isEqualTo ( "/greeting /456" ) ;
}
@Test
void getR eques tWithDynamicUri ( ) throws InterruptedException {
void gr eeting WithDynamicUri ( ) throws InterruptedException {
URI dynamicUri = this . server . url ( "/greeting/123" ) . uri ( ) ;
Optional < String > response = testService . getRequest WithDynamicUri( dynamicUri , "456" ) ;
Optional < String > response = this . service . getGreeting WithDynamicUri( dynamicUri , "456" ) ;
RecordedRequest request = this . server . takeRequest ( ) ;
assertThat ( response . orElse ( "empty" ) ) . isEqualTo ( "Hello Spring!" ) ;
@ -120,12 +117,12 @@ class RestTemplateHttpServiceProxyTests {
@@ -120,12 +117,12 @@ class RestTemplateHttpServiceProxyTests {
}
@Test
void postWithRequest Header ( ) throws InterruptedException {
te stS ervice. postReque stWithHeader ( "testHeader" , "testBody" ) ;
void postWithHeader ( ) throws InterruptedException {
service . postWithHeader ( "testHeader" , "testBody" ) ;
RecordedRequest request = this . server . takeRequest ( ) ;
assertThat ( request . getMethod ( ) ) . isEqualTo ( "POST" ) ;
assertThat ( request . getPath ( ) ) . isEqualTo ( "/test " ) ;
assertThat ( request . getPath ( ) ) . isEqualTo ( "/greeting " ) ;
assertThat ( request . getHeaders ( ) . get ( "testHeaderName" ) ) . isEqualTo ( "testHeader" ) ;
assertThat ( request . getBody ( ) . readUtf8 ( ) ) . isEqualTo ( "testBody" ) ;
}
@ -136,7 +133,7 @@ class RestTemplateHttpServiceProxyTests {
@@ -136,7 +133,7 @@ class RestTemplateHttpServiceProxyTests {
map . add ( "param1" , "value 1" ) ;
map . add ( "param2" , "value 2" ) ;
te stS ervice. postForm ( map ) ;
service . postForm ( map ) ;
RecordedRequest request = this . server . takeRequest ( ) ;
assertThat ( request . getHeaders ( ) . get ( "Content-Type" ) )
@ -151,7 +148,7 @@ class RestTemplateHttpServiceProxyTests {
@@ -151,7 +148,7 @@ class RestTemplateHttpServiceProxyTests {
MultipartFile file = new MockMultipartFile ( fileName , originalFileName , MediaType . APPLICATION_JSON_VALUE ,
"test" . getBytes ( ) ) ;
te stS ervice. postMultipart ( file , "test2" ) ;
service . postMultipart ( file , "test2" ) ;
RecordedRequest request = this . server . takeRequest ( ) ;
assertThat ( request . getHeaders ( ) . get ( "Content-Type" ) ) . startsWith ( "multipart/form-data;boundary=" ) ;
@ -163,8 +160,8 @@ class RestTemplateHttpServiceProxyTests {
@@ -163,8 +160,8 @@ class RestTemplateHttpServiceProxyTests {
}
@Test
void putRequest WithCookies ( ) throws InterruptedException {
te stS ervice. putReques tWithCookies ( "test1" , "test2" ) ;
void putWithCookies ( ) throws InterruptedException {
service . putWithCookies ( "test1" , "test2" ) ;
RecordedRequest request = this . server . takeRequest ( ) ;
assertThat ( request . getMethod ( ) ) . isEqualTo ( "PUT" ) ;
@ -172,8 +169,8 @@ class RestTemplateHttpServiceProxyTests {
@@ -172,8 +169,8 @@ class RestTemplateHttpServiceProxyTests {
}
@Test
void putRequest WithSameNameCookies ( ) throws InterruptedException {
te stS ervice. putReques tWithSameNameCookies ( "test1" , "test2" ) ;
void putWithSameNameCookies ( ) throws InterruptedException {
service . putWithSameNameCookies ( "test1" , "test2" ) ;
RecordedRequest request = this . server . takeRequest ( ) ;
assertThat ( request . getMethod ( ) ) . isEqualTo ( "PUT" ) ;
@ -186,20 +183,21 @@ class RestTemplateHttpServiceProxyTests {
@@ -186,20 +183,21 @@ class RestTemplateHttpServiceProxyTests {
this . server . enqueue ( response ) ;
}
private interface TestService {
@GetExchange ( "/test" )
String getRequest ( ) ;
private interface Service {
@GetExchange ( "/greeting" )
String getGreeting ( ) ;
@GetExchange ( "/test/{id}" )
ResponseEntity < String > getRequestWithPathVariable ( @PathVariable String id ) ;
@GetExchange ( "/greeting /{id}" )
ResponseEntity < String > getGreetingById ( @PathVariable String id ) ;
@GetExchange ( "/test /{id}" )
Optional < String > getRequest WithDynamicUri ( @Nullable URI uri , @PathVariable String id ) ;
@GetExchange ( "/greeting /{id}" )
Optional < String > getGreeting WithDynamicUri ( @Nullable URI uri , @PathVariable String id ) ;
@PostExchange ( "/test " )
void postRequest WithHeader ( @RequestHeader ( "testHeaderName" ) String testHeader ,
@RequestBody String requestBody ) ;
@PostExchange ( "/greeting " )
void postWithHeader (
@RequestHeader ( "testHeaderName" ) String testHeader , @Request Body String requestBody ) ;
@PostExchange ( contentType = "application/x-www-form-urlencoded" )
void postForm ( @RequestParam MultiValueMap < String , String > params ) ;
@ -208,12 +206,12 @@ class RestTemplateHttpServiceProxyTests {
@@ -208,12 +206,12 @@ class RestTemplateHttpServiceProxyTests {
void postMultipart ( MultipartFile file , @RequestPart String anotherPart ) ;
@PutExchange
void putRequest WithCookies ( @CookieValue String firstCookie ,
@CookieValue String secondCookie ) ;
void putWithCookies (
@CookieValue String firstCookie , @CookieValue String secondCookie ) ;
@PutExchange
void putRequest WithSameNameCookies ( @CookieValue ( "testCookie" ) String firstCookie ,
@CookieValue ( "testCookie" ) String secondCookie ) ;
void putWithSameNameCookies (
@CookieValue ( "testCookie" ) String firstCookie , @CookieValue ( "testCookie" ) String secondCookie ) ;
}