|
|
|
@ -1,5 +1,5 @@ |
|
|
|
/* |
|
|
|
/* |
|
|
|
* Copyright 2002-2021 the original author or authors. |
|
|
|
* Copyright 2002-2024 the original author or authors. |
|
|
|
* |
|
|
|
* |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
@ -23,19 +23,19 @@ import java.util.Objects; |
|
|
|
import java.util.function.Consumer; |
|
|
|
import java.util.function.Consumer; |
|
|
|
|
|
|
|
|
|
|
|
import org.hamcrest.Matcher; |
|
|
|
import org.hamcrest.Matcher; |
|
|
|
import org.hamcrest.MatcherAssert; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import org.springframework.http.CacheControl; |
|
|
|
import org.springframework.http.CacheControl; |
|
|
|
import org.springframework.http.ContentDisposition; |
|
|
|
import org.springframework.http.ContentDisposition; |
|
|
|
import org.springframework.http.HttpHeaders; |
|
|
|
import org.springframework.http.HttpHeaders; |
|
|
|
import org.springframework.http.MediaType; |
|
|
|
import org.springframework.http.MediaType; |
|
|
|
import org.springframework.lang.Nullable; |
|
|
|
import org.springframework.lang.Nullable; |
|
|
|
import org.springframework.test.util.AssertionErrors; |
|
|
|
|
|
|
|
import org.springframework.util.CollectionUtils; |
|
|
|
import org.springframework.util.CollectionUtils; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import static org.hamcrest.MatcherAssert.assertThat; |
|
|
|
import static org.springframework.test.util.AssertionErrors.assertEquals; |
|
|
|
import static org.springframework.test.util.AssertionErrors.assertEquals; |
|
|
|
import static org.springframework.test.util.AssertionErrors.assertNotNull; |
|
|
|
import static org.springframework.test.util.AssertionErrors.assertNotNull; |
|
|
|
import static org.springframework.test.util.AssertionErrors.assertTrue; |
|
|
|
import static org.springframework.test.util.AssertionErrors.assertTrue; |
|
|
|
|
|
|
|
import static org.springframework.test.util.AssertionErrors.fail; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Assertions on headers of the response. |
|
|
|
* Assertions on headers of the response. |
|
|
|
@ -73,8 +73,8 @@ public class HeaderAssertions { |
|
|
|
public WebTestClient.ResponseSpec valueEquals(String headerName, long value) { |
|
|
|
public WebTestClient.ResponseSpec valueEquals(String headerName, long value) { |
|
|
|
String actual = getHeaders().getFirst(headerName); |
|
|
|
String actual = getHeaders().getFirst(headerName); |
|
|
|
this.exchangeResult.assertWithDiagnostics(() -> |
|
|
|
this.exchangeResult.assertWithDiagnostics(() -> |
|
|
|
assertTrue("Response does not contain header '" + headerName + "'", actual != null)); |
|
|
|
assertNotNull("Response does not contain header '" + headerName + "'", actual)); |
|
|
|
return assertHeader(headerName, value, Long.parseLong(Objects.requireNonNull(actual))); |
|
|
|
return assertHeader(headerName, value, Long.parseLong(actual)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
@ -94,7 +94,7 @@ public class HeaderAssertions { |
|
|
|
headers.setDate("expected", value); |
|
|
|
headers.setDate("expected", value); |
|
|
|
headers.set("actual", headerValue); |
|
|
|
headers.set("actual", headerValue); |
|
|
|
|
|
|
|
|
|
|
|
assertEquals("Response header '" + headerName + "'='" + headerValue + "' " + |
|
|
|
assertEquals(getMessage(headerName) + "='" + headerValue + "' " + |
|
|
|
"does not match expected value '" + headers.getFirst("expected") + "'", |
|
|
|
"does not match expected value '" + headers.getFirst("expected") + "'", |
|
|
|
headers.getFirstDate("expected"), headers.getFirstDate("actual")); |
|
|
|
headers.getFirstDate("expected"), headers.getFirstDate("actual")); |
|
|
|
}); |
|
|
|
}); |
|
|
|
@ -109,7 +109,7 @@ public class HeaderAssertions { |
|
|
|
public WebTestClient.ResponseSpec valueMatches(String name, String pattern) { |
|
|
|
public WebTestClient.ResponseSpec valueMatches(String name, String pattern) { |
|
|
|
String value = getRequiredValue(name); |
|
|
|
String value = getRequiredValue(name); |
|
|
|
String message = getMessage(name) + "=[" + value + "] does not match [" + pattern + "]"; |
|
|
|
String message = getMessage(name) + "=[" + value + "] does not match [" + pattern + "]"; |
|
|
|
this.exchangeResult.assertWithDiagnostics(() -> AssertionErrors.assertTrue(message, value.matches(pattern))); |
|
|
|
this.exchangeResult.assertWithDiagnostics(() -> assertTrue(message, value.matches(pattern))); |
|
|
|
return this.responseSpec; |
|
|
|
return this.responseSpec; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -123,16 +123,16 @@ public class HeaderAssertions { |
|
|
|
* @since 5.3 |
|
|
|
* @since 5.3 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public WebTestClient.ResponseSpec valuesMatch(String name, String... patterns) { |
|
|
|
public WebTestClient.ResponseSpec valuesMatch(String name, String... patterns) { |
|
|
|
|
|
|
|
List<String> values = getRequiredValues(name); |
|
|
|
this.exchangeResult.assertWithDiagnostics(() -> { |
|
|
|
this.exchangeResult.assertWithDiagnostics(() -> { |
|
|
|
List<String> values = getRequiredValues(name); |
|
|
|
assertTrue( |
|
|
|
AssertionErrors.assertTrue( |
|
|
|
|
|
|
|
getMessage(name) + " has fewer or more values " + values + |
|
|
|
getMessage(name) + " has fewer or more values " + values + |
|
|
|
" than number of patterns to match with " + Arrays.toString(patterns), |
|
|
|
" than number of patterns to match with " + Arrays.toString(patterns), |
|
|
|
values.size() == patterns.length); |
|
|
|
values.size() == patterns.length); |
|
|
|
for (int i = 0; i < values.size(); i++) { |
|
|
|
for (int i = 0; i < values.size(); i++) { |
|
|
|
String value = values.get(i); |
|
|
|
String value = values.get(i); |
|
|
|
String pattern = patterns[i]; |
|
|
|
String pattern = patterns[i]; |
|
|
|
AssertionErrors.assertTrue( |
|
|
|
assertTrue( |
|
|
|
getMessage(name) + "[" + i + "]='" + value + "' does not match '" + pattern + "'", |
|
|
|
getMessage(name) + "[" + i + "]='" + value + "' does not match '" + pattern + "'", |
|
|
|
value.matches(pattern)); |
|
|
|
value.matches(pattern)); |
|
|
|
} |
|
|
|
} |
|
|
|
@ -150,7 +150,7 @@ public class HeaderAssertions { |
|
|
|
String value = getHeaders().getFirst(name); |
|
|
|
String value = getHeaders().getFirst(name); |
|
|
|
this.exchangeResult.assertWithDiagnostics(() -> { |
|
|
|
this.exchangeResult.assertWithDiagnostics(() -> { |
|
|
|
String message = getMessage(name); |
|
|
|
String message = getMessage(name); |
|
|
|
MatcherAssert.assertThat(message, value, matcher); |
|
|
|
assertThat(message, value, matcher); |
|
|
|
}); |
|
|
|
}); |
|
|
|
return this.responseSpec; |
|
|
|
return this.responseSpec; |
|
|
|
} |
|
|
|
} |
|
|
|
@ -165,7 +165,7 @@ public class HeaderAssertions { |
|
|
|
List<String> values = getHeaders().get(name); |
|
|
|
List<String> values = getHeaders().get(name); |
|
|
|
this.exchangeResult.assertWithDiagnostics(() -> { |
|
|
|
this.exchangeResult.assertWithDiagnostics(() -> { |
|
|
|
String message = getMessage(name); |
|
|
|
String message = getMessage(name); |
|
|
|
MatcherAssert.assertThat(message, values, matcher); |
|
|
|
assertThat(message, values, matcher); |
|
|
|
}); |
|
|
|
}); |
|
|
|
return this.responseSpec; |
|
|
|
return this.responseSpec; |
|
|
|
} |
|
|
|
} |
|
|
|
@ -201,8 +201,7 @@ public class HeaderAssertions { |
|
|
|
private List<String> getRequiredValues(String name) { |
|
|
|
private List<String> getRequiredValues(String name) { |
|
|
|
List<String> values = getHeaders().get(name); |
|
|
|
List<String> values = getHeaders().get(name); |
|
|
|
if (CollectionUtils.isEmpty(values)) { |
|
|
|
if (CollectionUtils.isEmpty(values)) { |
|
|
|
this.exchangeResult.assertWithDiagnostics(() -> |
|
|
|
this.exchangeResult.assertWithDiagnostics(() -> fail(getMessage(name) + " not found")); |
|
|
|
AssertionErrors.fail(getMessage(name) + " not found")); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
return Objects.requireNonNull(values); |
|
|
|
return Objects.requireNonNull(values); |
|
|
|
} |
|
|
|
} |
|
|
|
@ -214,7 +213,7 @@ public class HeaderAssertions { |
|
|
|
public WebTestClient.ResponseSpec exists(String name) { |
|
|
|
public WebTestClient.ResponseSpec exists(String name) { |
|
|
|
if (!getHeaders().containsKey(name)) { |
|
|
|
if (!getHeaders().containsKey(name)) { |
|
|
|
String message = getMessage(name) + " does not exist"; |
|
|
|
String message = getMessage(name) + " does not exist"; |
|
|
|
this.exchangeResult.assertWithDiagnostics(() -> AssertionErrors.fail(message)); |
|
|
|
this.exchangeResult.assertWithDiagnostics(() -> fail(message)); |
|
|
|
} |
|
|
|
} |
|
|
|
return this.responseSpec; |
|
|
|
return this.responseSpec; |
|
|
|
} |
|
|
|
} |
|
|
|
@ -225,7 +224,7 @@ public class HeaderAssertions { |
|
|
|
public WebTestClient.ResponseSpec doesNotExist(String name) { |
|
|
|
public WebTestClient.ResponseSpec doesNotExist(String name) { |
|
|
|
if (getHeaders().containsKey(name)) { |
|
|
|
if (getHeaders().containsKey(name)) { |
|
|
|
String message = getMessage(name) + " exists with value=[" + getHeaders().getFirst(name) + "]"; |
|
|
|
String message = getMessage(name) + " exists with value=[" + getHeaders().getFirst(name) + "]"; |
|
|
|
this.exchangeResult.assertWithDiagnostics(() -> AssertionErrors.fail(message)); |
|
|
|
this.exchangeResult.assertWithDiagnostics(() -> fail(message)); |
|
|
|
} |
|
|
|
} |
|
|
|
return this.responseSpec; |
|
|
|
return this.responseSpec; |
|
|
|
} |
|
|
|
} |
|
|
|
@ -272,7 +271,7 @@ public class HeaderAssertions { |
|
|
|
MediaType actual = getHeaders().getContentType(); |
|
|
|
MediaType actual = getHeaders().getContentType(); |
|
|
|
String message = getMessage("Content-Type") + "=[" + actual + "] is not compatible with [" + mediaType + "]"; |
|
|
|
String message = getMessage("Content-Type") + "=[" + actual + "] is not compatible with [" + mediaType + "]"; |
|
|
|
this.exchangeResult.assertWithDiagnostics(() -> |
|
|
|
this.exchangeResult.assertWithDiagnostics(() -> |
|
|
|
AssertionErrors.assertTrue(message, (actual != null && actual.isCompatibleWith(mediaType)))); |
|
|
|
assertTrue(message, (actual != null && actual.isCompatibleWith(mediaType)))); |
|
|
|
return this.responseSpec; |
|
|
|
return this.responseSpec; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -310,16 +309,16 @@ public class HeaderAssertions { |
|
|
|
return this.exchangeResult.getResponseHeaders(); |
|
|
|
return this.exchangeResult.getResponseHeaders(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private String getMessage(String headerName) { |
|
|
|
|
|
|
|
return "Response header '" + headerName + "'"; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private WebTestClient.ResponseSpec assertHeader(String name, @Nullable Object expected, @Nullable Object actual) { |
|
|
|
private WebTestClient.ResponseSpec assertHeader(String name, @Nullable Object expected, @Nullable Object actual) { |
|
|
|
this.exchangeResult.assertWithDiagnostics(() -> { |
|
|
|
this.exchangeResult.assertWithDiagnostics(() -> { |
|
|
|
String message = getMessage(name); |
|
|
|
String message = getMessage(name); |
|
|
|
AssertionErrors.assertEquals(message, expected, actual); |
|
|
|
assertEquals(message, expected, actual); |
|
|
|
}); |
|
|
|
}); |
|
|
|
return this.responseSpec; |
|
|
|
return this.responseSpec; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static String getMessage(String headerName) { |
|
|
|
|
|
|
|
return "Response header '" + headerName + "'"; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|