Added support for multipart with content type, fixes gh-599
This commit is contained in:
@@ -7,6 +7,9 @@ import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
|
||||
import org.springframework.cloud.contract.stubrunner.spring.AutoConfigureStubRunner;
|
||||
import org.springframework.cloud.contract.stubrunner.spring.StubRunnerProperties;
|
||||
import org.springframework.core.io.ByteArrayResource;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@@ -14,6 +17,9 @@ import com.example.loan.model.Client;
|
||||
import com.example.loan.model.LoanApplication;
|
||||
import com.example.loan.model.LoanApplicationResult;
|
||||
import com.example.loan.model.LoanApplicationStatus;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@@ -81,4 +87,36 @@ public class LoanApplicationServiceTests {
|
||||
assertThat(cookies).isEqualTo("foo bar");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldSuccessfullyWorkWithMultipart() {
|
||||
MultiValueMap<String, Object> parameters = new LinkedMultiValueMap<>();
|
||||
parameters.add("file1", new ByteArrayResource(("content").getBytes()) {
|
||||
@Override
|
||||
public String getFilename() {
|
||||
return "file1";
|
||||
}
|
||||
});
|
||||
parameters.add("file2", new ByteArrayResource(("content").getBytes()) {
|
||||
@Override
|
||||
public String getFilename() {
|
||||
return "file2";
|
||||
}
|
||||
});
|
||||
parameters.add("test", new ByteArrayResource(("{\n \"status\": \"test\"\n}").getBytes()) {
|
||||
@Override
|
||||
public String getFilename() {
|
||||
return "test";
|
||||
}
|
||||
});
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.set("Content-Type", "multipart/form-data");
|
||||
headers.set("Accept", "text/plain");
|
||||
String result = new RestTemplate().postForObject(
|
||||
"http://localhost:6565/tests",
|
||||
new HttpEntity<MultiValueMap<String, Object>>(parameters, headers),
|
||||
String.class);
|
||||
|
||||
assertThat(result).isEqualTo("{\"status\":\"ok\"}");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.example.fraud;
|
||||
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestPart;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
@RestController
|
||||
public class TestController {
|
||||
|
||||
@PostMapping("/tests")
|
||||
public Test createNew(@RequestPart MultipartFile file1,
|
||||
@RequestPart MultipartFile file2,
|
||||
@RequestPart Test test) {
|
||||
return new Test("ok");
|
||||
}
|
||||
}
|
||||
|
||||
class Test {
|
||||
private String status;
|
||||
|
||||
public Test(String status) {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public Test() {
|
||||
}
|
||||
|
||||
public String getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public void setStatus(String status) {
|
||||
this.status = status;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.example.fraud;
|
||||
|
||||
import com.example.fraud.TestController;
|
||||
import io.restassured.module.mockmvc.RestAssuredMockMvc;
|
||||
import org.junit.Before;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
public class MultipartBase {
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
RestAssuredMockMvc.standaloneSetup(new TestController());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
import org.springframework.cloud.contract.spec.Contract
|
||||
|
||||
Contract.make {
|
||||
request {
|
||||
method 'POST'
|
||||
url '/tests'
|
||||
multipart(
|
||||
[
|
||||
file1: named(
|
||||
name: value(consumer(regex(nonEmpty())), producer('filename1')),
|
||||
content: value(consumer(regex(nonEmpty())), producer('content1'))),
|
||||
file2: named(
|
||||
name: value(consumer(regex(nonEmpty())), producer('filename1')),
|
||||
content: value(c(regex(nonEmpty())), producer('content2'))),
|
||||
test : named(
|
||||
name: value(consumer(regex(nonEmpty())), producer('filename1')),
|
||||
content: value(c(regex(nonEmpty())), producer(file("test.json"))),
|
||||
contentType: "application/json")
|
||||
]
|
||||
)
|
||||
|
||||
headers {
|
||||
contentType('multipart/form-data')
|
||||
}
|
||||
}
|
||||
response {
|
||||
status 200
|
||||
body([
|
||||
status: 'ok'
|
||||
])
|
||||
headers {
|
||||
contentType('application/json')
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"status": "test"
|
||||
}
|
||||
Reference in New Issue
Block a user