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