@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 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(expected = ServerWebInputException.class) public void emptyBodyWithString() throws Exception { MethodParameter param = this.testMethod.annot(requestBody()).arg(String.class); resolveValueWithEmptyBody(param); }
@Test public void resolveArgumentPathVariable() throws Exception { getVariablesFor("cars").add("year", "2006"); getVariablesFor("bikes").add("year", "2005"); MethodParameter param = this.testMethod.annot(matrixAttribute().name("year")).arg(int.class); Object actual = this.resolver.resolveArgument(param, new BindingContext(), this.exchange).block(Duration.ZERO); assertEquals(2006, actual); }
@Test public void resolveArgumentNoParams() throws Exception { MethodParameter param = this.testMethod.annot(matrixAttribute().noName()) .arg(Map.class, String.class, String.class); @SuppressWarnings("unchecked") Map<String, String> map = (Map<String, String>) this.resolver.resolveArgument(param, this.mavContainer, this.webRequest, null); assertEquals(Collections.emptyMap(), map); }
@Test public void resolveArgumentNoMatch() throws Exception { MultiValueMap<String, String> params = getVariablesFor("cars"); params.add("anotherYear", "2012"); MethodParameter param = this.testMethod.annot(matrixAttribute().name("year")).arg(int.class); assertEquals("2013", this.resolver.resolveArgument(param, this.mavContainer, this.webRequest, null)); }
@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 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 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 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 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 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 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 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()); }
@Test public void resolveEmptyValueRequiredWithoutDefault() throws Exception { request.addParameter("name", ""); MethodParameter param = this.testMethod.annot(requestParam().notRequired()).arg(String.class); Object result = resolver.resolveArgument(param, null, webRequest, null); assertEquals("", 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 // 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 personRequired() { MethodParameter param = this.testMethod.annot(requestPart()).arg(Person.class); ServerWebExchange exchange = createExchange(new MultipartBodyBuilder()); Mono<Object> result = this.resolver.resolveArgument(param, new BindingContext(), exchange); StepVerifier.create(result).expectError(ServerWebInputException.class).verify(); }
@Test public void personNotRequired() { MethodParameter param = this.testMethod.annot(requestPart().notRequired()).arg(Person.class); ServerWebExchange exchange = createExchange(new MultipartBodyBuilder()); Mono<Object> result = this.resolver.resolveArgument(param, new BindingContext(), exchange); StepVerifier.create(result).verifyComplete(); }