@ -1,5 +1,5 @@
@@ -1,5 +1,5 @@
/ *
* Copyright 2002 - 2017 the original author or authors .
* Copyright 2002 - 2018 the original author or authors .
*
* Licensed under the Apache License , Version 2 . 0 ( the "License" ) ;
* you may not use this file except in compliance with the License .
@ -18,6 +18,8 @@ package org.springframework.web.reactive.function.client;
@@ -18,6 +18,8 @@ package org.springframework.web.reactive.function.client;
import java.time.Duration ;
import java.util.Collections ;
import java.util.HashMap ;
import java.util.Map ;
import org.junit.Before ;
import org.junit.Test ;
@ -40,6 +42,8 @@ import static org.mockito.Mockito.*;
@@ -40,6 +42,8 @@ import static org.mockito.Mockito.*;
* /
public class DefaultWebClientTests {
private WebClient . Builder builder ;
private ExchangeFunction exchangeFunction ;
@Captor
@ -50,16 +54,17 @@ public class DefaultWebClientTests {
@@ -50,16 +54,17 @@ public class DefaultWebClientTests {
public void setup ( ) {
MockitoAnnotations . initMocks ( this ) ;
this . exchangeFunction = mock ( ExchangeFunction . class ) ;
when ( this . exchangeFunction . exchange ( captor . capture ( ) ) ) . thenReturn ( Mono . empty ( ) ) ;
when ( this . exchangeFunction . exchange ( this . captor . capture ( ) ) ) . thenReturn ( Mono . empty ( ) ) ;
this . builder = WebClient . builder ( ) . baseUrl ( "/base" ) . exchangeFunction ( this . exchangeFunction ) ;
}
@Test
public void basic ( ) {
WebClient client = builder ( ) . build ( ) ;
client . get ( ) . uri ( "/path" ) . exchange ( ) ;
ClientRequest request = verifyExchange ( ) ;
this . builder . build ( ) . get ( ) . uri ( "/path" ) . exchange ( ) ;
ClientRequest request = verifyAndGetRequest ( ) ;
assertEquals ( "/base/path" , request . url ( ) . toString ( ) ) ;
assertEquals ( new HttpHeaders ( ) , request . headers ( ) ) ;
assertEquals ( Collections . emptyMap ( ) , request . cookies ( ) ) ;
@ -67,32 +72,36 @@ public class DefaultWebClientTests {
@@ -67,32 +72,36 @@ public class DefaultWebClientTests {
@Test
public void uriBuilder ( ) {
WebClient client = builder ( ) . build ( ) ;
client . get ( ) . uri ( builder - > builder . path ( "/path" ) . queryParam ( "q" , "12" ) . build ( ) ) . exchange ( ) ;
ClientRequest request = verifyExchange ( ) ;
this . builder . build ( ) . get ( )
. uri ( builder - > builder . path ( "/path" ) . queryParam ( "q" , "12" ) . build ( ) )
. exchange ( ) ;
ClientRequest request = verifyAndGetRequest ( ) ;
assertEquals ( "/base/path?q=12" , request . url ( ) . toString ( ) ) ;
verifyNoMoreInteractions ( this . exchangeFunction ) ;
}
@Test
public void uriBuilderWithPathOverride ( ) {
WebClient client = builder ( ) . build ( ) ;
client . get ( ) . uri ( builder - > builder . replacePath ( "/path" ) . build ( ) ) . exchange ( ) ;
ClientRequest request = verifyExchange ( ) ;
this . builder . build ( ) . get ( )
. uri ( builder - > builder . replacePath ( "/path" ) . build ( ) )
. exchange ( ) ;
ClientRequest request = verifyAndGetRequest ( ) ;
assertEquals ( "/path" , request . url ( ) . toString ( ) ) ;
verifyNoMoreInteractions ( this . exchangeFunction ) ;
}
@Test
public void requestHeaderAndCookie ( ) {
WebClient client = builder ( ) . build ( ) ;
client . get ( ) . uri ( "/path" ) . accept ( MediaType . APPLICATION_JSON )
this . builder . build ( ) . get ( ) . uri ( "/path" ) . accept ( MediaType . APPLICATION_JSON )
. cookies ( cookies - > cookies . add ( "id" , "123" ) ) // SPR-16178
. exchange ( ) ;
ClientRequest request = verifyExchange ( ) ;
ClientRequest request = verifyAndGetRequest ( ) ;
assertEquals ( "application/json" , request . headers ( ) . getFirst ( "Accept" ) ) ;
assertEquals ( "123" , request . cookies ( ) . getFirst ( "id" ) ) ;
verifyNoMoreInteractions ( this . exchangeFunction ) ;
@ -100,10 +109,14 @@ public class DefaultWebClientTests {
@@ -100,10 +109,14 @@ public class DefaultWebClientTests {
@Test
public void defaultHeaderAndCookie ( ) {
WebClient client = builder ( ) . defaultHeader ( "Accept" , "application/json" ) . defaultCookie ( "id" , "123" ) . build ( ) ;
WebClient client = this . builder
. defaultHeader ( "Accept" , "application/json" ) . defaultCookie ( "id" , "123" )
. build ( ) ;
client . get ( ) . uri ( "/path" ) . exchange ( ) ;
ClientRequest request = verifyExchange ( ) ;
ClientRequest request = verifyAndGetRequest ( ) ;
assertEquals ( "application/json" , request . headers ( ) . getFirst ( "Accept" ) ) ;
assertEquals ( "123" , request . cookies ( ) . getFirst ( "id" ) ) ;
verifyNoMoreInteractions ( this . exchangeFunction ) ;
@ -111,10 +124,15 @@ public class DefaultWebClientTests {
@@ -111,10 +124,15 @@ public class DefaultWebClientTests {
@Test
public void defaultHeaderAndCookieOverrides ( ) {
WebClient client = builder ( ) . defaultHeader ( "Accept" , "application/json" ) . defaultCookie ( "id" , "123" ) . build ( ) ;
WebClient client = this . builder
. defaultHeader ( "Accept" , "application/json" )
. defaultCookie ( "id" , "123" )
. build ( ) ;
client . get ( ) . uri ( "/path" ) . header ( "Accept" , "application/xml" ) . cookie ( "id" , "456" ) . exchange ( ) ;
ClientRequest request = verifyExchange ( ) ;
ClientRequest request = verifyAndGetRequest ( ) ;
assertEquals ( "application/xml" , request . headers ( ) . getFirst ( "Accept" ) ) ;
assertEquals ( "456" , request . cookies ( ) . getFirst ( "id" ) ) ;
verifyNoMoreInteractions ( this . exchangeFunction ) ;
@ -123,7 +141,7 @@ public class DefaultWebClientTests {
@@ -123,7 +141,7 @@ public class DefaultWebClientTests {
@Test ( expected = IllegalArgumentException . class )
public void bodyObjectPublisher ( ) {
Mono < Void > mono = Mono . empty ( ) ;
WebClient client = builder ( ) . build ( ) ;
WebClient client = this . builder . build ( ) ;
client . post ( ) . uri ( "http://example.com" ) . syncBody ( mono ) ;
}
@ -131,57 +149,73 @@ public class DefaultWebClientTests {
@@ -131,57 +149,73 @@ public class DefaultWebClientTests {
@Test
public void mutateDoesCopy ( ) {
WebClient . Builder builder = WebClient . builder ( ) ;
builder . filter ( ( request , next ) - > next . exchange ( request ) ) ;
builder . defaultHeader ( "foo" , "bar" ) ;
builder . defaultCookie ( "foo" , "bar" ) ;
// First, build the clients
WebClient . Builder builder = WebClient . builder ( )
. filter ( ( request , next ) - > next . exchange ( request ) )
. defaultHeader ( "foo" , "bar" )
. defaultCookie ( "foo" , "bar" ) ;
WebClient client1 = builder . build ( ) ;
builder . filter ( ( request , next ) - > next . exchange ( request ) ) ;
builder . defaultHeader ( "baz" , "qux" ) ;
builder . defaultCookie ( "baz" , "qux" ) ;
WebClient client2 = builder . build ( ) ;
WebClient client2 = builder . filter ( ( request , next ) - > next . exchange ( request ) )
. defaultHeader ( "baz" , "qux" )
. defaultCookie ( "baz" , "qux" )
. build ( ) ;
WebClient . Builder mutatedBuilder = client1 . mutate ( ) ;
WebClient client1a = client1 . mutate ( )
. filter ( ( request , next ) - > next . exchange ( request ) )
. defaultHeader ( "baz" , "qux" )
. defaultCookie ( "baz" , "qux" )
. build ( ) ;
mutatedBuilder . filter ( ( request , next ) - > next . exchange ( request ) ) ;
mutatedBuilder . defaultHeader ( "baz" , "qux" ) ;
mutatedBuilder . defaultCookie ( "baz" , "qux" ) ;
WebClient clientFromMutatedBuilder = mutatedBuilder . build ( ) ;
// Now, verify what each client has..
client1 . mutate ( ) . filters ( filters - > assertEquals ( 1 , filters . size ( ) ) ) ;
client1 . mutate ( ) . defaultHeaders ( headers - > assertEquals ( 1 , headers . size ( ) ) ) ;
client1 . mutate ( ) . defaultCookies ( cookies - > assertEquals ( 1 , cookies . size ( ) ) ) ;
WebClient . Builder builder1 = client1 . mutate ( ) ;
builder1 . filters ( filters - > assertEquals ( 1 , filters . size ( ) ) ) ;
builder1 . defaultHeaders ( headers - > assertEquals ( 1 , headers . size ( ) ) ) ;
builder1 . defaultCookies ( cookies - > assertEquals ( 1 , cookies . size ( ) ) ) ;
client2 . mutate ( ) . filters ( filters - > assertEquals ( 2 , filters . size ( ) ) ) ;
client2 . mutate ( ) . defaultHeaders ( headers - > assertEquals ( 2 , headers . size ( ) ) ) ;
client2 . mutate ( ) . defaultCookies ( cookies - > assertEquals ( 2 , cookies . size ( ) ) ) ;
WebClient . Builder builder2 = client2 . mutate ( ) ;
builder2 . filters ( filters - > assertEquals ( 2 , filters . size ( ) ) ) ;
builder2 . defaultHeaders ( headers - > assertEquals ( 2 , headers . size ( ) ) ) ;
builder2 . defaultCookies ( cookies - > assertEquals ( 2 , cookies . size ( ) ) ) ;
clientFromMutatedBuilder . mutate ( ) . filters ( filters - > assertEquals ( 2 , filters . size ( ) ) ) ;
clientFromMutatedBuilder . mutate ( ) . defaultHeaders ( headers - > assertEquals ( 2 , headers . size ( ) ) ) ;
clientFromMutatedBuilder . mutate ( ) . defaultCookies ( cookies - > assertEquals ( 2 , cookies . size ( ) ) ) ;
WebClient . Builder builder1a = client1a . mutate ( ) ;
builder1a . filters ( filters - > assertEquals ( 2 , filters . size ( ) ) ) ;
builder1a . defaultHeaders ( headers - > assertEquals ( 2 , headers . size ( ) ) ) ;
builder1a . defaultCookies ( cookies - > assertEquals ( 2 , cookies . size ( ) ) ) ;
}
@Test
public void attributes ( ) {
Map < String , Object > actual = new HashMap < > ( ) ;
ExchangeFilterFunction filter = ( request , next ) - > {
assertEquals ( "bar" , request . attributes ( ) . get ( "foo" ) ) ;
actual . putAll ( request . attributes ( ) ) ;
return next . exchange ( request ) ;
} ;
WebClient client = builder ( ) . filter ( filter ) . build ( ) ;
this . builder . filter ( filter ) . build ( )
. get ( ) . uri ( "/path" )
. attribute ( "foo" , "bar" )
. exchange ( ) ;
client . get ( ) . uri ( "/path" ) . attribute ( "foo" , "bar" ) . exchange ( ) ;
assertEquals ( "bar" , actual . get ( "foo" ) ) ;
}
@Test
public void apply ( ) {
WebClient client = builder ( )
. apply ( builder - > builder . defaultHeader ( "Accept" , "application/json" ) . defaultCookie ( "id" , "123" ) )
WebClient client = this . builder
. apply ( builder - > builder
. defaultHeader ( "Accept" , "application/json" )
. defaultCookie ( "id" , "123" ) )
. build ( ) ;
client . get ( ) . uri ( "/path" ) . exchange ( ) ;
ClientRequest request = verifyExchange ( ) ;
ClientRequest request = verifyAndGetRequest ( ) ;
assertEquals ( "application/json" , request . headers ( ) . getFirst ( "Accept" ) ) ;
assertEquals ( "123" , request . cookies ( ) . getFirst ( "id" ) ) ;
verifyNoMoreInteractions ( this . exchangeFunction ) ;
@ -189,17 +223,12 @@ public class DefaultWebClientTests {
@@ -189,17 +223,12 @@ public class DefaultWebClientTests {
@Test
public void switchToErrorOnEmptyClientResponseMono ( ) {
StepVerifier . create ( builder ( ) . build ( ) . get ( ) . uri ( "/path" ) . exchange ( ) )
StepVerifier . create ( builder . build ( ) . get ( ) . uri ( "/path" ) . exchange ( ) )
. expectErrorMessage ( "The underlying HTTP client completed without emitting a response." )
. verify ( Duration . ofSeconds ( 5 ) ) ;
}
private WebClient . Builder builder ( ) {
return WebClient . builder ( ) . baseUrl ( "/base" ) . exchangeFunction ( this . exchangeFunction ) ;
}
private ClientRequest verifyExchange ( ) {
private ClientRequest verifyAndGetRequest ( ) {
ClientRequest request = this . captor . getValue ( ) ;
Mockito . verify ( this . exchangeFunction ) . exchange ( request ) ;
verifyNoMoreInteractions ( this . exchangeFunction ) ;