Skip to content

Commit

Permalink
Fix HTTP request snippet's body for multipart PATCH requests
Browse files Browse the repository at this point in the history
Closes gh-933
  • Loading branch information
wilkinsona committed Jul 2, 2024
1 parent b6df324 commit 60f12e4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2023 the original author or authors.
* Copyright 2014-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -120,16 +120,17 @@ private String getRequestBody(OperationRequest request) {
if (StringUtils.hasText(content)) {
writer.printf("%n%s", content);
}
else if (isPutOrPost(request)) {
else if (isPutPostOrPatch(request)) {
if (!request.getParts().isEmpty()) {
writeParts(request, writer);
}
}
return httpRequest.toString();
}

private boolean isPutOrPost(OperationRequest request) {
return HttpMethod.PUT.equals(request.getMethod()) || HttpMethod.POST.equals(request.getMethod());
private boolean isPutPostOrPatch(OperationRequest request) {
return HttpMethod.PUT.equals(request.getMethod()) || HttpMethod.POST.equals(request.getMethod())
|| HttpMethod.PATCH.equals(request.getMethod());
}

private void writeParts(OperationRequest request, PrintWriter writer) {
Expand Down Expand Up @@ -169,7 +170,7 @@ private void writeMultipartEnd(PrintWriter writer) {
}

private boolean requiresFormEncodingContentTypeHeader(OperationRequest request) {
return request.getHeaders().get(HttpHeaders.CONTENT_TYPE) == null && isPutOrPost(request)
return request.getHeaders().get(HttpHeaders.CONTENT_TYPE) == null && isPutPostOrPatch(request)
&& !includeParametersInUri(request);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2023 the original author or authors.
* Copyright 2014-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -165,6 +165,36 @@ public void multipartPost() throws IOException {
.content(expectedContent));
}

@Test
public void multipartPut() throws IOException {
new HttpRequestSnippet().document(this.operationBuilder.request("http://localhost/upload")
.method("PUT")
.header(HttpHeaders.CONTENT_TYPE, MediaType.MULTIPART_FORM_DATA_VALUE)
.part("image", "<< data >>".getBytes())
.build());
String expectedContent = createPart(
String.format("Content-Disposition: " + "form-data; " + "name=image%n%n<< data >>"));
assertThat(this.generatedSnippets.httpRequest()).is(httpRequest(RequestMethod.PUT, "/upload")
.header("Content-Type", "multipart/form-data; boundary=" + BOUNDARY)
.header(HttpHeaders.HOST, "localhost")
.content(expectedContent));
}

@Test
public void multipartPatch() throws IOException {
new HttpRequestSnippet().document(this.operationBuilder.request("http://localhost/upload")
.method("PATCH")
.header(HttpHeaders.CONTENT_TYPE, MediaType.MULTIPART_FORM_DATA_VALUE)
.part("image", "<< data >>".getBytes())
.build());
String expectedContent = createPart(
String.format("Content-Disposition: " + "form-data; " + "name=image%n%n<< data >>"));
assertThat(this.generatedSnippets.httpRequest()).is(httpRequest(RequestMethod.PATCH, "/upload")
.header("Content-Type", "multipart/form-data; boundary=" + BOUNDARY)
.header(HttpHeaders.HOST, "localhost")
.content(expectedContent));
}

@Test
public void multipartPostWithFilename() throws IOException {
new HttpRequestSnippet().document(this.operationBuilder.request("http://localhost/upload")
Expand Down

0 comments on commit 60f12e4

Please sign in to comment.