spring-restdocs
spring-restdocs copied to clipboard
Add support for replacing binary multipart data with placeholder content
You can see in this Stack Overflow question, that when posting Multipart data, often there is binary data, which causes the resulting snippet to be a large amount of garbled data. It would be nice to have a core preprocessor that allows us to replace that binary content with a placeholder, like <<binary data>>
. The component should allow for selecting which parts should be replaced, as not all parts will be binary data.
You can add a custom OperationPreprocessor
:
class RequestPartsModifyingOperationPreprocessor extends OperationPreprocessorAdapter {
private static final byte[] MOCK_PART_CONTENT = "<binary>".getBytes(StandardCharsets.UTF_8);
private final OperationRequestFactory requestFactory = new OperationRequestFactory();
private final OperationRequestPartFactory requestPartFactory = new
OperationRequestPartFactory();
@Override
public OperationRequest preprocess(OperationRequest request) {
Collection<OperationRequestPart> parts = request.getParts();
if (CollectionUtils.isEmpty(parts)) {
return request;
}
List<OperationRequestPart> newParts = new ArrayList<>(parts.size());
OperationRequestPart part;
for (OperationRequestPart requestPart : parts) {
if (requestPart.getContent().length > 20) {
part = requestPartFactory
.create(requestPart.getName(), requestPart.getSubmittedFileName(),
MOCK_PART_CONTENT, requestPart.getHeaders());
} else {
part = requestPart;
}
newParts.add(part);
}
return this.requestFactory
.create(request.getUri(), request.getMethod(), request.getContent(),
request.getHeaders(), request.getParameters(), newParts, request.getCookies());
}
}
@someok Thank you, I got the same issue and your proposal can help me resolve it.