Search in sources :

Example 36 with MultiValueMap

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

the class WebMvcStompWebSocketEndpointRegistrationTests method handshakeHandlerInterceptorWithSockJsServiceAndAllowedOrigins.

@Test
public void handshakeHandlerInterceptorWithSockJsServiceAndAllowedOrigins() {
    WebMvcStompWebSocketEndpointRegistration registration = new WebMvcStompWebSocketEndpointRegistration(new String[] { "/foo" }, this.handler, this.scheduler);
    DefaultHandshakeHandler handshakeHandler = new DefaultHandshakeHandler();
    HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();
    String origin = "http://mydomain.com";
    registration.setHandshakeHandler(handshakeHandler).addInterceptors(interceptor).setAllowedOrigins(origin).withSockJS();
    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());
    SockJsHttpRequestHandler requestHandler = (SockJsHttpRequestHandler) entry.getKey();
    assertNotNull(requestHandler.getWebSocketHandler());
    DefaultSockJsService sockJsService = (DefaultSockJsService) requestHandler.getSockJsService();
    assertNotNull(sockJsService);
    Map<TransportType, TransportHandler> handlers = sockJsService.getTransportHandlers();
    WebSocketTransportHandler transportHandler = (WebSocketTransportHandler) handlers.get(TransportType.WEBSOCKET);
    assertSame(handshakeHandler, transportHandler.getHandshakeHandler());
    assertEquals(2, sockJsService.getHandshakeInterceptors().size());
    assertEquals(interceptor, sockJsService.getHandshakeInterceptors().get(0));
    assertEquals(OriginHandshakeInterceptor.class, sockJsService.getHandshakeInterceptors().get(1).getClass());
    assertTrue(sockJsService.getAllowedOrigins().contains(origin));
}
Also used : WebSocketTransportHandler(org.springframework.web.socket.sockjs.transport.handler.WebSocketTransportHandler) TransportHandler(org.springframework.web.socket.sockjs.transport.TransportHandler) 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) DefaultHandshakeHandler(org.springframework.web.socket.server.support.DefaultHandshakeHandler) WebSocketTransportHandler(org.springframework.web.socket.sockjs.transport.handler.WebSocketTransportHandler) SockJsHttpRequestHandler(org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler) DefaultSockJsService(org.springframework.web.socket.sockjs.transport.handler.DefaultSockJsService) List(java.util.List) TransportType(org.springframework.web.socket.sockjs.transport.TransportType) MultiValueMap(org.springframework.util.MultiValueMap) Map(java.util.Map) Test(org.junit.Test)

Example 37 with MultiValueMap

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

the class WebMvcStompWebSocketEndpointRegistrationTests method handshakeHandlerInterceptorWithSockJsService.

@Test
public void handshakeHandlerInterceptorWithSockJsService() {
    WebMvcStompWebSocketEndpointRegistration registration = new WebMvcStompWebSocketEndpointRegistration(new String[] { "/foo" }, this.handler, this.scheduler);
    DefaultHandshakeHandler handshakeHandler = new DefaultHandshakeHandler();
    HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();
    registration.setHandshakeHandler(handshakeHandler).addInterceptors(interceptor).withSockJS();
    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());
    SockJsHttpRequestHandler requestHandler = (SockJsHttpRequestHandler) entry.getKey();
    assertNotNull(requestHandler.getWebSocketHandler());
    DefaultSockJsService sockJsService = (DefaultSockJsService) requestHandler.getSockJsService();
    assertNotNull(sockJsService);
    Map<TransportType, TransportHandler> handlers = sockJsService.getTransportHandlers();
    WebSocketTransportHandler transportHandler = (WebSocketTransportHandler) handlers.get(TransportType.WEBSOCKET);
    assertSame(handshakeHandler, transportHandler.getHandshakeHandler());
    assertEquals(2, sockJsService.getHandshakeInterceptors().size());
    assertEquals(interceptor, sockJsService.getHandshakeInterceptors().get(0));
    assertEquals(OriginHandshakeInterceptor.class, sockJsService.getHandshakeInterceptors().get(1).getClass());
}
Also used : WebSocketTransportHandler(org.springframework.web.socket.sockjs.transport.handler.WebSocketTransportHandler) TransportHandler(org.springframework.web.socket.sockjs.transport.TransportHandler) 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) DefaultHandshakeHandler(org.springframework.web.socket.server.support.DefaultHandshakeHandler) WebSocketTransportHandler(org.springframework.web.socket.sockjs.transport.handler.WebSocketTransportHandler) SockJsHttpRequestHandler(org.springframework.web.socket.sockjs.support.SockJsHttpRequestHandler) DefaultSockJsService(org.springframework.web.socket.sockjs.transport.handler.DefaultSockJsService) List(java.util.List) TransportType(org.springframework.web.socket.sockjs.transport.TransportType) MultiValueMap(org.springframework.util.MultiValueMap) Map(java.util.Map) Test(org.junit.Test)

