Refactor WebTestClient consumeWith methods

Issue: SPR-15587
This commit is contained in:
Rossen Stoyanchev 2017-05-24 20:20:35 -04:00
parent 718162b8c4
commit 346d5d2fab
6 changed files with 27 additions and 38 deletions

View File

@ -395,9 +395,9 @@ class DefaultWebTestClient implements WebTestClient {
}
@Override
public <T extends S> T consumeWith(Consumer<B> consumer) {
public <T extends S> T consumeWith(Consumer<EntityExchangeResult<B>> consumer) {
B actual = this.result.getResponseBody();
this.result.assertWithDiagnostics(() -> consumer.accept(actual));
this.result.assertWithDiagnostics(() -> consumer.accept(this.result));
return self();
}
@ -493,13 +493,7 @@ class DefaultWebTestClient implements WebTestClient {
@Override
public JsonPathAssertions jsonPath(String expression, Object... args) {
return new JsonPathAssertions(this, expression, args);
}
@Override
public BodyContentSpec consumeAsStringWith(Consumer<String> consumer) {
this.result.assertWithDiagnostics(() -> consumer.accept(getBodyAsString()));
return this;
return new JsonPathAssertions(this, getBodyAsString(), expression, args);
}
private String getBodyAsString() {
@ -512,8 +506,8 @@ class DefaultWebTestClient implements WebTestClient {
}
@Override
public BodyContentSpec consumeWith(Consumer<byte[]> consumer) {
this.result.assertWithDiagnostics(() -> consumer.accept(this.result.getResponseBody()));
public BodyContentSpec consumeWith(Consumer<EntityExchangeResult<byte[]>> consumer) {
this.result.assertWithDiagnostics(() -> consumer.accept(this.result));
return this;
}

View File

@ -29,11 +29,14 @@ public class JsonPathAssertions {
private final WebTestClient.BodyContentSpec bodySpec;
private final String content;
private final JsonPathExpectationsHelper pathHelper;
JsonPathAssertions(WebTestClient.BodyContentSpec spec, String expression, Object... args) {
JsonPathAssertions(WebTestClient.BodyContentSpec spec, String content, String expression, Object... args) {
this.bodySpec = spec;
this.content = content;
this.pathHelper = new JsonPathExpectationsHelper(expression, args);
}
@ -42,9 +45,7 @@ public class JsonPathAssertions {
* Applies {@link JsonPathExpectationsHelper#assertValue(String, Object)}.
*/
public WebTestClient.BodyContentSpec isEqualTo(Object expectedValue) {
this.bodySpec.consumeAsStringWith(body -> {
this.pathHelper.assertValue(body, expectedValue);
});
this.pathHelper.assertValue(this.content, expectedValue);
return this.bodySpec;
}
@ -52,7 +53,7 @@ public class JsonPathAssertions {
* Applies {@link JsonPathExpectationsHelper#exists(String)}.
*/
public WebTestClient.BodyContentSpec exists() {
this.bodySpec.consumeAsStringWith(this.pathHelper::exists);
this.pathHelper.exists(this.content);
return this.bodySpec;
}
@ -60,7 +61,7 @@ public class JsonPathAssertions {
* Applies {@link JsonPathExpectationsHelper#doesNotExist(String)}.
*/
public WebTestClient.BodyContentSpec doesNotExist() {
this.bodySpec.consumeAsStringWith(this.pathHelper::doesNotExist);
this.pathHelper.doesNotExist(this.content);
return this.bodySpec;
}
@ -68,7 +69,7 @@ public class JsonPathAssertions {
* Applies {@link JsonPathExpectationsHelper#assertValueIsEmpty(String)}.
*/
public WebTestClient.BodyContentSpec isEmpty() {
this.bodySpec.consumeAsStringWith(this.pathHelper::assertValueIsEmpty);
this.pathHelper.assertValueIsEmpty(this.content);
return this.bodySpec;
}
@ -76,7 +77,7 @@ public class JsonPathAssertions {
* Applies {@link JsonPathExpectationsHelper#assertValueIsNotEmpty(String)}.
*/
public WebTestClient.BodyContentSpec isNotEmpty() {
this.bodySpec.consumeAsStringWith(this.pathHelper::assertValueIsNotEmpty);
this.pathHelper.assertValueIsNotEmpty(this.content);
return this.bodySpec;
}
@ -84,7 +85,7 @@ public class JsonPathAssertions {
* Applies {@link JsonPathExpectationsHelper#assertValueIsBoolean(String)}.
*/
public WebTestClient.BodyContentSpec isBoolean() {
this.bodySpec.consumeAsStringWith(this.pathHelper::assertValueIsBoolean);
this.pathHelper.assertValueIsBoolean(this.content);
return this.bodySpec;
}
@ -92,7 +93,7 @@ public class JsonPathAssertions {
* Applies {@link JsonPathExpectationsHelper#assertValueIsNumber(String)}.
*/
public WebTestClient.BodyContentSpec isNumber() {
this.bodySpec.consumeAsStringWith(this.pathHelper::assertValueIsNumber);
this.pathHelper.assertValueIsNumber(this.content);
return this.bodySpec;
}
@ -100,7 +101,7 @@ public class JsonPathAssertions {
* Applies {@link JsonPathExpectationsHelper#assertValueIsArray(String)}.
*/
public WebTestClient.BodyContentSpec isArray() {
this.bodySpec.consumeAsStringWith(this.pathHelper::assertValueIsArray);
this.pathHelper.assertValueIsArray(this.content);
return this.bodySpec;
}
@ -108,7 +109,7 @@ public class JsonPathAssertions {
* Applies {@link JsonPathExpectationsHelper#assertValueIsMap(String)}.
*/
public WebTestClient.BodyContentSpec isMap() {
this.bodySpec.consumeAsStringWith(this.pathHelper::assertValueIsMap);
this.pathHelper.assertValueIsMap(this.content);
return this.bodySpec;
}

View File

@ -579,9 +579,9 @@ public interface WebTestClient {
<T extends S> T isEqualTo(B expected);
/**
* Assert the extracted body with the given {@link Consumer}.
* Assert the exchange result with the given {@link Consumer}.
*/
<T extends S> T consumeWith(Consumer<B> consumer);
<T extends S> T consumeWith(Consumer<EntityExchangeResult<B>> consumer);
/**
* Return the exchange result with the decoded body.
@ -648,21 +648,12 @@ public interface WebTestClient {
*/
JsonPathAssertions jsonPath(String expression, Object... args);
/**
* Assert the response body content converted to a String with the given
* {@link Consumer}. The String is created with the {@link Charset} from
* the "content-type" response header or {@code UTF-8} otherwise.
* @param consumer the consumer for the response body; the input String
* may be {@code null} if there was no response body.
*/
BodyContentSpec consumeAsStringWith(Consumer<String> consumer);
/**
* Assert the response body content with the given {@link Consumer}.
* @param consumer the consumer for the response body; the input
* {@code byte[]} may be {@code null} if there was no response body.
*/
BodyContentSpec consumeWith(Consumer<byte[]> consumer);
BodyContentSpec consumeWith(Consumer<EntityExchangeResult<byte[]>> consumer);
/**
* Return the exchange result with body content as {@code byte[]}.

View File

@ -70,7 +70,8 @@ public class ResponseEntityTests {
.exchange()
.expectStatus().isOk()
.expectHeader().contentType(MediaType.APPLICATION_JSON_UTF8)
.expectBody(Person.class).consumeWith(p -> assertEquals(new Person("John"), p));
.expectBody(Person.class)
.consumeWith(result -> assertEquals(new Person("John"), result.getResponseBody()));
}
@Test

View File

@ -83,7 +83,8 @@ public class ApplicationContextTests {
this.client.get().uri("/principal")
.exchange()
.expectStatus().isOk()
.expectBody().consumeAsStringWith(body -> assertEquals("Hello Mr. Pablo!", body));
.expectBody(String.class)
.consumeWith(result -> assertEquals("Hello Mr. Pablo!", result.getResponseBody()));
}
@Test

View File

@ -74,7 +74,8 @@ public class ControllerTests {
this.client.get().uri("/principal")
.exchange()
.expectStatus().isOk()
.expectBody().consumeAsStringWith(body -> assertEquals("Hello Mr. Pablo!", body));
.expectBody(String.class)
.consumeWith(result -> assertEquals("Hello Mr. Pablo!", result.getResponseBody()));
}
@Test