本文整理了Java中org.springframework.mock.web.MockMultipartFile.<init>()
方法的一些代码示例,展示了MockMultipartFile.<init>()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。MockMultipartFile.<init>()
方法的具体详情如下:
包路径:org.springframework.mock.web.MockMultipartFile
类名称:MockMultipartFile
方法名:<init>
[英]Create a new MockMultipartFile with the given content.
[中]用给定的内容创建一个新的MockMultipartFile。
代码示例来源:origin: spring-projects/spring-framework
/**
* Create a new MockMultipartFile with the given content.
* @param name the name of the file
* @param content the content of the file
*/
public MockMultipartHttpServletRequestBuilder file(String name, byte[] content) {
this.files.add(new MockMultipartFile(name, content));
return this;
}
代码示例来源:origin: stackoverflow.com
@WebAppConfiguration
@ContextConfiguration(classes = WebConfig.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class Example {
@Autowired
private WebApplicationContext webApplicationContext;
@Test
public void test() throws Exception {
MockMultipartFile firstFile = new MockMultipartFile("data", "filename.txt", "text/plain", "some xml".getBytes());
MockMultipartFile secondFile = new MockMultipartFile("data", "other-file-name.data", "text/plain", "some other type".getBytes());
MockMultipartFile jsonFile = new MockMultipartFile("json", "", "application/json", "{\"json\": \"someValue\"}".getBytes());
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
mockMvc.perform(MockMvcRequestBuilders.fileUpload("/upload")
.file(firstFile)
.file(secondFile).file(jsonFile)
.param("some-random", "4"))
.andExpect(status().is(200))
.andExpect(content().string("success"));
}
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void mockMultipartHttpServletRequestWithInputStream() throws IOException {
MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
request.addFile(new MockMultipartFile("file1", new ByteArrayInputStream("myContent1".getBytes())));
request.addFile(new MockMultipartFile("file2", "myOrigFilename", "text/plain", new ByteArrayInputStream(
"myContent2".getBytes())));
doTestMultipartHttpServletRequest(request);
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void mockMultipartHttpServletRequestWithByteArray() throws IOException {
MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
assertFalse(request.getFileNames().hasNext());
assertNull(request.getFile("file1"));
assertNull(request.getFile("file2"));
assertTrue(request.getFileMap().isEmpty());
request.addFile(new MockMultipartFile("file1", "myContent1".getBytes()));
request.addFile(new MockMultipartFile("file2", "myOrigFilename", "text/plain", "myContent2".getBytes()));
doTestMultipartHttpServletRequest(request);
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void multipartRequestWithFileList() throws Exception {
byte[] fileContent = "bar".getBytes(StandardCharsets.UTF_8);
MockMultipartFile filePart1 = new MockMultipartFile("file", "orig", null, fileContent);
MockMultipartFile filePart2 = new MockMultipartFile("file", "orig", null, fileContent);
byte[] json = "{\"name\":\"yeeeah\"}".getBytes(StandardCharsets.UTF_8);
MockMultipartFile jsonPart = new MockMultipartFile("json", "json", "application/json", json);
standaloneSetup(new MultipartController()).build()
.perform(multipart("/multipartfilelist").file(filePart1).file(filePart2).file(jsonPart))
.andExpect(status().isFound())
.andExpect(model().attribute("fileContent", fileContent))
.andExpect(model().attribute("jsonContent", Collections.singletonMap("name", "yeeeah")));
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void multipartRequestWithOptionalFileArray() throws Exception {
byte[] fileContent = "bar".getBytes(StandardCharsets.UTF_8);
MockMultipartFile filePart1 = new MockMultipartFile("file", "orig", null, fileContent);
MockMultipartFile filePart2 = new MockMultipartFile("file", "orig", null, fileContent);
byte[] json = "{\"name\":\"yeeeah\"}".getBytes(StandardCharsets.UTF_8);
MockMultipartFile jsonPart = new MockMultipartFile("json", "json", "application/json", json);
standaloneSetup(new MultipartController()).build()
.perform(multipart("/optionalfilearray").file(filePart1).file(filePart2).file(jsonPart))
.andExpect(status().isFound())
.andExpect(model().attribute("fileContent", fileContent))
.andExpect(model().attribute("jsonContent", Collections.singletonMap("name", "yeeeah")));
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void multipartRequestWithFileArray() throws Exception {
byte[] fileContent = "bar".getBytes(StandardCharsets.UTF_8);
MockMultipartFile filePart1 = new MockMultipartFile("file", "orig", null, fileContent);
MockMultipartFile filePart2 = new MockMultipartFile("file", "orig", null, fileContent);
byte[] json = "{\"name\":\"yeeeah\"}".getBytes(StandardCharsets.UTF_8);
MockMultipartFile jsonPart = new MockMultipartFile("json", "json", "application/json", json);
standaloneSetup(new MultipartController()).build()
.perform(multipart("/multipartfilearray").file(filePart1).file(filePart2).file(jsonPart))
.andExpect(status().isFound())
.andExpect(model().attribute("fileContent", fileContent))
.andExpect(model().attribute("jsonContent", Collections.singletonMap("name", "yeeeah")));
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void multipartRequestWithOptionalFileList() throws Exception {
byte[] fileContent = "bar".getBytes(StandardCharsets.UTF_8);
MockMultipartFile filePart1 = new MockMultipartFile("file", "orig", null, fileContent);
MockMultipartFile filePart2 = new MockMultipartFile("file", "orig", null, fileContent);
byte[] json = "{\"name\":\"yeeeah\"}".getBytes(StandardCharsets.UTF_8);
MockMultipartFile jsonPart = new MockMultipartFile("json", "json", "application/json", json);
standaloneSetup(new MultipartController()).build()
.perform(multipart("/optionalfilelist").file(filePart1).file(filePart2).file(jsonPart))
.andExpect(status().isFound())
.andExpect(model().attribute("fileContent", fileContent))
.andExpect(model().attribute("jsonContent", Collections.singletonMap("name", "yeeeah")));
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void multipartRequestWithOptionalFile() throws Exception {
byte[] fileContent = "bar".getBytes(StandardCharsets.UTF_8);
MockMultipartFile filePart = new MockMultipartFile("file", "orig", null, fileContent);
byte[] json = "{\"name\":\"yeeeah\"}".getBytes(StandardCharsets.UTF_8);
MockMultipartFile jsonPart = new MockMultipartFile("json", "json", "application/json", json);
standaloneSetup(new MultipartController()).build()
.perform(multipart("/optionalfile").file(filePart).file(jsonPart))
.andExpect(status().isFound())
.andExpect(model().attribute("fileContent", fileContent))
.andExpect(model().attribute("jsonContent", Collections.singletonMap("name", "yeeeah")));
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void multipartRequestWithSingleFile() throws Exception {
byte[] fileContent = "bar".getBytes(StandardCharsets.UTF_8);
MockMultipartFile filePart = new MockMultipartFile("file", "orig", null, fileContent);
byte[] json = "{\"name\":\"yeeeah\"}".getBytes(StandardCharsets.UTF_8);
MockMultipartFile jsonPart = new MockMultipartFile("json", "json", "application/json", json);
standaloneSetup(new MultipartController()).build()
.perform(multipart("/multipartfile").file(filePart).file(jsonPart))
.andExpect(status().isFound())
.andExpect(model().attribute("fileContent", fileContent))
.andExpect(model().attribute("jsonContent", Collections.singletonMap("name", "yeeeah")));
}
代码示例来源:origin: spring-projects/spring-framework
@Test // SPR-13317
public void multipartRequestWrapped() throws Exception {
byte[] json = "{\"name\":\"yeeeah\"}".getBytes(StandardCharsets.UTF_8);
MockMultipartFile jsonPart = new MockMultipartFile("json", "json", "application/json", json);
Filter filter = new RequestWrappingFilter();
MockMvc mockMvc = standaloneSetup(new MultipartController()).addFilter(filter).build();
Map<String, String> jsonMap = Collections.singletonMap("name", "yeeeah");
mockMvc.perform(multipart("/json").file(jsonPart)).andExpect(model().attribute("json", jsonMap));
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void multipartRequestWithOptionalFileArrayNotPresent() throws Exception {
byte[] json = "{\"name\":\"yeeeah\"}".getBytes(StandardCharsets.UTF_8);
MockMultipartFile jsonPart = new MockMultipartFile("json", "json", "application/json", json);
standaloneSetup(new MultipartController()).build()
.perform(multipart("/optionalfilearray").file(jsonPart))
.andExpect(status().isFound())
.andExpect(model().attributeDoesNotExist("fileContent"))
.andExpect(model().attribute("jsonContent", Collections.singletonMap("name", "yeeeah")));
}
代码示例来源:origin: rest-assured/rest-assured
String mimeType = multiPart.getMimeType();
if (multiPart.isByteArray()) {
multipartFile = new MockMultipartFile(controlName, fileName, mimeType, (byte[]) multiPart.getContent());
} else if (multiPart.isFile() || multiPart.isInputStream()) {
InputStream inputStream;
multipartFile = new MockMultipartFile(controlName, fileName, mimeType, inputStream);
} catch (IOException e) {
return SafeExceptionRethrower.safeRethrow(e);
multipartFile = new MockMultipartFile(controlName, fileName, mimeType, ((String) multiPart.getContent()).getBytes());
代码示例来源:origin: spring-projects/spring-framework
@Test
public void multipartRequestWithOptionalFileNotPresent() throws Exception {
byte[] json = "{\"name\":\"yeeeah\"}".getBytes(StandardCharsets.UTF_8);
MockMultipartFile jsonPart = new MockMultipartFile("json", "json", "application/json", json);
standaloneSetup(new MultipartController()).build()
.perform(multipart("/optionalfile").file(jsonPart))
.andExpect(status().isFound())
.andExpect(model().attributeDoesNotExist("fileContent"))
.andExpect(model().attribute("jsonContent", Collections.singletonMap("name", "yeeeah")));
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void multipartRequestWithOptionalFileListNotPresent() throws Exception {
byte[] json = "{\"name\":\"yeeeah\"}".getBytes(StandardCharsets.UTF_8);
MockMultipartFile jsonPart = new MockMultipartFile("json", "json", "application/json", json);
standaloneSetup(new MultipartController()).build()
.perform(multipart("/optionalfilelist").file(jsonPart))
.andExpect(status().isFound())
.andExpect(model().attributeDoesNotExist("fileContent"))
.andExpect(model().attribute("jsonContent", Collections.singletonMap("name", "yeeeah")));
}
代码示例来源:origin: stackoverflow.com
public void testAddContacts() throws Exception{
File f = new File("C:\\Users\\Public\\Pictures\\Sample Pictures\\Penguins.jpg");
System.out.println(f.isFile()+" "+f.getName()+f.exists());
FileInputStream fi1 = new FileInputStream(f);
FileInputStream fi2 = new FileInputStream(new File("C:\\Users\\Public\\Pictures\\Sample Pictures\\Tulips.jpg"));
MockMultipartFile fstmp = new MockMultipartFile("upload", f.getName(), "multipart/form-data",fi1);
MockMultipartFile secmp = new MockMultipartFile("upload", "Tulips.jpg","multipart/form-data",fi2);
MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
mockMvc.perform(MockMvcRequestBuilders.fileUpload("/AddContacts")
.file(fstmp)
.file(secmp)
.param("name","abc").param("email","abc@gmail.com").param("phone", "1234567890"))
.andExpect(status().isOk());
}
代码示例来源:origin: spring-projects/spring-restdocs
@Test
public void mockMultipartFileUploadWithContentType() throws Exception {
OperationRequest request = createOperationRequest(
MockMvcRequestBuilders.multipart("/foo").file(new MockMultipartFile(
"file", "original", "image/png", new byte[] { 1, 2, 3, 4 })));
assertThat(request.getUri()).isEqualTo(URI.create("http://localhost/foo"));
assertThat(request.getMethod()).isEqualTo(HttpMethod.POST);
assertThat(request.getParts().size()).isEqualTo(1);
OperationRequestPart part = request.getParts().iterator().next();
assertThat(part.getName()).isEqualTo("file");
assertThat(part.getSubmittedFileName()).isEqualTo("original");
assertThat(part.getHeaders().getContentType()).isEqualTo(MediaType.IMAGE_PNG);
assertThat(part.getContent()).isEqualTo(new byte[] { 1, 2, 3, 4 });
}
代码示例来源:origin: spring-projects/spring-restdocs
@Test
public void mockMultipartFileUpload() throws Exception {
OperationRequest request = createOperationRequest(
MockMvcRequestBuilders.multipart("/foo")
.file(new MockMultipartFile("file", new byte[] { 1, 2, 3, 4 })));
assertThat(request.getUri()).isEqualTo(URI.create("http://localhost/foo"));
assertThat(request.getMethod()).isEqualTo(HttpMethod.POST);
assertThat(request.getParts().size()).isEqualTo(1);
OperationRequestPart part = request.getParts().iterator().next();
assertThat(part.getName()).isEqualTo("file");
assertThat(part.getSubmittedFileName()).isNull();
assertThat(part.getHeaders().size()).isEqualTo(1);
assertThat(part.getHeaders().getContentLength()).isEqualTo(4L);
assertThat(part.getContent()).isEqualTo(new byte[] { 1, 2, 3, 4 });
}
代码示例来源:origin: spring-projects/spring-restdocs
public void body() throws Exception {
// tag::body[]
MockMultipartFile image = new MockMultipartFile("image", "image.png", "image/png",
"<<png data>>".getBytes());
MockMultipartFile metadata = new MockMultipartFile("metadata", "",
"application/json", "{ \"version\": \"1.0\"}".getBytes());
this.mockMvc.perform(fileUpload("/images").file(image).file(metadata)
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andDo(document("image-upload", requestPartBody("metadata"))); // <1>
// end::body[]
}
代码示例来源:origin: spring-projects/spring-restdocs
public void fields() throws Exception {
// tag::fields[]
MockMultipartFile image = new MockMultipartFile("image", "image.png", "image/png",
"<<png data>>".getBytes());
MockMultipartFile metadata = new MockMultipartFile("metadata", "",
"application/json", "{ \"version\": \"1.0\"}".getBytes());
this.mockMvc.perform(fileUpload("/images").file(image).file(metadata)
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andDo(document("image-upload", requestPartFields("metadata", // <1>
fieldWithPath("version").description("The version of the image")))); // <2>
// end::fields[]
}
内容来源于网络,如有侵权,请联系作者删除!