From dfa7a4c4d537fca6d47aad65a7b2a45a08d2716d Mon Sep 17 00:00:00 2001 From: Johnny Lim Date: Sun, 19 Jan 2025 23:04:57 +0900 Subject: [PATCH] Polish HttpHeadersAssert Signed-off-by: Johnny Lim --- .../test/http/HttpHeadersAssert.java | 36 ++++++++++--------- .../test/http/HttpHeadersAssertTests.java | 10 +++--- 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/test/http/HttpHeadersAssert.java b/spring-test/src/main/java/org/springframework/test/http/HttpHeadersAssert.java index 8e6552e4d35d..6e78dd2b7130 100644 --- a/spring-test/src/main/java/org/springframework/test/http/HttpHeadersAssert.java +++ b/spring-test/src/main/java/org/springframework/test/http/HttpHeadersAssert.java @@ -86,7 +86,7 @@ public HttpHeadersAssert containsHeaders(String... names) { */ public HttpHeadersAssert containsOnlyHeaders(String... names) { this.namesAssert - .as("check headers contains only HTTP headers '%s'", Arrays.toString(names)) + .as("check headers contain only HTTP headers '%s'", Arrays.toString(names)) .containsOnly(names); return this.myself; } @@ -98,7 +98,7 @@ public HttpHeadersAssert containsOnlyHeaders(String... names) { */ public HttpHeadersAssert doesNotContainHeader(String name) { this.namesAssert - .as("check headers does not contain HTTP header '%s'", name) + .as("check headers do not contain HTTP header '%s'", name) .doesNotContain(name); return this.myself; } @@ -111,7 +111,7 @@ public HttpHeadersAssert doesNotContainHeader(String name) { */ public HttpHeadersAssert doesNotContainHeaders(String... names) { this.namesAssert - .as("check headers does not contain HTTP headers '%s'", Arrays.toString(names)) + .as("check headers do not contain HTTP headers '%s'", Arrays.toString(names)) .doesNotContain(names); return this.myself; } @@ -119,15 +119,16 @@ public HttpHeadersAssert doesNotContainHeaders(String... names) { /** * Verify that the actual HTTP headers contain a header with the given * {@code name} that satisfies the given {@code valueRequirements}. - * @param name the name of an HTTP header that should not be present + * @param name the name of the header * @param valueRequirements the group of assertions to run against the * values of the header with the given name + * @since 7.0 */ @SuppressWarnings("unchecked") public HttpHeadersAssert hasHeaderSatisfying(String name, Consumer> valueRequirements) { containsHeader(name); Assertions.assertThat(this.actual.get(name)) - .as("check primary value for HTTP header '%s'", name) + .as("check all values for HTTP header '%s'", name) .satisfies(values -> valueRequirements.accept((List) values)); return this.myself; } @@ -135,7 +136,7 @@ public HttpHeadersAssert hasHeaderSatisfying(String name, Consumer> /** * Verify that the actual HTTP headers contain a header with the given * {@code name} and {@link String} primary {@code value}. - * @param name the name of the cookie + * @param name the name of the header * @param value the expected value of the header */ public HttpHeadersAssert hasValue(String name, String value) { @@ -148,8 +149,8 @@ public HttpHeadersAssert hasValue(String name, String value) { /** * Verify that the actual HTTP headers contain a header with the given - * {@code name} and {@link Long} primary {@code value}. - * @param name the name of the cookie + * {@code name} and {@code long} primary {@code value}. + * @param name the name of the header * @param value the expected value of the header */ public HttpHeadersAssert hasValue(String name, long value) { @@ -163,7 +164,7 @@ public HttpHeadersAssert hasValue(String name, long value) { /** * Verify that the actual HTTP headers contain a header with the given * {@code name} and {@link Instant} primary {@code value}. - * @param name the name of the cookie + * @param name the name of the header * @param value the expected value of the header */ public HttpHeadersAssert hasValue(String name, Instant value) { @@ -178,7 +179,7 @@ public HttpHeadersAssert hasValue(String name, Instant value) { * Verify that the actual HTTP headers contain a header with the given * {@code name} and {@link String} primary {@code value}. *

This assertion fails if the header has secondary values. - * @param name the name of the cookie + * @param name the name of the header * @param value the expected only value of the header * @since 7.0 */ @@ -189,9 +190,9 @@ public HttpHeadersAssert hasSingleValue(String name, String value) { /** * Verify that the actual HTTP headers contain a header with the given - * {@code name} and {@link Long} primary {@code value}. + * {@code name} and {@code long} primary {@code value}. *

This assertion fails if the header has secondary values. - * @param name the name of the cookie + * @param name the name of the header * @param value the expected value of the header * @since 7.0 */ @@ -204,7 +205,7 @@ public HttpHeadersAssert hasSingleValue(String name, long value) { * Verify that the actual HTTP headers contain a header with the given * {@code name} and {@link Instant} primary {@code value}. *

This assertion fails if the header has secondary values. - * @param name the name of the cookie + * @param name the name of the header * @param value the expected value of the header * @since 7.0 */ @@ -265,7 +266,7 @@ public HttpHeadersAssert isNotEmpty() { } /** - * Verify that there is exactly {@code expected} headers present, when + * Verify that there are exactly {@code expected} number of headers present, when * considering header names in a case-insensitive manner. * @param expected the expected number of headers */ @@ -293,10 +294,13 @@ public HttpHeadersAssert hasSameSizeAs(HttpHeaders other) { private HttpHeadersAssert doesNotHaveSecondaryValues(String name) { containsHeader(name); List values = this.actual.get(name); - int size = (values != null) ? values.size() : 0; + if (values == null || values.isEmpty()) { + return this.myself; + } + int size = values.size(); Assertions.assertThat(size) .withFailMessage("Expected HTTP header '%s' to be present " + - "without secondary values, but found <%s> secondary values", name, size - 1) + "without secondary values, but found <%s> secondary value(s)", name, size - 1) .isOne(); return this.myself; } diff --git a/spring-test/src/test/java/org/springframework/test/http/HttpHeadersAssertTests.java b/spring-test/src/test/java/org/springframework/test/http/HttpHeadersAssertTests.java index 195543acac06..37ec4e8e72cb 100644 --- a/spring-test/src/test/java/org/springframework/test/http/HttpHeadersAssertTests.java +++ b/spring-test/src/test/java/org/springframework/test/http/HttpHeadersAssertTests.java @@ -84,7 +84,7 @@ void containsOnlyHeadersWithMissingOne() { headers.add("name2", "value2"); assertThatExceptionOfType(AssertionError.class) .isThrownBy(() -> assertThat(headers).containsOnlyHeaders("name1", "name2", "name3")) - .withMessageContainingAll("check headers contains only HTTP headers", + .withMessageContainingAll("check headers contain only HTTP headers", "could not find the following element(s)", "[\"name3\"]"); } @@ -96,7 +96,7 @@ void containsOnlyHeadersWithExtraOne() { headers.add("name3", "value3"); assertThatExceptionOfType(AssertionError.class) .isThrownBy(() -> assertThat(headers).containsOnlyHeaders("name1", "name2")) - .withMessageContainingAll("check headers contains only HTTP headers", + .withMessageContainingAll("check headers contain only HTTP headers", "the following element(s) were unexpected", "[\"name3\"]"); } @@ -259,7 +259,7 @@ void hasSingleValueWithSecondaryValues() { assertThatExceptionOfType(AssertionError.class) .isThrownBy(() -> assertThat(headers).hasSingleValue("header", "first")) .withMessage("Expected HTTP header 'header' to be present without secondary values, " + - "but found <2> secondary values"); + "but found <2> secondary value(s)"); } @Test @@ -276,7 +276,7 @@ void hasSingleValueWithLongMatchButSecondaryValues() { assertThatExceptionOfType(AssertionError.class) .isThrownBy(() -> assertThat(headers).hasSingleValue("header", 123)) .withMessage("Expected HTTP header 'header' to be present without secondary values, " + - "but found <2> secondary values"); + "but found <2> secondary value(s)"); } @Test @@ -296,7 +296,7 @@ void hasSingleValueWithInstantAndSecondaryValues() { assertThatExceptionOfType(AssertionError.class) .isThrownBy(() -> assertThat(headers).hasSingleValue("header", instant.minusSeconds(30))) .withMessage("Expected HTTP header 'header' to be present without secondary values, " + - "but found <1> secondary values"); + "but found <1> secondary value(s)"); } @Test