commit
c18d1ce7ca
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
* Copyright 2002-2019 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.
|
||||
|
|
@ -16,6 +16,7 @@
|
|||
|
||||
package org.springframework.mock.web;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collections;
|
||||
import java.util.Enumeration;
|
||||
import java.util.Iterator;
|
||||
|
|
@ -23,6 +24,8 @@ import java.util.List;
|
|||
import java.util.Map;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.Part;
|
||||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
|
|
@ -121,9 +124,17 @@ public class MockMultipartHttpServletRequest extends MockHttpServletRequest impl
|
|||
if (file != null) {
|
||||
return file.getContentType();
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
try {
|
||||
Part part = getPart(paramOrFileName);
|
||||
if (part != null) {
|
||||
return part.getContentType();
|
||||
}
|
||||
}
|
||||
catch (ServletException | IOException ex) {
|
||||
// Should never happen (we're not actually parsing)
|
||||
throw new IllegalStateException(ex);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -27,6 +27,8 @@ import java.util.Set;
|
|||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.util.FileCopyUtils;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
|
@ -61,6 +63,24 @@ class MockMultipartHttpServletRequestTests {
|
|||
doTestMultipartHttpServletRequest(request);
|
||||
}
|
||||
|
||||
@Test
|
||||
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);
|
||||
request.addPart(metadataPart);
|
||||
|
||||
HttpHeaders fileHttpHeaders = request.getMultipartHeaders("file");
|
||||
assertThat(fileHttpHeaders).isNotNull();
|
||||
assertThat(fileHttpHeaders.getContentType()).isEqualTo(MediaType.TEXT_PLAIN);
|
||||
|
||||
HttpHeaders dataHttpHeaders = request.getMultipartHeaders("metadata");
|
||||
assertThat(dataHttpHeaders).isNotNull();
|
||||
assertThat(dataHttpHeaders.getContentType()).isEqualTo(MediaType.APPLICATION_JSON);
|
||||
}
|
||||
|
||||
private void doTestMultipartHttpServletRequest(MultipartHttpServletRequest request) throws IOException {
|
||||
Set<String> fileNames = new HashSet<>();
|
||||
Iterator<String> fileIter = request.getFileNames();
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
* Copyright 2002-2019 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.
|
||||
|
|
@ -16,6 +16,7 @@
|
|||
|
||||
package org.springframework.mock.web.test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collections;
|
||||
import java.util.Enumeration;
|
||||
import java.util.Iterator;
|
||||
|
|
@ -23,6 +24,8 @@ import java.util.List;
|
|||
import java.util.Map;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.Part;
|
||||
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
|
|
@ -121,9 +124,17 @@ public class MockMultipartHttpServletRequest extends MockHttpServletRequest impl
|
|||
if (file != null) {
|
||||
return file.getContentType();
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
try {
|
||||
Part part = getPart(paramOrFileName);
|
||||
if (part != null) {
|
||||
return part.getContentType();
|
||||
}
|
||||
}
|
||||
catch (ServletException | IOException ex) {
|
||||
// Should never happen (we're not actually parsing)
|
||||
throw new IllegalStateException(ex);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
Loading…
Reference in New Issue