16
16
17
17
package org .springframework .boot .autoconfigure .hateoas ;
18
18
19
+ import static org .assertj .core .api .Assertions .*;
20
+
21
+ import java .util .Map ;
19
22
import java .util .Optional ;
20
23
21
24
import org .junit .jupiter .api .Test ;
22
-
23
25
import org .springframework .boot .autoconfigure .ImportAutoConfiguration ;
24
26
import org .springframework .boot .autoconfigure .hateoas .HypermediaAutoConfiguration .HypermediaConfiguration ;
25
27
import org .springframework .boot .autoconfigure .http .HttpMessageConvertersAutoConfiguration ;
26
28
import org .springframework .boot .autoconfigure .jackson .JacksonAutoConfiguration ;
29
+ import org .springframework .boot .autoconfigure .web .reactive .function .client .WebClientAutoConfiguration ;
27
30
import org .springframework .boot .autoconfigure .web .servlet .WebMvcAutoConfiguration ;
28
31
import org .springframework .boot .test .context .FilteredClassLoader ;
29
32
import org .springframework .boot .test .context .runner .WebApplicationContextRunner ;
33
+ import org .springframework .boot .web .codec .CodecCustomizer ;
30
34
import org .springframework .context .annotation .Configuration ;
31
35
import org .springframework .hateoas .MediaTypes ;
32
36
import org .springframework .hateoas .client .LinkDiscoverer ;
37
41
import org .springframework .hateoas .server .EntityLinks ;
38
42
import org .springframework .hateoas .server .mvc .TypeConstrainedMappingJackson2HttpMessageConverter ;
39
43
import org .springframework .http .MediaType ;
44
+ import org .springframework .http .codec .CodecConfigurer ;
45
+ import org .springframework .http .codec .HttpMessageReader ;
46
+ import org .springframework .http .codec .HttpMessageWriter ;
47
+ import org .springframework .http .codec .support .DefaultClientCodecConfigurer ;
40
48
import org .springframework .http .converter .HttpMessageConverter ;
49
+ import org .springframework .test .util .ReflectionTestUtils ;
50
+ import org .springframework .web .reactive .function .client .ExchangeStrategies ;
51
+ import org .springframework .web .reactive .function .client .WebClient ;
41
52
import org .springframework .web .servlet .mvc .method .annotation .RequestMappingHandlerAdapter ;
42
53
43
- import static org .assertj .core .api .Assertions .assertThat ;
44
-
45
54
/**
46
55
* Tests for {@link HypermediaAutoConfiguration}.
47
56
*
48
57
* @author Roy Clarkson
49
58
* @author Oliver Gierke
50
59
* @author Andy Wilkinson
51
60
* @author Madhura Bhave
61
+ * @author Greg Turnquist
52
62
*/
53
63
class HypermediaAutoConfigurationTests {
54
64
@@ -112,6 +122,39 @@ void customizationOfSupportedMediaTypesCanBeDisabled() {
112
122
});
113
123
}
114
124
125
+ @ Test
126
+ void codecsCustomizerShouldRegisterHypermediaTypesWithWebClient () {
127
+ this .contextRunner .withUserConfiguration (EnableHypermediaSupportConfig .class ,
128
+ EnableHypermediaWebClientSupportConfig .class ).run ((context ) -> {
129
+ WebClient webClient = context .getBean (WebClient .Builder .class ).build ();
130
+ ExchangeStrategies strategies = (ExchangeStrategies ) ReflectionTestUtils
131
+ .getField (ReflectionTestUtils .getField (webClient , "exchangeFunction" ), "strategies" );
132
+
133
+ assertThat (strategies .messageReaders ()).flatExtracting (HttpMessageReader ::getReadableMediaTypes )
134
+ .contains (MediaTypes .HAL_JSON );
135
+ assertThat (strategies .messageWriters ()).flatExtracting (HttpMessageWriter ::getWritableMediaTypes )
136
+ .contains (MediaTypes .HAL_JSON );
137
+ });
138
+ }
139
+
140
+ @ Test
141
+ void codecsCustomizerShouldRegisterHypermediaTypesWithCodecConfigurer () {
142
+ this .contextRunner .withUserConfiguration (EnableHypermediaSupportConfig .class ,
143
+ EnableHypermediaWebClientSupportConfig .class ).run ((context ) -> {
144
+ CodecCustomizer customizer = context .getBean (CodecCustomizer .class );
145
+ CodecConfigurer configurer = new DefaultClientCodecConfigurer ();
146
+ customizer .customize (configurer );
147
+ CodecConfigurer .CustomCodecs customCodecs = configurer .customCodecs ();
148
+
149
+ assertThat (((Map <HttpMessageReader <?>, Boolean >) ReflectionTestUtils .getField (customCodecs ,
150
+ "objectReaders" )).keySet ()).flatExtracting (HttpMessageReader ::getReadableMediaTypes )
151
+ .containsExactly (MediaTypes .HAL_JSON );
152
+ assertThat (((Map <HttpMessageWriter <?>, Boolean >) ReflectionTestUtils .getField (customCodecs ,
153
+ "objectWriters" )).keySet ()).flatExtracting (HttpMessageWriter ::getWritableMediaTypes )
154
+ .containsExactly (MediaTypes .HAL_JSON );
155
+ });
156
+ }
157
+
115
158
@ ImportAutoConfiguration ({ HttpMessageConvertersAutoConfiguration .class , WebMvcAutoConfiguration .class ,
116
159
JacksonAutoConfiguration .class , HypermediaAutoConfiguration .class })
117
160
static class BaseConfig {
@@ -124,4 +167,10 @@ static class EnableHypermediaSupportConfig {
124
167
125
168
}
126
169
170
+ @ Configuration (proxyBeanMethods = false )
171
+ @ ImportAutoConfiguration (WebClientAutoConfiguration .class )
172
+ static class EnableHypermediaWebClientSupportConfig {
173
+
174
+ }
175
+
127
176
}
0 commit comments