@Test public void resolveArgument() throws Exception { MultiValueMap<String, String> params = getVariablesFor("cars"); params.add("colors", "red"); params.add("colors", "green"); params.add("colors", "blue"); MethodParameter param = this.testMethod.annot(matrixAttribute().noName()).arg(List.class, String.class); assertEquals(Arrays.asList("red", "green", "blue"), this.resolver.resolveArgument(param, new BindingContext(), this.exchange).block(Duration.ZERO)); }
@Test public void doesNotSupportReactiveWrapper() { MethodParameter param; try { param = this.testMethod.annot(requestParam()).arg(Mono.class, String.class); this.resolver.supportsParameter(param); fail(); } catch (IllegalStateException ex) { assertTrue("Unexpected error message:\n" + ex.getMessage(), ex.getMessage().startsWith( "RequestParamMethodArgumentResolver doesn't support reactive type wrapper")); } try { param = this.testMethod.annotNotPresent(RequestParam.class).arg(Mono.class, String.class); this.resolver.supportsParameter(param); fail(); } catch (IllegalStateException ex) { assertTrue("Unexpected error message:\n" + ex.getMessage(), ex.getMessage().startsWith( "RequestParamMethodArgumentResolver doesn't support reactive type wrapper")); } }
@Test public void supportsParameter() { MethodParameter param; param = this.testMethod.annot(requestPart()).arg(Person.class); assertTrue(this.resolver.supportsParameter(param)); param = this.testMethod.annot(requestPart()).arg(Mono.class, Person.class); assertTrue(this.resolver.supportsParameter(param)); param = this.testMethod.annot(requestPart()).arg(Flux.class, Person.class); assertTrue(this.resolver.supportsParameter(param)); param = this.testMethod.annot(requestPart()).arg(Part.class); assertTrue(this.resolver.supportsParameter(param)); param = this.testMethod.annot(requestPart()).arg(Mono.class, Part.class); assertTrue(this.resolver.supportsParameter(param)); param = this.testMethod.annot(requestPart()).arg(Flux.class, Part.class); assertTrue(this.resolver.supportsParameter(param)); param = this.testMethod.annotNotPresent(RequestPart.class).arg(Person.class); assertFalse(this.resolver.supportsParameter(param)); }
resolver = new RequestParamMethodArgumentResolver(null, true); MethodParameter param = this.testMethod.annot(requestParam().notRequired("bar")).arg(String.class); assertTrue(resolver.supportsParameter(param)); assertTrue(resolver.supportsParameter(param)); param = this.testMethod.annot(requestParam().name("name")).arg(Map.class); assertTrue(resolver.supportsParameter(param)); assertTrue(resolver.supportsParameter(param)); param = this.testMethod.annot(requestParam().noName()).arg(Map.class); assertFalse(resolver.supportsParameter(param)); assertTrue(resolver.supportsParameter(param)); param = this.testMethod.annot(requestPart()).arg(MultipartFile.class); assertFalse(resolver.supportsParameter(param)); param = this.testMethod.annot(requestParam()).arg(String.class); assertTrue(resolver.supportsParameter(param)); param = this.testMethod.annot(requestParam().notRequired()).arg(String.class); assertTrue(resolver.supportsParameter(param));
@Test(expected = ServerWebInputException.class) public void emptyBodyWithString() throws Exception { MethodParameter param = this.testMethod.annot(requestBody()).arg(String.class); resolveValueWithEmptyBody(param); }
@Test public void resolveWithName() throws Exception { MethodParameter param = this.testMethod.annot(requestAttribute().name("specialFoo")).arg(); Foo foo = new Foo(); this.exchange.getAttributes().put("specialFoo", foo); Mono<Object> mono = this.resolver.resolveArgument(param, new BindingContext(), this.exchange); assertSame(foo, mono.block()); }
@Test public void getHandlerGlobMatch() { Method expected = on(TestController.class).annot(requestMapping("/ba*").method(GET, HEAD)).resolveMethod(); ServerWebExchange exchange = MockServerWebExchange.from(get("/bar")); HandlerMethod hm = (HandlerMethod) this.handlerMapping.getHandler(exchange).block(); assertEquals(expected, hm.getMethod()); }
@Test public void getHandlerBestMatch() { Method expected = on(TestController.class).annot(getMapping("/foo").params("p")).resolveMethod(); ServerWebExchange exchange = MockServerWebExchange.from(get("/foo?p=anything")); HandlerMethod hm = (HandlerMethod) this.handlerMapping.getHandler(exchange).block(); assertEquals(expected, hm.getMethod()); }
@Test public void stringBody() throws Exception { String body = "line1"; MethodParameter param = this.testMethod.annot(requestBody()).arg(String.class); String value = resolveValue(param, body); assertEquals(body, value); }
@Test public void resolveOptional() throws Exception { MethodParameter param = this.testMethod.annot(requestAttribute().name("foo")).arg(Optional.class, Foo.class); Mono<Object> mono = this.resolver.resolveArgument(param, new BindingContext(), this.exchange); assertNotNull(mono.block()); assertEquals(Optional.class, mono.block().getClass()); assertFalse(((Optional<?>) mono.block()).isPresent()); ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer(); initializer.setConversionService(new DefaultFormattingConversionService()); BindingContext bindingContext = new BindingContext(initializer); Foo foo = new Foo(); this.exchange.getAttributes().put("foo", foo); mono = this.resolver.resolveArgument(param, bindingContext, this.exchange); assertNotNull(mono.block()); assertEquals(Optional.class, mono.block().getClass()); Optional<?> optional = (Optional<?>) mono.block(); assertTrue(optional.isPresent()); assertSame(foo, optional.get()); }
@Test public void getHandlerEmptyPathMatch() { Method expected = on(TestController.class).annot(requestMapping("")).resolveMethod(); ServerWebExchange exchange = MockServerWebExchange.from(get("")); HandlerMethod hm = (HandlerMethod) this.handlerMapping.getHandler(exchange).block(); assertEquals(expected, hm.getMethod()); exchange = MockServerWebExchange.from(get("/")); hm = (HandlerMethod) this.handlerMapping.getHandler(exchange).block(); assertEquals(expected, hm.getMethod()); }
@Test public void getHandlerDirectMatch() { Method expected = on(TestController.class).annot(getMapping("/foo").params()).resolveMethod(); ServerWebExchange exchange = MockServerWebExchange.from(get("/foo")); HandlerMethod hm = (HandlerMethod) this.handlerMapping.getHandler(exchange).block(); assertEquals(expected, hm.getMethod()); }
@Test public void resolveArgument() throws Exception { MultiValueMap<String, String> params = getVariablesFor("cars"); params.add("colors", "red"); params.add("colors", "green"); params.add("colors", "blue"); MethodParameter param = this.testMethod.annot(matrixAttribute().noName()).arg(List.class, String.class); assertEquals(Arrays.asList("red", "green", "blue"), this.resolver.resolveArgument(param, this.mavContainer, this.webRequest, null)); }
@Test public void resolveMapOfString() throws Exception { String name = "foo"; String value = "bar"; request.addParameter(name, value); Map<String, String> expected = Collections.singletonMap(name, value); MethodParameter param = this.testMethod.annot(requestParam().noName()).arg(Map.class, String.class, String.class); Object result = resolver.resolveArgument(param, null, webRequest, null); assertTrue(result instanceof Map); assertEquals("Invalid result", expected, result); }
@Test public void fluxPerson() { MethodParameter param = this.testMethod.annot(requestPart()).arg(Flux.class, Person.class); MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder(); bodyBuilder.part("name", new Person("Jones")); bodyBuilder.part("name", new Person("James")); Flux<Person> actual = resolveArgument(param, bodyBuilder); List<Person> persons = actual.collectList().block(); assertEquals("Jones", persons.get(0).getName()); assertEquals("James", persons.get(1).getName()); }
@Test public void emptyBodyWithCompletableFuture() throws Exception { MethodParameter param = this.testMethod.annot(requestBody()).arg(CompletableFuture.class, String.class); CompletableFuture<String> future = resolveValueWithEmptyBody(param); future.whenComplete((text, ex) -> { assertNull(text); assertNotNull(ex); }); param = this.testMethod.annot(requestBody().notRequired()).arg(CompletableFuture.class, String.class); future = resolveValueWithEmptyBody(param); future.whenComplete((text, ex) -> { assertNotNull(text); assertNull(ex); }); }
@Test // SPR-16158 public void resolveMonoParameter() throws Exception { MethodParameter param = this.testMethod.annot(requestAttribute().noName()).arg(Mono.class, Foo.class); // Mono attribute Foo foo = new Foo(); Mono<Foo> fooMono = Mono.just(foo); this.exchange.getAttributes().put("fooMono", fooMono); Mono<Object> mono = this.resolver.resolveArgument(param, new BindingContext(), this.exchange); assertSame(fooMono, mono.block(Duration.ZERO)); // RxJava Single attribute Single<Foo> singleMono = Single.just(foo); this.exchange.getAttributes().clear(); this.exchange.getAttributes().put("fooMono", singleMono); mono = this.resolver.resolveArgument(param, new BindingContext(), this.exchange); Object value = mono.block(Duration.ZERO); assertTrue(value instanceof Mono); assertSame(foo, ((Mono<?>) value).block(Duration.ZERO)); // No attribute --> Mono.empty this.exchange.getAttributes().clear(); mono = this.resolver.resolveArgument(param, new BindingContext(), this.exchange); assertSame(Mono.empty(), mono.block(Duration.ZERO)); }
@Test public void resolveArgumentNoMatch() throws Exception { MultiValueMap<String, String> params2 = getMatrixVariables("planes"); params2.add("colors", "yellow"); params2.add("colors", "orange"); MethodParameter param = this.testMethod.annot(matrixAttribute().pathVar("cars")) .arg(MultiValueMap.class, String.class, String.class); @SuppressWarnings("unchecked") Map<String, String> map = (Map<String, String>) this.resolver.resolveArgument( param, new BindingContext(), this.exchange).block(Duration.ZERO); assertEquals(Collections.emptyMap(), map); }
@Test @SuppressWarnings("unchecked") public void resolveMapOfMultipartFile() throws Exception { MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest(); MultipartFile expected1 = new MockMultipartFile("mfile", "Hello World".getBytes()); MultipartFile expected2 = new MockMultipartFile("other", "Hello World 3".getBytes()); request.addFile(expected1); request.addFile(expected2); webRequest = new ServletWebRequest(request); MethodParameter param = this.testMethod.annot(requestParam().noName()).arg(Map.class, String.class, MultipartFile.class); Object result = resolver.resolveArgument(param, null, webRequest, null); assertTrue(result instanceof Map); Map<String, MultipartFile> resultMap = (Map<String, MultipartFile>) result; assertEquals(2, resultMap.size()); assertEquals(expected1, resultMap.get("mfile")); assertEquals(expected2, resultMap.get("other")); }
@Test public void monoPerson() { MethodParameter param = this.testMethod.annot(requestPart()).arg(Mono.class, Person.class); MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder(); bodyBuilder.part("name", new Person("Jones")); Mono<Person> actual = resolveArgument(param, bodyBuilder); assertEquals("Jones", actual.block().getName()); }