Search in sources :

Example 31 with MultiValueMap

use of org.springframework.util.MultiValueMap in project spring-framework by spring-projects.

the class RequestParamMapMethodArgumentResolverTests method resolveMultiValueMapArgument.

@Test
public void resolveMultiValueMapArgument() throws Exception {
    String name = "foo";
    String value1 = "bar";
    String value2 = "baz";
    request.addParameter(name, value1, value2);
    MultiValueMap<String, String> expected = new LinkedMultiValueMap<>(1);
    expected.add(name, value1);
    expected.add(name, value2);
    MethodParameter param = this.testMethod.annotPresent(RequestParam.class).arg(MultiValueMap.class);
    Object result = resolver.resolveArgument(param, null, webRequest, null);
    assertTrue(result instanceof MultiValueMap);
    assertEquals("Invalid result", expected, result);
}
Also used : RequestParam(org.springframework.web.bind.annotation.RequestParam) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) MethodParameter(org.springframework.core.MethodParameter) MultiValueMap(org.springframework.util.MultiValueMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) Test(org.junit.Test)

Example 32 with MultiValueMap

use of org.springframework.util.MultiValueMap in project spring-framework by spring-projects.

the class BodyExtractorsTests method toFormData.

@Test
public void toFormData() throws Exception {
    BodyExtractor<Mono<MultiValueMap<String, String>>, ServerHttpRequest> extractor = BodyExtractors.toFormData();
    DefaultDataBufferFactory factory = new DefaultDataBufferFactory();
    DefaultDataBuffer dataBuffer = factory.wrap(ByteBuffer.wrap("name+1=value+1&name+2=value+2%2B1&name+2=value+2%2B2&name+3".getBytes(StandardCharsets.UTF_8)));
    Flux<DataBuffer> body = Flux.just(dataBuffer);
    MockServerHttpRequest request = MockServerHttpRequest.post("/").contentType(MediaType.APPLICATION_FORM_URLENCODED).body(body);
    Mono<MultiValueMap<String, String>> result = extractor.extract(request, this.context);
    StepVerifier.create(result).consumeNextWith(form -> {
        assertEquals("Invalid result", 3, form.size());
        assertEquals("Invalid result", "value 1", form.getFirst("name 1"));
        List<String> values = form.get("name 2");
        assertEquals("Invalid result", 2, values.size());
        assertEquals("Invalid result", "value 2+1", values.get(0));
        assertEquals("Invalid result", "value 2+2", values.get(1));
        assertNull("Invalid result", form.getFirst("name 3"));
    }).expectComplete().verify();
}
Also used : DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) MockServerHttpRequest(org.springframework.mock.http.server.reactive.test.MockServerHttpRequest) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) Mono(reactor.core.publisher.Mono) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) MockServerHttpRequest(org.springframework.mock.http.server.reactive.test.MockServerHttpRequest) MultiValueMap(org.springframework.util.MultiValueMap) DefaultDataBuffer(org.springframework.core.io.buffer.DefaultDataBuffer) DataBuffer(org.springframework.core.io.buffer.DataBuffer) Test(org.junit.Test)

Example 33 with MultiValueMap

use of org.springframework.util.MultiValueMap in project spring-framework by spring-projects.

the class BodyInsertersTests method ofFormData.

@Test
public void ofFormData() throws Exception {
    MultiValueMap<String, String> body = new LinkedMultiValueMap<>();
    body.set("name 1", "value 1");
    body.add("name 2", "value 2+1");
    body.add("name 2", "value 2+2");
    body.add("name 3", null);
    BodyInserter<MultiValueMap<String, String>, ClientHttpRequest> inserter = BodyInserters.fromFormData(body);
    MockClientHttpRequest request = new MockClientHttpRequest(HttpMethod.GET, URI.create("http://example.com"));
    Mono<Void> result = inserter.insert(request, this.context);
    StepVerifier.create(result).expectComplete().verify();
    StepVerifier.create(request.getBody()).consumeNextWith(dataBuffer -> {
        byte[] resultBytes = new byte[dataBuffer.readableByteCount()];
        dataBuffer.read(resultBytes);
        assertArrayEquals("name+1=value+1&name+2=value+2%2B1&name+2=value+2%2B2&name+3".getBytes(StandardCharsets.UTF_8), resultBytes);
    }).expectComplete().verify();
}
Also used : LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) MockClientHttpRequest(org.springframework.mock.http.client.reactive.test.MockClientHttpRequest) ClientHttpRequest(org.springframework.http.client.reactive.ClientHttpRequest) MockClientHttpRequest(org.springframework.mock.http.client.reactive.test.MockClientHttpRequest) MultiValueMap(org.springframework.util.MultiValueMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) Test(org.junit.Test)

Example 34 with MultiValueMap

use of org.springframework.util.MultiValueMap in project spring-framework by spring-projects.

