@Override public Collection<OperationRequestPart> getParts() { return this.delegate.getParts(); }
private void writeHeaders(OperationRequest request, List<String> lines) { HttpHeaders headers = request.getHeaders(); for (Entry<String, List<String>> entry : headers.entrySet()) { for (String header : entry.getValue()) { // HTTPie adds Content-Type automatically with --form if (!request.getParts().isEmpty() && entry.getKey().equals(HttpHeaders.CONTENT_TYPE) && header.startsWith(MediaType.MULTIPART_FORM_DATA_VALUE)) { continue; } lines.add(String.format("'%s:%s'", entry.getKey(), header)); } } }
private Set<String> extractActualRequestParts(Operation operation) { Set<String> actualRequestParts = new HashSet<>(); for (OperationRequestPart requestPart : operation.getRequest().getParts()) { actualRequestParts.add(requestPart.getName()); } return actualRequestParts; }
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()); } }
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"); }
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"); }
private void writeOptions(OperationRequest request, PrintWriter writer) { if (!request.getParts().isEmpty() || (!request.getParameters().getUniqueParameters(request.getUri()) .isEmpty() && !includeParametersInUri(request) && includeParametersAsFormOptions(request))) { writer.print("--form "); } }
@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"); }
@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"); }
/** * Creates a new {@code OperationRequest} based on the given {@code original} but with * the given {@code newHeaders}. * @param original the original request * @param newHeaders the new headers * @return the new request with the new headers */ public OperationRequest createFrom(OperationRequest original, HttpHeaders newHeaders) { return new StandardOperationRequest(original.getUri(), original.getMethod(), original.getContent(), newHeaders, original.getParameters(), original.getParts(), original.getCookies()); }
@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 }); }
@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 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"); }
/** * Creates a new {@code OperationRequest} based on the given {@code original} but with * the given {@code newParameters}. * @param original the original request * @param newParameters the new parameters * @return the new request with the new parameters */ public OperationRequest createFrom(OperationRequest original, Parameters newParameters) { return new StandardOperationRequest(original.getUri(), original.getMethod(), original.getContent(), original.getHeaders(), newParameters, original.getParts(), original.getCookies()); }
@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"); }
@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 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"); }
/** * Creates a new {@code OperationRequest} based on the given {@code original} but with * the given {@code newContent}. If the original request had a {@code Content-Length} * header it will be modified to match the length of the new content. * @param original the original request * @param newContent the new content * @return the new request with the new content */ public OperationRequest createFrom(OperationRequest original, byte[] newContent) { return new StandardOperationRequest(original.getUri(), original.getMethod(), newContent, getUpdatedHeaders(original.getHeaders(), newContent), original.getParameters(), original.getParts(), original.getCookies()); }
@Test public void multipartWithStringBody() { RequestSpecification requestSpec = RestAssured.given().port(tomcat.getPort()) .multiPart("control", "foo"); requestSpec.post(); OperationRequest request = this.factory .convert((FilterableRequestSpecification) requestSpec); assertThat(request.getParts().iterator().next().getContentAsString()) .isEqualTo("foo"); }
@Test public void multipartWithObjectBody() { RequestSpecification requestSpec = RestAssured.given().port(tomcat.getPort()) .multiPart("control", new ObjectBody("bar")); requestSpec.post(); OperationRequest request = this.factory .convert((FilterableRequestSpecification) requestSpec); assertThat(request.getParts().iterator().next().getContentAsString()) .isEqualTo("{\"foo\":\"bar\"}"); }