/
MappingsEndpointTests.java
305 lines (262 loc) · 12.7 KB
/
MappingsEndpointTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*
* Copyright 2012-2021 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.actuate.web.mappings;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;
import javax.servlet.FilterRegistration;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import org.junit.jupiter.api.Test;
import org.springframework.boot.actuate.web.mappings.MappingsEndpoint.ApplicationMappings;
import org.springframework.boot.actuate.web.mappings.MappingsEndpoint.ContextMappings;
import org.springframework.boot.actuate.web.mappings.reactive.DispatcherHandlerMappingDescription;
import org.springframework.boot.actuate.web.mappings.reactive.DispatcherHandlersMappingDescriptionProvider;
import org.springframework.boot.actuate.web.mappings.servlet.DispatcherServletMappingDescription;
import org.springframework.boot.actuate.web.mappings.servlet.DispatcherServletsMappingDescriptionProvider;
import org.springframework.boot.actuate.web.mappings.servlet.FilterRegistrationMappingDescription;
import org.springframework.boot.actuate.web.mappings.servlet.FiltersMappingDescriptionProvider;
import org.springframework.boot.actuate.web.mappings.servlet.ServletRegistrationMappingDescription;
import org.springframework.boot.actuate.web.mappings.servlet.ServletsMappingDescriptionProvider;
import org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner;
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebApplicationContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mock.web.MockServletConfig;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.ConfigurableWebApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.reactive.config.EnableWebFlux;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerResponse;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.util.pattern.PathPatternParser;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
import static org.springframework.web.reactive.function.server.RequestPredicates.POST;
import static org.springframework.web.reactive.function.server.RouterFunctions.route;
/**
* Tests for {@link MappingsEndpoint}.
*
* @author Andy Wilkinson
* @author Stephane Nicoll
*/
class MappingsEndpointTests {
@Test
void servletWebMappings() {
Supplier<ConfigurableWebApplicationContext> contextSupplier = prepareContextSupplier();
new WebApplicationContextRunner(contextSupplier)
.withUserConfiguration(EndpointConfiguration.class, ServletWebConfiguration.class).run((context) -> {
ContextMappings contextMappings = contextMappings(context);
assertThat(contextMappings.getParentId()).isNull();
assertThat(contextMappings.getMappings()).containsOnlyKeys("dispatcherServlets", "servletFilters",
"servlets");
Map<String, List<DispatcherServletMappingDescription>> dispatcherServlets = mappings(
contextMappings, "dispatcherServlets");
assertThat(dispatcherServlets).containsOnlyKeys("dispatcherServlet");
List<DispatcherServletMappingDescription> handlerMappings = dispatcherServlets
.get("dispatcherServlet");
assertThat(handlerMappings).hasSize(1);
List<ServletRegistrationMappingDescription> servlets = mappings(contextMappings, "servlets");
assertThat(servlets).hasSize(1);
List<FilterRegistrationMappingDescription> filters = mappings(contextMappings, "servletFilters");
assertThat(filters).hasSize(1);
});
}
@Test
void servletWebMappingsWithPathPatternParser() {
Supplier<ConfigurableWebApplicationContext> contextSupplier = prepareContextSupplier();
new WebApplicationContextRunner(contextSupplier).withUserConfiguration(EndpointConfiguration.class,
ServletWebConfiguration.class, PathPatternParserConfiguration.class).run((context) -> {
ContextMappings contextMappings = contextMappings(context);
assertThat(contextMappings.getParentId()).isNull();
assertThat(contextMappings.getMappings()).containsOnlyKeys("dispatcherServlets", "servletFilters",
"servlets");
Map<String, List<DispatcherServletMappingDescription>> dispatcherServlets = mappings(
contextMappings, "dispatcherServlets");
assertThat(dispatcherServlets).containsOnlyKeys("dispatcherServlet");
List<DispatcherServletMappingDescription> handlerMappings = dispatcherServlets
.get("dispatcherServlet");
assertThat(handlerMappings).hasSize(1);
List<ServletRegistrationMappingDescription> servlets = mappings(contextMappings, "servlets");
assertThat(servlets).hasSize(1);
List<FilterRegistrationMappingDescription> filters = mappings(contextMappings, "servletFilters");
assertThat(filters).hasSize(1);
});
}
@Test
void servletWebMappingsWithAdditionalDispatcherServlets() {
Supplier<ConfigurableWebApplicationContext> contextSupplier = prepareContextSupplier();
new WebApplicationContextRunner(contextSupplier).withUserConfiguration(EndpointConfiguration.class,
ServletWebConfiguration.class, CustomDispatcherServletConfiguration.class).run((context) -> {
ContextMappings contextMappings = contextMappings(context);
Map<String, List<DispatcherServletMappingDescription>> dispatcherServlets = mappings(
contextMappings, "dispatcherServlets");
assertThat(dispatcherServlets).containsOnlyKeys("dispatcherServlet",
"customDispatcherServletRegistration", "anotherDispatcherServletRegistration");
assertThat(dispatcherServlets.get("dispatcherServlet")).hasSize(1);
assertThat(dispatcherServlets.get("customDispatcherServletRegistration")).hasSize(1);
assertThat(dispatcherServlets.get("anotherDispatcherServletRegistration")).hasSize(1);
});
}
@SuppressWarnings("unchecked")
private Supplier<ConfigurableWebApplicationContext> prepareContextSupplier() {
ServletContext servletContext = mock(ServletContext.class);
given(servletContext.getInitParameterNames()).willReturn(Collections.emptyEnumeration());
given(servletContext.getAttributeNames()).willReturn(Collections.emptyEnumeration());
FilterRegistration filterRegistration = mock(FilterRegistration.class);
given((Map<String, FilterRegistration>) servletContext.getFilterRegistrations())
.willReturn(Collections.singletonMap("testFilter", filterRegistration));
ServletRegistration servletRegistration = mock(ServletRegistration.class);
given((Map<String, ServletRegistration>) servletContext.getServletRegistrations())
.willReturn(Collections.singletonMap("testServlet", servletRegistration));
return () -> {
AnnotationConfigServletWebApplicationContext context = new AnnotationConfigServletWebApplicationContext();
context.setServletContext(servletContext);
return context;
};
}
@Test
void reactiveWebMappings() {
new ReactiveWebApplicationContextRunner()
.withUserConfiguration(EndpointConfiguration.class, ReactiveWebConfiguration.class).run((context) -> {
ContextMappings contextMappings = contextMappings(context);
assertThat(contextMappings.getParentId()).isNull();
assertThat(contextMappings.getMappings()).containsOnlyKeys("dispatcherHandlers");
Map<String, List<DispatcherHandlerMappingDescription>> dispatcherHandlers = mappings(
contextMappings, "dispatcherHandlers");
assertThat(dispatcherHandlers).containsOnlyKeys("webHandler");
List<DispatcherHandlerMappingDescription> handlerMappings = dispatcherHandlers.get("webHandler");
assertThat(handlerMappings).hasSize(3);
});
}
private ContextMappings contextMappings(ApplicationContext context) {
ApplicationMappings applicationMappings = context.getBean(MappingsEndpoint.class).mappings();
assertThat(applicationMappings.getContexts()).containsOnlyKeys(context.getId());
return applicationMappings.getContexts().get(context.getId());
}
@SuppressWarnings("unchecked")
private <T> T mappings(ContextMappings contextMappings, String key) {
return (T) contextMappings.getMappings().get(key);
}
@Configuration(proxyBeanMethods = false)
static class EndpointConfiguration {
@Bean
MappingsEndpoint mappingsEndpoint(Collection<MappingDescriptionProvider> descriptionProviders,
ApplicationContext context) {
return new MappingsEndpoint(descriptionProviders, context);
}
}
@Configuration(proxyBeanMethods = false)
@EnableWebFlux
@Controller
static class ReactiveWebConfiguration {
@Bean
DispatcherHandlersMappingDescriptionProvider dispatcherHandlersMappingDescriptionProvider() {
return new DispatcherHandlersMappingDescriptionProvider();
}
@Bean
RouterFunction<ServerResponse> routerFunction() {
return route(GET("/one"), (request) -> ServerResponse.ok().build()).andRoute(POST("/two"),
(request) -> ServerResponse.ok().build());
}
@RequestMapping("/three")
void three() {
}
}
@Configuration(proxyBeanMethods = false)
@EnableWebMvc
@Controller
static class ServletWebConfiguration {
@Bean
DispatcherServletsMappingDescriptionProvider dispatcherServletsMappingDescriptionProvider() {
return new DispatcherServletsMappingDescriptionProvider();
}
@Bean
ServletsMappingDescriptionProvider servletsMappingDescriptionProvider() {
return new ServletsMappingDescriptionProvider();
}
@Bean
FiltersMappingDescriptionProvider filtersMappingDescriptionProvider() {
return new FiltersMappingDescriptionProvider();
}
@Bean
DispatcherServlet dispatcherServlet(WebApplicationContext context) throws ServletException {
DispatcherServlet dispatcherServlet = new DispatcherServlet(context);
dispatcherServlet.init(new MockServletConfig());
return dispatcherServlet;
}
@RequestMapping("/three")
void three() {
}
}
@Configuration
static class CustomDispatcherServletConfiguration {
@Bean
ServletRegistrationBean<DispatcherServlet> customDispatcherServletRegistration(WebApplicationContext context) {
ServletRegistrationBean<DispatcherServlet> registration = new ServletRegistrationBean<>(
createTestDispatcherServlet(context));
registration.setName("customDispatcherServletRegistration");
return registration;
}
@Bean
DispatcherServlet anotherDispatcherServlet(WebApplicationContext context) {
return createTestDispatcherServlet(context);
}
@Bean
ServletRegistrationBean<DispatcherServlet> anotherDispatcherServletRegistration(
DispatcherServlet dispatcherServlet, WebApplicationContext context) {
ServletRegistrationBean<DispatcherServlet> registrationBean = new ServletRegistrationBean<>(
anotherDispatcherServlet(context));
registrationBean.setName("anotherDispatcherServletRegistration");
return registrationBean;
}
private DispatcherServlet createTestDispatcherServlet(WebApplicationContext context) {
try {
DispatcherServlet dispatcherServlet = new DispatcherServlet(context);
dispatcherServlet.init(new MockServletConfig());
return dispatcherServlet;
}
catch (ServletException ex) {
throw new IllegalStateException(ex);
}
}
}
@Configuration
static class PathPatternParserConfiguration {
@Bean
WebMvcConfigurer pathPatternParserConfigurer() {
return new WebMvcConfigurer() {
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
configurer.setPatternParser(new PathPatternParser());
}
};
}
}
}