Search in sources :

Example 66 with MockServerHttpRequest

use of org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest in project spring-framework by spring-projects.

the class DispatcherHandlerTests method preFlightRequest.

@Test
void preFlightRequest() {
    WebHandler webHandler = mock(WebHandler.class);
    HandlerMapping handlerMapping = mock(HandlerMapping.class);
    given((handlerMapping).getHandler(any())).willReturn(Mono.just(webHandler));
    StaticApplicationContext context = new StaticApplicationContext();
    context.registerBean("handlerMapping", HandlerMapping.class, () -> handlerMapping);
    context.registerBean(HandlerAdapter.class, SimpleHandlerAdapter::new);
    context.registerBean(HandlerResultHandler.class, StringHandlerResultHandler::new);
    context.refresh();
    MockServerHttpRequest request = MockServerHttpRequest.options("/").header(HttpHeaders.ORIGIN, "https://domain.com").header(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "GET").build();
    MockServerWebExchange exchange = MockServerWebExchange.from(request);
    new DispatcherHandler(context).handle(exchange).block(Duration.ofSeconds(0));
    verifyNoInteractions(webHandler);
}
Also used : StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) WebHandler(org.springframework.web.server.WebHandler) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) SimpleHandlerAdapter(org.springframework.web.reactive.result.SimpleHandlerAdapter) Test(org.junit.jupiter.api.Test)

Example 67 with MockServerHttpRequest

use of org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest in project spring-framework by spring-projects.

the class CorsUtilsTests method isSameOriginWithDifferentSchemes.

// SPR-16362
@Test
@SuppressWarnings("deprecation")
public void isSameOriginWithDifferentSchemes() {
    MockServerHttpRequest request = MockServerHttpRequest.get("http://mydomain1.example").header(HttpHeaders.ORIGIN, "https://mydomain1.example").build();
    assertThat(CorsUtils.isSameOrigin(request)).isFalse();
}
Also used : MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) Test(org.junit.jupiter.api.Test)

Example 68 with MockServerHttpRequest

use of org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest in project spring-framework by spring-projects.

the class DefaultCorsProcessorTests method preventDuplicatedVaryHeaders.

@Test
public void preventDuplicatedVaryHeaders() {
    MockServerHttpRequest request = MockServerHttpRequest.method(HttpMethod.GET, "http://domain1.example/test.html").header(HttpHeaders.ORIGIN, "http://domain1.example").build();
    ServerWebExchange exchange = MockServerWebExchange.from(request);
    ServerHttpResponse response = exchange.getResponse();
    HttpHeaders responseHeaders = response.getHeaders();
    responseHeaders.add(VARY, ORIGIN);
    responseHeaders.add(VARY, ACCESS_CONTROL_REQUEST_METHOD);
    responseHeaders.add(VARY, ACCESS_CONTROL_REQUEST_HEADERS);
    this.processor.process(this.conf, exchange);
    assertThat(responseHeaders.get(VARY)).containsOnlyOnce(ORIGIN, ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS);
}
Also used : MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) ServerWebExchange(org.springframework.web.server.ServerWebExchange) HttpHeaders(org.springframework.http.HttpHeaders) MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) ServerHttpResponse(org.springframework.http.server.reactive.ServerHttpResponse) Test(org.junit.jupiter.api.Test)

Example 69 with MockServerHttpRequest

use of org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest in project spring-framework by spring-projects.

the class HandshakeWebSocketServiceTests method sessionAttributePredicate.

@Test
public void sessionAttributePredicate() {
    MockWebSession session = new MockWebSession();
    session.getAttributes().put("a1", "v1");
    session.getAttributes().put("a2", "v2");
    session.getAttributes().put("a3", "v3");
    session.getAttributes().put("a4", "v4");
    session.getAttributes().put("a5", "v5");
    MockServerHttpRequest request = initHandshakeRequest();
    MockServerWebExchange exchange = MockServerWebExchange.builder(request).session(session).build();
    TestRequestUpgradeStrategy upgradeStrategy = new TestRequestUpgradeStrategy();
    HandshakeWebSocketService service = new HandshakeWebSocketService(upgradeStrategy);
    service.setSessionAttributePredicate(name -> Arrays.asList("a1", "a3", "a5").contains(name));
    service.handleRequest(exchange, mock(WebSocketHandler.class)).block();
    HandshakeInfo info = upgradeStrategy.handshakeInfo;
    assertThat(info).isNotNull();
    Map<String, Object> attributes = info.getAttributes();
    assertThat(attributes).hasSize(3).containsEntry("a1", "v1").containsEntry("a3", "v3").containsEntry("a5", "v5");
}
Also used : MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) MockWebSession(org.springframework.web.testfixture.server.MockWebSession) HandshakeInfo(org.springframework.web.reactive.socket.HandshakeInfo) Test(org.junit.jupiter.api.Test)

Example 70 with MockServerHttpRequest

use of org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest in project spring-framework by spring-projects.

the class DefaultPartHttpMessageReaderTests method noEndBoundary.

@ParameterizedDefaultPartHttpMessageReaderTest
public void noEndBoundary(String displayName, DefaultPartHttpMessageReader reader) {
    MockServerHttpRequest request = createRequest(new ClassPathResource("no-end-boundary.multipart", getClass()), "boundary");
    Flux<Part> result = reader.read(forClass(Part.class), request, emptyMap());
    StepVerifier.create(result).expectError(DecodingException.class).verify();
}
Also used : MockServerHttpRequest(org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest) DecodingException(org.springframework.core.codec.DecodingException) ClassPathResource(org.springframework.core.io.ClassPathResource)

Aggregations

MockServerHttpRequest (org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest)182 Test (org.junit.jupiter.api.Test)160 MockServerWebExchange (org.springframework.web.testfixture.server.MockServerWebExchange)101 Mono (reactor.core.publisher.Mono)52 DataBuffer (org.springframework.core.io.buffer.DataBuffer)51 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)50 Collections (java.util.Collections)48 StepVerifier (reactor.test.StepVerifier)46 HttpHeaders (org.springframework.http.HttpHeaders)39 Flux (reactor.core.publisher.Flux)37 MediaType (org.springframework.http.MediaType)34 ServerWebExchange (org.springframework.web.server.ServerWebExchange)34 MultiValueMap (org.springframework.util.MultiValueMap)33 HttpStatus (org.springframework.http.HttpStatus)31 StandardCharsets (java.nio.charset.StandardCharsets)29 MockServerHttpResponse (org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse)29 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)27 ClassPathResource (org.springframework.core.io.ClassPathResource)25 DefaultDataBuffer (org.springframework.core.io.buffer.DefaultDataBuffer)24 Optional (java.util.Optional)23