Unverified Commit 758ff920 by Spencer Gibb

Merge pull request #1351 from VanRoy/improve-formbodywrapperfilter-test

* improve-formbodywrapperfilter-test: Add test on FormBodyWrapperFilter to avoid regressions (#1171)
parents 61de1181 7ba80639
...@@ -16,10 +16,7 @@ ...@@ -16,10 +16,7 @@
package org.springframework.cloud.netflix.zuul; package org.springframework.cloud.netflix.zuul;
import static org.junit.Assert.assertEquals;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.util.Map; import java.util.Map;
import org.junit.Before; import org.junit.Before;
...@@ -59,6 +56,10 @@ import com.netflix.loadbalancer.ServerList; ...@@ -59,6 +56,10 @@ import com.netflix.loadbalancer.ServerList;
import com.netflix.zuul.ZuulFilter; import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext; import com.netflix.zuul.context.RequestContext;
import static java.nio.charset.Charset.defaultCharset;
import static org.junit.Assert.assertEquals;
import static org.springframework.util.StreamUtils.copyToString;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
...@@ -122,6 +123,24 @@ public class FormZuulProxyApplicationTests { ...@@ -122,6 +123,24 @@ public class FormZuulProxyApplicationTests {
} }
@Test @Test
public void postWithMultipartFileAndForm() {
MultiValueMap<String, Object> form = new LinkedMultiValueMap<String, Object>();
HttpHeaders part = new HttpHeaders();
part.setContentType(MediaType.TEXT_PLAIN);
part.setContentDispositionFormData("file", "foo.txt");
form.set("foo", new HttpEntity<byte[]>("bar".getBytes(), part));
form.set("field", "data");
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
ResponseEntity<String> result = new TestRestTemplate().exchange(
"http://localhost:" + this.port + "/simple/fileandform", HttpMethod.POST,
new HttpEntity<MultiValueMap<String, Object>>(form, headers),
String.class);
assertEquals(HttpStatus.OK, result.getStatusCode());
assertEquals("Posted! bar!field!data", result.getBody());
}
@Test
public void postWithUTF8Form() { public void postWithUTF8Form() {
MultiValueMap<String, String> form = new LinkedMultiValueMap<String, String>(); MultiValueMap<String, String> form = new LinkedMultiValueMap<String, String>();
form.set("foo", "bar"); form.set("foo", "bar");
...@@ -190,22 +209,15 @@ class FormZuulProxyApplication { ...@@ -190,22 +209,15 @@ class FormZuulProxyApplication {
@RequestMapping(value = "/file", method = RequestMethod.POST) @RequestMapping(value = "/file", method = RequestMethod.POST)
public String file(@RequestParam(required = false) MultipartFile file) public String file(@RequestParam(required = false) MultipartFile file)
throws IOException { throws IOException {
byte[] bytes = new byte[0];
if (file != null) { return "Posted! " + copyToString(file.getInputStream(), defaultCharset());
if (file.getSize() > 1024) { }
bytes = new byte[1024];
InputStream inputStream = file.getInputStream(); @RequestMapping(value = "/fileandform", method = RequestMethod.POST)
inputStream.read(bytes); public String fileAndForm(@RequestParam MultipartFile file, @RequestParam String field)
byte[] buffer = new byte[1024 * 1024 * 10]; throws IOException {
while (inputStream.read(buffer) >= 0) {
log.info("Read more bytes"); return "Posted! " + copyToString(file.getInputStream(), defaultCharset()) + "!field!" + field;
}
}
else {
bytes = file.getBytes();
}
}
return "Posted! " + new String(bytes);
} }
@Bean @Bean
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment