Migrate Hamcrest assertions to AssertJ

Migrate all existing `assertThat(..., Matcher)` assertions to AssertJ
and add checkstyle rules to ensure they don't return.

See gh-23022
This commit is contained in:
Phillip Webb
2019-05-23 15:48:03 -07:00
parent 2294625cf0
commit 95a9d46a87
322 changed files with 4358 additions and 4814 deletions

View File

@@ -19,10 +19,10 @@ package org.springframework.http;
import java.time.Duration;
import java.util.concurrent.TimeUnit;
import org.hamcrest.Matchers;
import org.junit.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Brian Clozel
@@ -32,73 +32,73 @@ public class CacheControlTests {
@Test
public void emptyCacheControl() throws Exception {
CacheControl cc = CacheControl.empty();
assertThat(cc.getHeaderValue(), Matchers.nullValue());
assertThat(cc.getHeaderValue()).isNull();
}
@Test
public void maxAge() throws Exception {
CacheControl cc = CacheControl.maxAge(1, TimeUnit.HOURS);
assertThat(cc.getHeaderValue(), Matchers.equalTo("max-age=3600"));
assertThat(cc.getHeaderValue()).isEqualTo("max-age=3600");
}
@Test
public void maxAge_duration() throws Exception {
CacheControl cc = CacheControl.maxAge(Duration.ofHours(1));
assertThat(cc.getHeaderValue(), Matchers.equalTo("max-age=3600"));
assertThat(cc.getHeaderValue()).isEqualTo("max-age=3600");
}
@Test
public void maxAgeAndDirectives() throws Exception {
CacheControl cc = CacheControl.maxAge(3600, TimeUnit.SECONDS).cachePublic().noTransform();
assertThat(cc.getHeaderValue(), Matchers.equalTo("max-age=3600, no-transform, public"));
assertThat(cc.getHeaderValue()).isEqualTo("max-age=3600, no-transform, public");
}
@Test
public void maxAgeAndSMaxAge() throws Exception {
CacheControl cc = CacheControl.maxAge(1, TimeUnit.HOURS).sMaxAge(30, TimeUnit.MINUTES);
assertThat(cc.getHeaderValue(), Matchers.equalTo("max-age=3600, s-maxage=1800"));
assertThat(cc.getHeaderValue()).isEqualTo("max-age=3600, s-maxage=1800");
}
@Test
public void maxAgeAndSMaxAge_duration() throws Exception {
CacheControl cc = CacheControl.maxAge(Duration.ofHours(1)).sMaxAge(Duration.ofMinutes(30));
assertThat(cc.getHeaderValue(), Matchers.equalTo("max-age=3600, s-maxage=1800"));
assertThat(cc.getHeaderValue()).isEqualTo("max-age=3600, s-maxage=1800");
}
@Test
public void noCachePrivate() throws Exception {
CacheControl cc = CacheControl.noCache().cachePrivate();
assertThat(cc.getHeaderValue(), Matchers.equalTo("no-cache, private"));
assertThat(cc.getHeaderValue()).isEqualTo("no-cache, private");
}
@Test
public void noStore() throws Exception {
CacheControl cc = CacheControl.noStore();
assertThat(cc.getHeaderValue(), Matchers.equalTo("no-store"));
assertThat(cc.getHeaderValue()).isEqualTo("no-store");
}
@Test
public void staleIfError() throws Exception {
CacheControl cc = CacheControl.maxAge(1, TimeUnit.HOURS).staleIfError(2, TimeUnit.HOURS);
assertThat(cc.getHeaderValue(), Matchers.equalTo("max-age=3600, stale-if-error=7200"));
assertThat(cc.getHeaderValue()).isEqualTo("max-age=3600, stale-if-error=7200");
}
@Test
public void staleIfError_duration() throws Exception {
CacheControl cc = CacheControl.maxAge(Duration.ofHours(1)).staleIfError(2, TimeUnit.HOURS);
assertThat(cc.getHeaderValue(), Matchers.equalTo("max-age=3600, stale-if-error=7200"));
assertThat(cc.getHeaderValue()).isEqualTo("max-age=3600, stale-if-error=7200");
}
@Test
public void staleWhileRevalidate() throws Exception {
CacheControl cc = CacheControl.maxAge(1, TimeUnit.HOURS).staleWhileRevalidate(2, TimeUnit.HOURS);
assertThat(cc.getHeaderValue(), Matchers.equalTo("max-age=3600, stale-while-revalidate=7200"));
assertThat(cc.getHeaderValue()).isEqualTo("max-age=3600, stale-while-revalidate=7200");
}
@Test
public void staleWhileRevalidate_duration() throws Exception {
CacheControl cc = CacheControl.maxAge(Duration.ofHours(1)).staleWhileRevalidate(2, TimeUnit.HOURS);
assertThat(cc.getHeaderValue(), Matchers.equalTo("max-age=3600, stale-while-revalidate=7200"));
assertThat(cc.getHeaderValue()).isEqualTo("max-age=3600, stale-while-revalidate=7200");
}
}

View File

@@ -36,13 +36,8 @@ import java.util.TimeZone;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.emptyCollectionOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
@@ -67,22 +62,22 @@ public class HttpHeadersTests {
public void getOrEmpty() {
String key = "FOO";
assertThat(headers.get(key), is(nullValue()));
assertThat(headers.getOrEmpty(key), is(empty()));
assertThat(headers.get(key)).isNull();
assertThat(headers.getOrEmpty(key)).isEmpty();
headers.add(key, "bar");
assertThat(headers.getOrEmpty(key), is(Arrays.asList("bar")));
assertThat(headers.getOrEmpty(key)).containsExactly("bar");
headers.remove(key);
assertThat(headers.get(key), is(nullValue()));
assertThat(headers.getOrEmpty(key), is(empty()));
assertThat(headers.get(key)).isNull();
assertThat(headers.getOrEmpty(key)).isEmpty();
}
@Test
public void getFirst() {
headers.add(HttpHeaders.CACHE_CONTROL, "max-age=1000, public");
headers.add(HttpHeaders.CACHE_CONTROL, "s-maxage=1000");
assertThat(headers.getFirst(HttpHeaders.CACHE_CONTROL), is("max-age=1000, public"));
assertThat(headers.getFirst(HttpHeaders.CACHE_CONTROL)).isEqualTo("max-age=1000, public");
}
@Test
@@ -223,7 +218,7 @@ public class HttpHeadersTests {
headers.add(HttpHeaders.IF_MATCH, "W/\"v2,1\", \"v2,2\"");
assertEquals("Invalid If-Match header", "\"v2,0\"", headers.get(HttpHeaders.IF_MATCH).get(0));
assertEquals("Invalid If-Match header", "W/\"v2,1\", \"v2,2\"", headers.get(HttpHeaders.IF_MATCH).get(1));
assertThat(headers.getIfMatch(), contains("\"v2,0\"", "W/\"v2,1\"", "\"v2,2\""));
assertThat(headers.getIfMatch()).contains("\"v2,0\"", "W/\"v2,1\"", "\"v2,2\"");
}
@Test
@@ -250,7 +245,7 @@ public class HttpHeadersTests {
ifNoneMatchList.add(ifNoneMatch1);
ifNoneMatchList.add(ifNoneMatch2);
headers.setIfNoneMatch(ifNoneMatchList);
assertThat(headers.getIfNoneMatch(), contains("\"v2.6\"", "\"v2.7\"", "\"v2.8\""));
assertThat(headers.getIfNoneMatch()).contains("\"v2.6\"", "\"v2.7\"", "\"v2.8\"");
assertEquals("Invalid If-None-Match header", "\"v2.6\", \"v2.7\", \"v2.8\"", headers.getFirst("If-None-Match"));
}
@@ -399,7 +394,7 @@ public class HttpHeadersTests {
@Test // SPR-11917
public void getAllowEmptySet() {
headers.setAllow(Collections.emptySet());
assertThat(headers.getAllow(), is(emptyCollectionOf(HttpMethod.class)));
assertThat(headers.getAllow()).isEmpty();
}
@Test
@@ -414,7 +409,7 @@ public class HttpHeadersTests {
@Test
public void accessControlAllowHeaders() {
List<String> allowedHeaders = headers.getAccessControlAllowHeaders();
assertThat(allowedHeaders, is(emptyCollectionOf(String.class)));
assertThat(allowedHeaders).isEmpty();
headers.setAccessControlAllowHeaders(Arrays.asList("header1", "header2"));
allowedHeaders = headers.getAccessControlAllowHeaders();
assertEquals(allowedHeaders, Arrays.asList("header1", "header2"));
@@ -423,7 +418,7 @@ public class HttpHeadersTests {
@Test
public void accessControlAllowHeadersMultipleValues() {
List<String> allowedHeaders = headers.getAccessControlAllowHeaders();
assertThat(allowedHeaders, is(emptyCollectionOf(String.class)));
assertThat(allowedHeaders).isEmpty();
headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS, "header1, header2");
headers.add(HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS, "header3");
allowedHeaders = headers.getAccessControlAllowHeaders();
@@ -433,7 +428,7 @@ public class HttpHeadersTests {
@Test
public void accessControlAllowMethods() {
List<HttpMethod> allowedMethods = headers.getAccessControlAllowMethods();
assertThat(allowedMethods, is(emptyCollectionOf(HttpMethod.class)));
assertThat(allowedMethods).isEmpty();
headers.setAccessControlAllowMethods(Arrays.asList(HttpMethod.GET, HttpMethod.POST));
allowedMethods = headers.getAccessControlAllowMethods();
assertEquals(allowedMethods, Arrays.asList(HttpMethod.GET, HttpMethod.POST));
@@ -449,7 +444,7 @@ public class HttpHeadersTests {
@Test
public void accessControlExposeHeaders() {
List<String> exposedHeaders = headers.getAccessControlExposeHeaders();
assertThat(exposedHeaders, is(emptyCollectionOf(String.class)));
assertThat(exposedHeaders).isEmpty();
headers.setAccessControlExposeHeaders(Arrays.asList("header1", "header2"));
exposedHeaders = headers.getAccessControlExposeHeaders();
assertEquals(exposedHeaders, Arrays.asList("header1", "header2"));
@@ -465,7 +460,7 @@ public class HttpHeadersTests {
@Test
public void accessControlRequestHeaders() {
List<String> requestHeaders = headers.getAccessControlRequestHeaders();
assertThat(requestHeaders, is(emptyCollectionOf(String.class)));
assertThat(requestHeaders).isEmpty();
headers.setAccessControlRequestHeaders(Arrays.asList("header1", "header2"));
requestHeaders = headers.getAccessControlRequestHeaders();
assertEquals(requestHeaders, Arrays.asList("header1", "header2"));
@@ -520,20 +515,20 @@ public class HttpHeadersTests {
@Test
public void firstDate() {
headers.setDate(HttpHeaders.DATE, 1496370120000L);
assertThat(headers.getFirstDate(HttpHeaders.DATE), is(1496370120000L));
assertThat(headers.getFirstDate(HttpHeaders.DATE)).isEqualTo(1496370120000L);
headers.clear();
headers.add(HttpHeaders.DATE, "Fri, 02 Jun 2017 02:22:00 GMT");
headers.add(HttpHeaders.DATE, "Sat, 18 Dec 2010 10:20:00 GMT");
assertThat(headers.getFirstDate(HttpHeaders.DATE), is(1496370120000L));
assertThat(headers.getFirstDate(HttpHeaders.DATE)).isEqualTo(1496370120000L);
}
@Test
public void firstZonedDateTime() {
ZonedDateTime date = ZonedDateTime.of(2017, 6, 2, 2, 22, 0, 0, ZoneId.of("GMT"));
headers.setZonedDateTime(HttpHeaders.DATE, date);
assertThat(headers.getFirst(HttpHeaders.DATE), is("Fri, 02 Jun 2017 02:22:00 GMT"));
assertThat(headers.getFirst(HttpHeaders.DATE)).isEqualTo("Fri, 02 Jun 2017 02:22:00 GMT");
assertTrue(headers.getFirstZonedDateTime(HttpHeaders.DATE).isEqual(date));
headers.clear();

View File

@@ -20,10 +20,7 @@ import java.time.Duration;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.allOf;
import static org.hamcrest.CoreMatchers.endsWith;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
/**
@@ -51,13 +48,13 @@ public class ResponseCookieTests {
String expires = HttpHeaders.formatDate(System.currentTimeMillis() + maxAge.toMillis());
expires = expires.substring(0, expires.indexOf(":") + 1);
assertThat(ResponseCookie.from("id", "1fWa").maxAge(maxAge).build().toString(), allOf(
startsWith("id=1fWa; Max-Age=31536000; Expires=" + expires),
endsWith(" GMT")));
assertThat(ResponseCookie.from("id", "1fWa").maxAge(maxAge).build().toString())
.startsWith("id=1fWa; Max-Age=31536000; Expires=" + expires)
.endsWith(" GMT");
assertThat(ResponseCookie.from("id", "1fWa").maxAge(maxAge.getSeconds()).build().toString(), allOf(
startsWith("id=1fWa; Max-Age=31536000; Expires=" + expires),
endsWith(" GMT")));
assertThat(ResponseCookie.from("id", "1fWa").maxAge(maxAge.getSeconds()).build().toString())
.startsWith("id=1fWa; Max-Age=31536000; Expires=" + expires)
.endsWith(" GMT");
}
@Test

View File

@@ -22,10 +22,9 @@ import java.util.List;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import org.hamcrest.Matchers;
import org.junit.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
@@ -265,8 +264,8 @@ public class ResponseEntityTests {
assertTrue(responseEntity.getHeaders().containsKey(HttpHeaders.CACHE_CONTROL));
assertEquals(entity, responseEntity.getBody());
String cacheControlHeader = responseEntity.getHeaders().getCacheControl();
assertThat(cacheControlHeader,
Matchers.equalTo("max-age=3600, must-revalidate, private, proxy-revalidate, s-maxage=1800"));
assertThat(cacheControlHeader).isEqualTo(
"max-age=3600, must-revalidate, private, proxy-revalidate, s-maxage=1800");
}
@Test
@@ -284,7 +283,7 @@ public class ResponseEntityTests {
assertEquals(entity, responseEntity.getBody());
String cacheControlHeader = responseEntity.getHeaders().getCacheControl();
assertThat(cacheControlHeader, Matchers.equalTo("no-store"));
assertThat(cacheControlHeader).isEqualTo("no-store");
}
@Test

View File

@@ -22,14 +22,14 @@ import okhttp3.mockwebserver.Dispatcher;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import okhttp3.mockwebserver.RecordedRequest;
import org.hamcrest.Matchers;
import org.junit.After;
import org.junit.Before;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Brian Clozel
@@ -64,8 +64,8 @@ public class AbstractMockWebServerTestCase {
public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
try {
if (request.getPath().equals("/echo")) {
assertThat(request.getHeader("Host"),
Matchers.containsString("localhost:" + port));
assertThat(request.getHeader("Host"))
.contains("localhost:" + port);
MockResponse response = new MockResponse()
.setHeaders(request.getHeaders())
.setHeader("Content-Length", request.getBody().size())
@@ -81,26 +81,25 @@ public class AbstractMockWebServerTestCase {
return new MockResponse().setResponseCode(404);
}
else if(request.getPath().startsWith("/params")) {
assertThat(request.getPath(), Matchers.containsString("param1=value"));
assertThat(request.getPath(), Matchers.containsString("param2=value1&param2=value2"));
assertThat(request.getPath()).contains("param1=value");
assertThat(request.getPath()).contains("param2=value1&param2=value2");
return new MockResponse();
}
else if(request.getPath().equals("/methods/post")) {
assertThat(request.getMethod(), Matchers.is("POST"));
assertThat(request.getMethod()).isEqualTo("POST");
String transferEncoding = request.getHeader("Transfer-Encoding");
if(StringUtils.hasLength(transferEncoding)) {
assertThat(transferEncoding, Matchers.is("chunked"));
assertThat(transferEncoding).isEqualTo("chunked");
}
else {
long contentLength = Long.parseLong(request.getHeader("Content-Length"));
assertThat("Invalid content-length",
request.getBody().size(), Matchers.is(contentLength));
assertThat(request.getBody().size()).isEqualTo(contentLength);
}
return new MockResponse().setResponseCode(200);
}
else if(request.getPath().startsWith("/methods/")) {
String expectedMethod = request.getPath().replace("/methods/","").toUpperCase();
assertThat(request.getMethod(), Matchers.is(expectedMethod));
assertThat(request.getMethod()).isEqualTo(expectedMethod);
return new MockResponse();
}
return new MockResponse().setResponseCode(404);

View File

@@ -26,8 +26,7 @@ import org.junit.Test;
import org.springframework.util.StreamUtils;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
@@ -54,7 +53,7 @@ public class SimpleClientHttpResponseTests {
given(this.connection.getInputStream()).willReturn(is);
InputStream responseStream = this.response.getBody();
assertThat(StreamUtils.copyToString(responseStream, StandardCharsets.UTF_8), is("Spring"));
assertThat(StreamUtils.copyToString(responseStream, StandardCharsets.UTF_8)).isEqualTo("Spring");
this.response.close();
assertTrue(is.isClosed());
@@ -70,11 +69,11 @@ public class SimpleClientHttpResponseTests {
InputStream responseStream = this.response.getBody();
responseStream.read(buf);
assertThat(new String(buf, StandardCharsets.UTF_8), is("Spring"));
assertThat(is.available(), is(6));
assertThat(new String(buf, StandardCharsets.UTF_8)).isEqualTo("Spring");
assertThat(is.available()).isEqualTo(6);
this.response.close();
assertThat(is.available(), is(0));
assertThat(is.available()).isEqualTo(0);
assertTrue(is.isClosed());
verify(this.connection, never()).disconnect();
}
@@ -87,11 +86,11 @@ public class SimpleClientHttpResponseTests {
InputStream responseStream = this.response.getBody();
responseStream.read(buf);
assertThat(new String(buf, StandardCharsets.UTF_8), is("Spring"));
assertThat(is.available(), is(6));
assertThat(new String(buf, StandardCharsets.UTF_8)).isEqualTo("Spring");
assertThat(is.available()).isEqualTo(6);
this.response.close();
assertThat(is.available(), is(0));
assertThat(is.available()).isEqualTo(0);
assertTrue(is.isClosed());
verify(this.connection, never()).disconnect();
}
@@ -117,7 +116,7 @@ public class SimpleClientHttpResponseTests {
given(this.connection.getInputStream()).willReturn(is);
this.response.close();
assertThat(is.available(), is(0));
assertThat(is.available()).isEqualTo(0);
assertTrue(is.isClosed());
verify(this.connection, never()).disconnect();
}

View File

@@ -19,7 +19,6 @@ package org.springframework.http.client.support;
import java.util.Arrays;
import java.util.List;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.springframework.core.Ordered;
@@ -29,7 +28,8 @@ import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link InterceptingHttpAccessor}.
@@ -49,9 +49,9 @@ public class InterceptingHttpAccessorTests {
);
accessor.setInterceptors(interceptors);
assertThat(accessor.getInterceptors().get(0), Matchers.instanceOf(FirstClientHttpRequestInterceptor.class));
assertThat(accessor.getInterceptors().get(1), Matchers.instanceOf(SecondClientHttpRequestInterceptor.class));
assertThat(accessor.getInterceptors().get(2), Matchers.instanceOf(ThirdClientHttpRequestInterceptor.class));
assertThat(accessor.getInterceptors().get(0)).isInstanceOf(FirstClientHttpRequestInterceptor.class);
assertThat(accessor.getInterceptors().get(1)).isInstanceOf(SecondClientHttpRequestInterceptor.class);
assertThat(accessor.getInterceptors().get(2)).isInstanceOf(ThirdClientHttpRequestInterceptor.class);
}

View File

@@ -18,6 +18,7 @@ package org.springframework.http.codec;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.junit.Test;
@@ -34,10 +35,7 @@ import org.springframework.mock.http.server.reactive.test.MockServerHttpResponse
import org.springframework.util.MimeTypeUtils;
import org.springframework.util.StringUtils;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.startsWith;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertArrayEquals;
import static org.springframework.http.MediaType.TEXT_PLAIN;
import static org.springframework.mock.http.server.reactive.test.MockServerHttpRequest.get;
@@ -62,9 +60,10 @@ public class ResourceHttpMessageWriterTests {
@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void getWritableMediaTypes() throws Exception {
assertThat(this.writer.getWritableMediaTypes(),
containsInAnyOrder(MimeTypeUtils.APPLICATION_OCTET_STREAM, MimeTypeUtils.ALL));
assertThat((List) this.writer.getWritableMediaTypes())
.containsExactlyInAnyOrder(MimeTypeUtils.APPLICATION_OCTET_STREAM, MimeTypeUtils.ALL);
}
@Test
@@ -72,9 +71,9 @@ public class ResourceHttpMessageWriterTests {
testWrite(get("/").build());
assertThat(this.response.getHeaders().getContentType(), is(TEXT_PLAIN));
assertThat(this.response.getHeaders().getContentLength(), is(39L));
assertThat(this.response.getHeaders().getFirst(HttpHeaders.ACCEPT_RANGES), is("bytes"));
assertThat(this.response.getHeaders().getContentType()).isEqualTo(TEXT_PLAIN);
assertThat(this.response.getHeaders().getContentLength()).isEqualTo(39L);
assertThat(this.response.getHeaders().getFirst(HttpHeaders.ACCEPT_RANGES)).isEqualTo("bytes");
String content = "Spring Framework test resource content.";
StepVerifier.create(this.response.getBodyAsString()).expectNext(content).expectComplete().verify();
@@ -85,9 +84,9 @@ public class ResourceHttpMessageWriterTests {
testWrite(get("/").range(of(0, 5)).build());
assertThat(this.response.getHeaders().getContentType(), is(TEXT_PLAIN));
assertThat(this.response.getHeaders().getFirst(HttpHeaders.CONTENT_RANGE), is("bytes 0-5/39"));
assertThat(this.response.getHeaders().getContentLength(), is(6L));
assertThat(this.response.getHeaders().getContentType()).isEqualTo(TEXT_PLAIN);
assertThat(this.response.getHeaders().getFirst(HttpHeaders.CONTENT_RANGE)).isEqualTo("bytes 0-5/39");
assertThat(this.response.getHeaders().getContentLength()).isEqualTo(6L);
StepVerifier.create(this.response.getBodyAsString()).expectNext("Spring").expectComplete().verify();
}
@@ -101,7 +100,7 @@ public class ResourceHttpMessageWriterTests {
String contentType = headers.getContentType().toString();
String boundary = contentType.substring(30);
assertThat(contentType, startsWith("multipart/byteranges;boundary="));
assertThat(contentType).startsWith("multipart/byteranges;boundary=");
StepVerifier.create(this.response.getBodyAsString())
.consumeNextWith(content -> {
@@ -136,8 +135,8 @@ public class ResourceHttpMessageWriterTests {
testWrite(get("/").header(HttpHeaders.RANGE, "invalid").build());
assertThat(this.response.getHeaders().getFirst(HttpHeaders.ACCEPT_RANGES), is("bytes"));
assertThat(this.response.getStatusCode(), is(HttpStatus.REQUESTED_RANGE_NOT_SATISFIABLE));
assertThat(this.response.getHeaders().getFirst(HttpHeaders.ACCEPT_RANGES)).isEqualTo("bytes");
assertThat(this.response.getStatusCode()).isEqualTo(HttpStatus.REQUESTED_RANGE_NOT_SATISFIABLE);
}

View File

@@ -32,13 +32,13 @@ import org.springframework.core.codec.AbstractEncoderTestCase;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.MediaType;
import org.springframework.http.codec.Pojo;
import org.springframework.tests.XmlContent;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.springframework.core.io.buffer.DataBufferUtils.release;
import static org.xmlunit.matchers.CompareMatcher.isSimilarTo;
/**
* @author Sebastien Deleuze
@@ -109,7 +109,7 @@ public class Jaxb2XmlEncoderTests extends AbstractEncoderTestCase<Jaxb2XmlEncode
dataBuffer.read(resultBytes);
release(dataBuffer);
String actual = new String(resultBytes, UTF_8);
assertThat(actual, isSimilarTo(expected));
assertThat(XmlContent.from(actual)).isSimilarTo(expected);
};
}

View File

@@ -30,7 +30,6 @@ import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUpload;
import org.apache.commons.fileupload.RequestContext;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.springframework.core.io.ClassPathResource;
@@ -44,10 +43,7 @@ import org.springframework.http.converter.support.AllEncompassingFormHttpMessage
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import static org.hamcrest.CoreMatchers.allOf;
import static org.hamcrest.CoreMatchers.endsWith;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
@@ -150,7 +146,7 @@ public class FormHttpMessageConverterTests {
final MediaType contentType = outputMessage.getHeaders().getContentType();
// SPR-17030
assertThat(contentType.getParameters().keySet(), Matchers.contains("charset", "boundary"));
assertThat(contentType.getParameters()).containsKeys("charset", "boundary");
// see if Commons FileUpload can read what we wrote
FileItemFactory fileItemFactory = new DiskFileItemFactory();
@@ -234,8 +230,9 @@ public class FormHttpMessageConverterTests {
// With developer builds we get: <MyBean><string>foo</string></MyBean>
// But on CI server we get: <MyBean xmlns=""><string>foo</string></MyBean>
// So... we make a compromise:
assertThat(item.getString(),
allOf(startsWith("<MyBean"), endsWith("><string>foo</string></MyBean>")));
assertThat(item.getString())
.startsWith("<MyBean")
.endsWith("><string>foo</string></MyBean>");
}

View File

@@ -33,10 +33,8 @@ import org.springframework.http.MockHttpInputMessage;
import org.springframework.http.MockHttpOutputMessage;
import org.springframework.util.FileCopyUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsInstanceOf.instanceOf;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
@@ -73,7 +71,7 @@ public class ResourceHttpMessageConverterTests {
inputMessage.getHeaders().setContentDisposition(
ContentDisposition.builder("attachment").filename("yourlogo.jpg").build());
Resource actualResource = converter.read(Resource.class, inputMessage);
assertThat(FileCopyUtils.copyToByteArray(actualResource.getInputStream()), is(body));
assertThat(FileCopyUtils.copyToByteArray(actualResource.getInputStream())).isEqualTo(body);
assertEquals("yourlogo.jpg", actualResource.getFilename());
}
@@ -85,8 +83,8 @@ public class ResourceHttpMessageConverterTests {
inputMessage.getHeaders().setContentDisposition(
ContentDisposition.builder("attachment").filename("yourlogo.jpg").build());
Resource actualResource = converter.read(InputStreamResource.class, inputMessage);
assertThat(actualResource, instanceOf(InputStreamResource.class));
assertThat(actualResource.getInputStream(), is(body));
assertThat(actualResource).isInstanceOf(InputStreamResource.class);
assertThat(actualResource.getInputStream()).isEqualTo(body);
assertEquals("yourlogo.jpg", actualResource.getFilename());
}
}

View File

@@ -23,7 +23,6 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.mockito.BDDMockito;
import org.mockito.Mockito;
@@ -38,8 +37,7 @@ import org.springframework.http.MediaType;
import org.springframework.http.MockHttpOutputMessage;
import org.springframework.util.StringUtils;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
@@ -87,11 +85,11 @@ public class ResourceRegionHttpMessageConverterTests {
converter.write(region, MediaType.TEXT_PLAIN, outputMessage);
HttpHeaders headers = outputMessage.getHeaders();
assertThat(headers.getContentType(), is(MediaType.TEXT_PLAIN));
assertThat(headers.getContentLength(), is(6L));
assertThat(headers.get(HttpHeaders.CONTENT_RANGE).size(), is(1));
assertThat(headers.get(HttpHeaders.CONTENT_RANGE).get(0), is("bytes 0-5/39"));
assertThat(outputMessage.getBodyAsString(StandardCharsets.UTF_8), is("Spring"));
assertThat(headers.getContentType()).isEqualTo(MediaType.TEXT_PLAIN);
assertThat(headers.getContentLength()).isEqualTo(6L);
assertThat(headers.get(HttpHeaders.CONTENT_RANGE)).hasSize(1);
assertThat(headers.get(HttpHeaders.CONTENT_RANGE).get(0)).isEqualTo("bytes 0-5/39");
assertThat(outputMessage.getBodyAsString(StandardCharsets.UTF_8)).isEqualTo("Spring");
}
@Test
@@ -102,11 +100,11 @@ public class ResourceRegionHttpMessageConverterTests {
converter.write(region, MediaType.TEXT_PLAIN, outputMessage);
HttpHeaders headers = outputMessage.getHeaders();
assertThat(headers.getContentType(), is(MediaType.TEXT_PLAIN));
assertThat(headers.getContentLength(), is(32L));
assertThat(headers.get(HttpHeaders.CONTENT_RANGE).size(), is(1));
assertThat(headers.get(HttpHeaders.CONTENT_RANGE).get(0), is("bytes 7-38/39"));
assertThat(outputMessage.getBodyAsString(StandardCharsets.UTF_8), is("Framework test resource content."));
assertThat(headers.getContentType()).isEqualTo(MediaType.TEXT_PLAIN);
assertThat(headers.getContentLength()).isEqualTo(32L);
assertThat(headers.get(HttpHeaders.CONTENT_RANGE)).hasSize(1);
assertThat(headers.get(HttpHeaders.CONTENT_RANGE).get(0)).isEqualTo("bytes 7-38/39");
assertThat(outputMessage.getBodyAsString(StandardCharsets.UTF_8)).isEqualTo("Framework test resource content.");
}
@Test
@@ -122,30 +120,30 @@ public class ResourceRegionHttpMessageConverterTests {
converter.write(regions, MediaType.TEXT_PLAIN, outputMessage);
HttpHeaders headers = outputMessage.getHeaders();
assertThat(headers.getContentType().toString(), Matchers.startsWith("multipart/byteranges;boundary="));
assertThat(headers.getContentType().toString()).startsWith("multipart/byteranges;boundary=");
String boundary = "--" + headers.getContentType().toString().substring(30);
String content = outputMessage.getBodyAsString(StandardCharsets.UTF_8);
String[] ranges = StringUtils.tokenizeToStringArray(content, "\r\n", false, true);
assertThat(ranges[0], is(boundary));
assertThat(ranges[1], is("Content-Type: text/plain"));
assertThat(ranges[2], is("Content-Range: bytes 0-5/39"));
assertThat(ranges[3], is("Spring"));
assertThat(ranges[0]).isEqualTo(boundary);
assertThat(ranges[1]).isEqualTo("Content-Type: text/plain");
assertThat(ranges[2]).isEqualTo("Content-Range: bytes 0-5/39");
assertThat(ranges[3]).isEqualTo("Spring");
assertThat(ranges[4], is(boundary));
assertThat(ranges[5], is("Content-Type: text/plain"));
assertThat(ranges[6], is("Content-Range: bytes 7-15/39"));
assertThat(ranges[7], is("Framework"));
assertThat(ranges[4]).isEqualTo(boundary);
assertThat(ranges[5]).isEqualTo("Content-Type: text/plain");
assertThat(ranges[6]).isEqualTo("Content-Range: bytes 7-15/39");
assertThat(ranges[7]).isEqualTo("Framework");
assertThat(ranges[8], is(boundary));
assertThat(ranges[9], is("Content-Type: text/plain"));
assertThat(ranges[10], is("Content-Range: bytes 17-20/39"));
assertThat(ranges[11], is("test"));
assertThat(ranges[8]).isEqualTo(boundary);
assertThat(ranges[9]).isEqualTo("Content-Type: text/plain");
assertThat(ranges[10]).isEqualTo("Content-Range: bytes 17-20/39");
assertThat(ranges[11]).isEqualTo("test");
assertThat(ranges[12], is(boundary));
assertThat(ranges[13], is("Content-Type: text/plain"));
assertThat(ranges[14], is("Content-Range: bytes 22-38/39"));
assertThat(ranges[15], is("resource content."));
assertThat(ranges[12]).isEqualTo(boundary);
assertThat(ranges[13]).isEqualTo("Content-Type: text/plain");
assertThat(ranges[14]).isEqualTo("Content-Range: bytes 22-38/39");
assertThat(ranges[15]).isEqualTo("resource content.");
}
@Test // SPR-15041
@@ -160,9 +158,9 @@ public class ResourceRegionHttpMessageConverterTests {
converter.write(Collections.singletonList(resourceRegion), null, outputMessage);
assertThat(outputMessage.getHeaders().getContentType(), is(MediaType.APPLICATION_OCTET_STREAM));
assertThat(outputMessage.getHeaders().getFirst(HttpHeaders.CONTENT_RANGE), is("bytes 0-5/12"));
assertThat(outputMessage.getBodyAsString(StandardCharsets.UTF_8), is("Spring"));
assertThat(outputMessage.getHeaders().getContentType()).isEqualTo(MediaType.APPLICATION_OCTET_STREAM);
assertThat(outputMessage.getHeaders().getFirst(HttpHeaders.CONTENT_RANGE)).isEqualTo("bytes 0-5/12");
assertThat(outputMessage.getBodyAsString(StandardCharsets.UTF_8)).isEqualTo("Spring");
}
}

View File

@@ -35,11 +35,11 @@ import org.xmlunit.diff.NodeMatcher;
import org.springframework.http.MediaType;
import org.springframework.http.MockHttpInputMessage;
import org.springframework.http.MockHttpOutputMessage;
import org.springframework.tests.XmlContent;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.xmlunit.matchers.CompareMatcher.isSimilarTo;
/**
* @author Arjen Poutsma
@@ -114,8 +114,8 @@ public class AtomFeedHttpMessageConverterTests {
"<entry><id>id1</id><title>title1</title></entry>" +
"<entry><id>id2</id><title>title2</title></entry></feed>";
NodeMatcher nm = new DefaultNodeMatcher(ElementSelectors.byName);
assertThat(outputMessage.getBodyAsString(StandardCharsets.UTF_8),
isSimilarTo(expected).ignoreWhitespace().withNodeMatcher(nm));
assertThat(XmlContent.of(outputMessage.getBodyAsString(StandardCharsets.UTF_8)))
.isSimilarToIgnoringWhitespace(expected, nm);
}
@Test

View File

@@ -28,13 +28,13 @@ import com.rometools.rome.feed.rss.Item;
import org.junit.Before;
import org.junit.Test;
import org.xml.sax.SAXException;
import org.xmlunit.matchers.CompareMatcher;
import org.springframework.http.MediaType;
import org.springframework.http.MockHttpInputMessage;
import org.springframework.http.MockHttpOutputMessage;
import org.springframework.tests.XmlContent;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
@@ -112,7 +112,8 @@ public class RssChannelHttpMessageConverterTests {
"<item><title>title1</title></item>" +
"<item><title>title2</title></item>" +
"</channel></rss>";
assertThat(outputMessage.getBodyAsString(StandardCharsets.UTF_8), isSimilarTo(expected));
assertThat(XmlContent.of(outputMessage.getBodyAsString(StandardCharsets.UTF_8)))
.isSimilarToIgnoringWhitespace(expected);
}
@Test
@@ -135,8 +136,4 @@ public class RssChannelHttpMessageConverterTests {
outputMessage.getHeaders().getContentType());
}
private static CompareMatcher isSimilarTo(final String content) {
return CompareMatcher.isSimilarTo(content)
.ignoreWhitespace();
}
}

View File

@@ -83,11 +83,9 @@ import org.junit.Test;
import org.springframework.beans.FatalBeanException;
import org.springframework.util.StringUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
@@ -295,7 +293,7 @@ public class Jackson2ObjectMapperBuilderTests {
.build();
DateTime dateTime = new DateTime(1322903730000L, DateTimeZone.UTC);
assertEquals("1322903730000", new String(objectMapper.writeValueAsBytes(dateTime), "UTF-8"));
assertThat(new String(objectMapper.writeValueAsBytes(new Integer(4)), "UTF-8"), containsString("customid"));
assertThat(new String(objectMapper.writeValueAsBytes(new Integer(4)), "UTF-8")).contains("customid");
}
@Test // SPR-12634
@@ -308,7 +306,7 @@ public class Jackson2ObjectMapperBuilderTests {
.build();
DateTime dateTime = new DateTime(1322903730000L, DateTimeZone.UTC);
assertEquals("1322903730000", new String(objectMapper.writeValueAsBytes(dateTime), "UTF-8"));
assertThat(new String(objectMapper.writeValueAsBytes(new Integer(4)), "UTF-8"), containsString("customid"));
assertThat(new String(objectMapper.writeValueAsBytes(new Integer(4)), "UTF-8")).contains("customid");
}
@Test // SPR-12634
@@ -319,7 +317,7 @@ public class Jackson2ObjectMapperBuilderTests {
.serializerByType(Integer.class, new CustomIntegerSerializer()).build();
DateTime dateTime = new DateTime(1322903730000L, DateTimeZone.UTC);
assertEquals("1322903730000", new String(objectMapper.writeValueAsBytes(dateTime), "UTF-8"));
assertThat(new String(objectMapper.writeValueAsBytes(new Integer(4)), "UTF-8"), containsString("customid"));
assertThat(new String(objectMapper.writeValueAsBytes(new Integer(4)), "UTF-8")).contains("customid");
}
@Test // gh-22576
@@ -427,16 +425,16 @@ public class Jackson2ObjectMapperBuilderTests {
.filters(new SimpleFilterProvider().setFailOnUnknownId(false)).build();
JacksonFilteredBean bean = new JacksonFilteredBean("value1", "value2");
String output = objectMapper.writeValueAsString(bean);
assertThat(output, containsString("value1"));
assertThat(output, containsString("value2"));
assertThat(output).contains("value1");
assertThat(output).contains("value2");
SimpleFilterProvider provider = new SimpleFilterProvider()
.setFailOnUnknownId(false)
.setDefaultFilter(SimpleBeanPropertyFilter.serializeAllExcept("property2"));
objectMapper = Jackson2ObjectMapperBuilder.json().filters(provider).build();
output = objectMapper.writeValueAsString(bean);
assertThat(output, containsString("value1"));
assertThat(output, not(containsString("value2")));
assertThat(output).contains("value1");
assertThat(output).doesNotContain("value2");
}
@Test
@@ -529,7 +527,7 @@ public class Jackson2ObjectMapperBuilderTests {
assertEquals(XmlMapper.class, objectMapper.getClass());
ListContainer<String> container = new ListContainer<>(Arrays.asList("foo", "bar"));
String output = objectMapper.writeValueAsString(container);
assertThat(output, containsString("<list>foo</list><list>bar</list></ListContainer>"));
assertThat(output).contains("<list>foo</list><list>bar</list></ListContainer>");
}
@Test // SPR-14435
@@ -562,9 +560,9 @@ public class Jackson2ObjectMapperBuilderTests {
.build();
String json = objectMapper.writeValueAsString(new JacksonVisibilityBean());
assertThat(json, containsString("property1"));
assertThat(json, containsString("property2"));
assertThat(json, not(containsString("property3")));
assertThat(json).contains("property1");
assertThat(json).contains("property2");
assertThat(json).doesNotContain("property3");
}
public static class CustomIntegerModule extends Module {

View File

@@ -64,9 +64,8 @@ import org.junit.Test;
import org.springframework.beans.FatalBeanException;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
@@ -228,7 +227,7 @@ public class Jackson2ObjectMapperFactoryBeanTests {
DateTime dateTime = new DateTime(1322903730000L, DateTimeZone.UTC);
assertEquals("1322903730000", new String(objectMapper.writeValueAsBytes(dateTime), "UTF-8"));
assertThat(new String(objectMapper.writeValueAsBytes(new Integer(4)), "UTF-8"), containsString("customid"));
assertThat(new String(objectMapper.writeValueAsBytes(new Integer(4)), "UTF-8")).contains("customid");
}
@Test // SPR-12634
@@ -242,7 +241,7 @@ public class Jackson2ObjectMapperFactoryBeanTests {
DateTime dateTime = new DateTime(1322903730000L, DateTimeZone.UTC);
assertEquals("1322903730000", new String(objectMapper.writeValueAsBytes(dateTime), "UTF-8"));
assertThat(new String(objectMapper.writeValueAsBytes(new Integer(4)), "UTF-8"), containsString("customid"));
assertThat(new String(objectMapper.writeValueAsBytes(new Integer(4)), "UTF-8")).contains("customid");
}
@Test
@@ -301,8 +300,8 @@ public class Jackson2ObjectMapperFactoryBeanTests {
JacksonFilteredBean bean = new JacksonFilteredBean("value1", "value2");
String output = objectMapper.writeValueAsString(bean);
assertThat(output, containsString("value1"));
assertThat(output, containsString("value2"));
assertThat(output).contains("value1");
assertThat(output).contains("value2");
}
@Test

View File

@@ -42,10 +42,8 @@ import org.springframework.http.converter.HttpMessageConversionException;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.lang.Nullable;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
@@ -359,9 +357,9 @@ public class MappingJackson2HttpMessageConverterTests {
this.converter.writeInternal(jacksonValue, null, outputMessage);
String result = outputMessage.getBodyAsString(StandardCharsets.UTF_8);
assertThat(result, containsString("\"withView1\":\"with\""));
assertThat(result, not(containsString("\"withView2\":\"with\"")));
assertThat(result, not(containsString("\"withoutView\":\"without\"")));
assertThat(result).contains("\"withView1\":\"with\"");
assertThat(result).doesNotContain("\"withView2\":\"with\"");
assertThat(result).doesNotContain("\"withoutView\":\"without\"");
}
@Test
@@ -377,9 +375,9 @@ public class MappingJackson2HttpMessageConverterTests {
this.converter.writeInternal(jacksonValue, null, outputMessage);
String result = outputMessage.getBodyAsString(StandardCharsets.UTF_8);
assertThat(result, not(containsString("\"withView1\":\"with\"")));
assertThat(result, not(containsString("\"withView2\":\"with\"")));
assertThat(result, containsString("\"withoutView\":\"without\""));
assertThat(result).doesNotContain("\"withView1\":\"with\"");
assertThat(result).doesNotContain("\"withView2\":\"with\"");
assertThat(result).contains("\"withoutView\":\"without\"");
}
@Test
@@ -396,8 +394,8 @@ public class MappingJackson2HttpMessageConverterTests {
this.converter.writeInternal(jacksonValue, null, outputMessage);
String result = outputMessage.getBodyAsString(StandardCharsets.UTF_8);
assertThat(result, containsString("\"property1\":\"value\""));
assertThat(result, not(containsString("\"property2\":\"value\"")));
assertThat(result).contains("\"property1\":\"value\"");
assertThat(result).doesNotContain("\"property2\":\"value\"");
}
@Test // SPR-13318

View File

@@ -39,9 +39,10 @@ import org.springframework.http.MediaType;
import org.springframework.http.MockHttpInputMessage;
import org.springframework.http.MockHttpOutputMessage;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.tests.XmlContent;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
@@ -49,7 +50,6 @@ import static org.xmlunit.diff.ComparisonType.XML_STANDALONE;
import static org.xmlunit.diff.DifferenceEvaluators.Default;
import static org.xmlunit.diff.DifferenceEvaluators.chain;
import static org.xmlunit.diff.DifferenceEvaluators.downgradeDifferencesToEqual;
import static org.xmlunit.matchers.CompareMatcher.isSimilarTo;
/**
* Tests for {@link Jaxb2RootElementHttpMessageConverter}.
@@ -183,8 +183,8 @@ public class Jaxb2RootElementHttpMessageConverterTests {
assertEquals("Invalid content-type", new MediaType("application", "xml"),
outputMessage.getHeaders().getContentType());
DifferenceEvaluator ev = chain(Default, downgradeDifferencesToEqual(XML_STANDALONE));
assertThat("Invalid result", outputMessage.getBodyAsString(StandardCharsets.UTF_8),
isSimilarTo("<rootElement><type s=\"Hello World\"/></rootElement>").withDifferenceEvaluator(ev));
assertThat(XmlContent.of(outputMessage.getBodyAsString(StandardCharsets.UTF_8)))
.isSimilarTo("<rootElement><type s=\"Hello World\"/></rootElement>", ev);
}
@Test
@@ -194,8 +194,8 @@ public class Jaxb2RootElementHttpMessageConverterTests {
assertEquals("Invalid content-type", new MediaType("application", "xml"),
outputMessage.getHeaders().getContentType());
DifferenceEvaluator ev = chain(Default, downgradeDifferencesToEqual(XML_STANDALONE));
assertThat("Invalid result", outputMessage.getBodyAsString(StandardCharsets.UTF_8),
isSimilarTo("<rootElement><type s=\"Hello World\"/></rootElement>").withDifferenceEvaluator(ev));
assertThat(XmlContent.of(outputMessage.getBodyAsString(StandardCharsets.UTF_8)))
.isSimilarTo("<rootElement><type s=\"Hello World\"/></rootElement>", ev);
}
// SPR-11488
@@ -206,8 +206,8 @@ public class Jaxb2RootElementHttpMessageConverterTests {
MyJaxb2RootElementHttpMessageConverter myConverter = new MyJaxb2RootElementHttpMessageConverter();
myConverter.write(new MyRootElement(new MyCustomElement("a", "b")), null, outputMessage);
DifferenceEvaluator ev = chain(Default, downgradeDifferencesToEqual(XML_STANDALONE));
assertThat("Invalid result", outputMessage.getBodyAsString(StandardCharsets.UTF_8),
isSimilarTo("<myRootElement><element>a|||b</element></myRootElement>").withDifferenceEvaluator(ev));
assertThat(XmlContent.of(outputMessage.getBodyAsString(StandardCharsets.UTF_8)))
.isSimilarTo("<myRootElement><element>a|||b</element></myRootElement>", ev);
}
@Test

View File

@@ -30,10 +30,8 @@ import org.springframework.http.MockHttpOutputMessage;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.json.MappingJacksonValue;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
@@ -137,9 +135,9 @@ public class MappingJackson2XmlHttpMessageConverterTests {
this.converter.write(jacksonValue, null, outputMessage);
String result = outputMessage.getBodyAsString(StandardCharsets.UTF_8);
assertThat(result, containsString("<withView1>with</withView1>"));
assertThat(result, not(containsString("<withView2>with</withView2>")));
assertThat(result, not(containsString("<withoutView>without</withoutView>")));
assertThat(result).contains("<withView1>with</withView1>");
assertThat(result).doesNotContain("<withView2>with</withView2>");
assertThat(result).doesNotContain("<withoutView>without</withoutView>");
}
@Test

View File

@@ -44,14 +44,14 @@ import org.springframework.http.MediaType;
import org.springframework.http.MockHttpInputMessage;
import org.springframework.http.MockHttpOutputMessage;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.tests.XmlContent;
import org.springframework.util.FileCopyUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
import static org.xmlunit.matchers.CompareMatcher.isSimilarTo;
/**
* @author Arjen Poutsma
@@ -143,7 +143,7 @@ public class SourceHttpMessageConverterTests {
SAXSource result = (SAXSource) converter.read(SAXSource.class, inputMessage);
InputSource inputSource = result.getInputSource();
String s = FileCopyUtils.copyToString(new InputStreamReader(inputSource.getByteStream()));
assertThat("Invalid result", s, isSimilarTo(BODY));
assertThat(XmlContent.from(s)).isSimilarTo(BODY);
}
@Test
@@ -270,7 +270,7 @@ public class SourceHttpMessageConverterTests {
inputMessage.getHeaders().setContentType(new MediaType("application", "xml"));
StreamSource result = (StreamSource) converter.read(StreamSource.class, inputMessage);
String s = FileCopyUtils.copyToString(new InputStreamReader(result.getInputStream()));
assertThat("Invalid result", s, isSimilarTo(BODY));
assertThat(XmlContent.of(s)).isSimilarTo(BODY);
}
@Test
@@ -292,8 +292,8 @@ public class SourceHttpMessageConverterTests {
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
converter.write(domSource, null, outputMessage);
assertThat("Invalid result", outputMessage.getBodyAsString(StandardCharsets.UTF_8),
isSimilarTo("<root>Hello World</root>"));
assertThat(XmlContent.of(outputMessage.getBodyAsString(StandardCharsets.UTF_8)))
.isSimilarTo("<root>Hello World</root>");
assertEquals("Invalid content-type", new MediaType("application", "xml"),
outputMessage.getHeaders().getContentType());
assertEquals("Invalid content-length", outputMessage.getBodyAsBytes().length,
@@ -307,8 +307,8 @@ public class SourceHttpMessageConverterTests {
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
converter.write(saxSource, null, outputMessage);
assertThat("Invalid result", outputMessage.getBodyAsString(StandardCharsets.UTF_8),
isSimilarTo("<root>Hello World</root>"));
assertThat(XmlContent.of(outputMessage.getBodyAsString(StandardCharsets.UTF_8)))
.isSimilarTo("<root>Hello World</root>");
assertEquals("Invalid content-type", new MediaType("application", "xml"),
outputMessage.getHeaders().getContentType());
}
@@ -320,8 +320,8 @@ public class SourceHttpMessageConverterTests {
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
converter.write(streamSource, null, outputMessage);
assertThat("Invalid result", outputMessage.getBodyAsString(StandardCharsets.UTF_8),
isSimilarTo("<root>Hello World</root>"));
assertThat(XmlContent.of(outputMessage.getBodyAsString(StandardCharsets.UTF_8)))
.isSimilarTo("<root>Hello World</root>");
assertEquals("Invalid content-type", new MediaType("application", "xml"),
outputMessage.getHeaders().getContentType());
}

View File

@@ -19,7 +19,6 @@ package org.springframework.http.server.reactive;
import java.net.URI;
import java.time.Duration;
import org.hamcrest.Matchers;
import org.junit.Ignore;
import org.junit.Test;
import reactor.core.publisher.Flux;
@@ -33,7 +32,8 @@ import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Stephane Maldini
@@ -58,7 +58,7 @@ public class AsyncIntegrationTests extends AbstractHttpHandlerIntegrationTests {
ResponseEntity<String> response = new RestTemplate().exchange(
RequestEntity.get(url).build(), String.class);
assertThat(response.getBody(), Matchers.equalTo("hello"));
assertThat(response.getBody()).isEqualTo("hello");
}

View File

@@ -32,10 +32,7 @@ import org.springframework.http.ResponseCookie;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.equalToIgnoringCase;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
/**
@@ -75,11 +72,14 @@ public class CookieIntegrationTests extends AbstractHttpHandlerIntegrationTests
List<String> headerValues = response.getHeaders().get("Set-Cookie");
assertEquals(2, headerValues.size());
assertThat(splitCookie(headerValues.get(0)), containsInAnyOrder(equalTo("SID=31d4d96e407aad42"),
equalToIgnoringCase("Path=/"), equalToIgnoringCase("Secure"), equalToIgnoringCase("HttpOnly")));
assertThat(splitCookie(headerValues.get(1)), containsInAnyOrder(equalTo("lang=en-US"),
equalToIgnoringCase("Path=/"), equalToIgnoringCase("Domain=example.com")));
List<String> cookie0 = splitCookie(headerValues.get(0));
assertThat(cookie0.remove("SID=31d4d96e407aad42")).as("SID").isTrue();
assertThat(cookie0.stream().map(String::toLowerCase))
.containsExactlyInAnyOrder("path=/", "secure", "httponly");
List<String> cookie1 = splitCookie(headerValues.get(1));
assertThat(cookie1.remove("lang=en-US")).as("lang").isTrue();
assertThat(cookie1.stream().map(String::toLowerCase))
.containsExactlyInAnyOrder("path=/", "domain=example.com");
}
// No client side HttpCookie support yet

View File

@@ -36,9 +36,7 @@ import org.springframework.web.bind.ServletRequestParameterPropertyValues;
import org.springframework.web.context.request.ServletWebRequest;
import org.springframework.web.multipart.support.StringMultipartFileEditor;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.isA;
import static org.hamcrest.Matchers.notNullValue;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
@@ -149,14 +147,9 @@ public class WebRequestDataBinderTests {
request.addParameter("_someMap", "visible");
binder.bind(new ServletWebRequest(request));
assertThat(target.getSomeSet(), notNullValue());
assertThat(target.getSomeSet(), isA(Set.class));
assertThat(target.getSomeList(), notNullValue());
assertThat(target.getSomeList(), isA(List.class));
assertThat(target.getSomeMap(), notNullValue());
assertThat(target.getSomeMap(), isA(Map.class));
assertThat(target.getSomeSet()).isNotNull().isInstanceOf(Set.class);
assertThat(target.getSomeList()).isNotNull().isInstanceOf(List.class);
assertThat(target.getSomeMap()).isNotNull().isInstanceOf(Map.class);
}
@Test

View File

@@ -26,13 +26,12 @@ import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import okhttp3.mockwebserver.RecordedRequest;
import okio.Buffer;
import org.hamcrest.Matchers;
import org.junit.After;
import org.junit.Before;
import org.springframework.http.MediaType;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
@@ -170,9 +169,9 @@ public class AbstractMockWebServerTestCase {
private MockResponse formRequest(RecordedRequest request) {
assertEquals("application/x-www-form-urlencoded;charset=UTF-8", request.getHeader("Content-Type"));
String body = request.getBody().readUtf8();
assertThat(body, Matchers.containsString("name+1=value+1"));
assertThat(body, Matchers.containsString("name+2=value+2%2B1"));
assertThat(body, Matchers.containsString("name+2=value+2%2B2"));
assertThat(body).contains("name+1=value+1");
assertThat(body).contains("name+2=value+2%2B1");
assertThat(body).contains("name+2=value+2%2B2");
return new MockResponse().setResponseCode(200);
}

View File

@@ -27,8 +27,7 @@ import org.junit.Test;
import org.springframework.http.HttpStatus;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
/**
@@ -52,7 +51,7 @@ public class HttpStatusCodeExceptionTests {
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
HttpStatusCodeException ex2 =
(HttpStatusCodeException) new ObjectInputStream(in).readObject();
assertThat(ex2.getResponseBodyAsString(), equalTo(ex1.getResponseBodyAsString()));
assertThat(ex2.getResponseBodyAsString()).isEqualTo(ex1.getResponseBodyAsString());
}
@Test

View File

@@ -47,9 +47,8 @@ import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.util.StreamUtils;
import org.springframework.web.util.DefaultUriBuilderFactory;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
@@ -634,7 +633,7 @@ public class RestTemplateTests {
entityHeaders.add("MyHeader", "MyEntityValue");
HttpEntity<Void> entity = new HttpEntity<>(null, entityHeaders);
template.exchange("https://example.com", POST, entity, Void.class);
assertThat(requestHeaders.get("MyHeader"), contains("MyEntityValue", "MyInterceptorValue"));
assertThat(requestHeaders.get("MyHeader")).contains("MyEntityValue", "MyInterceptorValue");
verify(response).close();
}
@@ -658,7 +657,7 @@ public class RestTemplateTests {
entityHeaders.add("MyHeader", "MyEntityValue");
HttpEntity<String> entity = new HttpEntity<>("Hello World", entityHeaders);
template.exchange("https://example.com", POST, entity, Void.class);
assertThat(requestHeaders.get("MyHeader"), contains("MyEntityValue", "MyInterceptorValue"));
assertThat(requestHeaders.get("MyHeader")).contains("MyEntityValue", "MyInterceptorValue");
verify(response).close();
}

View File

@@ -24,8 +24,7 @@ import org.springframework.context.annotation.AnnotationBeanNameGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertNotNull;
/**
@@ -80,7 +79,7 @@ public class AnnotationConfigWebApplicationContextTests {
});
ctx.setConfigLocation(Config.class.getName());
ctx.refresh();
assertThat(ctx.containsBean("custom-myConfig"), is(true));
assertThat(ctx.containsBean("custom-myConfig")).isTrue();
}

View File

@@ -24,9 +24,8 @@ import org.springframework.core.env.PropertySource;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.tests.mock.jndi.SimpleNamingContextBuilder;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Unit tests for {@link StandardServletEnvironment}.
@@ -44,16 +43,16 @@ public class StandardServletEnvironmentTests {
MutablePropertySources sources = env.getPropertySources();
assertThat(sources.precedenceOf(PropertySource.named(
StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME)), equalTo(0));
StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME))).isEqualTo(0);
assertThat(sources.precedenceOf(PropertySource.named(
StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME)), equalTo(1));
StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME))).isEqualTo(1);
assertThat(sources.precedenceOf(PropertySource.named(
StandardServletEnvironment.JNDI_PROPERTY_SOURCE_NAME)), equalTo(2));
StandardServletEnvironment.JNDI_PROPERTY_SOURCE_NAME))).isEqualTo(2);
assertThat(sources.precedenceOf(PropertySource.named(
StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME)), equalTo(3));
StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME))).isEqualTo(3);
assertThat(sources.precedenceOf(PropertySource.named(
StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME)), equalTo(4));
assertThat(sources.size(), is(5));
StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME))).isEqualTo(4);
assertThat(sources).hasSize(5);
}
}

View File

@@ -26,8 +26,7 @@ import org.springframework.http.HttpMethod;
import org.springframework.mock.web.test.MockHttpServletRequest;
import org.springframework.mock.web.test.MockHttpServletResponse;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
@@ -67,8 +66,8 @@ public class DefaultCorsProcessorTests {
this.processor.processRequest(this.conf, this.request, this.response);
assertFalse(this.response.containsHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN));
assertThat(this.response.getHeaders(HttpHeaders.VARY), contains(HttpHeaders.ORIGIN,
HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, HttpHeaders.ACCESS_CONTROL_REQUEST_HEADERS));
assertThat(this.response.getHeaders(HttpHeaders.VARY)).contains(HttpHeaders.ORIGIN,
HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, HttpHeaders.ACCESS_CONTROL_REQUEST_HEADERS);
assertEquals(HttpServletResponse.SC_OK, this.response.getStatus());
}
@@ -79,8 +78,8 @@ public class DefaultCorsProcessorTests {
this.processor.processRequest(this.conf, this.request, this.response);
assertFalse(this.response.containsHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN));
assertThat(this.response.getHeaders(HttpHeaders.VARY), contains(HttpHeaders.ORIGIN,
HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, HttpHeaders.ACCESS_CONTROL_REQUEST_HEADERS));
assertThat(this.response.getHeaders(HttpHeaders.VARY)).contains(HttpHeaders.ORIGIN,
HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, HttpHeaders.ACCESS_CONTROL_REQUEST_HEADERS);
assertEquals(HttpServletResponse.SC_OK, this.response.getStatus());
}
@@ -91,8 +90,8 @@ public class DefaultCorsProcessorTests {
this.processor.processRequest(this.conf, this.request, this.response);
assertFalse(this.response.containsHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN));
assertThat(this.response.getHeaders(HttpHeaders.VARY), contains(HttpHeaders.ORIGIN,
HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, HttpHeaders.ACCESS_CONTROL_REQUEST_HEADERS));
assertThat(this.response.getHeaders(HttpHeaders.VARY)).contains(HttpHeaders.ORIGIN,
HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, HttpHeaders.ACCESS_CONTROL_REQUEST_HEADERS);
assertEquals(HttpServletResponse.SC_FORBIDDEN, this.response.getStatus());
}
@@ -117,8 +116,8 @@ public class DefaultCorsProcessorTests {
assertEquals("*", this.response.getHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN));
assertFalse(this.response.containsHeader(HttpHeaders.ACCESS_CONTROL_MAX_AGE));
assertFalse(this.response.containsHeader(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS));
assertThat(this.response.getHeaders(HttpHeaders.VARY), contains(HttpHeaders.ORIGIN,
HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, HttpHeaders.ACCESS_CONTROL_REQUEST_HEADERS));
assertThat(this.response.getHeaders(HttpHeaders.VARY)).contains(HttpHeaders.ORIGIN,
HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, HttpHeaders.ACCESS_CONTROL_REQUEST_HEADERS);
assertEquals(HttpServletResponse.SC_OK, this.response.getStatus());
}
@@ -136,8 +135,8 @@ public class DefaultCorsProcessorTests {
assertEquals("https://domain2.com", this.response.getHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN));
assertTrue(this.response.containsHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS));
assertEquals("true", this.response.getHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS));
assertThat(this.response.getHeaders(HttpHeaders.VARY), contains(HttpHeaders.ORIGIN,
HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, HttpHeaders.ACCESS_CONTROL_REQUEST_HEADERS));
assertThat(this.response.getHeaders(HttpHeaders.VARY)).contains(HttpHeaders.ORIGIN,
HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, HttpHeaders.ACCESS_CONTROL_REQUEST_HEADERS);
assertEquals(HttpServletResponse.SC_OK, this.response.getStatus());
}
@@ -153,8 +152,8 @@ public class DefaultCorsProcessorTests {
assertEquals("https://domain2.com", this.response.getHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN));
assertTrue(this.response.containsHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS));
assertEquals("true", this.response.getHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS));
assertThat(this.response.getHeaders(HttpHeaders.VARY), contains(HttpHeaders.ORIGIN,
HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, HttpHeaders.ACCESS_CONTROL_REQUEST_HEADERS));
assertThat(this.response.getHeaders(HttpHeaders.VARY)).contains(HttpHeaders.ORIGIN,
HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, HttpHeaders.ACCESS_CONTROL_REQUEST_HEADERS);
assertEquals(HttpServletResponse.SC_OK, this.response.getStatus());
}
@@ -166,8 +165,8 @@ public class DefaultCorsProcessorTests {
this.processor.processRequest(this.conf, this.request, this.response);
assertTrue(this.response.containsHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN));
assertThat(this.response.getHeaders(HttpHeaders.VARY), contains(HttpHeaders.ORIGIN,
HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, HttpHeaders.ACCESS_CONTROL_REQUEST_HEADERS));
assertThat(this.response.getHeaders(HttpHeaders.VARY)).contains(HttpHeaders.ORIGIN,
HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, HttpHeaders.ACCESS_CONTROL_REQUEST_HEADERS);
assertEquals(HttpServletResponse.SC_OK, this.response.getStatus());
}
@@ -185,8 +184,8 @@ public class DefaultCorsProcessorTests {
assertTrue(this.response.containsHeader(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS));
assertTrue(this.response.getHeader(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS).contains("header1"));
assertTrue(this.response.getHeader(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS).contains("header2"));
assertThat(this.response.getHeaders(HttpHeaders.VARY), contains(HttpHeaders.ORIGIN,
HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, HttpHeaders.ACCESS_CONTROL_REQUEST_HEADERS));
assertThat(this.response.getHeaders(HttpHeaders.VARY)).contains(HttpHeaders.ORIGIN,
HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, HttpHeaders.ACCESS_CONTROL_REQUEST_HEADERS);
assertEquals(HttpServletResponse.SC_OK, this.response.getStatus());
}
@@ -198,8 +197,8 @@ public class DefaultCorsProcessorTests {
this.conf.addAllowedOrigin("*");
this.processor.processRequest(this.conf, this.request, this.response);
assertThat(this.response.getHeaders(HttpHeaders.VARY), contains(HttpHeaders.ORIGIN,
HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, HttpHeaders.ACCESS_CONTROL_REQUEST_HEADERS));
assertThat(this.response.getHeaders(HttpHeaders.VARY)).contains(HttpHeaders.ORIGIN,
HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, HttpHeaders.ACCESS_CONTROL_REQUEST_HEADERS);
assertEquals(HttpServletResponse.SC_OK, this.response.getStatus());
}
@@ -211,8 +210,8 @@ public class DefaultCorsProcessorTests {
this.conf.addAllowedOrigin("*");
this.processor.processRequest(this.conf, this.request, this.response);
assertThat(this.response.getHeaders(HttpHeaders.VARY), contains(HttpHeaders.ORIGIN,
HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, HttpHeaders.ACCESS_CONTROL_REQUEST_HEADERS));
assertThat(this.response.getHeaders(HttpHeaders.VARY)).contains(HttpHeaders.ORIGIN,
HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, HttpHeaders.ACCESS_CONTROL_REQUEST_HEADERS);
assertEquals(HttpServletResponse.SC_FORBIDDEN, this.response.getStatus());
}
@@ -226,8 +225,8 @@ public class DefaultCorsProcessorTests {
this.processor.processRequest(this.conf, this.request, this.response);
assertEquals(HttpServletResponse.SC_OK, this.response.getStatus());
assertEquals("GET,HEAD", this.response.getHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS));
assertThat(this.response.getHeaders(HttpHeaders.VARY), contains(HttpHeaders.ORIGIN,
HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, HttpHeaders.ACCESS_CONTROL_REQUEST_HEADERS));
assertThat(this.response.getHeaders(HttpHeaders.VARY)).contains(HttpHeaders.ORIGIN,
HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, HttpHeaders.ACCESS_CONTROL_REQUEST_HEADERS);
}
@Test
@@ -237,8 +236,8 @@ public class DefaultCorsProcessorTests {
this.processor.processRequest(this.conf, this.request, this.response);
assertFalse(this.response.containsHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN));
assertThat(this.response.getHeaders(HttpHeaders.VARY), contains(HttpHeaders.ORIGIN,
HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, HttpHeaders.ACCESS_CONTROL_REQUEST_HEADERS));
assertThat(this.response.getHeaders(HttpHeaders.VARY)).contains(HttpHeaders.ORIGIN,
HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, HttpHeaders.ACCESS_CONTROL_REQUEST_HEADERS);
assertEquals(HttpServletResponse.SC_FORBIDDEN, this.response.getStatus());
}
@@ -250,8 +249,8 @@ public class DefaultCorsProcessorTests {
this.processor.processRequest(this.conf, this.request, this.response);
assertFalse(this.response.containsHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN));
assertThat(this.response.getHeaders(HttpHeaders.VARY), contains(HttpHeaders.ORIGIN,
HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, HttpHeaders.ACCESS_CONTROL_REQUEST_HEADERS));
assertThat(this.response.getHeaders(HttpHeaders.VARY)).contains(HttpHeaders.ORIGIN,
HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, HttpHeaders.ACCESS_CONTROL_REQUEST_HEADERS);
assertEquals(HttpServletResponse.SC_FORBIDDEN, this.response.getStatus());
}
@@ -264,8 +263,8 @@ public class DefaultCorsProcessorTests {
this.processor.processRequest(this.conf, this.request, this.response);
assertFalse(this.response.containsHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN));
assertThat(this.response.getHeaders(HttpHeaders.VARY), contains(HttpHeaders.ORIGIN,
HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, HttpHeaders.ACCESS_CONTROL_REQUEST_HEADERS));
assertThat(this.response.getHeaders(HttpHeaders.VARY)).contains(HttpHeaders.ORIGIN,
HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, HttpHeaders.ACCESS_CONTROL_REQUEST_HEADERS);
assertEquals(HttpServletResponse.SC_FORBIDDEN, this.response.getStatus());
}
@@ -287,8 +286,8 @@ public class DefaultCorsProcessorTests {
assertTrue(this.response.containsHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS));
assertEquals("GET,PUT", this.response.getHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS));
assertFalse(this.response.containsHeader(HttpHeaders.ACCESS_CONTROL_MAX_AGE));
assertThat(this.response.getHeaders(HttpHeaders.VARY), contains(HttpHeaders.ORIGIN,
HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, HttpHeaders.ACCESS_CONTROL_REQUEST_HEADERS));
assertThat(this.response.getHeaders(HttpHeaders.VARY)).contains(HttpHeaders.ORIGIN,
HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, HttpHeaders.ACCESS_CONTROL_REQUEST_HEADERS);
assertEquals(HttpServletResponse.SC_OK, this.response.getStatus());
}
@@ -309,8 +308,8 @@ public class DefaultCorsProcessorTests {
assertEquals("https://domain2.com", this.response.getHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN));
assertTrue(this.response.containsHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS));
assertEquals("true", this.response.getHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS));
assertThat(this.response.getHeaders(HttpHeaders.VARY), contains(HttpHeaders.ORIGIN,
HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, HttpHeaders.ACCESS_CONTROL_REQUEST_HEADERS));
assertThat(this.response.getHeaders(HttpHeaders.VARY)).contains(HttpHeaders.ORIGIN,
HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, HttpHeaders.ACCESS_CONTROL_REQUEST_HEADERS);
assertEquals(HttpServletResponse.SC_OK, this.response.getStatus());
}
@@ -329,8 +328,8 @@ public class DefaultCorsProcessorTests {
this.processor.processRequest(this.conf, this.request, this.response);
assertTrue(this.response.containsHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN));
assertEquals("https://domain2.com", this.response.getHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN));
assertThat(this.response.getHeaders(HttpHeaders.VARY), contains(HttpHeaders.ORIGIN,
HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, HttpHeaders.ACCESS_CONTROL_REQUEST_HEADERS));
assertThat(this.response.getHeaders(HttpHeaders.VARY)).contains(HttpHeaders.ORIGIN,
HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, HttpHeaders.ACCESS_CONTROL_REQUEST_HEADERS);
assertEquals(HttpServletResponse.SC_OK, this.response.getStatus());
}
@@ -351,8 +350,8 @@ public class DefaultCorsProcessorTests {
assertTrue(this.response.getHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS).contains("Header1"));
assertTrue(this.response.getHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS).contains("Header2"));
assertFalse(this.response.getHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS).contains("Header3"));
assertThat(this.response.getHeaders(HttpHeaders.VARY), contains(HttpHeaders.ORIGIN,
HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, HttpHeaders.ACCESS_CONTROL_REQUEST_HEADERS));
assertThat(this.response.getHeaders(HttpHeaders.VARY)).contains(HttpHeaders.ORIGIN,
HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, HttpHeaders.ACCESS_CONTROL_REQUEST_HEADERS);
assertEquals(HttpServletResponse.SC_OK, this.response.getStatus());
}
@@ -371,8 +370,8 @@ public class DefaultCorsProcessorTests {
assertTrue(this.response.getHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS).contains("Header1"));
assertTrue(this.response.getHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS).contains("Header2"));
assertFalse(this.response.getHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS).contains("*"));
assertThat(this.response.getHeaders(HttpHeaders.VARY), contains(HttpHeaders.ORIGIN,
HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, HttpHeaders.ACCESS_CONTROL_REQUEST_HEADERS));
assertThat(this.response.getHeaders(HttpHeaders.VARY)).contains(HttpHeaders.ORIGIN,
HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, HttpHeaders.ACCESS_CONTROL_REQUEST_HEADERS);
assertEquals(HttpServletResponse.SC_OK, this.response.getStatus());
}
@@ -388,8 +387,8 @@ public class DefaultCorsProcessorTests {
this.processor.processRequest(this.conf, this.request, this.response);
assertTrue(this.response.containsHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN));
assertFalse(this.response.containsHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_HEADERS));
assertThat(this.response.getHeaders(HttpHeaders.VARY), contains(HttpHeaders.ORIGIN,
HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, HttpHeaders.ACCESS_CONTROL_REQUEST_HEADERS));
assertThat(this.response.getHeaders(HttpHeaders.VARY)).contains(HttpHeaders.ORIGIN,
HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, HttpHeaders.ACCESS_CONTROL_REQUEST_HEADERS);
assertEquals(HttpServletResponse.SC_OK, this.response.getStatus());
}

View File

@@ -28,8 +28,7 @@ import org.springframework.mock.web.test.server.MockServerWebExchange;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.server.ServerWebExchange;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
@@ -70,8 +69,8 @@ public class DefaultCorsProcessorTests {
ServerHttpResponse response = exchange.getResponse();
assertFalse(response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN));
assertThat(response.getHeaders().get(VARY), contains(ORIGIN,
ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS));
assertThat(response.getHeaders().get(VARY)).contains(ORIGIN,
ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS);
assertNull(response.getStatusCode());
}
@@ -86,8 +85,8 @@ public class DefaultCorsProcessorTests {
ServerHttpResponse response = exchange.getResponse();
assertFalse(response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN));
assertThat(response.getHeaders().get(VARY), contains(ORIGIN,
ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS));
assertThat(response.getHeaders().get(VARY)).contains(ORIGIN,
ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS);
assertNull(response.getStatusCode());
}
@@ -98,8 +97,8 @@ public class DefaultCorsProcessorTests {
ServerHttpResponse response = exchange.getResponse();
assertFalse(response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN));
assertThat(response.getHeaders().get(VARY), contains(ORIGIN,
ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS));
assertThat(response.getHeaders().get(VARY)).contains(ORIGIN,
ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS);
assertEquals(HttpStatus.FORBIDDEN, response.getStatusCode());
}
@@ -124,8 +123,8 @@ public class DefaultCorsProcessorTests {
assertEquals("*", response.getHeaders().getFirst(ACCESS_CONTROL_ALLOW_ORIGIN));
assertFalse(response.getHeaders().containsKey(HttpHeaders.ACCESS_CONTROL_MAX_AGE));
assertFalse(response.getHeaders().containsKey(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS));
assertThat(response.getHeaders().get(VARY), contains(ORIGIN,
ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS));
assertThat(response.getHeaders().get(VARY)).contains(ORIGIN,
ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS);
assertNull(response.getStatusCode());
}
@@ -143,8 +142,8 @@ public class DefaultCorsProcessorTests {
assertEquals("https://domain2.com", response.getHeaders().getFirst(ACCESS_CONTROL_ALLOW_ORIGIN));
assertTrue(response.getHeaders().containsKey(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS));
assertEquals("true", response.getHeaders().getFirst(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS));
assertThat(response.getHeaders().get(VARY), contains(ORIGIN,
ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS));
assertThat(response.getHeaders().get(VARY)).contains(ORIGIN,
ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS);
assertNull(response.getStatusCode());
}
@@ -160,8 +159,8 @@ public class DefaultCorsProcessorTests {
assertEquals("https://domain2.com", response.getHeaders().getFirst(ACCESS_CONTROL_ALLOW_ORIGIN));
assertTrue(response.getHeaders().containsKey(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS));
assertEquals("true", response.getHeaders().getFirst(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS));
assertThat(response.getHeaders().get(VARY), contains(ORIGIN,
ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS));
assertThat(response.getHeaders().get(VARY)).contains(ORIGIN,
ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS);
assertNull(response.getStatusCode());
}
@@ -173,8 +172,8 @@ public class DefaultCorsProcessorTests {
ServerHttpResponse response = exchange.getResponse();
assertTrue(response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN));
assertThat(response.getHeaders().get(VARY), contains(ORIGIN,
ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS));
assertThat(response.getHeaders().get(VARY)).contains(ORIGIN,
ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS);
assertNull(response.getStatusCode());
}
@@ -192,8 +191,8 @@ public class DefaultCorsProcessorTests {
assertTrue(response.getHeaders().containsKey(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS));
assertTrue(response.getHeaders().getFirst(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS).contains("header1"));
assertTrue(response.getHeaders().getFirst(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS).contains("header2"));
assertThat(response.getHeaders().get(VARY), contains(ORIGIN,
ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS));
assertThat(response.getHeaders().get(VARY)).contains(ORIGIN,
ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS);
assertNull(response.getStatusCode());
}
@@ -205,8 +204,8 @@ public class DefaultCorsProcessorTests {
this.processor.process(this.conf, exchange);
ServerHttpResponse response = exchange.getResponse();
assertThat(response.getHeaders().get(VARY), contains(ORIGIN,
ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS));
assertThat(response.getHeaders().get(VARY)).contains(ORIGIN,
ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS);
assertNull(response.getStatusCode());
}
@@ -219,8 +218,8 @@ public class DefaultCorsProcessorTests {
this.processor.process(this.conf, exchange);
ServerHttpResponse response = exchange.getResponse();
assertThat(response.getHeaders().get(VARY), contains(ORIGIN,
ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS));
assertThat(response.getHeaders().get(VARY)).contains(ORIGIN,
ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS);
assertEquals(HttpStatus.FORBIDDEN, response.getStatusCode());
}
@@ -233,8 +232,8 @@ public class DefaultCorsProcessorTests {
ServerHttpResponse response = exchange.getResponse();
assertNull(response.getStatusCode());
assertThat(response.getHeaders().get(VARY), contains(ORIGIN,
ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS));
assertThat(response.getHeaders().get(VARY)).contains(ORIGIN,
ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS);
assertEquals("GET,HEAD", response.getHeaders().getFirst(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS));
}
@@ -245,8 +244,8 @@ public class DefaultCorsProcessorTests {
ServerHttpResponse response = exchange.getResponse();
assertFalse(response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN));
assertThat(response.getHeaders().get(VARY), contains(ORIGIN,
ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS));
assertThat(response.getHeaders().get(VARY)).contains(ORIGIN,
ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS);
assertEquals(HttpStatus.FORBIDDEN, response.getStatusCode());
}
@@ -258,8 +257,8 @@ public class DefaultCorsProcessorTests {
ServerHttpResponse response = exchange.getResponse();
assertFalse(response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN));
assertThat(response.getHeaders().get(VARY), contains(ORIGIN,
ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS));
assertThat(response.getHeaders().get(VARY)).contains(ORIGIN,
ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS);
assertEquals(HttpStatus.FORBIDDEN, response.getStatusCode());
}
@@ -273,8 +272,8 @@ public class DefaultCorsProcessorTests {
ServerHttpResponse response = exchange.getResponse();
assertFalse(response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN));
assertThat(response.getHeaders().get(VARY), contains(ORIGIN,
ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS));
assertThat(response.getHeaders().get(VARY)).contains(ORIGIN,
ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS);
assertEquals(HttpStatus.FORBIDDEN, response.getStatusCode());
}
@@ -298,8 +297,8 @@ public class DefaultCorsProcessorTests {
assertTrue(response.getHeaders().containsKey(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS));
assertEquals("GET,PUT", response.getHeaders().getFirst(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS));
assertFalse(response.getHeaders().containsKey(HttpHeaders.ACCESS_CONTROL_MAX_AGE));
assertThat(response.getHeaders().get(VARY), contains(ORIGIN,
ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS));
assertThat(response.getHeaders().get(VARY)).contains(ORIGIN,
ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS);
assertNull(response.getStatusCode());
}
@@ -322,8 +321,8 @@ public class DefaultCorsProcessorTests {
assertEquals("https://domain2.com", response.getHeaders().getFirst(ACCESS_CONTROL_ALLOW_ORIGIN));
assertTrue(response.getHeaders().containsKey(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS));
assertEquals("true", response.getHeaders().getFirst(HttpHeaders.ACCESS_CONTROL_ALLOW_CREDENTIALS));
assertThat(response.getHeaders().get(VARY), contains(ORIGIN,
ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS));
assertThat(response.getHeaders().get(VARY)).contains(ORIGIN,
ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS);
assertNull(response.getStatusCode());
}
@@ -344,8 +343,8 @@ public class DefaultCorsProcessorTests {
ServerHttpResponse response = exchange.getResponse();
assertTrue(response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN));
assertEquals("https://domain2.com", response.getHeaders().getFirst(ACCESS_CONTROL_ALLOW_ORIGIN));
assertThat(response.getHeaders().get(VARY), contains(ORIGIN,
ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS));
assertThat(response.getHeaders().get(VARY)).contains(ORIGIN,
ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS);
assertNull(response.getStatusCode());
}
@@ -368,8 +367,8 @@ public class DefaultCorsProcessorTests {
assertTrue(response.getHeaders().getFirst(ACCESS_CONTROL_ALLOW_HEADERS).contains("Header1"));
assertTrue(response.getHeaders().getFirst(ACCESS_CONTROL_ALLOW_HEADERS).contains("Header2"));
assertFalse(response.getHeaders().getFirst(ACCESS_CONTROL_ALLOW_HEADERS).contains("Header3"));
assertThat(response.getHeaders().get(VARY), contains(ORIGIN,
ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS));
assertThat(response.getHeaders().get(VARY)).contains(ORIGIN,
ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS);
assertNull(response.getStatusCode());
}
@@ -390,8 +389,8 @@ public class DefaultCorsProcessorTests {
assertTrue(response.getHeaders().getFirst(ACCESS_CONTROL_ALLOW_HEADERS).contains("Header1"));
assertTrue(response.getHeaders().getFirst(ACCESS_CONTROL_ALLOW_HEADERS).contains("Header2"));
assertFalse(response.getHeaders().getFirst(ACCESS_CONTROL_ALLOW_HEADERS).contains("*"));
assertThat(response.getHeaders().get(VARY), contains(ORIGIN,
ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS));
assertThat(response.getHeaders().get(VARY)).contains(ORIGIN,
ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS);
assertNull(response.getStatusCode());
}
@@ -409,8 +408,8 @@ public class DefaultCorsProcessorTests {
ServerHttpResponse response = exchange.getResponse();
assertTrue(response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_ORIGIN));
assertFalse(response.getHeaders().containsKey(ACCESS_CONTROL_ALLOW_HEADERS));
assertThat(response.getHeaders().get(VARY), contains(ORIGIN,
ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS));
assertThat(response.getHeaders().get(VARY)).contains(ORIGIN,
ACCESS_CONTROL_REQUEST_METHOD, ACCESS_CONTROL_REQUEST_HEADERS);
assertNull(response.getStatusCode());
}

View File

@@ -18,7 +18,6 @@ package org.springframework.web.filter.reactive;
import java.time.Duration;
import org.hamcrest.Matchers;
import org.junit.Test;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
@@ -31,7 +30,7 @@ import org.springframework.mock.web.test.server.MockServerWebExchange;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilterChain;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
/**
@@ -81,7 +80,7 @@ public class HiddenHttpMethodFilterTests {
public void filterWithInvalidMethodValue() {
StepVerifier.create(postForm("_method=INVALID"))
.consumeErrorWith(error -> {
assertThat(error, Matchers.instanceOf(IllegalArgumentException.class));
assertThat(error).isInstanceOf(IllegalArgumentException.class);
assertEquals("HttpMethod 'INVALID' not supported", error.getMessage());
})
.verify();

View File

@@ -29,8 +29,7 @@ import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.multipart.MultipartFile;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
@@ -86,12 +85,12 @@ public class StandardMultipartHttpServletRequestTests {
MockHttpOutputMessage output = new MockHttpOutputMessage();
new FormHttpMessageConverter().write(map, null, output);
assertThat(output.getBodyAsString(StandardCharsets.UTF_8), containsString(
assertThat(output.getBodyAsString(StandardCharsets.UTF_8)).contains(
"Content-Disposition: form-data; name=\"file\"; filename=\"myFile.txt\"\r\n" +
"Content-Type: text/plain\r\n" +
"Content-Length: 6\r\n" +
"\r\n" +
"myBody\r\n"));
"myBody\r\n");
}

View File

@@ -33,11 +33,8 @@ import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNull;
@@ -715,73 +712,73 @@ public class UriComponentsBuilderTests {
@Test
public void queryParamWithValueWithEquals() {
UriComponents uriComponents = UriComponentsBuilder.fromUriString("https://example.com/foo?bar=baz").build();
assertThat(uriComponents.toUriString(), equalTo("https://example.com/foo?bar=baz"));
assertThat(uriComponents.getQueryParams().get("bar").get(0), equalTo("baz"));
assertThat(uriComponents.toUriString()).isEqualTo("https://example.com/foo?bar=baz");
assertThat(uriComponents.getQueryParams().get("bar").get(0)).isEqualTo("baz");
}
@Test
public void queryParamWithoutValueWithEquals() {
UriComponents uriComponents = UriComponentsBuilder.fromUriString("https://example.com/foo?bar=").build();
assertThat(uriComponents.toUriString(), equalTo("https://example.com/foo?bar="));
assertThat(uriComponents.getQueryParams().get("bar").get(0), equalTo(""));
assertThat(uriComponents.toUriString()).isEqualTo("https://example.com/foo?bar=");
assertThat(uriComponents.getQueryParams().get("bar").get(0)).isEqualTo("");
}
@Test
public void queryParamWithoutValueWithoutEquals() {
UriComponents uriComponents = UriComponentsBuilder.fromUriString("https://example.com/foo?bar").build();
assertThat(uriComponents.toUriString(), equalTo("https://example.com/foo?bar"));
assertThat(uriComponents.toUriString()).isEqualTo("https://example.com/foo?bar");
// TODO [SPR-13537] Change equalTo(null) to equalTo("").
assertThat(uriComponents.getQueryParams().get("bar").get(0), equalTo(null));
assertThat(uriComponents.getQueryParams().get("bar").get(0)).isNull();
}
@Test
public void relativeUrls() {
String baseUrl = "https://example.com";
assertThat(UriComponentsBuilder.fromUriString(baseUrl + "/foo/../bar").build().toString(),
equalTo(baseUrl + "/foo/../bar"));
assertThat(UriComponentsBuilder.fromUriString(baseUrl + "/foo/../bar").build().toUriString(),
equalTo(baseUrl + "/foo/../bar"));
assertThat(UriComponentsBuilder.fromUriString(baseUrl + "/foo/../bar").build().toUri().getPath(),
equalTo("/foo/../bar"));
assertThat(UriComponentsBuilder.fromUriString("../../").build().toString(),
equalTo("../../"));
assertThat(UriComponentsBuilder.fromUriString("../../").build().toUriString(),
equalTo("../../"));
assertThat(UriComponentsBuilder.fromUriString("../../").build().toUri().getPath(),
equalTo("../../"));
assertThat(UriComponentsBuilder.fromUriString(baseUrl).path("foo/../bar").build().toString(),
equalTo(baseUrl + "/foo/../bar"));
assertThat(UriComponentsBuilder.fromUriString(baseUrl).path("foo/../bar").build().toUriString(),
equalTo(baseUrl + "/foo/../bar"));
assertThat(UriComponentsBuilder.fromUriString(baseUrl).path("foo/../bar").build().toUri().getPath(),
equalTo("/foo/../bar"));
assertThat(UriComponentsBuilder.fromUriString(baseUrl + "/foo/../bar").build().toString())
.isEqualTo(baseUrl + "/foo/../bar");
assertThat(UriComponentsBuilder.fromUriString(baseUrl + "/foo/../bar").build().toUriString())
.isEqualTo(baseUrl + "/foo/../bar");
assertThat(UriComponentsBuilder.fromUriString(baseUrl + "/foo/../bar").build().toUri().getPath())
.isEqualTo("/foo/../bar");
assertThat(UriComponentsBuilder.fromUriString("../../").build().toString())
.isEqualTo("../../");
assertThat(UriComponentsBuilder.fromUriString("../../").build().toUriString())
.isEqualTo("../../");
assertThat(UriComponentsBuilder.fromUriString("../../").build().toUri().getPath())
.isEqualTo("../../");
assertThat(UriComponentsBuilder.fromUriString(baseUrl).path("foo/../bar").build().toString())
.isEqualTo(baseUrl + "/foo/../bar");
assertThat(UriComponentsBuilder.fromUriString(baseUrl).path("foo/../bar").build().toUriString())
.isEqualTo(baseUrl + "/foo/../bar");
assertThat(UriComponentsBuilder.fromUriString(baseUrl).path("foo/../bar").build().toUri().getPath())
.isEqualTo("/foo/../bar");
}
@Test
public void emptySegments() {
String baseUrl = "https://example.com/abc/";
assertThat(UriComponentsBuilder.fromUriString(baseUrl).path("/x/y/z").build().toString(),
equalTo("https://example.com/abc/x/y/z"));
assertThat(UriComponentsBuilder.fromUriString(baseUrl).pathSegment("x", "y", "z").build().toString(),
equalTo("https://example.com/abc/x/y/z"));
assertThat(UriComponentsBuilder.fromUriString(baseUrl).path("/x/").path("/y/z").build().toString(),
equalTo("https://example.com/abc/x/y/z"));
assertThat(UriComponentsBuilder.fromUriString(baseUrl).pathSegment("x").path("y").build().toString(),
equalTo("https://example.com/abc/x/y"));
assertThat(UriComponentsBuilder.fromUriString(baseUrl).path("/x/y/z").build().toString())
.isEqualTo("https://example.com/abc/x/y/z");
assertThat(UriComponentsBuilder.fromUriString(baseUrl).pathSegment("x", "y", "z").build().toString())
.isEqualTo("https://example.com/abc/x/y/z");
assertThat(UriComponentsBuilder.fromUriString(baseUrl).path("/x/").path("/y/z").build().toString())
.isEqualTo("https://example.com/abc/x/y/z");
assertThat(UriComponentsBuilder.fromUriString(baseUrl).pathSegment("x").path("y").build().toString())
.isEqualTo("https://example.com/abc/x/y");
}
@Test
public void parsesEmptyFragment() {
UriComponents components = UriComponentsBuilder.fromUriString("/example#").build();
assertThat(components.getFragment(), is(nullValue()));
assertThat(components.toString(), equalTo("/example"));
assertThat(components.getFragment()).isNull();
assertThat(components.toString()).isEqualTo("/example");
}
@Test // SPR-13257
public void parsesEmptyUri() {
UriComponents components = UriComponentsBuilder.fromUriString("").build();
assertThat(components.toString(), equalTo(""));
assertThat(components.toString()).isEqualTo("");
}
@Test

View File

@@ -27,12 +27,9 @@ import java.util.Collections;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertEquals;
import static org.springframework.web.util.UriComponentsBuilder.fromUriString;
@@ -193,7 +190,7 @@ public class UriComponentsTests {
oos.writeObject(uriComponents);
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()));
UriComponents readObject = (UriComponents) ois.readObject();
assertThat(uriComponents.toString(), equalTo(readObject.toString()));
assertThat(uriComponents.toString()).isEqualTo(readObject.toString());
}
@Test
@@ -212,10 +209,10 @@ public class UriComponentsTests {
UriComponents uric1 = UriComponentsBuilder.fromUriString(url).path("/{foo}").query("bar={baz}").build();
UriComponents uric2 = UriComponentsBuilder.fromUriString(url).path("/{foo}").query("bar={baz}").build();
UriComponents uric3 = UriComponentsBuilder.fromUriString(url).path("/{foo}").query("bin={baz}").build();
assertThat(uric1, instanceOf(HierarchicalUriComponents.class));
assertThat(uric1, equalTo(uric1));
assertThat(uric1, equalTo(uric2));
assertThat(uric1, not(equalTo(uric3)));
assertThat(uric1).isInstanceOf(HierarchicalUriComponents.class);
assertThat(uric1).isEqualTo(uric1);
assertThat(uric1).isEqualTo(uric2);
assertThat(uric1).isNotEqualTo(uric3);
}
@Test
@@ -224,10 +221,10 @@ public class UriComponentsTests {
UriComponents uric1 = UriComponentsBuilder.fromUriString(baseUrl + "/foo/bar").build();
UriComponents uric2 = UriComponentsBuilder.fromUriString(baseUrl + "/foo/bar").build();
UriComponents uric3 = UriComponentsBuilder.fromUriString(baseUrl + "/foo/bin").build();
assertThat(uric1, instanceOf(OpaqueUriComponents.class));
assertThat(uric1, equalTo(uric1));
assertThat(uric1, equalTo(uric2));
assertThat(uric1, not(equalTo(uric3)));
assertThat(uric1).isInstanceOf(OpaqueUriComponents.class);
assertThat(uric1).isEqualTo(uric1);
assertThat(uric1).isEqualTo(uric2);
assertThat(uric1).isNotEqualTo(uric3);
}
}

View File

@@ -23,7 +23,6 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.springframework.http.server.PathContainer;
@@ -33,7 +32,6 @@ import org.springframework.web.util.pattern.PathPattern.PathRemainingMatchInfo;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
@@ -981,7 +979,7 @@ public class PathPatternTests {
// Comparator<String> patternComparator = new AntPathMatcher().getPatternComparator("");
// System.out.println(patternComparator.compare("/{foo}","/{foo}.*"));
assertThat(p1.compareTo(p2), Matchers.greaterThan(0));
assertThat(p1.compareTo(p2)).isGreaterThan(0);
}
@Test