the class RequestResponseBodyMethodProcessorTests method resolveArgumentRawTypeFromParameterizedType.

@Test
public void resolveArgumentRawTypeFromParameterizedType() throws Exception {
    String content = "fruit=apple&vegetable=kale";
    this.servletRequest.setMethod("GET");
    this.servletRequest.setContent(content.getBytes("UTF-8"));
    this.servletRequest.setContentType(MediaType.APPLICATION_FORM_URLENCODED_VALUE);
    List<HttpMessageConverter<?>> converters = new ArrayList<>();
    converters.add(new AllEncompassingFormHttpMessageConverter());
    RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters);
    @SuppressWarnings("unchecked") MultiValueMap<String, String> result = (MultiValueMap<String, String>) processor.resolveArgument(paramMultiValueMap, container, request, factory);
    assertNotNull(result);
    assertEquals("apple", result.getFirst("fruit"));
    assertEquals("kale", result.getFirst("vegetable"));
}
Also used : ArrayList(java.util.ArrayList) AllEncompassingFormHttpMessageConverter(org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter) ResourceHttpMessageConverter(org.springframework.http.converter.ResourceHttpMessageConverter) MappingJackson2XmlHttpMessageConverter(org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) ByteArrayHttpMessageConverter(org.springframework.http.converter.ByteArrayHttpMessageConverter) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) AllEncompassingFormHttpMessageConverter(org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter) MultiValueMap(org.springframework.util.MultiValueMap) Test(org.junit.Test)

Example 35 with MultiValueMap

use of org.springframework.util.MultiValueMap in project spring-framework by spring-projects.

the class WebMvcStompWebSocketEndpointRegistrationTests method handshakeHandlerAndInterceptor.

@Test
public void handshakeHandlerAndInterceptor() {
    WebMvcStompWebSocketEndpointRegistration registration = new WebMvcStompWebSocketEndpointRegistration(new String[] { "/foo" }, this.handler, this.scheduler);
    DefaultHandshakeHandler handshakeHandler = new DefaultHandshakeHandler();
    HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();
    registration.setHandshakeHandler(handshakeHandler).addInterceptors(interceptor);
    MultiValueMap<HttpRequestHandler, String> mappings = registration.getMappings();
    assertEquals(1, mappings.size());
    Map.Entry<HttpRequestHandler, List<String>> entry = mappings.entrySet().iterator().next();
    assertEquals(Arrays.asList("/foo"), entry.getValue());
    WebSocketHttpRequestHandler requestHandler = (WebSocketHttpRequestHandler) entry.getKey();
    assertNotNull(requestHandler.getWebSocketHandler());
    assertSame(handshakeHandler, requestHandler.getHandshakeHandler());
    assertEquals(2, requestHandler.getHandshakeInterceptors().size());
    assertEquals(interceptor, requestHandler.getHandshakeInterceptors().get(0));
    assertEquals(OriginHandshakeInterceptor.class, requestHandler.getHandshakeInterceptors().get(1).getClass());
}
Also used : HttpSessionHandshakeInterceptor(org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor) WebSocketHttpRequestHandler(org.springframework.web.socket.server.support.WebSocketHttpRequestHandler) SockJsHttpRequestHandler(org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler) HttpRequestHandler(org.springframework.web.HttpRequestHandler) List(java.util.List) DefaultHandshakeHandler(org.springframework.web.socket.server.support.DefaultHandshakeHandler) MultiValueMap(org.springframework.util.MultiValueMap) Map(java.util.Map) WebSocketHttpRequestHandler(org.springframework.web.socket.server.support.WebSocketHttpRequestHandler) Test(org.junit.Test)

Aggregations

MultiValueMap (org.springframework.util.MultiValueMap)48 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)34 Test (org.junit.Test)18 Map (java.util.Map)17 HttpHeaders (org.springframework.http.HttpHeaders)17 List (java.util.List)10 HttpEntity (org.springframework.http.HttpEntity)9 RestTemplate (org.springframework.web.client.RestTemplate)7 URI (java.net.URI)4 LinkedHashMap (java.util.LinkedHashMap)4 DefaultOAuth2AccessToken (org.springframework.security.oauth2.common.DefaultOAuth2AccessToken)4 OAuth2AccessToken (org.springframework.security.oauth2.common.OAuth2AccessToken)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 ResponseEntity (org.springframework.http.ResponseEntity)3 HttpRequestHandler (org.springframework.web.HttpRequestHandler)3 DefaultHandshakeHandler (org.springframework.web.socket.server.support.DefaultHandshakeHandler)3 HttpSessionHandshakeInterceptor (org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor)3 WebSocketHttpRequestHandler (org.springframework.web.socket.server.support.WebSocketHttpRequestHandler)3 SockJsHttpRequestHandler (org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler)3