spring-restdocs icon indicating copy to clipboard operation
spring-restdocs copied to clipboard

Add support for replacing binary multipart data with placeholder content

Open psamsotha opened this issue 8 years ago • 2 comments

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.

psamsotha avatar Jun 17 '16 09:06 psamsotha

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 avatar Oct 30 '17 19:10 someok

@someok Thank you, I got the same issue and your proposal can help me resolve it.

kaka2507 avatar Aug 01 '18 16:08 kaka2507