org.springframework.mock.web.MockMultipartFile类的使用及代码示例

x33g5p2x  于2022-01-25 转载在 其他  
字(13.8k)|赞(0)|评价(0)|浏览(1234)

本文整理了Java中org.springframework.mock.web.MockMultipartFile类的一些代码示例,展示了MockMultipartFile类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。MockMultipartFile类的具体详情如下:
包路径:org.springframework.mock.web.MockMultipartFile
类名称:MockMultipartFile

MockMultipartFile介绍

[英]Mock implementation of the org.springframework.web.multipart.MultipartFileinterface.

Useful in conjunction with a MockMultipartHttpServletRequestfor testing application controllers that access multipart uploads.
[中]组织的模拟实现。springframework。网状物多部分。多部分文件接口。
与MockMultipartTtpServletRequest结合使用,可用于测试访问多部分上载的应用程序控制器。

代码示例

Official Spring framework guide

代码示例来源:origin: spring-guides/gs-uploading-files

@Test
public void saveAndLoad() {
  service.store(new MockMultipartFile("foo", "foo.txt", MediaType.TEXT_PLAIN_VALUE,
      "Hello World".getBytes()));
  assertThat(service.load("foo.txt")).exists();
}

Official Spring framework guide

代码示例来源:origin: spring-guides/gs-uploading-files

@Test(expected = StorageException.class)
public void saveNotPermitted() {
  service.store(new MockMultipartFile("foo", "../foo.txt",
      MediaType.TEXT_PLAIN_VALUE, "Hello World".getBytes()));
}

Official Spring framework guide

代码示例来源:origin: spring-guides/gs-uploading-files

@Test
public void savePermitted() {
  service.store(new MockMultipartFile("foo", "bar/../foo.txt",
      MediaType.TEXT_PLAIN_VALUE, "Hello World".getBytes()));
}

Official Spring framework guide

代码示例来源:origin: spring-guides/gs-uploading-files

@Test
public void shouldSaveUploadedFile() throws Exception {
  MockMultipartFile multipartFile = new MockMultipartFile("file", "test.txt",
      "text/plain", "Spring Framework".getBytes());
  this.mvc.perform(fileUpload("/").file(multipartFile))
      .andExpect(status().isFound())
      .andExpect(header().string("Location", "/"));
  then(this.storageService).should().store(multipartFile);
}

代码示例来源: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

@Test
public void testUpload() throws Exception {

      String endpoint = BASE_URL + "/upload/photo";

      FileInputStream fis = new FileInputStream("/home/me/Desktop/someDir/image.jpg");
      MockMultipartFile multipartFile = new MockMultipartFile("file", fis);

      HashMap<String, String> contentTypeParams = new HashMap<String, String>();
      contentTypeParams.put("boundary", "265001916915724");
      MediaType mediaType = new MediaType("multipart", "form-data", contentTypeParams);

      mockMvc.perform(
          post(endpoint)
          .content(multipartFile.getBytes())
          .contentType(mediaType))
          .andExpect(status().isOk());
}

代码示例来源: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")));
}

相关文章

微信公众号

最新文章

更多

MockMultipartFile类方法