This commit is contained in:
Stéphane Nicoll
2024-03-18 08:59:22 +01:00
parent 2b56ca08d4
commit 73a1c2d509
9 changed files with 58 additions and 55 deletions

View File

@@ -51,21 +51,21 @@ class UriAssertTests {
}
@Test
void matchesPattern() {
assertThat("/orders/1").matchesPattern("/orders/*");
void matchesAntPattern() {
assertThat("/orders/1").matchesAntPattern("/orders/*");
}
@Test
void matchesPatternWithNonValidPattern() {
void matchesAntPatternWithNonValidPattern() {
assertThatExceptionOfType(AssertionError.class)
.isThrownBy(() -> assertThat("/orders/1").matchesPattern("/orders/"))
.isThrownBy(() -> assertThat("/orders/1").matchesAntPattern("/orders/"))
.withMessage("'/orders/' is not an Ant-style path pattern");
}
@Test
void matchesPatternWithWrongValue() {
void matchesAntPatternWithWrongValue() {
assertThatExceptionOfType(AssertionError.class)
.isThrownBy(() -> assertThat("/orders/1").matchesPattern("/resources/*"))
.isThrownBy(() -> assertThat("/orders/1").matchesAntPattern("/resources/*"))
.withMessageContainingAll("Test URI", "/resources/*", "/orders/1");
}

View File

@@ -18,13 +18,13 @@ package org.springframework.test.web.servlet.assertj;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import org.junit.jupiter.api.Test;
import org.springframework.http.HttpHeaders;
import org.springframework.mock.web.MockHttpServletResponse;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
/**
@@ -34,12 +34,10 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
*/
public class AbstractMockHttpServletResponseAssertTests {
private MockHttpServletResponse response = new MockHttpServletResponse();
@Test
void hasForwardedUrl() {
String forwardedUrl = "https://example.com/42";
MockHttpServletResponse response = new MockHttpServletResponse();
response.setForwardedUrl(forwardedUrl);
assertThat(response).hasForwardedUrl(forwardedUrl);
}
@@ -47,6 +45,7 @@ public class AbstractMockHttpServletResponseAssertTests {
@Test
void hasForwardedUrlWithWrongValue() {
String forwardedUrl = "https://example.com/42";
MockHttpServletResponse response = new MockHttpServletResponse();
response.setForwardedUrl(forwardedUrl);
assertThatExceptionOfType(AssertionError.class)
.isThrownBy(() -> assertThat(response).hasForwardedUrl("another"))
@@ -56,6 +55,7 @@ public class AbstractMockHttpServletResponseAssertTests {
@Test
void hasRedirectedUrl() {
String redirectedUrl = "https://example.com/42";
MockHttpServletResponse response = new MockHttpServletResponse();
response.addHeader(HttpHeaders.LOCATION, redirectedUrl);
assertThat(response).hasRedirectedUrl(redirectedUrl);
}
@@ -63,6 +63,7 @@ public class AbstractMockHttpServletResponseAssertTests {
@Test
void hasRedirectedUrlWithWrongValue() {
String redirectedUrl = "https://example.com/42";
MockHttpServletResponse response = new MockHttpServletResponse();
response.addHeader(HttpHeaders.LOCATION, redirectedUrl);
assertThatExceptionOfType(AssertionError.class)
.isThrownBy(() -> assertThat(response).hasRedirectedUrl("another"))
@@ -71,15 +72,17 @@ public class AbstractMockHttpServletResponseAssertTests {
@Test
void bodyHasContent() throws UnsupportedEncodingException {
MockHttpServletResponse response = new MockHttpServletResponse();
response.getWriter().write("OK");
assertThat(response).body().asString().isEqualTo("OK");
}
@Test
void bodyHasContentWithResponseCharacterEncoding() throws UnsupportedEncodingException {
byte[] bytes = "OK".getBytes(UTF_8);
byte[] bytes = "OK".getBytes(StandardCharsets.UTF_8);
MockHttpServletResponse response = new MockHttpServletResponse();
response.getWriter().write("OK");
response.setContentType(UTF_8.name());
response.setContentType(StandardCharsets.UTF_8.name());
assertThat(response).body().isEqualTo(bytes);
}

View File

@@ -52,130 +52,130 @@ class CookieMapAssertTests {
@Test
void containsCookieWhenCookieExistsShouldPass() {
cookies().containsCookie("framework");
assertThat(cookies()).containsCookie("framework");
}
@Test
void containsCookieWhenCookieMissingShouldFail() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
cookies().containsCookie("missing"));
assertThat(cookies()).containsCookie("missing"));
}
@Test
void containsCookiesWhenCookiesExistShouldPass() {
cookies().containsCookies("framework", "age");
assertThat(cookies()).containsCookies("framework", "age");
}
@Test
void containsCookiesWhenCookieMissingShouldFail() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
cookies().containsCookies("framework", "missing"));
assertThat(cookies()).containsCookies("framework", "missing"));
}
@Test
void doesNotContainCookieWhenCookieMissingShouldPass() {
cookies().doesNotContainCookie("missing");
assertThat(cookies()).doesNotContainCookie("missing");
}
@Test
void doesNotContainCookieWhenCookieExistsShouldFail() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
cookies().doesNotContainCookie("framework"));
assertThat(cookies()).doesNotContainCookie("framework"));
}
@Test
void doesNotContainCookiesWhenCookiesMissingShouldPass() {
cookies().doesNotContainCookies("missing", "missing2");
assertThat(cookies()).doesNotContainCookies("missing", "missing2");
}
@Test
void doesNotContainCookiesWhenAtLeastOneCookieExistShouldFail() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
cookies().doesNotContainCookies("missing", "framework"));
assertThat(cookies()).doesNotContainCookies("missing", "framework"));
}
@Test
void hasValueEqualsWhenCookieValueMatchesShouldPass() {
cookies().hasValue("framework", "spring");
assertThat(cookies()).hasValue("framework", "spring");
}
@Test
void hasValueEqualsWhenCookieValueDiffersShouldFail() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
cookies().hasValue("framework", "other"));
assertThat(cookies()).hasValue("framework", "other"));
}
@Test
void hasCookieSatisfyingWhenCookieValueMatchesShouldPass() {
cookies().hasCookieSatisfying("framework", cookie ->
assertThat(cookies()).hasCookieSatisfying("framework", cookie ->
assertThat(cookie.getValue()).startsWith("spr"));
}
@Test
void hasCookieSatisfyingWhenCookieValueDiffersShouldFail() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
cookies().hasCookieSatisfying("framework", cookie ->
assertThat(cookies()).hasCookieSatisfying("framework", cookie ->
assertThat(cookie.getValue()).startsWith("not")));
}
@Test
void hasMaxAgeWhenCookieAgeMatchesShouldPass() {
cookies().hasMaxAge("age", Duration.ofMinutes(20));
assertThat(cookies()).hasMaxAge("age", Duration.ofMinutes(20));
}
@Test
void hasMaxAgeWhenCookieAgeDiffersShouldFail() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
cookies().hasMaxAge("age", Duration.ofMinutes(30)));
assertThat(cookies()).hasMaxAge("age", Duration.ofMinutes(30)));
}
@Test
void pathWhenCookiePathMatchesShouldPass() {
cookies().hasPath("path", "/spring");
assertThat(cookies()).hasPath("path", "/spring");
}
@Test
void pathWhenCookiePathDiffersShouldFail() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
cookies().hasPath("path", "/other"));
assertThat(cookies()).hasPath("path", "/other"));
}
@Test
void hasDomainWhenCookieDomainMatchesShouldPass() {
cookies().hasDomain("domain", "spring.io");
assertThat(cookies()).hasDomain("domain", "spring.io");
}
@Test
void hasDomainWhenCookieDomainDiffersShouldFail() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
cookies().hasDomain("domain", "example.org"));
assertThat(cookies()).hasDomain("domain", "example.org"));
}
@Test
void isSecureWhenCookieSecureMatchesShouldPass() {
cookies().isSecure("framework", true);
assertThat(cookies()).isSecure("framework", true);
}
@Test
void isSecureWhenCookieSecureDiffersShouldFail() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
cookies().isSecure("domain", true));
assertThat(cookies()).isSecure("domain", true));
}
@Test
void isHttpOnlyWhenCookieHttpOnlyMatchesShouldPass() {
cookies().isHttpOnly("framework", true);
assertThat(cookies()).isHttpOnly("framework", true);
}
@Test
void isHttpOnlyWhenCookieHttpOnlyDiffersShouldFail() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
cookies().isHttpOnly("domain", true));
assertThat(cookies()).isHttpOnly("domain", true));
}
private static CookieMapAssert cookies() {
return assertThat((AssertProvider<CookieMapAssert>) () -> new CookieMapAssert(cookies));
private static AssertProvider<CookieMapAssert> cookies() {
return () -> new CookieMapAssert(cookies);
}
}

View File

@@ -128,12 +128,12 @@ class HandlerResultAssertTests {
static class TestController {
@GetMapping("/greet")
public ResponseEntity<String> greet() {
ResponseEntity<String> greet() {
return ResponseEntity.ok().body("Hello");
}
@PostMapping("/update")
public void update() {
void update() {
}
}

View File

@@ -19,6 +19,7 @@ package org.springframework.test.web.servlet.assertj;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import org.assertj.core.api.AssertProvider;
import org.junit.jupiter.api.Test;
@@ -26,7 +27,6 @@ import org.junit.jupiter.api.Test;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.json.JsonContent;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.assertj.core.api.Assertions.assertThat;
/**
@@ -40,8 +40,8 @@ class ResponseBodyAssertTests {
@Test
void isEqualToWithByteArray() {
MockHttpServletResponse response = createResponse("hello");
response.setCharacterEncoding(UTF_8.name());
assertThat(fromResponse(response)).isEqualTo("hello".getBytes(UTF_8));
response.setCharacterEncoding(StandardCharsets.UTF_8.name());
assertThat(fromResponse(response)).isEqualTo("hello".getBytes(StandardCharsets.UTF_8));
}
@Test