Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ private void writePartsIfNecessary(OperationRequest request, List<String> lines)
StringBuilder oneLine = new StringBuilder();
oneLine.append(String.format("-F '%s=", part.getName()));
if (!StringUtils.hasText(part.getSubmittedFileName())) {
oneLine.append(escapeSingleQuotes(part.getContentAsString()));
oneLine.append(escapeSingleQuotes(quoteFormValueIfNecessary(part.getContentAsString())));
}
else {
oneLine.append(String.format("@%s", escapeSingleQuotes(part.getSubmittedFileName())));
Expand All @@ -159,6 +159,14 @@ private void writeContent(CliOperationRequest request, List<String> lines) {
}
}

private static String quoteFormValueIfNecessary(String value) {
if (value.startsWith("@") || value.startsWith("<") || value.startsWith("(") || value.startsWith("\"")
|| value.contains(";")) {
return "\"" + value.replace("\\", "\\\\").replace("\"", "\\\"") + "\"";
}
return value;
}

private static String escapeSingleQuotes(String input) {
return input.replace("'", "'\\''");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,91 @@ void multipartPostWithNoSubmittedFileName(OperationBuilder operationBuilder, Ass
.isCodeBlock((codeBlock) -> codeBlock.withLanguage("bash").content(expectedContent));
}

@RenderedSnippetTest
void multipartPostWithNoSubmittedFileNameAndContentContainingSemicolon(OperationBuilder operationBuilder,
AssertableSnippets snippets) throws IOException {
new CurlRequestSnippet(this.commandFormatter).document(operationBuilder.request("http://localhost/upload")
.method("POST")
.header(HttpHeaders.CONTENT_TYPE, MediaType.MULTIPART_FORM_DATA_VALUE)
.part("metadata", "{\"description\": \"foo; bar\"}".getBytes())
.build());
String expectedContent = "$ curl 'http://localhost/upload' -i -X POST -H "
+ "'Content-Type: multipart/form-data' -F " + "'metadata=\"{\\\"description\\\": \\\"foo; bar\\\"}\"'";
assertThat(snippets.curlRequest())
.isCodeBlock((codeBlock) -> codeBlock.withLanguage("bash").content(expectedContent));
}

@RenderedSnippetTest
void multipartPostWithNoSubmittedFileNameAndContentStartingWithAt(OperationBuilder operationBuilder,
AssertableSnippets snippets) throws IOException {
new CurlRequestSnippet(this.commandFormatter).document(operationBuilder.request("http://localhost/upload")
.method("POST")
.header(HttpHeaders.CONTENT_TYPE, MediaType.MULTIPART_FORM_DATA_VALUE)
.part("mention", "@alice".getBytes())
.build());
String expectedContent = "$ curl 'http://localhost/upload' -i -X POST -H "
+ "'Content-Type: multipart/form-data' -F " + "'mention=\"@alice\"'";
assertThat(snippets.curlRequest())
.isCodeBlock((codeBlock) -> codeBlock.withLanguage("bash").content(expectedContent));
}

@RenderedSnippetTest
void multipartPostWithNoSubmittedFileNameAndContentStartingWithLessThan(OperationBuilder operationBuilder,
AssertableSnippets snippets) throws IOException {
new CurlRequestSnippet(this.commandFormatter).document(operationBuilder.request("http://localhost/upload")
.method("POST")
.header(HttpHeaders.CONTENT_TYPE, MediaType.MULTIPART_FORM_DATA_VALUE)
.part("note", "<b>bold</b>".getBytes())
.build());
String expectedContent = "$ curl 'http://localhost/upload' -i -X POST -H "
+ "'Content-Type: multipart/form-data' -F " + "'note=\"<b>bold</b>\"'";
assertThat(snippets.curlRequest())
.isCodeBlock((codeBlock) -> codeBlock.withLanguage("bash").content(expectedContent));
}

@RenderedSnippetTest
void multipartPostWithNoSubmittedFileNameAndContentStartingWithParenthesisAndContentType(
OperationBuilder operationBuilder, AssertableSnippets snippets) throws IOException {
new CurlRequestSnippet(this.commandFormatter).document(operationBuilder.request("http://localhost/upload")
.method("POST")
.header(HttpHeaders.CONTENT_TYPE, MediaType.MULTIPART_FORM_DATA_VALUE)
.part("note", "(optional)".getBytes())
.header(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN_VALUE)
.build());
String expectedContent = "$ curl 'http://localhost/upload' -i -X POST -H "
+ "'Content-Type: multipart/form-data' -F " + "'note=\"(optional)\";type=text/plain'";
assertThat(snippets.curlRequest())
.isCodeBlock((codeBlock) -> codeBlock.withLanguage("bash").content(expectedContent));
}

@RenderedSnippetTest
void multipartPostWithNoSubmittedFileNameAndQuotedContentContainingBackslash(OperationBuilder operationBuilder,
AssertableSnippets snippets) throws IOException {
new CurlRequestSnippet(this.commandFormatter).document(operationBuilder.request("http://localhost/upload")
.method("POST")
.header(HttpHeaders.CONTENT_TYPE, MediaType.MULTIPART_FORM_DATA_VALUE)
.part("path", "C:\\temp;D:\\temp".getBytes())
.build());
String expectedContent = "$ curl 'http://localhost/upload' -i -X POST -H "
+ "'Content-Type: multipart/form-data' -F " + "'path=\"C:\\\\temp;D:\\\\temp\"'";
assertThat(snippets.curlRequest())
.isCodeBlock((codeBlock) -> codeBlock.withLanguage("bash").content(expectedContent));
}

@RenderedSnippetTest
void multipartPostWithNoSubmittedFileNameAndContentThatDoesNotNeedQuoting(OperationBuilder operationBuilder,
AssertableSnippets snippets) throws IOException {
new CurlRequestSnippet(this.commandFormatter).document(operationBuilder.request("http://localhost/upload")
.method("POST")
.header(HttpHeaders.CONTENT_TYPE, MediaType.MULTIPART_FORM_DATA_VALUE)
.part("note", "say \"hi\" \\o/".getBytes())
.build());
String expectedContent = "$ curl 'http://localhost/upload' -i -X POST -H "
+ "'Content-Type: multipart/form-data' -F " + "'note=say \"hi\" \\o/'";
assertThat(snippets.curlRequest())
.isCodeBlock((codeBlock) -> codeBlock.withLanguage("bash").content(expectedContent));
}

@RenderedSnippetTest
void multipartPostWithContentType(OperationBuilder operationBuilder, AssertableSnippets snippets)
throws IOException {
Expand Down
Loading