diff --git a/spring-test/src/main/java/org/springframework/test/web/reactive/server/ExchangeResult.java b/spring-test/src/main/java/org/springframework/test/web/reactive/server/ExchangeResult.java index 544f46e8744c..4d229f598cb9 100644 --- a/spring-test/src/main/java/org/springframework/test/web/reactive/server/ExchangeResult.java +++ b/spring-test/src/main/java/org/springframework/test/web/reactive/server/ExchangeResult.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -50,6 +50,7 @@ * respectively. * * @author Rossen Stoyanchev + * @author Sam Brannen * @since 5.0 * @see EntityExchangeResult * @see FluxExchangeResult @@ -171,6 +172,8 @@ public byte[] getRequestBodyContent() { /** * Return the HTTP status code as an {@link HttpStatus} enum value. + * @throws IllegalArgumentException in case of an unknown HTTP status code + * @see #getRawStatusCode() */ public HttpStatus getStatus() { return this.response.getStatusCode(); @@ -248,13 +251,22 @@ public String toString() { "\n" + formatBody(getRequestHeaders().getContentType(), this.requestBody) + "\n" + "\n" + - "< " + getStatus() + " " + getStatus().getReasonPhrase() + "\n" + + "< " + formatStatus() + "\n" + "< " + formatHeaders(getResponseHeaders(), "\n< ") + "\n" + "\n" + formatBody(getResponseHeaders().getContentType(), this.responseBody) +"\n" + formatMockServerResult(); } + private String formatStatus() { + try { + return getStatus() + " " + getStatus().getReasonPhrase(); + } + catch (Exception ex) { + return Integer.toString(getRawStatusCode()); + } + } + private String formatHeaders(HttpHeaders headers, String delimiter) { return headers.entrySet().stream() .map(entry -> entry.getKey() + ": " + entry.getValue()) diff --git a/spring-test/src/test/java/org/springframework/test/web/reactive/server/StatusAssertionTests.java b/spring-test/src/test/java/org/springframework/test/web/reactive/server/StatusAssertionTests.java index f84c2fc75a11..70301b9e9050 100644 --- a/spring-test/src/test/java/org/springframework/test/web/reactive/server/StatusAssertionTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/reactive/server/StatusAssertionTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -36,6 +36,7 @@ * Unit tests for {@link StatusAssertions}. * * @author Rossen Stoyanchev + * @author Sam Brannen */ class StatusAssertionTests { @@ -56,9 +57,20 @@ void isEqualTo() { assertions.isEqualTo(408)); } - @Test // gh-23630 + @Test // gh-23630, gh-29283 void isEqualToWithCustomStatus() { - statusAssertions(600).isEqualTo(600); + StatusAssertions assertions = statusAssertions(600); + + // Success + assertions.isEqualTo(600); + + // Wrong status + assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> + assertions.isEqualTo(HttpStatus.REQUEST_TIMEOUT)); + + // Wrong status value + assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> + assertions.isEqualTo(408)); } @Test