@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 public void bindExistingMono() throws Exception { Foo foo = new Foo(); foo.setName("Jim"); this.bindContext.getModel().addAttribute("fooMono", Mono.just(foo)); MethodParameter parameter = this.testMethod.annotNotPresent(ModelAttribute.class).arg(Foo.class); testBindFoo("foo", parameter, value -> { assertEquals(Foo.class, value.getClass()); return (Foo) value; }); assertSame(foo, this.bindContext.getModel().asMap().get("foo")); }
@Test public void resolveMultiValueMapArgument() throws Exception { MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(MultiValueMap.class); ServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/path?foo=bar&foo=baz")); Object result= resolve(param, exchange); assertTrue(result instanceof MultiValueMap); assertEquals(Collections.singletonMap("foo", Arrays.asList("bar", "baz")), result); }
@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 emptyBodyWithStringNotRequired() throws Exception { MethodParameter param = this.testMethod.annot(requestBody().notRequired()).arg(String.class); String body = resolveValueWithEmptyBody(param); assertNull(body); }
@Test public void resolveEmptyValueWithoutDefault() throws Exception { request.addParameter("stringNotAnnot", ""); MethodParameter param = this.testMethod.annotNotPresent(RequestParam.class).arg(String.class); Object result = resolver.resolveArgument(param, null, webRequest, null); assertEquals("", result); }
@Test public void resolveStringArray() throws Exception { String[] expected = new String[] {"foo", "bar"}; request.addParameter("name", expected); MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(String[].class); Object result = resolver.resolveArgument(param, null, webRequest, null); assertTrue(result instanceof String[]); assertArrayEquals("Invalid result", expected, (String[]) result); }
@Test public void resolveEmptyValueWithoutDefault() { MethodParameter param = this.testMethod.annotNotPresent(RequestParam.class).arg(String.class); MockServerHttpRequest request = MockServerHttpRequest.get("/path?stringNotAnnot=").build(); assertEquals("", resolve(param, MockServerWebExchange.from(request))); }
@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()); }
@Test public void listPerson() { MethodParameter param = this.testMethod.annot(requestPart()).arg(List.class, Person.class); MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder(); bodyBuilder.part("name", new Person("Jones")); bodyBuilder.part("name", new Person("James")); List<Person> actual = resolveArgument(param, bodyBuilder); assertEquals("Jones", actual.get(0).getName()); assertEquals("James", actual.get(1).getName()); }
/** * Find a unique argument matching the given type. * @param type the expected type */ public MethodParameter arg(ResolvableType type) { return new ArgResolver().arg(type); }
/** * Find a unique argument matching the given type. * @param type the expected type * @param generics optional array of generic types */ public MethodParameter arg(Class<?> type, Class<?>... generics) { return new ArgResolver().arg(type, generics); }
@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 createAndBind() throws Exception { testBindFoo("foo", this.testMethod.annotPresent(ModelAttribute.class).arg(Foo.class), value -> { assertEquals(Foo.class, value.getClass()); return (Foo) value; }); }
@Test public void resolveDefaultValue() throws Exception { MethodParameter param = this.testMethod.annot(requestParam().notRequired("bar")).arg(String.class); Object result = resolver.resolveArgument(param, null, webRequest, null); assertTrue(result instanceof String); assertEquals("Invalid result", "bar", result); }
@Test public void resolveMultipartFile() throws Exception { MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest(); MultipartFile expected = new MockMultipartFile("mfile", "Hello World".getBytes()); request.addFile(expected); webRequest = new ServletWebRequest(request); MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(MultipartFile.class); Object result = resolver.resolveArgument(param, null, webRequest, null); assertTrue(result instanceof MultipartFile); assertEquals("Invalid result", expected, result); }
@Test // SPR-10180 public void resolveEmptyValueToDefault() { ServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/path?name=")); MethodParameter param = this.testMethod.annot(requestParam().notRequired("bar")).arg(String.class); Object result = resolve(param, exchange); assertEquals("bar", result); }
@Test public void resolveWithQueryString() { MethodParameter param = this.testMethod.annot(requestParam().notRequired("bar")).arg(String.class); MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/path?name=foo")); assertEquals("foo", resolve(param, exchange)); }
@Test public void supportsParameter() { assertFalse(this.resolver.supportsParameter(this.testMethod.arg(String.class))); assertTrue(this.resolver.supportsParameter(this.testMethod.annot(matrixAttribute().noName()) .arg(Map.class, String.class, String.class))); assertTrue(this.resolver.supportsParameter(this.testMethod.annot(matrixAttribute().noPathVar()) .arg(MultiValueMap.class, String.class, String.class))); assertTrue(this.resolver.supportsParameter(this.testMethod.annot(matrixAttribute().pathVar("cars")) .arg(MultiValueMap.class, String.class, String.class))); assertFalse(this.resolver.supportsParameter(this.testMethod.annot(matrixAttribute().name("name")) .arg(Map.class, String.class, String.class))); }
@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()); }