private Collection<OperationRequestPart> modify( Collection<OperationRequestPart> parts) { List<OperationRequestPart> modifiedParts = new ArrayList<>(); OperationRequestPartFactory factory = new OperationRequestPartFactory(); for (OperationRequestPart part : parts) { modifiedParts.add(factory.create(part.getName(), part.getSubmittedFileName(), this.contentModifier.modifyContent(part.getContent(), part.getHeaders().getContentType()), modify(part.getHeaders()))); } return modifiedParts; }
private void writePart(OperationRequestPart part, PrintWriter writer) { writePart(part.getName(), part.getContentAsString(), part.getSubmittedFileName(), part.getHeaders().getContentType(), writer); }
private void writeFormDataIfNecessary(OperationRequest request, List<String> lines) { for (OperationRequestPart part : request.getParts()) { StringBuilder oneLine = new StringBuilder(); oneLine.append(String.format("'%s'", part.getName())); if (!StringUtils.hasText(part.getSubmittedFileName())) { // https://github.com/jkbrzt/httpie/issues/342 oneLine.append(String.format("@<(echo '%s')", part.getContentAsString())); } else { oneLine.append(String.format("@'%s'", part.getSubmittedFileName())); } lines.add(oneLine.toString()); } }
@Override protected MediaType getContentType(Operation operation) { return findPart(operation).getHeaders().getContentType(); }
@Override protected byte[] getContent(Operation operation) throws IOException { return findPart(operation).getContent(); }
private Set<String> extractActualRequestParts(Operation operation) { Set<String> actualRequestParts = new HashSet<>(); for (OperationRequestPart requestPart : operation.getRequest().getParts()) { actualRequestParts.add(requestPart.getName()); } return actualRequestParts; }
@Test public void multipartWithByteArrayInputStreamBody() { RequestSpecification requestSpec = RestAssured.given().port(tomcat.getPort()) .multiPart("foo", "foo.txt", new ByteArrayInputStream("foo".getBytes())); requestSpec.post(); OperationRequest request = this.factory .convert((FilterableRequestSpecification) requestSpec); assertThat(request.getParts().iterator().next().getContentAsString()) .isEqualTo("foo"); }
@Override protected MediaType getContentType(Operation operation) { return findPart(operation).getHeaders().getContentType(); }
@Override protected byte[] getContent(Operation operation) throws IOException { return findPart(operation).getContent(); }
private OperationRequestPart findPart(Operation operation) { for (OperationRequestPart candidate : operation.getRequest().getParts()) { if (candidate.getName().equals(this.partName)) { return candidate; } } throw new SnippetException("A request part named '" + this.partName + "' was not found in the request"); }
@Test public void multipartWithFileBody() { RequestSpecification requestSpec = RestAssured.given().port(tomcat.getPort()) .multiPart(new File("src/test/resources/body.txt")); requestSpec.post(); OperationRequest request = this.factory .convert((FilterableRequestSpecification) requestSpec); assertThat(request.getParts().iterator().next().getContentAsString()) .isEqualTo("file"); }
@Test public void mockMultipartFileUploadWithContentType() throws Exception { OperationRequest request = createOperationRequest( MockMvcRequestBuilders.multipart("/foo").file(new MockMultipartFile( "file", "original", "image/png", new byte[] { 1, 2, 3, 4 }))); assertThat(request.getUri()).isEqualTo(URI.create("http://localhost/foo")); assertThat(request.getMethod()).isEqualTo(HttpMethod.POST); assertThat(request.getParts().size()).isEqualTo(1); OperationRequestPart part = request.getParts().iterator().next(); assertThat(part.getName()).isEqualTo("file"); assertThat(part.getSubmittedFileName()).isEqualTo("original"); assertThat(part.getHeaders().getContentType()).isEqualTo(MediaType.IMAGE_PNG); assertThat(part.getContent()).isEqualTo(new byte[] { 1, 2, 3, 4 }); }
private void writePartsIfNecessary(OperationRequest request, List<String> lines) { for (OperationRequestPart part : request.getParts()) { StringBuilder oneLine = new StringBuilder(); oneLine.append(String.format("-F '%s=", part.getName())); if (!StringUtils.hasText(part.getSubmittedFileName())) { oneLine.append(part.getContentAsString()); } else { oneLine.append(String.format("@%s", part.getSubmittedFileName())); } if (part.getHeaders().getContentType() != null) { oneLine.append(";type="); oneLine.append(part.getHeaders().getContentType().toString()); } oneLine.append("'"); lines.add(oneLine.toString()); } }
@Test public void urisInRequestPartHeadersCanBeModified() { OperationRequest processed = this.preprocessor.host("api.example.com").preprocess( createRequestWithPartWithHeader("Foo", "http://locahost:12345")); assertThat(processed.getParts().iterator().next().getHeaders().getFirst("Foo")) .isEqualTo("http://api.example.com:12345"); }
@Test public void urisInRequestPartContentCanBeModified() { OperationRequest processed = this.preprocessor.host("api.example.com") .preprocess(createRequestWithPartWithContent( "The uri 'http://localhost:12345' should be used")); assertThat(new String(processed.getParts().iterator().next().getContent())) .isEqualTo("The uri 'http://api.example.com:12345' should be used"); }
private OperationRequestPart findPart(Operation operation) { for (OperationRequestPart candidate : operation.getRequest().getParts()) { if (candidate.getName().equals(this.partName)) { return candidate; } } throw new SnippetException("A request part named '" + this.partName + "' was not found in the request"); }
@Test public void multipartWithByteArrayBody() { RequestSpecification requestSpec = RestAssured.given().port(tomcat.getPort()) .multiPart("control", "file", "foo".getBytes()); requestSpec.post(); OperationRequest request = this.factory .convert((FilterableRequestSpecification) requestSpec); assertThat(request.getParts().iterator().next().getContentAsString()) .isEqualTo("foo"); }
@Test public void mockMultipartFileUpload() throws Exception { OperationRequest request = createOperationRequest( MockMvcRequestBuilders.multipart("/foo") .file(new MockMultipartFile("file", new byte[] { 1, 2, 3, 4 }))); assertThat(request.getUri()).isEqualTo(URI.create("http://localhost/foo")); assertThat(request.getMethod()).isEqualTo(HttpMethod.POST); assertThat(request.getParts().size()).isEqualTo(1); OperationRequestPart part = request.getParts().iterator().next(); assertThat(part.getName()).isEqualTo("file"); assertThat(part.getSubmittedFileName()).isNull(); assertThat(part.getHeaders().size()).isEqualTo(1); assertThat(part.getHeaders().getContentLength()).isEqualTo(4L); assertThat(part.getContent()).isEqualTo(new byte[] { 1, 2, 3, 4 }); }
@Test public void urisInRequestPartHeadersCanBeModified() { OperationRequest processed = this.preprocessor.host("api.example.com").preprocess( createRequestWithPartWithHeader("Foo", "http://locahost:12345")); assertThat(processed.getParts().iterator().next().getHeaders().getFirst("Foo")) .isEqualTo("http://api.example.com:12345"); }
@Test public void urisInRequestPartContentCanBeModified() { OperationRequest processed = this.preprocessor.host("api.example.com") .preprocess(createRequestWithPartWithContent( "The uri 'http://localhost:12345' should be used")); assertThat(new String(processed.getParts().iterator().next().getContent())) .isEqualTo("The uri 'http://api.example.com:12345' should be used"); }