diff --git a/spring-test/src/main/java/org/springframework/mock/web/MockPart.java b/spring-test/src/main/java/org/springframework/mock/web/MockPart.java index 4e0a1d365c02..897891ff10a9 100644 --- a/spring-test/src/main/java/org/springframework/mock/web/MockPart.java +++ b/spring-test/src/main/java/org/springframework/mock/web/MockPart.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2023 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. @@ -63,24 +63,21 @@ public MockPart(String name, @Nullable byte[] content) { * @see #getHeaders() */ public MockPart(String name, @Nullable String filename, @Nullable byte[] content) { - Assert.hasLength(name, "'name' must not be empty"); - this.name = name; - this.filename = filename; - this.content = (content != null ? content : new byte[0]); - this.headers.setContentDispositionFormData(name, filename); + this(name, filename, content, null); } /** - * Constructor for a part with a filename, byte[] content and MediaType mediaType. + * Constructor for a part with a filename, byte[] content, and content type. + * @since 6.1.2 * @see #getHeaders() */ - public MockPart(String name, @Nullable String filename, @Nullable byte[] content, @Nullable MediaType mediaType) { + public MockPart(String name, @Nullable String filename, @Nullable byte[] content, @Nullable MediaType contentType) { Assert.hasLength(name, "'name' must not be empty"); this.name = name; this.filename = filename; this.content = (content != null ? content : new byte[0]); this.headers.setContentDispositionFormData(name, filename); - this.headers.setContentType(mediaType); + this.headers.setContentType(contentType); } diff --git a/spring-test/src/test/java/org/springframework/mock/web/MockMultipartHttpServletRequestTests.java b/spring-test/src/test/java/org/springframework/mock/web/MockMultipartHttpServletRequestTests.java index 6905183737e6..82ecd7858bbf 100644 --- a/spring-test/src/test/java/org/springframework/mock/web/MockMultipartHttpServletRequestTests.java +++ b/spring-test/src/test/java/org/springframework/mock/web/MockMultipartHttpServletRequestTests.java @@ -68,8 +68,7 @@ void mockMultiPartHttpServletRequestWithMixedData() { MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest(); request.addFile(new MockMultipartFile("file", "myOrigFilename", MediaType.TEXT_PLAIN_VALUE, "myContent2".getBytes())); - MockPart metadataPart = new MockPart("metadata", "{\"foo\": \"bar\"}".getBytes()); - metadataPart.getHeaders().setContentType(MediaType.APPLICATION_JSON); + MockPart metadataPart = new MockPart("metadata", null, "{\"foo\": \"bar\"}".getBytes(), MediaType.APPLICATION_JSON); request.addPart(metadataPart); HttpHeaders fileHttpHeaders = request.getMultipartHeaders("file"); diff --git a/spring-web/src/testFixtures/java/org/springframework/web/testfixture/servlet/MockPart.java b/spring-web/src/testFixtures/java/org/springframework/web/testfixture/servlet/MockPart.java index ac1ce65d0ef0..e9ca45c033df 100644 --- a/spring-web/src/testFixtures/java/org/springframework/web/testfixture/servlet/MockPart.java +++ b/spring-web/src/testFixtures/java/org/springframework/web/testfixture/servlet/MockPart.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2023 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. @@ -34,7 +34,7 @@ * * @author Rossen Stoyanchev * @author Juergen Hoeller - * @since 3.1 + * @since 4.3.12 * @see MockHttpServletRequest#addPart * @see MockMultipartFile */ @@ -63,11 +63,21 @@ public MockPart(String name, @Nullable byte[] content) { * @see #getHeaders() */ public MockPart(String name, @Nullable String filename, @Nullable byte[] content) { + this(name, filename, content, null); + } + + /** + * Constructor for a part with a filename, byte[] content, and content type. + * @since 6.1.2 + * @see #getHeaders() + */ + public MockPart(String name, @Nullable String filename, @Nullable byte[] content, @Nullable MediaType contentType) { Assert.hasLength(name, "'name' must not be empty"); this.name = name; this.filename = filename; this.content = (content != null ? content : new byte[0]); this.headers.setContentDispositionFormData(name, filename); + this.headers.setContentType(contentType); }