23 changed files with 560 additions and 1004 deletions
@ -1,145 +0,0 @@
@@ -1,145 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-present the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.test.web.reactive.server; |
||||
|
||||
import java.net.URI; |
||||
import java.time.Duration; |
||||
|
||||
import org.junit.jupiter.api.Test; |
||||
import reactor.core.publisher.Mono; |
||||
|
||||
import org.springframework.http.HttpMethod; |
||||
import org.springframework.http.HttpStatus; |
||||
import org.springframework.http.ResponseCookie; |
||||
import org.springframework.mock.http.client.reactive.MockClientHttpRequest; |
||||
import org.springframework.mock.http.client.reactive.MockClientHttpResponse; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
||||
import static org.hamcrest.Matchers.equalTo; |
||||
import static org.mockito.Mockito.mock; |
||||
|
||||
/** |
||||
* Tests for {@link CookieAssertions} |
||||
* |
||||
* @author Rossen Stoyanchev |
||||
*/ |
||||
public class CookieAssertionsTests { |
||||
|
||||
private final ResponseCookie cookie = ResponseCookie.from("foo", "bar") |
||||
.maxAge(Duration.ofMinutes(30)) |
||||
.domain("foo.com") |
||||
.path("/foo") |
||||
.secure(true) |
||||
.httpOnly(true) |
||||
.partitioned(true) |
||||
.sameSite("Lax") |
||||
.build(); |
||||
|
||||
private final CookieAssertions assertions = cookieAssertions(cookie); |
||||
|
||||
|
||||
@Test |
||||
void valueEquals() { |
||||
assertions.valueEquals("foo", "bar"); |
||||
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> assertions.valueEquals("what?!", "bar")); |
||||
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> assertions.valueEquals("foo", "what?!")); |
||||
} |
||||
|
||||
@Test |
||||
void value() { |
||||
assertions.value("foo", equalTo("bar")); |
||||
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> assertions.value("foo", equalTo("what?!"))); |
||||
} |
||||
|
||||
@Test |
||||
void exists() { |
||||
assertions.exists("foo"); |
||||
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> assertions.exists("what?!")); |
||||
} |
||||
|
||||
@Test |
||||
void doesNotExist() { |
||||
assertions.doesNotExist("what?!"); |
||||
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> assertions.doesNotExist("foo")); |
||||
} |
||||
|
||||
@Test |
||||
void maxAge() { |
||||
assertions.maxAge("foo", Duration.ofMinutes(30)); |
||||
assertThatExceptionOfType(AssertionError.class) |
||||
.isThrownBy(() -> assertions.maxAge("foo", Duration.ofMinutes(29))); |
||||
|
||||
assertions.maxAge("foo", equalTo(Duration.ofMinutes(30).getSeconds())); |
||||
assertThatExceptionOfType(AssertionError.class) |
||||
.isThrownBy(() -> assertions.maxAge("foo", equalTo(Duration.ofMinutes(29).getSeconds()))); |
||||
} |
||||
|
||||
@Test |
||||
void domain() { |
||||
assertions.domain("foo", "foo.com"); |
||||
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> assertions.domain("foo", "what.com")); |
||||
|
||||
assertions.domain("foo", equalTo("foo.com")); |
||||
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> assertions.domain("foo", equalTo("what.com"))); |
||||
} |
||||
|
||||
@Test |
||||
void path() { |
||||
assertions.path("foo", "/foo"); |
||||
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> assertions.path("foo", "/what")); |
||||
|
||||
assertions.path("foo", equalTo("/foo")); |
||||
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> assertions.path("foo", equalTo("/what"))); |
||||
} |
||||
|
||||
@Test |
||||
void secure() { |
||||
assertions.secure("foo", true); |
||||
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> assertions.secure("foo", false)); |
||||
} |
||||
|
||||
@Test |
||||
void httpOnly() { |
||||
assertions.httpOnly("foo", true); |
||||
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> assertions.httpOnly("foo", false)); |
||||
} |
||||
|
||||
@Test |
||||
void partitioned() { |
||||
assertions.partitioned("foo", true); |
||||
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> assertions.partitioned("foo", false)); |
||||
} |
||||
|
||||
@Test |
||||
void sameSite() { |
||||
assertions.sameSite("foo", "Lax"); |
||||
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> assertions.sameSite("foo", "Strict")); |
||||
} |
||||
|
||||
|
||||
private CookieAssertions cookieAssertions(ResponseCookie cookie) { |
||||
MockClientHttpRequest request = new MockClientHttpRequest(HttpMethod.GET, URI.create("/")); |
||||
MockClientHttpResponse response = new MockClientHttpResponse(HttpStatus.OK); |
||||
response.getCookies().add(cookie.getName(), cookie); |
||||
|
||||
ExchangeResult result = new ExchangeResult( |
||||
request, response, Mono.empty(), Mono.empty(), Duration.ZERO, null, null); |
||||
|
||||
return new CookieAssertions(result, mock()); |
||||
} |
||||
|
||||
} |
||||
@ -1,259 +0,0 @@
@@ -1,259 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-present the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.test.web.reactive.server; |
||||
|
||||
import java.net.URI; |
||||
import java.time.Duration; |
||||
import java.time.ZoneId; |
||||
import java.time.ZonedDateTime; |
||||
import java.util.concurrent.TimeUnit; |
||||
|
||||
import org.junit.jupiter.api.Test; |
||||
import reactor.core.publisher.Mono; |
||||
|
||||
import org.springframework.http.CacheControl; |
||||
import org.springframework.http.HttpHeaders; |
||||
import org.springframework.http.HttpMethod; |
||||
import org.springframework.http.HttpStatus; |
||||
import org.springframework.http.MediaType; |
||||
import org.springframework.mock.http.client.reactive.MockClientHttpRequest; |
||||
import org.springframework.mock.http.client.reactive.MockClientHttpResponse; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
||||
import static org.hamcrest.Matchers.containsString; |
||||
import static org.hamcrest.Matchers.hasItems; |
||||
import static org.mockito.Mockito.mock; |
||||
|
||||
/** |
||||
* Tests for {@link HeaderAssertions}. |
||||
* |
||||
* @author Rossen Stoyanchev |
||||
* @author Sam Brannen |
||||
*/ |
||||
class HeaderAssertionTests { |
||||
|
||||
@Test |
||||
void valueEquals() { |
||||
HttpHeaders headers = new HttpHeaders(); |
||||
headers.add("foo", "bar"); |
||||
HeaderAssertions assertions = headerAssertions(headers); |
||||
|
||||
// Success
|
||||
assertions.valueEquals("foo", "bar"); |
||||
|
||||
// Missing header
|
||||
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> |
||||
assertions.valueEquals("what?!", "bar")); |
||||
|
||||
// Wrong value
|
||||
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> |
||||
assertions.valueEquals("foo", "what?!")); |
||||
|
||||
// Wrong # of values
|
||||
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> |
||||
assertions.valueEquals("foo", "bar", "what?!")); |
||||
} |
||||
|
||||
@Test |
||||
void valueEqualsWithMultipleValues() { |
||||
HttpHeaders headers = new HttpHeaders(); |
||||
headers.add("foo", "bar"); |
||||
headers.add("foo", "baz"); |
||||
HeaderAssertions assertions = headerAssertions(headers); |
||||
|
||||
// Success
|
||||
assertions.valueEquals("foo", "bar", "baz"); |
||||
|
||||
// Wrong value
|
||||
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> |
||||
assertions.valueEquals("foo", "bar", "what?!")); |
||||
|
||||
// Too few values
|
||||
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> |
||||
assertions.valueEquals("foo", "bar")); |
||||
} |
||||
|
||||
@Test |
||||
void valueMatches() { |
||||
HttpHeaders headers = new HttpHeaders(); |
||||
headers.setContentType(MediaType.parseMediaType("application/json;charset=UTF-8")); |
||||
HeaderAssertions assertions = headerAssertions(headers); |
||||
|
||||
// Success
|
||||
assertions.valueMatches("Content-Type", ".*UTF-8.*"); |
||||
|
||||
// Wrong pattern
|
||||
assertThatExceptionOfType(AssertionError.class) |
||||
.isThrownBy(() -> assertions.valueMatches("Content-Type", ".*ISO-8859-1.*")) |
||||
.satisfies(ex -> assertThat(ex).hasMessage("Response header " + |
||||
"'Content-Type'=[application/json;charset=UTF-8] does not match " + |
||||
"[.*ISO-8859-1.*]")); |
||||
} |
||||
|
||||
@Test |
||||
void valueMatchesWithNonexistentHeader() { |
||||
HttpHeaders headers = new HttpHeaders(); |
||||
headers.setContentType(MediaType.parseMediaType("application/json;charset=UTF-8")); |
||||
HeaderAssertions assertions = headerAssertions(headers); |
||||
|
||||
assertThatExceptionOfType(AssertionError.class) |
||||
.isThrownBy(() -> assertions.valueMatches("Content-XYZ", ".*ISO-8859-1.*")) |
||||
.withMessage("Response header 'Content-XYZ' not found"); |
||||
} |
||||
|
||||
@Test |
||||
void valuesMatch() { |
||||
HttpHeaders headers = new HttpHeaders(); |
||||
headers.add("foo", "value1"); |
||||
headers.add("foo", "value2"); |
||||
headers.add("foo", "value3"); |
||||
HeaderAssertions assertions = headerAssertions(headers); |
||||
|
||||
assertions.valuesMatch("foo", "val.*1", "val.*2", "val.*3"); |
||||
|
||||
assertThatExceptionOfType(AssertionError.class) |
||||
.isThrownBy(() -> assertions.valuesMatch("foo", ".*", "val.*5")) |
||||
.satisfies(ex -> assertThat(ex).hasMessage( |
||||
"Response header 'foo' has fewer or more values [value1, value2, value3] " + |
||||
"than number of patterns to match with [.*, val.*5]")); |
||||
|
||||
assertThatExceptionOfType(AssertionError.class) |
||||
.isThrownBy(() -> assertions.valuesMatch("foo", ".*", "val.*5", ".*")) |
||||
.satisfies(ex -> assertThat(ex).hasMessage( |
||||
"Response header 'foo'[1]='value2' does not match 'val.*5'")); |
||||
} |
||||
|
||||
@Test |
||||
void valueMatcher() { |
||||
HttpHeaders headers = new HttpHeaders(); |
||||
headers.add("foo", "bar"); |
||||
HeaderAssertions assertions = headerAssertions(headers); |
||||
|
||||
assertions.value("foo", containsString("a")); |
||||
} |
||||
|
||||
@Test |
||||
void valuesMatcher() { |
||||
HttpHeaders headers = new HttpHeaders(); |
||||
headers.add("foo", "bar"); |
||||
headers.add("foo", "baz"); |
||||
HeaderAssertions assertions = headerAssertions(headers); |
||||
|
||||
assertions.values("foo", hasItems("bar", "baz")); |
||||
} |
||||
|
||||
@Test |
||||
void exists() { |
||||
HttpHeaders headers = new HttpHeaders(); |
||||
headers.setContentType(MediaType.APPLICATION_JSON); |
||||
HeaderAssertions assertions = headerAssertions(headers); |
||||
|
||||
// Success
|
||||
assertions.exists("Content-Type"); |
||||
|
||||
// Header should not exist
|
||||
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> |
||||
assertions.exists("Framework")) |
||||
.satisfies(ex -> assertThat(ex).hasMessage("Response header 'Framework' does not exist")); |
||||
} |
||||
|
||||
@Test |
||||
void doesNotExist() { |
||||
HttpHeaders headers = new HttpHeaders(); |
||||
headers.setContentType(MediaType.parseMediaType("application/json;charset=UTF-8")); |
||||
HeaderAssertions assertions = headerAssertions(headers); |
||||
|
||||
// Success
|
||||
assertions.doesNotExist("Framework"); |
||||
|
||||
// Existing header
|
||||
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> |
||||
assertions.doesNotExist("Content-Type")) |
||||
.satisfies(ex -> assertThat(ex).hasMessage("Response header " + |
||||
"'Content-Type' exists with value=[application/json;charset=UTF-8]")); |
||||
} |
||||
|
||||
@Test |
||||
void contentTypeCompatibleWith() { |
||||
HttpHeaders headers = new HttpHeaders(); |
||||
headers.setContentType(MediaType.APPLICATION_XML); |
||||
HeaderAssertions assertions = headerAssertions(headers); |
||||
|
||||
// Success
|
||||
assertions.contentTypeCompatibleWith(MediaType.parseMediaType("application/*")); |
||||
|
||||
// MediaTypes not compatible
|
||||
assertThatExceptionOfType(AssertionError.class) |
||||
.isThrownBy(() -> assertions.contentTypeCompatibleWith(MediaType.TEXT_XML)) |
||||
.withMessage("Response header 'Content-Type'=[application/xml] is not compatible with [text/xml]"); |
||||
} |
||||
|
||||
@Test |
||||
void cacheControl() { |
||||
CacheControl control = CacheControl.maxAge(1, TimeUnit.HOURS).noTransform(); |
||||
|
||||
HttpHeaders headers = new HttpHeaders(); |
||||
headers.setCacheControl(control.getHeaderValue()); |
||||
HeaderAssertions assertions = headerAssertions(headers); |
||||
|
||||
// Success
|
||||
assertions.cacheControl(control); |
||||
|
||||
// Wrong value
|
||||
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> |
||||
assertions.cacheControl(CacheControl.noStore())); |
||||
} |
||||
|
||||
@Test |
||||
void expires() { |
||||
HttpHeaders headers = new HttpHeaders(); |
||||
ZonedDateTime expires = ZonedDateTime.of(2018, 1, 1, 0, 0, 0, 0, ZoneId.of("UTC")); |
||||
headers.setExpires(expires); |
||||
HeaderAssertions assertions = headerAssertions(headers); |
||||
assertions.expires(expires.toInstant().toEpochMilli()); |
||||
|
||||
// Wrong value
|
||||
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> |
||||
assertions.expires(expires.toInstant().toEpochMilli() + 1)); |
||||
} |
||||
|
||||
@Test |
||||
void lastModified() { |
||||
HttpHeaders headers = new HttpHeaders(); |
||||
ZonedDateTime lastModified = ZonedDateTime.of(2018, 1, 1, 0, 0, 0, 0, ZoneId.of("UTC")); |
||||
headers.setLastModified(lastModified.toInstant().toEpochMilli()); |
||||
HeaderAssertions assertions = headerAssertions(headers); |
||||
assertions.lastModified(lastModified.toInstant().toEpochMilli()); |
||||
|
||||
// Wrong value
|
||||
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> |
||||
assertions.lastModified(lastModified.toInstant().toEpochMilli() + 1)); |
||||
} |
||||
|
||||
private HeaderAssertions headerAssertions(HttpHeaders responseHeaders) { |
||||
MockClientHttpRequest request = new MockClientHttpRequest(HttpMethod.GET, URI.create("/")); |
||||
MockClientHttpResponse response = new MockClientHttpResponse(HttpStatus.OK); |
||||
response.getHeaders().putAll(responseHeaders); |
||||
|
||||
ExchangeResult result = new ExchangeResult( |
||||
request, response, Mono.empty(), Mono.empty(), Duration.ZERO, null, null); |
||||
|
||||
return new HeaderAssertions(result, mock()); |
||||
} |
||||
|
||||
} |
||||
@ -1,176 +0,0 @@
@@ -1,176 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-present the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.test.web.reactive.server; |
||||
|
||||
import java.net.URI; |
||||
import java.time.Duration; |
||||
|
||||
import org.junit.jupiter.api.Test; |
||||
import reactor.core.publisher.Mono; |
||||
|
||||
import org.springframework.http.HttpMethod; |
||||
import org.springframework.http.HttpStatus; |
||||
import org.springframework.mock.http.client.reactive.MockClientHttpRequest; |
||||
import org.springframework.mock.http.client.reactive.MockClientHttpResponse; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
||||
import static org.hamcrest.Matchers.equalTo; |
||||
import static org.hamcrest.Matchers.greaterThan; |
||||
import static org.mockito.Mockito.mock; |
||||
|
||||
/** |
||||
* Tests for {@link StatusAssertions}. |
||||
* |
||||
* @author Rossen Stoyanchev |
||||
* @author Sam Brannen |
||||
*/ |
||||
class StatusAssertionTests { |
||||
|
||||
@Test |
||||
void isEqualTo() { |
||||
StatusAssertions assertions = statusAssertions(HttpStatus.CONFLICT); |
||||
|
||||
// Success
|
||||
assertions.isEqualTo(HttpStatus.CONFLICT); |
||||
assertions.isEqualTo(409); |
||||
|
||||
// Wrong status
|
||||
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> |
||||
assertions.isEqualTo(HttpStatus.REQUEST_TIMEOUT)); |
||||
|
||||
// Wrong status value
|
||||
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> |
||||
assertions.isEqualTo(408)); |
||||
} |
||||
|
||||
@Test // gh-23630, gh-29283
|
||||
void isEqualToWithCustomStatus() { |
||||
StatusAssertions assertions = statusAssertions(600); |
||||
|
||||
// Success
|
||||
// assertions.isEqualTo(600);
|
||||
|
||||
// Wrong status
|
||||
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> |
||||
assertions.isEqualTo(HttpStatus.REQUEST_TIMEOUT)); |
||||
|
||||
// Wrong status value
|
||||
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> |
||||
assertions.isEqualTo(408)); |
||||
} |
||||
|
||||
@Test |
||||
void reasonEquals() { |
||||
StatusAssertions assertions = statusAssertions(HttpStatus.CONFLICT); |
||||
|
||||
// Success
|
||||
assertions.reasonEquals("Conflict"); |
||||
|
||||
// Wrong reason
|
||||
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> |
||||
assertions.reasonEquals("Request Timeout")); |
||||
} |
||||
|
||||
@Test |
||||
void statusSeries1xx() { |
||||
StatusAssertions assertions = statusAssertions(HttpStatus.CONTINUE); |
||||
|
||||
// Success
|
||||
assertions.is1xxInformational(); |
||||
|
||||
// Wrong series
|
||||
assertThatExceptionOfType(AssertionError.class).isThrownBy(assertions::is2xxSuccessful); |
||||
} |
||||
|
||||
@Test |
||||
void statusSeries2xx() { |
||||
StatusAssertions assertions = statusAssertions(HttpStatus.OK); |
||||
|
||||
// Success
|
||||
assertions.is2xxSuccessful(); |
||||
|
||||
// Wrong series
|
||||
assertThatExceptionOfType(AssertionError.class).isThrownBy(assertions::is5xxServerError); |
||||
} |
||||
|
||||
@Test |
||||
void statusSeries3xx() { |
||||
StatusAssertions assertions = statusAssertions(HttpStatus.PERMANENT_REDIRECT); |
||||
|
||||
// Success
|
||||
assertions.is3xxRedirection(); |
||||
|
||||
// Wrong series
|
||||
assertThatExceptionOfType(AssertionError.class).isThrownBy(assertions::is2xxSuccessful); |
||||
} |
||||
|
||||
@Test |
||||
void statusSeries4xx() { |
||||
StatusAssertions assertions = statusAssertions(HttpStatus.BAD_REQUEST); |
||||
|
||||
// Success
|
||||
assertions.is4xxClientError(); |
||||
|
||||
// Wrong series
|
||||
assertThatExceptionOfType(AssertionError.class).isThrownBy(assertions::is2xxSuccessful); |
||||
} |
||||
|
||||
@Test |
||||
void statusSeries5xx() { |
||||
StatusAssertions assertions = statusAssertions(HttpStatus.INTERNAL_SERVER_ERROR); |
||||
|
||||
// Success
|
||||
assertions.is5xxServerError(); |
||||
|
||||
// Wrong series
|
||||
assertThatExceptionOfType(AssertionError.class).isThrownBy(assertions::is2xxSuccessful); |
||||
} |
||||
|
||||
@Test |
||||
void matchesStatusValue() { |
||||
StatusAssertions assertions = statusAssertions(HttpStatus.CONFLICT); |
||||
|
||||
// Success
|
||||
assertions.value(equalTo(409)); |
||||
assertions.value(greaterThan(400)); |
||||
|
||||
// Wrong status
|
||||
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> |
||||
assertions.value(equalTo(200))); |
||||
} |
||||
|
||||
@Test // gh-26658
|
||||
void matchesCustomStatusValue() { |
||||
statusAssertions(600).value(equalTo(600)); |
||||
} |
||||
|
||||
|
||||
private StatusAssertions statusAssertions(HttpStatus status) { |
||||
return statusAssertions(status.value()); |
||||
} |
||||
|
||||
private StatusAssertions statusAssertions(int status) { |
||||
MockClientHttpRequest request = new MockClientHttpRequest(HttpMethod.GET, URI.create("/")); |
||||
MockClientHttpResponse response = new MockClientHttpResponse(status); |
||||
|
||||
ExchangeResult result = new ExchangeResult( |
||||
request, response, Mono.empty(), Mono.empty(), Duration.ZERO, null, null); |
||||
|
||||
return new StatusAssertions(result, mock()); |
||||
} |
||||
|
||||
} |
||||
@ -1,269 +0,0 @@
@@ -1,269 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-present the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.test.web.servlet.client; |
||||
|
||||
import java.io.IOException; |
||||
|
||||
import org.junit.jupiter.api.Test; |
||||
|
||||
import org.springframework.http.HttpHeaders; |
||||
import org.springframework.http.HttpStatus; |
||||
import org.springframework.http.HttpStatusCode; |
||||
import org.springframework.mock.http.client.MockClientHttpRequest; |
||||
import org.springframework.web.client.RestClient; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
||||
import static org.hamcrest.Matchers.equalTo; |
||||
import static org.hamcrest.Matchers.greaterThan; |
||||
import static org.mockito.BDDMockito.mock; |
||||
import static org.mockito.BDDMockito.when; |
||||
|
||||
/** |
||||
* Tests for {@link StatusAssertions}. |
||||
* |
||||
* @author Rob Worsnop |
||||
*/ |
||||
class StatusAssertionTests { |
||||
|
||||
@Test |
||||
void isEqualTo() { |
||||
StatusAssertions assertions = statusAssertions(HttpStatus.CONFLICT); |
||||
|
||||
// Success
|
||||
assertions.isEqualTo(HttpStatus.CONFLICT); |
||||
assertions.isEqualTo(409); |
||||
|
||||
// Wrong status
|
||||
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> |
||||
assertions.isEqualTo(HttpStatus.REQUEST_TIMEOUT)); |
||||
|
||||
// Wrong status value
|
||||
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> |
||||
assertions.isEqualTo(408)); |
||||
} |
||||
|
||||
@Test |
||||
void isEqualToWithCustomStatus() { |
||||
StatusAssertions assertions = statusAssertions(600); |
||||
|
||||
// Success
|
||||
assertions.isEqualTo(600); |
||||
|
||||
// Wrong status
|
||||
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> |
||||
statusAssertions(601).isEqualTo(600)); |
||||
|
||||
} |
||||
|
||||
@Test |
||||
void reasonEquals() { |
||||
StatusAssertions assertions = statusAssertions(HttpStatus.CONFLICT); |
||||
|
||||
// Success
|
||||
assertions.reasonEquals("Conflict"); |
||||
|
||||
// Wrong reason
|
||||
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> |
||||
statusAssertions(HttpStatus.INTERNAL_SERVER_ERROR).reasonEquals("Conflict")); |
||||
} |
||||
|
||||
@Test |
||||
void statusSeries1xx() { |
||||
StatusAssertions assertions = statusAssertions(HttpStatus.CONTINUE); |
||||
|
||||
// Success
|
||||
assertions.is1xxInformational(); |
||||
|
||||
// Wrong series
|
||||
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> |
||||
statusAssertions(HttpStatus.OK).is1xxInformational()); |
||||
} |
||||
|
||||
@Test |
||||
void statusSeries2xx() { |
||||
StatusAssertions assertions = statusAssertions(HttpStatus.OK); |
||||
|
||||
// Success
|
||||
assertions.is2xxSuccessful(); |
||||
|
||||
// Wrong series
|
||||
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> |
||||
statusAssertions(HttpStatus.INTERNAL_SERVER_ERROR).is2xxSuccessful()); |
||||
} |
||||
|
||||
@Test |
||||
void statusSeries3xx() { |
||||
StatusAssertions assertions = statusAssertions(HttpStatus.PERMANENT_REDIRECT); |
||||
|
||||
// Success
|
||||
assertions.is3xxRedirection(); |
||||
|
||||
// Wrong series
|
||||
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> |
||||
statusAssertions(HttpStatus.INTERNAL_SERVER_ERROR).is3xxRedirection()); |
||||
} |
||||
|
||||
@Test |
||||
void statusSeries4xx() { |
||||
StatusAssertions assertions = statusAssertions(HttpStatus.BAD_REQUEST); |
||||
|
||||
// Success
|
||||
assertions.is4xxClientError(); |
||||
|
||||
// Wrong series
|
||||
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> |
||||
statusAssertions(HttpStatus.INTERNAL_SERVER_ERROR).is4xxClientError()); |
||||
} |
||||
|
||||
@Test |
||||
void statusSeries5xx() { |
||||
StatusAssertions assertions = statusAssertions(HttpStatus.INTERNAL_SERVER_ERROR); |
||||
|
||||
// Success
|
||||
assertions.is5xxServerError(); |
||||
|
||||
// Wrong series
|
||||
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> |
||||
statusAssertions(HttpStatus.OK).is5xxServerError()); |
||||
} |
||||
|
||||
@Test |
||||
void matchesStatusValue() { |
||||
StatusAssertions assertions = statusAssertions(HttpStatus.CONFLICT); |
||||
|
||||
// Success
|
||||
assertions.value(equalTo(409)); |
||||
assertions.value(greaterThan(400)); |
||||
|
||||
// Wrong status
|
||||
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> |
||||
assertions.value(equalTo(200))); |
||||
} |
||||
|
||||
@Test |
||||
void matchesCustomStatusValue() { |
||||
statusAssertions(600).value(equalTo(600)); |
||||
} |
||||
|
||||
@Test |
||||
void consumesStatusValue() { |
||||
StatusAssertions assertions = statusAssertions(HttpStatus.CONFLICT); |
||||
|
||||
// Success
|
||||
assertions.value((Integer value) -> assertThat(value).isEqualTo(409)); |
||||
} |
||||
|
||||
@Test |
||||
void statusIsAccepted() { |
||||
// Success
|
||||
statusAssertions(HttpStatus.ACCEPTED).isAccepted(); |
||||
|
||||
// Wrong status
|
||||
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> statusAssertions(HttpStatus.INTERNAL_SERVER_ERROR).isAccepted()); |
||||
} |
||||
|
||||
@Test |
||||
void statusIsNoContent() { |
||||
// Success
|
||||
statusAssertions(HttpStatus.NO_CONTENT).isNoContent(); |
||||
|
||||
// Wrong status
|
||||
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> statusAssertions(HttpStatus.INTERNAL_SERVER_ERROR).isNoContent()); |
||||
} |
||||
|
||||
@Test |
||||
void statusIsFound() { |
||||
// Success
|
||||
statusAssertions(HttpStatus.FOUND).isFound(); |
||||
|
||||
// Wrong status
|
||||
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> statusAssertions(HttpStatus.INTERNAL_SERVER_ERROR).isFound()); |
||||
} |
||||
|
||||
@Test |
||||
void statusIsSeeOther() { |
||||
// Success
|
||||
statusAssertions(HttpStatus.SEE_OTHER).isSeeOther(); |
||||
|
||||
// Wrong status
|
||||
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> statusAssertions(HttpStatus.INTERNAL_SERVER_ERROR).isSeeOther()); |
||||
} |
||||
|
||||
@Test |
||||
void statusIsNotModified() { |
||||
// Success
|
||||
statusAssertions(HttpStatus.NOT_MODIFIED).isNotModified(); |
||||
|
||||
// Wrong status
|
||||
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> statusAssertions(HttpStatus.INTERNAL_SERVER_ERROR).isNotModified()); |
||||
} |
||||
|
||||
@Test |
||||
void statusIsTemporaryRedirect() { |
||||
// Success
|
||||
statusAssertions(HttpStatus.TEMPORARY_REDIRECT).isTemporaryRedirect(); |
||||
|
||||
// Wrong status
|
||||
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> statusAssertions(HttpStatus.INTERNAL_SERVER_ERROR).isTemporaryRedirect()); |
||||
} |
||||
|
||||
@Test |
||||
void statusIsPermanentRedirect() { |
||||
// Success
|
||||
statusAssertions(HttpStatus.PERMANENT_REDIRECT).isPermanentRedirect(); |
||||
|
||||
// Wrong status
|
||||
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> statusAssertions(HttpStatus.INTERNAL_SERVER_ERROR).isPermanentRedirect()); |
||||
} |
||||
|
||||
@Test |
||||
void statusIsUnauthorized() { |
||||
// Success
|
||||
statusAssertions(HttpStatus.UNAUTHORIZED).isUnauthorized(); |
||||
|
||||
// Wrong status
|
||||
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> statusAssertions(HttpStatus.INTERNAL_SERVER_ERROR).isUnauthorized()); |
||||
} |
||||
|
||||
@Test |
||||
void statusIsForbidden() { |
||||
// Success
|
||||
statusAssertions(HttpStatus.FORBIDDEN).isForbidden(); |
||||
|
||||
// Wrong status
|
||||
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> statusAssertions(HttpStatus.INTERNAL_SERVER_ERROR).isForbidden()); |
||||
} |
||||
|
||||
private StatusAssertions statusAssertions(HttpStatus status) { |
||||
return statusAssertions(status.value()); |
||||
} |
||||
|
||||
private StatusAssertions statusAssertions(int status) { |
||||
try { |
||||
RestClient.RequestHeadersSpec.ConvertibleClientHttpResponse response = mock(); |
||||
when(response.getStatusCode()).thenReturn(HttpStatusCode.valueOf(status)); |
||||
when(response.getHeaders()).thenReturn(new HttpHeaders()); |
||||
ExchangeResult result = new ExchangeResult(new MockClientHttpRequest(), response, null); |
||||
return new StatusAssertions(result, mock()); |
||||
} |
||||
catch (IOException ex) { |
||||
throw new AssertionError(ex); |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,280 @@
@@ -0,0 +1,280 @@
|
||||
/* |
||||
* Copyright 2002-present the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.test.web.support; |
||||
|
||||
import org.junit.jupiter.api.Test; |
||||
|
||||
import org.springframework.http.HttpStatus; |
||||
import org.springframework.http.HttpStatusCode; |
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat; |
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType; |
||||
import static org.hamcrest.Matchers.equalTo; |
||||
import static org.hamcrest.Matchers.greaterThan; |
||||
|
||||
/** |
||||
* Tests for {@link AbstractStatusAssertions}. |
||||
* |
||||
* @author Rossen Stoyanchev |
||||
* @author Rob Worsnop |
||||
*/ |
||||
class StatusAssertionTests { |
||||
|
||||
@Test |
||||
void isEqualTo() { |
||||
TestStatusAssertions assertions = new TestStatusAssertions(HttpStatus.CONFLICT); |
||||
|
||||
// Success
|
||||
assertions.isEqualTo(HttpStatus.CONFLICT); |
||||
assertions.isEqualTo(409); |
||||
|
||||
// Wrong status
|
||||
assertThatExceptionOfType(AssertionError.class) |
||||
.isThrownBy(() -> assertions.isEqualTo(HttpStatus.REQUEST_TIMEOUT)); |
||||
|
||||
// Wrong status value
|
||||
assertThatExceptionOfType(AssertionError.class) |
||||
.isThrownBy(() -> assertions.isEqualTo(408)); |
||||
} |
||||
|
||||
@Test |
||||
void isEqualToWithCustomStatus() { |
||||
TestStatusAssertions assertions = new TestStatusAssertions(600); |
||||
|
||||
// Success
|
||||
assertions.isEqualTo(600); |
||||
|
||||
// Wrong status
|
||||
assertThatExceptionOfType(AssertionError.class) |
||||
.isThrownBy(() -> new TestStatusAssertions(601).isEqualTo(600)); |
||||
|
||||
} |
||||
|
||||
@Test |
||||
void reasonEquals() { |
||||
TestStatusAssertions assertions = new TestStatusAssertions(HttpStatus.CONFLICT); |
||||
|
||||
// Success
|
||||
assertions.reasonEquals("Conflict"); |
||||
|
||||
// Wrong reason
|
||||
assertThatExceptionOfType(AssertionError.class) |
||||
.isThrownBy(() -> new TestStatusAssertions(HttpStatus.INTERNAL_SERVER_ERROR).reasonEquals("Conflict")); |
||||
} |
||||
|
||||
@Test |
||||
void statusSeries1xx() { |
||||
TestStatusAssertions assertions = new TestStatusAssertions(HttpStatus.CONTINUE); |
||||
|
||||
// Success
|
||||
assertions.is1xxInformational(); |
||||
|
||||
// Wrong series
|
||||
assertThatExceptionOfType(AssertionError.class) |
||||
.isThrownBy(() -> new TestStatusAssertions(HttpStatus.OK).is1xxInformational()); |
||||
} |
||||
|
||||
@Test |
||||
void statusSeries2xx() { |
||||
TestStatusAssertions assertions = new TestStatusAssertions(HttpStatus.OK); |
||||
|
||||
// Success
|
||||
assertions.is2xxSuccessful(); |
||||
|
||||
// Wrong series
|
||||
assertThatExceptionOfType(AssertionError.class) |
||||
.isThrownBy(() -> new TestStatusAssertions(HttpStatus.INTERNAL_SERVER_ERROR).is2xxSuccessful()); |
||||
} |
||||
|
||||
@Test |
||||
void statusSeries3xx() { |
||||
TestStatusAssertions assertions = new TestStatusAssertions(HttpStatus.PERMANENT_REDIRECT); |
||||
|
||||
// Success
|
||||
assertions.is3xxRedirection(); |
||||
|
||||
// Wrong series
|
||||
assertThatExceptionOfType(AssertionError.class) |
||||
.isThrownBy(() -> new TestStatusAssertions(HttpStatus.INTERNAL_SERVER_ERROR).is3xxRedirection()); |
||||
} |
||||
|
||||
@Test |
||||
void statusSeries4xx() { |
||||
TestStatusAssertions assertions = new TestStatusAssertions(HttpStatus.BAD_REQUEST); |
||||
|
||||
// Success
|
||||
assertions.is4xxClientError(); |
||||
|
||||
// Wrong series
|
||||
assertThatExceptionOfType(AssertionError.class) |
||||
.isThrownBy(() -> new TestStatusAssertions(HttpStatus.INTERNAL_SERVER_ERROR).is4xxClientError()); |
||||
} |
||||
|
||||
@Test |
||||
void statusSeries5xx() { |
||||
TestStatusAssertions assertions = new TestStatusAssertions(HttpStatus.INTERNAL_SERVER_ERROR); |
||||
|
||||
// Success
|
||||
assertions.is5xxServerError(); |
||||
|
||||
// Wrong series
|
||||
assertThatExceptionOfType(AssertionError.class) |
||||
.isThrownBy(() -> new TestStatusAssertions(HttpStatus.OK).is5xxServerError()); |
||||
} |
||||
|
||||
@Test |
||||
void matchesStatusValue() { |
||||
TestStatusAssertions assertions = new TestStatusAssertions(HttpStatus.CONFLICT); |
||||
|
||||
// Success
|
||||
assertions.value(equalTo(409)); |
||||
assertions.value(greaterThan(400)); |
||||
|
||||
// Wrong status
|
||||
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> assertions.value(equalTo(200))); |
||||
} |
||||
|
||||
@Test |
||||
void matchesCustomStatusValue() { |
||||
new TestStatusAssertions(600).value(equalTo(600)); |
||||
} |
||||
|
||||
@Test |
||||
void consumesStatusValue() { |
||||
TestStatusAssertions assertions = new TestStatusAssertions(HttpStatus.CONFLICT); |
||||
|
||||
// Success
|
||||
assertions.value((Integer value) -> assertThat(value).isEqualTo(409)); |
||||
} |
||||
|
||||
@Test |
||||
void statusIsAccepted() { |
||||
// Success
|
||||
new TestStatusAssertions(HttpStatus.ACCEPTED).isAccepted(); |
||||
|
||||
// Wrong status
|
||||
assertThatExceptionOfType(AssertionError.class) |
||||
.isThrownBy(() -> new TestStatusAssertions(HttpStatus.INTERNAL_SERVER_ERROR).isAccepted()); |
||||
} |
||||
|
||||
@Test |
||||
void statusIsNoContent() { |
||||
// Success
|
||||
new TestStatusAssertions(HttpStatus.NO_CONTENT).isNoContent(); |
||||
|
||||
// Wrong status
|
||||
assertThatExceptionOfType(AssertionError.class) |
||||
.isThrownBy(() -> new TestStatusAssertions(HttpStatus.INTERNAL_SERVER_ERROR).isNoContent()); |
||||
} |
||||
|
||||
@Test |
||||
void statusIsFound() { |
||||
// Success
|
||||
new TestStatusAssertions(HttpStatus.FOUND).isFound(); |
||||
|
||||
// Wrong status
|
||||
assertThatExceptionOfType(AssertionError.class) |
||||
.isThrownBy(() -> new TestStatusAssertions(HttpStatus.INTERNAL_SERVER_ERROR).isFound()); |
||||
} |
||||
|
||||
@Test |
||||
void statusIsSeeOther() { |
||||
// Success
|
||||
new TestStatusAssertions(HttpStatus.SEE_OTHER).isSeeOther(); |
||||
|
||||
// Wrong status
|
||||
assertThatExceptionOfType(AssertionError.class) |
||||
.isThrownBy(() -> new TestStatusAssertions(HttpStatus.INTERNAL_SERVER_ERROR).isSeeOther()); |
||||
} |
||||
|
||||
@Test |
||||
void statusIsNotModified() { |
||||
// Success
|
||||
new TestStatusAssertions(HttpStatus.NOT_MODIFIED).isNotModified(); |
||||
|
||||
// Wrong status
|
||||
assertThatExceptionOfType(AssertionError.class) |
||||
.isThrownBy(() -> new TestStatusAssertions(HttpStatus.INTERNAL_SERVER_ERROR).isNotModified()); |
||||
} |
||||
|
||||
@Test |
||||
void statusIsTemporaryRedirect() { |
||||
// Success
|
||||
new TestStatusAssertions(HttpStatus.TEMPORARY_REDIRECT).isTemporaryRedirect(); |
||||
|
||||
// Wrong status
|
||||
assertThatExceptionOfType(AssertionError.class) |
||||
.isThrownBy(() -> new TestStatusAssertions(HttpStatus.INTERNAL_SERVER_ERROR).isTemporaryRedirect()); |
||||
} |
||||
|
||||
@Test |
||||
void statusIsPermanentRedirect() { |
||||
// Success
|
||||
new TestStatusAssertions(HttpStatus.PERMANENT_REDIRECT).isPermanentRedirect(); |
||||
|
||||
// Wrong status
|
||||
assertThatExceptionOfType(AssertionError.class) |
||||
.isThrownBy(() -> new TestStatusAssertions(HttpStatus.INTERNAL_SERVER_ERROR).isPermanentRedirect()); |
||||
} |
||||
|
||||
@Test |
||||
void statusIsUnauthorized() { |
||||
// Success
|
||||
new TestStatusAssertions(HttpStatus.UNAUTHORIZED).isUnauthorized(); |
||||
|
||||
// Wrong status
|
||||
assertThatExceptionOfType(AssertionError.class) |
||||
.isThrownBy(() -> new TestStatusAssertions(HttpStatus.INTERNAL_SERVER_ERROR).isUnauthorized()); |
||||
} |
||||
|
||||
@Test |
||||
void statusIsForbidden() { |
||||
// Success
|
||||
new TestStatusAssertions(HttpStatus.FORBIDDEN).isForbidden(); |
||||
|
||||
// Wrong status
|
||||
assertThatExceptionOfType(AssertionError.class) |
||||
.isThrownBy(() -> new TestStatusAssertions(HttpStatus.INTERNAL_SERVER_ERROR).isForbidden()); |
||||
} |
||||
|
||||
|
||||
private static class TestStatusAssertions extends AbstractStatusAssertions<TestExchangeResult, Object> { |
||||
|
||||
TestStatusAssertions(HttpStatus status) { |
||||
this(status.value()); |
||||
} |
||||
|
||||
TestStatusAssertions(int status) { |
||||
super(new TestExchangeResult(HttpStatusCode.valueOf(status)), ""); |
||||
} |
||||
|
||||
@Override |
||||
protected HttpStatusCode getStatus() { |
||||
return getExchangeResult().status(); |
||||
} |
||||
|
||||
@Override |
||||
protected void assertWithDiagnostics(Runnable assertion) { |
||||
assertion.run(); |
||||
} |
||||
} |
||||
|
||||
|
||||
private record TestExchangeResult(HttpStatusCode status) { |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue