Add bufferContent option to MockRestServiceServer

Issue: SPR-14694
This commit is contained in:
Rossen Stoyanchev
2018-03-15 22:09:37 -04:00
parent f7e75a5b82
commit 541ee13934
2 changed files with 83 additions and 8 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 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.
@@ -20,6 +20,7 @@ import java.io.IOException;
import java.net.URI;
import org.springframework.http.HttpMethod;
import org.springframework.http.client.BufferingClientHttpRequestFactory;
import org.springframework.http.client.ClientHttpRequest;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.ClientHttpResponse;
@@ -201,6 +202,14 @@ public class MockRestServiceServer {
*/
MockRestServiceServerBuilder ignoreExpectOrder(boolean ignoreExpectOrder);
/**
* Use the {@link BufferingClientHttpRequestFactory} wrapper to buffer
* the input and output streams, and for example, allow multiple reads
* of the response body.
* @since 5.0.5
*/
MockRestServiceServerBuilder bufferContent();
/**
* Build the {@code MockRestServiceServer} and set up the underlying
* {@code RestTemplate} or {@code AsyncRestTemplate} with a
@@ -226,6 +235,9 @@ public class MockRestServiceServer {
private boolean ignoreExpectOrder;
private boolean bufferContent;
public DefaultBuilder(RestTemplate restTemplate) {
Assert.notNull(restTemplate, "RestTemplate must not be null");
this.restTemplate = restTemplate;
@@ -244,6 +256,12 @@ public class MockRestServiceServer {
return this;
}
@Override
public MockRestServiceServerBuilder bufferContent() {
this.bufferContent = true;
return this;
}
@Override
public MockRestServiceServer build() {
if (this.ignoreExpectOrder) {
@@ -259,7 +277,12 @@ public class MockRestServiceServer {
MockRestServiceServer server = new MockRestServiceServer(manager);
MockClientHttpRequestFactory factory = server.new MockClientHttpRequestFactory();
if (this.restTemplate != null) {
this.restTemplate.setRequestFactory(factory);
if (this.bufferContent) {
this.restTemplate.setRequestFactory(new BufferingClientHttpRequestFactory(factory));
}
else {
this.restTemplate.setRequestFactory(factory);
}
}
if (this.asyncRestTemplate != null) {
this.asyncRestTemplate.setAsyncRequestFactory(factory);