This repository has been archived by the owner on Dec 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replacement for binary content of multipart requests (#369)
* feat: added MultipartContentOperationPreprocessor for replacing binary content of multipart/form-data requests * docs: add MultipartContentOperationPreprocessor to docs
- Loading branch information
1 parent
2559b34
commit 2a23c31
Showing
4 changed files
with
133 additions
and
0 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
...c/main/java/capital/scalable/restdocs/response/MultipartContentOperationPreprocessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package capital.scalable.restdocs.response; | ||
|
||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.restdocs.operation.OperationRequest; | ||
import org.springframework.restdocs.operation.OperationRequestFactory; | ||
import org.springframework.restdocs.operation.OperationRequestPart; | ||
import org.springframework.restdocs.operation.OperationRequestPartFactory; | ||
import org.springframework.restdocs.operation.preprocess.OperationPreprocessorAdapter; | ||
|
||
import java.util.List; | ||
|
||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
import static java.util.stream.Collectors.toList; | ||
|
||
public class MultipartContentOperationPreprocessor extends OperationPreprocessorAdapter { | ||
private static final byte[] BINARY_REPLACEMENT = "<binary>".getBytes(UTF_8); | ||
|
||
private final OperationRequestPartFactory partFactory = new OperationRequestPartFactory(); | ||
private final OperationRequestFactory requestFactory = new OperationRequestFactory(); | ||
|
||
@Override | ||
public OperationRequest preprocess(OperationRequest request) { | ||
if (isMultipart(request)) { | ||
List<OperationRequestPart> parts = request.getParts().stream() | ||
.map(this::replaceBinary) | ||
.collect(toList()); | ||
return requestFactory.create(request.getUri(), | ||
request.getMethod(), request.getContent(), request.getHeaders(), request.getParameters() | ||
, parts); | ||
} | ||
return request; | ||
} | ||
|
||
private boolean isMultipart(OperationRequest request) { | ||
List<String> contentTypes = request.getHeaders().get(HttpHeaders.CONTENT_TYPE); | ||
return contentTypes != null && contentTypes.contains(MediaType.MULTIPART_FORM_DATA_VALUE); | ||
} | ||
|
||
private OperationRequestPart replaceBinary(OperationRequestPart part) { | ||
return partFactory.create(part.getName(), part.getSubmittedFileName() | ||
, BINARY_REPLACEMENT, part.getHeaders()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
...st/java/capital/scalable/restdocs/response/MultipartContentOperationPreprocessorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package capital.scalable.restdocs.response; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.restdocs.operation.OperationRequest; | ||
import org.springframework.restdocs.operation.OperationRequestFactory; | ||
import org.springframework.restdocs.operation.OperationRequestPart; | ||
import org.springframework.restdocs.operation.OperationRequestPartFactory; | ||
import org.springframework.restdocs.operation.Parameters; | ||
|
||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static org.junit.Assert.assertArrayEquals; | ||
import static org.junit.Assert.assertEquals; | ||
|
||
public class MultipartContentOperationPreprocessorTest { | ||
private static final byte[] EXPECTED_CONTENT = "<binary>".getBytes(StandardCharsets.UTF_8); | ||
private static final byte[] BINARY_CONTENT = new byte[]{1, 2, 3, 3, 3}; | ||
private MultipartContentOperationPreprocessor preprocessor; | ||
private OperationRequestFactory requestFactory; | ||
private OperationRequestPartFactory partFactory; | ||
|
||
@Before | ||
public void setUp() { | ||
preprocessor = new MultipartContentOperationPreprocessor(); | ||
requestFactory = new OperationRequestFactory(); | ||
partFactory = new OperationRequestPartFactory(); | ||
} | ||
|
||
@Test | ||
public void shouldReplaceContent() throws URISyntaxException { | ||
OperationRequest request = createRequestWithContentType(MediaType.MULTIPART_FORM_DATA_VALUE); | ||
|
||
OperationRequest processedRequest = preprocessor.preprocess(request); | ||
assertArrayEquals(request.getContent(), processedRequest.getContent()); | ||
assertEquals(request.getHeaders(), processedRequest.getHeaders()); | ||
assertEquals(request.getMethod(), processedRequest.getMethod()); | ||
assertEquals(request.getUri(), processedRequest.getUri()); | ||
assertEquals(request.getParameters(), processedRequest.getParameters()); | ||
for (OperationRequestPart part : processedRequest.getParts()) { | ||
assertArrayEquals(EXPECTED_CONTENT, part.getContent()); | ||
} | ||
} | ||
|
||
@Test | ||
public void shouldNotReplaceWhenNotMultipart() throws URISyntaxException { | ||
OperationRequest request = createRequestWithContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE); | ||
|
||
OperationRequest processedRequest = preprocessor.preprocess(request); | ||
assertArrayEquals(request.getContent(), processedRequest.getContent()); | ||
assertEquals(request.getHeaders(), processedRequest.getHeaders()); | ||
assertEquals(request.getMethod(), processedRequest.getMethod()); | ||
assertEquals(request.getUri(), processedRequest.getUri()); | ||
assertEquals(request.getParameters(), processedRequest.getParameters()); | ||
|
||
OperationRequestPart[] expectedParts = request.getParts().toArray(new OperationRequestPart[0]); | ||
OperationRequestPart[] operationRequestParts = processedRequest.getParts().toArray(new OperationRequestPart[0]); | ||
for (int i = 0; i < operationRequestParts.length; i++) { | ||
assertEquals(expectedParts[i], operationRequestParts[i]); | ||
} | ||
} | ||
|
||
private OperationRequest createRequestWithContentType(String contentType) throws URISyntaxException { | ||
HttpHeaders headers = new HttpHeaders(); | ||
headers.add(HttpHeaders.CONTENT_TYPE, contentType); | ||
Parameters parameters = new Parameters(); | ||
parameters.add("parameter", "value"); | ||
List<OperationRequestPart> requestParts = new ArrayList<>(); | ||
requestParts.add(partFactory.create("first", "file1", BINARY_CONTENT, new HttpHeaders())); | ||
return requestFactory.create(new URI("http://localhost"), HttpMethod.POST, BINARY_CONTENT, headers, parameters, requestParts); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters