|
| 1 | +/* |
| 2 | + * Copyright 2012-2017 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.autoconfigure.web.reactive.function.client; |
| 18 | + |
| 19 | +import java.net.URI; |
| 20 | + |
| 21 | +import org.junit.After; |
| 22 | +import org.junit.Test; |
| 23 | +import reactor.core.publisher.Mono; |
| 24 | + |
| 25 | +import org.springframework.boot.web.reactive.function.client.WebClientCustomizer; |
| 26 | +import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
| 27 | +import org.springframework.context.annotation.Bean; |
| 28 | +import org.springframework.context.annotation.Configuration; |
| 29 | +import org.springframework.http.HttpMethod; |
| 30 | +import org.springframework.http.client.reactive.ClientHttpConnector; |
| 31 | +import org.springframework.web.reactive.function.client.WebClient; |
| 32 | + |
| 33 | +import static org.assertj.core.api.Assertions.assertThat; |
| 34 | +import static org.mockito.ArgumentMatchers.any; |
| 35 | +import static org.mockito.ArgumentMatchers.eq; |
| 36 | +import static org.mockito.BDDMockito.given; |
| 37 | +import static org.mockito.Mockito.mock; |
| 38 | +import static org.mockito.Mockito.times; |
| 39 | +import static org.mockito.Mockito.verify; |
| 40 | + |
| 41 | +/** |
| 42 | + * Tests for {@link WebClientAutoConfiguration} |
| 43 | + * |
| 44 | + * @author Brian Clozel |
| 45 | + */ |
| 46 | +public class WebClientAutoConfigurationTests { |
| 47 | + |
| 48 | + private AnnotationConfigApplicationContext context; |
| 49 | + |
| 50 | + @After |
| 51 | + public void close() { |
| 52 | + if (this.context != null) { |
| 53 | + this.context.close(); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void webClientShouldApplyCustomizers() throws Exception { |
| 59 | + load(WebClientCustomizerConfig.class); |
| 60 | + WebClient.Builder builder = this.context.getBean(WebClient.Builder.class); |
| 61 | + WebClientCustomizer customizer = this.context.getBean(WebClientCustomizer.class); |
| 62 | + builder.build(); |
| 63 | + verify(customizer).customize(any(WebClient.Builder.class)); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void shouldGetPrototypeScopedBean() throws Exception { |
| 68 | + load(WebClientCustomizerConfig.class); |
| 69 | + |
| 70 | + ClientHttpConnector firstConnector = mock(ClientHttpConnector.class); |
| 71 | + given(firstConnector.connect(any(), any(), any())).willReturn(Mono.empty()); |
| 72 | + WebClient.Builder firstBuilder = this.context.getBean(WebClient.Builder.class); |
| 73 | + firstBuilder.clientConnector(firstConnector).baseUrl("http://first.example.org"); |
| 74 | + |
| 75 | + ClientHttpConnector secondConnector = mock(ClientHttpConnector.class); |
| 76 | + given(secondConnector.connect(any(), any(), any())).willReturn(Mono.empty()); |
| 77 | + WebClient.Builder secondBuilder = this.context.getBean(WebClient.Builder.class); |
| 78 | + secondBuilder.clientConnector(secondConnector).baseUrl("http://second.example.org"); |
| 79 | + |
| 80 | + assertThat(firstBuilder).isNotEqualTo(secondBuilder); |
| 81 | + |
| 82 | + firstBuilder.build().get().uri("/foo").exchange().block(); |
| 83 | + secondBuilder.build().get().uri("/foo").exchange().block(); |
| 84 | + |
| 85 | + verify(firstConnector).connect(eq(HttpMethod.GET), eq(URI.create("http://first.example.org/foo")), any()); |
| 86 | + verify(secondConnector).connect(eq(HttpMethod.GET), eq(URI.create("http://second.example.org/foo")), any()); |
| 87 | + WebClientCustomizer customizer = this.context.getBean(WebClientCustomizer.class); |
| 88 | + verify(customizer, times(1)).customize(any(WebClient.Builder.class)); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void shouldNotCreateClientBuilderIfAlreadyPresent() throws Exception { |
| 93 | + load(WebClientCustomizerConfig.class, CustomWebClientBuilderConfig.class); |
| 94 | + WebClient.Builder builder = this.context.getBean(WebClient.Builder.class); |
| 95 | + assertThat(builder).isInstanceOf(MyWebClientBuilder.class); |
| 96 | + } |
| 97 | + |
| 98 | + |
| 99 | + private void load(Class<?>... config) { |
| 100 | + AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); |
| 101 | + ctx.register(config); |
| 102 | + ctx.register(WebClientAutoConfiguration.class); |
| 103 | + ctx.refresh(); |
| 104 | + this.context = ctx; |
| 105 | + } |
| 106 | + |
| 107 | + @Configuration |
| 108 | + static class WebClientCustomizerConfig { |
| 109 | + |
| 110 | + @Bean |
| 111 | + public WebClientCustomizer webClientCustomizer() { |
| 112 | + return mock(WebClientCustomizer.class); |
| 113 | + } |
| 114 | + |
| 115 | + } |
| 116 | + |
| 117 | + @Configuration |
| 118 | + static class CustomWebClientBuilderConfig { |
| 119 | + |
| 120 | + @Bean |
| 121 | + public MyWebClientBuilder myWebClientBuilder() { |
| 122 | + return mock(MyWebClientBuilder.class); |
| 123 | + } |
| 124 | + |
| 125 | + } |
| 126 | + |
| 127 | + interface MyWebClientBuilder extends WebClient.Builder { |
| 128 | + |
| 129 | + } |
| 130 | + |
| 131 | +} |
0 commit comments