Search in sources :

Example 71 with MockServerWebExchange

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

the class ResponseEntityResultHandlerTests method handleReturnValueEtagInvalidIfNoneMatch.

// SPR-14559
@Test
public void handleReturnValueEtagInvalidIfNoneMatch() throws Exception {
    MockServerWebExchange exchange = MockServerWebExchange.from(get("/path").ifNoneMatch("unquoted"));
    ResponseEntity<String> entity = ResponseEntity.ok().eTag("\"deadb33f8badf00d\"").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));
    assertThat(exchange.getResponse().getStatusCode()).isEqualTo(HttpStatus.OK);
    assertResponseBody(exchange, "body");
}
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 72 with MockServerWebExchange

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

the class ResponseEntityResultHandlerTests method handleReturnValueETagAndLastModified.

@Test
public void handleReturnValueETagAndLastModified() throws Exception {
    String eTag = "\"deadb33f8badf00d\"";
    Instant currentTime = Instant.now().truncatedTo(ChronoUnit.SECONDS);
    Instant oneMinAgo = currentTime.minusSeconds(60);
    MockServerWebExchange exchange = MockServerWebExchange.from(get("/path").ifNoneMatch(eTag).ifModifiedSince(currentTime.toEpochMilli()));
    ResponseEntity<String> entity = ResponseEntity.ok().eTag(eTag).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, eTag, 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 73 with MockServerWebExchange

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

the class ResponseEntityResultHandlerTests method handleWithPresetContentType.

// SPR-17082
@Test
public void handleWithPresetContentType() {
    ResponseEntity<Void> value = ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).build();
    MethodParameter returnType = on(TestController.class).resolveReturnType(entity(Void.class));
    HandlerResult result = handlerResult(value, returnType);
    MockServerWebExchange exchange = MockServerWebExchange.from(get("/path"));
    exchange.getResponse().getHeaders().setContentType(MediaType.TEXT_PLAIN);
    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().getContentType()).isEqualTo(MediaType.APPLICATION_JSON);
    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 74 with MockServerWebExchange

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

the class ResponseEntityResultHandlerTests method handleWithObjectMapperByTypeRegistration.

// gh-26212
@Test
public void handleWithObjectMapperByTypeRegistration() {
    MediaType halFormsMediaType = MediaType.parseMediaType("application/prs.hal-forms+json");
    MediaType halMediaType = MediaType.parseMediaType("application/hal+json");
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.configure(SerializationFeature.INDENT_OUTPUT, true);
    Jackson2JsonEncoder encoder = new Jackson2JsonEncoder();
    encoder.registerObjectMappersForType(Person.class, map -> map.put(halMediaType, objectMapper));
    EncoderHttpMessageWriter<?> writer = new EncoderHttpMessageWriter<>(encoder);
    ResponseEntityResultHandler handler = new ResponseEntityResultHandler(Collections.singletonList(writer), new RequestedContentTypeResolverBuilder().build());
    MockServerWebExchange exchange = MockServerWebExchange.from(get("/path").header("Accept", halFormsMediaType + "," + halMediaType));
    ResponseEntity<Person> value = ResponseEntity.ok().body(new Person("Jason"));
    MethodParameter returnType = on(TestController.class).resolveReturnType(entity(Person.class));
    HandlerResult result = handlerResult(value, returnType);
    handler.handleResult(exchange, result).block();
    assertThat(exchange.getResponse().getHeaders().getContentType()).isEqualTo(halMediaType);
    assertThat(exchange.getResponse().getBodyAsString().block()).isEqualTo("{" + NEWLINE_SYSTEM_PROPERTY + "  \"name\" : \"Jason\"" + NEWLINE_SYSTEM_PROPERTY + "}");
}
Also used : MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) HandlerResult(org.springframework.web.reactive.HandlerResult) Jackson2JsonEncoder(org.springframework.http.codec.json.Jackson2JsonEncoder) RequestedContentTypeResolverBuilder(org.springframework.web.reactive.accept.RequestedContentTypeResolverBuilder) EncoderHttpMessageWriter(org.springframework.http.codec.EncoderHttpMessageWriter) MediaType(org.springframework.http.MediaType) MethodParameter(org.springframework.core.MethodParameter) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.jupiter.api.Test)

Example 75 with MockServerWebExchange

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

the class ResponseEntityResultHandlerTests method responseEntityHeaders.

@Test
public void responseEntityHeaders() throws Exception {
    URI location = new URI("/path");
    ResponseEntity<Void> value = ResponseEntity.created(location).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.CREATED);
    assertThat(exchange.getResponse().getHeaders().size()).isEqualTo(1);
    assertThat(exchange.getResponse().getHeaders().getLocation()).isEqualTo(location);
    assertResponseBodyIsEmpty(exchange);
}
Also used : HandlerResult(org.springframework.web.reactive.HandlerResult) MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) MethodParameter(org.springframework.core.MethodParameter) URI(java.net.URI) 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