diff --git a/spring-restdocs-core/src/main/java/org/springframework/restdocs/http/HttpRequestSnippet.java b/spring-restdocs-core/src/main/java/org/springframework/restdocs/http/HttpRequestSnippet.java index aaa7e592..ef761720 100644 --- a/spring-restdocs-core/src/main/java/org/springframework/restdocs/http/HttpRequestSnippet.java +++ b/spring-restdocs-core/src/main/java/org/springframework/restdocs/http/HttpRequestSnippet.java @@ -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. @@ -120,7 +120,7 @@ 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); } @@ -128,8 +128,9 @@ else if (isPutOrPost(request)) { 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) { @@ -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); } diff --git a/spring-restdocs-core/src/test/java/org/springframework/restdocs/http/HttpRequestSnippetTests.java b/spring-restdocs-core/src/test/java/org/springframework/restdocs/http/HttpRequestSnippetTests.java index f273334d..41cccdd1 100644 --- a/spring-restdocs-core/src/test/java/org/springframework/restdocs/http/HttpRequestSnippetTests.java +++ b/spring-restdocs-core/src/test/java/org/springframework/restdocs/http/HttpRequestSnippetTests.java @@ -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. @@ -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")