|
| 1 | +/* |
| 2 | + * Copyright 2002-2022 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 | + * https://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.web.reactive.result.method.annotation; |
| 18 | + |
| 19 | +import java.lang.reflect.Method; |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.Collections; |
| 22 | +import java.util.List; |
| 23 | + |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | + |
| 26 | +import org.springframework.core.LocalVariableTableParameterNameDiscoverer; |
| 27 | +import org.springframework.core.ReactiveAdapterRegistry; |
| 28 | +import org.springframework.core.convert.ConversionService; |
| 29 | +import org.springframework.format.support.DefaultFormattingConversionService; |
| 30 | +import org.springframework.web.bind.WebDataBinder; |
| 31 | +import org.springframework.web.bind.annotation.InitBinder; |
| 32 | +import org.springframework.web.bind.annotation.RequestParam; |
| 33 | +import org.springframework.web.bind.support.ConfigurableWebBindingInitializer; |
| 34 | +import org.springframework.web.reactive.BindingContext; |
| 35 | +import org.springframework.web.reactive.result.method.SyncHandlerMethodArgumentResolver; |
| 36 | +import org.springframework.web.reactive.result.method.SyncInvocableHandlerMethod; |
| 37 | +import org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest; |
| 38 | +import org.springframework.web.testfixture.server.MockServerWebExchange; |
| 39 | + |
| 40 | +import static org.assertj.core.api.Assertions.assertThat; |
| 41 | +import static org.assertj.core.api.Assertions.assertThatIllegalStateException; |
| 42 | + |
| 43 | +/** |
| 44 | + * Unit tests for {@link InitBinderBindingContext}. |
| 45 | + * |
| 46 | + * @author Rossen Stoyanchev |
| 47 | + */ |
| 48 | +public class InitBinderBindingContextTests { |
| 49 | + |
| 50 | + private final ConfigurableWebBindingInitializer bindingInitializer = new ConfigurableWebBindingInitializer(); |
| 51 | + |
| 52 | + private final List<SyncHandlerMethodArgumentResolver> argumentResolvers = new ArrayList<>(); |
| 53 | + |
| 54 | + |
| 55 | + @Test |
| 56 | + public void createBinder() throws Exception { |
| 57 | + MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/")); |
| 58 | + BindingContext context = createBindingContext("initBinder", WebDataBinder.class); |
| 59 | + WebDataBinder dataBinder = context.createDataBinder(exchange, null, null); |
| 60 | + |
| 61 | + assertThat(dataBinder.getDisallowedFields()).isNotNull(); |
| 62 | + assertThat(dataBinder.getDisallowedFields()[0]).isEqualTo("id"); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void createBinderWithGlobalInitialization() throws Exception { |
| 67 | + ConversionService conversionService = new DefaultFormattingConversionService(); |
| 68 | + bindingInitializer.setConversionService(conversionService); |
| 69 | + |
| 70 | + MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/")); |
| 71 | + BindingContext context = createBindingContext("initBinder", WebDataBinder.class); |
| 72 | + WebDataBinder dataBinder = context.createDataBinder(exchange, null, null); |
| 73 | + |
| 74 | + assertThat(dataBinder.getConversionService()).isSameAs(conversionService); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void createBinderWithAttrName() throws Exception { |
| 79 | + MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/")); |
| 80 | + BindingContext context = createBindingContext("initBinderWithAttributeName", WebDataBinder.class); |
| 81 | + WebDataBinder dataBinder = context.createDataBinder(exchange, null, "foo"); |
| 82 | + |
| 83 | + assertThat(dataBinder.getDisallowedFields()).isNotNull(); |
| 84 | + assertThat(dataBinder.getDisallowedFields()[0]).isEqualTo("id"); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void createBinderWithAttrNameNoMatch() throws Exception { |
| 89 | + MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/")); |
| 90 | + BindingContext context = createBindingContext("initBinderWithAttributeName", WebDataBinder.class); |
| 91 | + WebDataBinder dataBinder = context.createDataBinder(exchange, null, "invalidName"); |
| 92 | + |
| 93 | + assertThat(dataBinder.getDisallowedFields()).isNull(); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void createBinderNullAttrName() throws Exception { |
| 98 | + MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/")); |
| 99 | + BindingContext context = createBindingContext("initBinderWithAttributeName", WebDataBinder.class); |
| 100 | + WebDataBinder dataBinder = context.createDataBinder(exchange, null, null); |
| 101 | + |
| 102 | + assertThat(dataBinder.getDisallowedFields()).isNull(); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + public void returnValueNotExpected() throws Exception { |
| 107 | + MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/")); |
| 108 | + BindingContext context = createBindingContext("initBinderReturnValue", WebDataBinder.class); |
| 109 | + assertThatIllegalStateException().isThrownBy(() -> |
| 110 | + context.createDataBinder(exchange, null, "invalidName")); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + public void createBinderTypeConversion() throws Exception { |
| 115 | + MockServerHttpRequest request = MockServerHttpRequest.get("/path?requestParam=22").build(); |
| 116 | + MockServerWebExchange exchange = MockServerWebExchange.from(request); |
| 117 | + ReactiveAdapterRegistry adapterRegistry = ReactiveAdapterRegistry.getSharedInstance(); |
| 118 | + this.argumentResolvers.add(new RequestParamMethodArgumentResolver(null, adapterRegistry, false)); |
| 119 | + |
| 120 | + BindingContext context = createBindingContext("initBinderTypeConversion", WebDataBinder.class, int.class); |
| 121 | + WebDataBinder dataBinder = context.createDataBinder(exchange, null, "foo"); |
| 122 | + |
| 123 | + assertThat(dataBinder.getDisallowedFields()).isNotNull(); |
| 124 | + assertThat(dataBinder.getDisallowedFields()[0]).isEqualToIgnoringCase("requestParam-22"); |
| 125 | + } |
| 126 | + |
| 127 | + |
| 128 | + private BindingContext createBindingContext(String methodName, Class<?>... parameterTypes) throws Exception { |
| 129 | + Object handler = new InitBinderHandler(); |
| 130 | + Method method = handler.getClass().getMethod(methodName, parameterTypes); |
| 131 | + |
| 132 | + SyncInvocableHandlerMethod handlerMethod = new SyncInvocableHandlerMethod(handler, method); |
| 133 | + handlerMethod.setArgumentResolvers(new ArrayList<>(this.argumentResolvers)); |
| 134 | + handlerMethod.setParameterNameDiscoverer(new LocalVariableTableParameterNameDiscoverer()); |
| 135 | + |
| 136 | + return new InitBinderBindingContext(this.bindingInitializer, Collections.singletonList(handlerMethod)); |
| 137 | + } |
| 138 | + |
| 139 | + |
| 140 | + private static class InitBinderHandler { |
| 141 | + |
| 142 | + @InitBinder |
| 143 | + public void initBinder(WebDataBinder dataBinder) { |
| 144 | + dataBinder.setDisallowedFields("id"); |
| 145 | + } |
| 146 | + |
| 147 | + @InitBinder(value="foo") |
| 148 | + public void initBinderWithAttributeName(WebDataBinder dataBinder) { |
| 149 | + dataBinder.setDisallowedFields("id"); |
| 150 | + } |
| 151 | + |
| 152 | + @InitBinder |
| 153 | + public String initBinderReturnValue(WebDataBinder dataBinder) { |
| 154 | + return "invalid"; |
| 155 | + } |
| 156 | + |
| 157 | + @InitBinder |
| 158 | + public void initBinderTypeConversion(WebDataBinder dataBinder, @RequestParam int requestParam) { |
| 159 | + dataBinder.setDisallowedFields("requestParam-" + requestParam); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | +} |
0 commit comments