Search in sources :

Example 26 with ServerHttpResponse

use of org.springframework.http.server.reactive.ServerHttpResponse in project spring-framework by spring-projects.

the class DefaultCorsProcessorTests method actualRequestWithOriginHeaderAndAllowedOrigin.

@Test
public void actualRequestWithOriginHeaderAndAllowedOrigin() throws Exception {
    ServerWebExchange exchange = actualRequest();
    this.conf.addAllowedOrigin("*");
    this.processor.processRequest(this.conf, exchange);
    ServerHttpResponse response = exchange.getResponse();
    assertTrue(response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN));
    assertEquals("*", response.getHeaders().getFirst(ACCESS_CONTROL_ALLOW_ORIGIN));
    assertFalse(response.getHeaders().containsKey(HttpHeaders.ACCESS_CONTROL_MAX_AGE));
    assertFalse(response.getHeaders().containsKey(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS));
    assertNull(response.getStatusCode());
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) Test(org.junit.Test)

Example 27 with ServerHttpResponse

use of org.springframework.http.server.reactive.ServerHttpResponse in project spring-framework by spring-projects.

the class DefaultCorsProcessorTests method actualRequestCredentialsWithOriginWildcard.

@Test
public void actualRequestCredentialsWithOriginWildcard() throws Exception {
    ServerWebExchange exchange = actualRequest();
    this.conf.addAllowedOrigin("*");
    this.conf.setAllowCredentials(true);
    this.processor.processRequest(this.conf, exchange);
    ServerHttpResponse response = exchange.getResponse();
    assertTrue(response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN));
    assertEquals("http://domain2.com", response.getHeaders().getFirst(ACCESS_CONTROL_ALLOW_ORIGIN));
    assertTrue(response.getHeaders().containsKey(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS));
    assertEquals("true", response.getHeaders().getFirst(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS));
    assertNull(response.getStatusCode());
}
Also used : ServerWebExchange(org.springframework.web.server.ServerWebExchange) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) Test(org.junit.Test)

Example 28 with ServerHttpResponse

use of org.springframework.http.server.reactive.ServerHttpResponse in project spring-framework by spring-projects.

the class TomcatRequestUpgradeStrategy method upgrade.

@Override
public Mono<Void> upgrade(ServerWebExchange exchange, WebSocketHandler handler, Optional<String> subProtocol) {
    ServerHttpRequest request = exchange.getRequest();
    ServerHttpResponse response = exchange.getResponse();
    HttpServletRequest servletRequest = getHttpServletRequest(request);
    HttpServletResponse servletResponse = getHttpServletResponse(response);
    Endpoint endpoint = new StandardWebSocketHandlerAdapter(handler, session -> {
        HandshakeInfo info = getHandshakeInfo(exchange, subProtocol);
        DataBufferFactory factory = response.bufferFactory();
        return new StandardWebSocketSession(session, info, factory);
    });
    String requestURI = servletRequest.getRequestURI();
    DefaultServerEndpointConfig config = new DefaultServerEndpointConfig(requestURI, endpoint);
    config.setSubprotocols(subProtocol.map(Collections::singletonList).orElse(Collections.emptyList()));
    try {
        WsServerContainer container = getContainer(servletRequest);
        container.doUpgrade(servletRequest, servletResponse, config, Collections.emptyMap());
    } catch (ServletException | IOException ex) {
        return Mono.error(ex);
    }
    return Mono.empty();
}
Also used : WsServerContainer(org.apache.tomcat.websocket.server.WsServerContainer) StandardWebSocketHandlerAdapter(org.springframework.web.reactive.socket.adapter.StandardWebSocketHandlerAdapter) ServletServerHttpRequest(org.springframework.http.server.reactive.ServletServerHttpRequest) ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) ServletServerHttpResponse(org.springframework.http.server.reactive.ServletServerHttpResponse) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Endpoint(javax.websocket.Endpoint) StandardWebSocketSession(org.springframework.web.reactive.socket.adapter.StandardWebSocketSession) Collections(java.util.Collections) DataBufferFactory(org.springframework.core.io.buffer.DataBufferFactory) HandshakeInfo(org.springframework.web.reactive.socket.HandshakeInfo)

Example 29 with ServerHttpResponse

use of org.springframework.http.server.reactive.ServerHttpResponse in project spring-framework by spring-projects.

the class BodyInsertersTests method ofServerSentEventClass.

@Test
public void ofServerSentEventClass() throws Exception {
    Flux<String> body = Flux.just("foo");
    BodyInserter<Flux<String>, ServerHttpResponse> inserter = BodyInserters.fromServerSentEvents(body, String.class);
    MockServerHttpResponse response = new MockServerHttpResponse();
    Mono<Void> result = inserter.insert(response, this.context);
    StepVerifier.create(result).expectNextCount(0).expectComplete().verify();
}
Also used : Flux(reactor.core.publisher.Flux) MockServerHttpResponse(org.springframework.mock.http.server.reactive.test.MockServerHttpResponse) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) MockServerHttpResponse(org.springframework.mock.http.server.reactive.test.MockServerHttpResponse) Test(org.junit.Test)

Example 30 with ServerHttpResponse

use of org.springframework.http.server.reactive.ServerHttpResponse in project spring-framework by spring-projects.

the class DefaultCorsProcessor method processRequest.

@Override
public boolean processRequest(CorsConfiguration config, ServerWebExchange exchange) {
    ServerHttpRequest request = exchange.getRequest();
    ServerHttpResponse response = exchange.getResponse();
    if (!CorsUtils.isCorsRequest(request)) {
        return true;
    }
    if (responseHasCors(response)) {
        logger.debug("Skip CORS: response already contains \"Access-Control-Allow-Origin\" header");
        return true;
    }
    if (CorsUtils.isSameOrigin(request)) {
        logger.debug("Skip CORS: request is from same origin");
        return true;
    }
    boolean preFlightRequest = CorsUtils.isPreFlightRequest(request);
    if (config == null) {
        if (preFlightRequest) {
            rejectRequest(response);
            return false;
        } else {
            return true;
        }
    }
    return handleInternal(exchange, config, preFlightRequest);
}
Also used : ServerHttpRequest(org.springframework.http.server.reactive.ServerHttpRequest) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse)

Aggregations

ServerHttpResponse (org.springframework.http.server.reactive.ServerHttpResponse)30 Test (org.junit.Test)21 ServerWebExchange (org.springframework.web.server.ServerWebExchange)20 ServerHttpRequest (org.springframework.http.server.reactive.ServerHttpRequest)6 ResolvableType (org.springframework.core.ResolvableType)4 MediaType (org.springframework.http.MediaType)4 Flux (reactor.core.publisher.Flux)4 IOException (java.io.IOException)3 Collections (java.util.Collections)3 Mono (reactor.core.publisher.Mono)3 List (java.util.List)2 Map (java.util.Map)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 ReactiveAdapter (org.springframework.core.ReactiveAdapter)2 DataBufferFactory (org.springframework.core.io.buffer.DataBufferFactory)2 HttpHeaders (org.springframework.http.HttpHeaders)2 HttpMethod (org.springframework.http.HttpMethod)2 ServletServerHttpRequest (org.springframework.http.server.reactive.ServletServerHttpRequest)2 ServletServerHttpResponse (org.springframework.http.server.reactive.ServletServerHttpResponse)2