Search in sources :

Example 66 with MockServerWebExchange

use of org.springframework.web.testfixture.server.MockServerWebExchange in project spring-framework by spring-projects.

the class ResponseEntityResultHandlerTests method handleMonoWithWildcardBodyTypeAndNullBody.

// SPR-14877
@Test
public void handleMonoWithWildcardBodyTypeAndNullBody() throws Exception {
    MockServerWebExchange exchange = MockServerWebExchange.from(get("/path"));
    exchange.getAttributes().put(PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE, Collections.singleton(APPLICATION_JSON));
    MethodParameter returnType = on(TestController.class).resolveReturnType(Mono.class, ResponseEntity.class);
    HandlerResult result = new HandlerResult(new TestController(), Mono.just(notFound().build()), returnType);
    this.resultHandler.handleResult(exchange, result).block(Duration.ofSeconds(5));
    assertThat(exchange.getResponse().getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
    assertResponseBodyIsEmpty(exchange);
}
Also used : MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) HandlerResult(org.springframework.web.reactive.HandlerResult) MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.jupiter.api.Test)

Example 67 with MockServerWebExchange

use of org.springframework.web.testfixture.server.MockServerWebExchange in project spring-framework by spring-projects.

the class ResponseEntityResultHandlerTests method responseEntityStatusCode.

@Test
public void responseEntityStatusCode() throws Exception {
    ResponseEntity<Void> value = ResponseEntity.noContent().build();
    MethodParameter returnType = on(TestController.class).resolveReturnType(entity(Void.class));
    HandlerResult result = handlerResult(value, returnType);
    MockServerWebExchange exchange = MockServerWebExchange.from(get("/path"));
    this.resultHandler.handleResult(exchange, result).block(Duration.ofSeconds(5));
    assertThat(exchange.getResponse().getStatusCode()).isEqualTo(HttpStatus.NO_CONTENT);
    assertThat(exchange.getResponse().getHeaders().size()).isEqualTo(0);
    assertResponseBodyIsEmpty(exchange);
}
Also used : HandlerResult(org.springframework.web.reactive.HandlerResult) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.jupiter.api.Test)

Example 68 with MockServerWebExchange

use of org.springframework.web.testfixture.server.MockServerWebExchange in project spring-framework by spring-projects.

the class ResponseEntityResultHandlerTests method handleReturnValueLastModified.

@Test
public void handleReturnValueLastModified() throws Exception {
    Instant currentTime = Instant.now().truncatedTo(ChronoUnit.SECONDS);
    Instant oneMinAgo = currentTime.minusSeconds(60);
    long timestamp = currentTime.toEpochMilli();
    MockServerWebExchange exchange = MockServerWebExchange.from(get("/path").ifModifiedSince(timestamp));
    ResponseEntity<String> entity = ResponseEntity.ok().lastModified(oneMinAgo.toEpochMilli()).body("body");
    MethodParameter returnType = on(TestController.class).resolveReturnType(entity(String.class));
    HandlerResult result = handlerResult(entity, returnType);
    this.resultHandler.handleResult(exchange, result).block(Duration.ofSeconds(5));
    assertConditionalResponse(exchange, HttpStatus.NOT_MODIFIED, null, null, oneMinAgo);
}
Also used : Instant(java.time.Instant) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) HandlerResult(org.springframework.web.reactive.HandlerResult) MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.jupiter.api.Test)

Example 69 with MockServerWebExchange

use of org.springframework.web.testfixture.server.MockServerWebExchange in project spring-framework by spring-projects.

the class ResponseEntityResultHandlerTests method malformedAcceptHeader.

// gh-24539
@Test
public void malformedAcceptHeader() {
    ResponseEntity<String> value = ResponseEntity.badRequest().body("Foo");
    MethodParameter returnType = on(TestController.class).resolveReturnType(entity(String.class));
    HandlerResult result = handlerResult(value, returnType);
    MockServerWebExchange exchange = MockServerWebExchange.from(get("/path").header("Accept", "null"));
    this.resultHandler.handleResult(exchange, result).block(Duration.ofSeconds(5));
    MockServerHttpResponse response = exchange.getResponse();
    response.setComplete().block();
    assertThat(response.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST);
    assertThat(response.getHeaders().getContentType()).isNull();
    assertResponseBodyIsEmpty(exchange);
}
Also used : HandlerResult(org.springframework.web.reactive.HandlerResult) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) MethodParameter(org.springframework.core.MethodParameter) MockServerHttpResponse(org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse) Test(org.junit.jupiter.api.Test)

Example 70 with MockServerWebExchange

use of org.springframework.web.testfixture.server.MockServerWebExchange in project spring-framework by spring-projects.

the class ResponseEntityResultHandlerTests method httpHeaders.

@Test
public void httpHeaders() throws Exception {
    HttpHeaders headers = new HttpHeaders();
    headers.setAllow(new LinkedHashSet<>(Arrays.asList(HttpMethod.GET, HttpMethod.POST, HttpMethod.OPTIONS)));
    MethodParameter returnType = on(TestController.class).resolveReturnType(entity(Void.class));
    HandlerResult result = handlerResult(headers, returnType);
    MockServerWebExchange exchange = MockServerWebExchange.from(get("/path"));
    this.resultHandler.handleResult(exchange, result).block(Duration.ofSeconds(5));
    assertThat(exchange.getResponse().getStatusCode()).isEqualTo(HttpStatus.OK);
    assertThat(exchange.getResponse().getHeaders().size()).isEqualTo(1);
    assertThat(exchange.getResponse().getHeaders().getFirst("Allow")).isEqualTo("GET,POST,OPTIONS");
    assertResponseBodyIsEmpty(exchange);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HandlerResult(org.springframework.web.reactive.HandlerResult) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.jupiter.api.Test)

Aggregations

MockServerWebExchange (org.springframework.web.testfixture.server.MockServerWebExchange)224 Test (org.junit.jupiter.api.Test)216 MockServerHttpRequest (org.springframework.web.testfixture.http.server.reactive.MockServerHttpRequest)61 ClassPathResource (org.springframework.core.io.ClassPathResource)26 HttpHeaders (org.springframework.http.HttpHeaders)25 Resource (org.springframework.core.io.Resource)24 HandlerResult (org.springframework.web.reactive.HandlerResult)23 MethodParameter (org.springframework.core.MethodParameter)22 MockServerHttpResponse (org.springframework.web.testfixture.http.server.reactive.MockServerHttpResponse)22 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)19 MediaType (org.springframework.http.MediaType)19 Mono (reactor.core.publisher.Mono)18 BeforeEach (org.junit.jupiter.api.BeforeEach)17 HttpMethod (org.springframework.http.HttpMethod)15 StepVerifier (reactor.test.StepVerifier)13 Arrays (java.util.Arrays)12 Collections (java.util.Collections)12 List (java.util.List)12 HttpStatus (org.springframework.http.HttpStatus)12 IOException (java.io.IOException)10