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());
}
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());
}
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();
}
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();
}
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);
}
Aggregations