Skip to content

Commit

Permalink
Use getRawStatusCode() for custom status code support in value methods
Browse files Browse the repository at this point in the history
  • Loading branch information
jhoeller committed Mar 12, 2021
1 parent a7db92b commit a6ab716
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 23 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2021 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.
Expand Down Expand Up @@ -71,8 +71,7 @@ public WebTestClient.ResponseSpec isOk() {
* Assert the response status code is {@code HttpStatus.CREATED} (201).
*/
public WebTestClient.ResponseSpec isCreated() {
HttpStatus expected = HttpStatus.CREATED;
return assertStatusAndReturn(expected);
return assertStatusAndReturn(HttpStatus.CREATED);
}

/**
Expand Down Expand Up @@ -158,8 +157,8 @@ public WebTestClient.ResponseSpec isNotFound() {
*/
public WebTestClient.ResponseSpec reasonEquals(String reason) {
String actual = this.exchangeResult.getStatus().getReasonPhrase();
String message = "Response status reason";
this.exchangeResult.assertWithDiagnostics(() -> AssertionErrors.assertEquals(message, reason, actual));
this.exchangeResult.assertWithDiagnostics(() ->
AssertionErrors.assertEquals("Response status reason", reason, actual));
return this.responseSpec;
}

Expand Down Expand Up @@ -195,8 +194,7 @@ public WebTestClient.ResponseSpec is4xxClientError() {
* Assert the response status code is in the 5xx range.
*/
public WebTestClient.ResponseSpec is5xxServerError() {
HttpStatus.Series expected = HttpStatus.Series.SERVER_ERROR;
return assertSeriesAndReturn(expected);
return assertSeriesAndReturn(HttpStatus.Series.SERVER_ERROR);
}

/**
Expand All @@ -205,8 +203,8 @@ public WebTestClient.ResponseSpec is5xxServerError() {
* @since 5.1
*/
public WebTestClient.ResponseSpec value(Matcher<Integer> matcher) {
int value = this.exchangeResult.getStatus().value();
this.exchangeResult.assertWithDiagnostics(() -> MatcherAssert.assertThat("Response status", value, matcher));
int actual = this.exchangeResult.getRawStatusCode();
this.exchangeResult.assertWithDiagnostics(() -> MatcherAssert.assertThat("Response status", actual, matcher));
return this.responseSpec;
}

Expand All @@ -216,8 +214,8 @@ public WebTestClient.ResponseSpec value(Matcher<Integer> matcher) {
* @since 5.1
*/
public WebTestClient.ResponseSpec value(Consumer<Integer> consumer) {
int value = this.exchangeResult.getStatus().value();
this.exchangeResult.assertWithDiagnostics(() -> consumer.accept(value));
int actual = this.exchangeResult.getRawStatusCode();
this.exchangeResult.assertWithDiagnostics(() -> consumer.accept(actual));
return this.responseSpec;
}

Expand All @@ -230,10 +228,8 @@ private WebTestClient.ResponseSpec assertStatusAndReturn(HttpStatus expected) {

private WebTestClient.ResponseSpec assertSeriesAndReturn(HttpStatus.Series expected) {
HttpStatus status = this.exchangeResult.getStatus();
this.exchangeResult.assertWithDiagnostics(() -> {
String message = "Range for response status value " + status;
AssertionErrors.assertEquals(message, expected, status.series());
});
this.exchangeResult.assertWithDiagnostics(() ->
AssertionErrors.assertEquals("Range for response status value " + status, expected, status.series()));
return this.responseSpec;
}

Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2021 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.
Expand Down Expand Up @@ -34,6 +34,7 @@

/**
* Unit tests for {@link StatusAssertions}.
*
* @author Rossen Stoyanchev
*/
public class StatusAssertionTests {
Expand Down Expand Up @@ -73,20 +74,19 @@ public void reasonEquals() {
}

@Test
public void statusSerius1xx() {
public void statusSeries1xx() {
StatusAssertions assertions = statusAssertions(HttpStatus.CONTINUE);

// Success
assertions.is1xxInformational();

// Wrong series

assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
assertions.is2xxSuccessful());
}

@Test
public void statusSerius2xx() {
public void statusSeries2xx() {
StatusAssertions assertions = statusAssertions(HttpStatus.OK);

// Success
Expand All @@ -98,7 +98,7 @@ public void statusSerius2xx() {
}

@Test
public void statusSerius3xx() {
public void statusSeries3xx() {
StatusAssertions assertions = statusAssertions(HttpStatus.PERMANENT_REDIRECT);

// Success
Expand All @@ -110,7 +110,7 @@ public void statusSerius3xx() {
}

@Test
public void statusSerius4xx() {
public void statusSeries4xx() {
StatusAssertions assertions = statusAssertions(HttpStatus.BAD_REQUEST);

// Success
Expand All @@ -122,7 +122,7 @@ public void statusSerius4xx() {
}

@Test
public void statusSerius5xx() {
public void statusSeries5xx() {
StatusAssertions assertions = statusAssertions(HttpStatus.INTERNAL_SERVER_ERROR);

// Success
Expand All @@ -134,7 +134,7 @@ public void statusSerius5xx() {
}

@Test
public void matches() {
public void matchesStatusValue() {
StatusAssertions assertions = statusAssertions(HttpStatus.CONFLICT);

// Success
Expand All @@ -146,6 +146,11 @@ public void matches() {
assertions.value(equalTo(200)));
}

@Test
public void matchesCustomStatus() {
statusAssertions(600).value(equalTo(600));
}


private StatusAssertions statusAssertions(HttpStatus status) {
return statusAssertions(status.value());
Expand Down

0 comments on commit a6ab716

Please sign in to comment.