|
| 1 | +/* |
| 2 | + * Copyright 2002-2024 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.test.web.servlet.request; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.io.InputStream; |
| 21 | +import java.io.InputStreamReader; |
| 22 | +import java.nio.charset.Charset; |
| 23 | +import java.nio.charset.StandardCharsets; |
| 24 | +import java.util.ArrayList; |
| 25 | +import java.util.Collection; |
| 26 | +import java.util.List; |
| 27 | + |
| 28 | +import jakarta.servlet.ServletContext; |
| 29 | +import jakarta.servlet.http.Part; |
| 30 | + |
| 31 | +import org.springframework.http.HttpMethod; |
| 32 | +import org.springframework.http.MediaType; |
| 33 | +import org.springframework.lang.Nullable; |
| 34 | +import org.springframework.mock.web.MockHttpServletRequest; |
| 35 | +import org.springframework.mock.web.MockMultipartFile; |
| 36 | +import org.springframework.mock.web.MockMultipartHttpServletRequest; |
| 37 | +import org.springframework.util.Assert; |
| 38 | +import org.springframework.util.FileCopyUtils; |
| 39 | +import org.springframework.util.LinkedMultiValueMap; |
| 40 | +import org.springframework.util.MultiValueMap; |
| 41 | + |
| 42 | +/** |
| 43 | + * Base builder for {@link MockMultipartHttpServletRequest}. |
| 44 | + * |
| 45 | + * @author Rossen Stoyanchev |
| 46 | + * @author Arjen Poutsma |
| 47 | + * @author Stephane Nicoll |
| 48 | + * @since 6.2 |
| 49 | + * @param <B> a self reference to the builder type |
| 50 | + */ |
| 51 | +public abstract class AbstractMockMultipartHttpServletRequestBuilder<B extends AbstractMockMultipartHttpServletRequestBuilder<B>> |
| 52 | + extends AbstractMockHttpServletRequestBuilder<B> { |
| 53 | + |
| 54 | + private final List<MockMultipartFile> files = new ArrayList<>(); |
| 55 | + |
| 56 | + private final MultiValueMap<String, Part> parts = new LinkedMultiValueMap<>(); |
| 57 | + |
| 58 | + |
| 59 | + protected AbstractMockMultipartHttpServletRequestBuilder(HttpMethod httpMethod) { |
| 60 | + super(httpMethod); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Add a new {@link MockMultipartFile} with the given content. |
| 65 | + * @param name the name of the file |
| 66 | + * @param content the content of the file |
| 67 | + */ |
| 68 | + public B file(String name, byte[] content) { |
| 69 | + this.files.add(new MockMultipartFile(name, content)); |
| 70 | + return self(); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Add the given {@link MockMultipartFile}. |
| 75 | + * @param file the multipart file |
| 76 | + */ |
| 77 | + public B file(MockMultipartFile file) { |
| 78 | + this.files.add(file); |
| 79 | + return self(); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Add {@link Part} components to the request. |
| 84 | + * @param parts one or more parts to add |
| 85 | + * @since 5.0 |
| 86 | + */ |
| 87 | + public B part(Part... parts) { |
| 88 | + Assert.notEmpty(parts, "'parts' must not be empty"); |
| 89 | + for (Part part : parts) { |
| 90 | + this.parts.add(part.getName(), part); |
| 91 | + } |
| 92 | + return self(); |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public Object merge(@Nullable Object parent) { |
| 97 | + if (parent == null) { |
| 98 | + return this; |
| 99 | + } |
| 100 | + if (parent instanceof AbstractMockHttpServletRequestBuilder<?>) { |
| 101 | + super.merge(parent); |
| 102 | + if (parent instanceof AbstractMockMultipartHttpServletRequestBuilder<?> parentBuilder) { |
| 103 | + this.files.addAll(parentBuilder.files); |
| 104 | + parentBuilder.parts.keySet().forEach(name -> |
| 105 | + this.parts.putIfAbsent(name, parentBuilder.parts.get(name))); |
| 106 | + } |
| 107 | + } |
| 108 | + else { |
| 109 | + throw new IllegalArgumentException("Cannot merge with [" + parent.getClass().getName() + "]"); |
| 110 | + } |
| 111 | + return this; |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Create a new {@link MockMultipartHttpServletRequest} based on the |
| 116 | + * supplied {@code ServletContext} and the {@code MockMultipartFiles} |
| 117 | + * added to this builder. |
| 118 | + */ |
| 119 | + @Override |
| 120 | + protected final MockHttpServletRequest createServletRequest(ServletContext servletContext) { |
| 121 | + MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest(servletContext); |
| 122 | + Charset defaultCharset = (request.getCharacterEncoding() != null ? |
| 123 | + Charset.forName(request.getCharacterEncoding()) : StandardCharsets.UTF_8); |
| 124 | + |
| 125 | + this.files.forEach(request::addFile); |
| 126 | + this.parts.values().stream().flatMap(Collection::stream).forEach(part -> { |
| 127 | + request.addPart(part); |
| 128 | + try { |
| 129 | + String name = part.getName(); |
| 130 | + String filename = part.getSubmittedFileName(); |
| 131 | + InputStream is = part.getInputStream(); |
| 132 | + if (filename != null) { |
| 133 | + request.addFile(new MockMultipartFile(name, filename, part.getContentType(), is)); |
| 134 | + } |
| 135 | + else { |
| 136 | + InputStreamReader reader = new InputStreamReader(is, getCharsetOrDefault(part, defaultCharset)); |
| 137 | + String value = FileCopyUtils.copyToString(reader); |
| 138 | + request.addParameter(part.getName(), value); |
| 139 | + } |
| 140 | + } |
| 141 | + catch (IOException ex) { |
| 142 | + throw new IllegalStateException("Failed to read content for part " + part.getName(), ex); |
| 143 | + } |
| 144 | + }); |
| 145 | + |
| 146 | + return request; |
| 147 | + } |
| 148 | + |
| 149 | + private Charset getCharsetOrDefault(Part part, Charset defaultCharset) { |
| 150 | + if (part.getContentType() != null) { |
| 151 | + MediaType mediaType = MediaType.parseMediaType(part.getContentType()); |
| 152 | + if (mediaType.getCharset() != null) { |
| 153 | + return mediaType.getCharset(); |
| 154 | + } |
| 155 | + } |
| 156 | + return defaultCharset; |
| 157 | + } |
| 158 | + |
| 159 | +} |
0 commit comments