Example 38 with MultiValueMap

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

the class RequestHeaderMapMethodArgumentResolverTests method resolveMultiValueMapArgument.

@Test
public void resolveMultiValueMapArgument() throws Exception {
    String name = "foo";
    String value1 = "bar";
    String value2 = "baz";
    ServerWebExchange exchange = MockServerHttpRequest.get("/").header(name, value1, value2).toExchange();
    MultiValueMap<String, String> expected = new LinkedMultiValueMap<>(1);
    expected.add(name, value1);
    expected.add(name, value2);
    Mono<Object> mono = resolver.resolveArgument(paramMultiValueMap, null, exchange);
    Object result = mono.block();
    assertTrue(result instanceof MultiValueMap);
    assertEquals("Invalid result", expected, result);
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) MultiValueMap(org.springframework.util.MultiValueMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) Test(org.junit.Test)

Example 39 with MultiValueMap

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

the class MatrixVariablesMapMethodArgumentResolverTests method setup.

@Before
public void setup() throws Exception {
    this.resolver = new MatrixVariableMapMethodArgumentResolver();
    this.mavContainer = new ModelAndViewContainer();
    this.request = new MockHttpServletRequest();
    this.webRequest = new ServletWebRequest(request, new MockHttpServletResponse());
    Map<String, MultiValueMap<String, String>> params = new LinkedHashMap<>();
    this.request.setAttribute(HandlerMapping.MATRIX_VARIABLES_ATTRIBUTE, params);
    Method method = getClass().getMethod("handle", String.class, Map.class, MultiValueMap.class, MultiValueMap.class, Map.class);
    this.paramString = new SynthesizingMethodParameter(method, 0);
    this.paramMap = new SynthesizingMethodParameter(method, 1);
    this.paramMultivalueMap = new SynthesizingMethodParameter(method, 2);
    this.paramMapForPathVar = new SynthesizingMethodParameter(method, 3);
    this.paramMapWithName = new SynthesizingMethodParameter(method, 4);
}
Also used : SynthesizingMethodParameter(org.springframework.core.annotation.SynthesizingMethodParameter) ModelAndViewContainer(org.springframework.web.method.support.ModelAndViewContainer) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) Method(java.lang.reflect.Method) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) MultiValueMap(org.springframework.util.MultiValueMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) LinkedHashMap(java.util.LinkedHashMap) Before(org.junit.Before)

Example 40 with MultiValueMap

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

the class MatrixVariablesMethodArgumentResolverTests method setup.

@Before
public void setup() throws Exception {
    this.resolver = new MatrixVariableMethodArgumentResolver();
    this.mavContainer = new ModelAndViewContainer();
    this.request = new MockHttpServletRequest();
    this.webRequest = new ServletWebRequest(request, new MockHttpServletResponse());
    Map<String, MultiValueMap<String, String>> params = new LinkedHashMap<>();
    this.request.setAttribute(HandlerMapping.MATRIX_VARIABLES_ATTRIBUTE, params);
    Method method = getClass().getMethod("handle", String.class, List.class, int.class);
    this.paramString = new MethodParameter(method, 0);
    this.paramColors = new MethodParameter(method, 1);
    this.paramColors.initParameterNameDiscovery(new LocalVariableTableParameterNameDiscoverer());
    this.paramYear = new MethodParameter(method, 2);
}
Also used : LocalVariableTableParameterNameDiscoverer(org.springframework.core.LocalVariableTableParameterNameDiscoverer) ModelAndViewContainer(org.springframework.web.method.support.ModelAndViewContainer) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) Method(java.lang.reflect.Method) MethodParameter(org.springframework.core.MethodParameter) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) MultiValueMap(org.springframework.util.MultiValueMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) LinkedHashMap(java.util.LinkedHashMap) Before(org.junit.Before)

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