Skip to content

Commit

Permalink
multipart/form-data endpoint success from REST client but fails in Ka…
Browse files Browse the repository at this point in the history
…rate tests karatelabs#797
  • Loading branch information
Nishant-sehgal committed Oct 3, 2019
1 parent 7b5438f commit cecbb66
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,88 +41,92 @@
* @author pthomas3
*/
public class MockMultiPart implements Part {

private final MultiPartItem item;
private final byte[] bytes;
private final Map<String, String> headers;
private static final String CONTENT_TYPE = "content-type";
private static final String CONTENT_DISPOSITION = "content-disposition";

public MockMultiPart(MultiPartItem item) {
this.item = item;
ScriptValue sv = item.getValue();
if (sv.isNull()) {
bytes = new byte[0];
} else {
String temp = sv.getAsString();
bytes = temp.getBytes();
}
headers = new HashMap<>(2);
String ct = item.getContentType();
if (ct == null) {
ct = HttpUtils.getContentType(sv);
}
headers.put(CONTENT_TYPE, ct);
String disposition = "form-data";
String filename = item.getFilename();
if (filename != null) {
disposition = disposition + "; filename=\"" + filename + "\"";
}
disposition = disposition + "; name=\"" + item.getName() + "\"";
headers.put(CONTENT_DISPOSITION, disposition);
}

public boolean isFile() {
return item.getValue().getType() == Type.INPUT_STREAM;
}

public String getValue() {
return item.getValue().getAsString();
}

@Override
public InputStream getInputStream() throws IOException {
return new ByteArrayInputStream(bytes);
}

@Override
public String getContentType() {
return headers.get(CONTENT_TYPE);
}

@Override
public String getName() {
return item.getName();
}

@Override
public long getSize() {
return bytes.length;
}

@Override
public void write(String string) throws IOException {

}

@Override
public void delete() throws IOException {

}

@Override
public String getHeader(String string) {
return headers.get(string);
}

@Override
public Collection<String> getHeaders(String string) {
return Collections.singletonList(headers.get(string));
}

@Override
public Collection<String> getHeaderNames() {
return headers.keySet();
}

}

private final MultiPartItem item;
private final byte[] bytes;
private final Map<String, String> headers;
private static final String CONTENT_TYPE = "content-type";
private static final String CONTENT_DISPOSITION = "content-disposition";

public MockMultiPart(MultiPartItem item) {
this.item = item;
ScriptValue sv = item.getValue();
if (sv.isNull()) {
bytes = new byte[0];
} else {
String temp = sv.getAsString();
bytes = temp.getBytes();
}
headers = new HashMap<>(2);
String ct = item.getContentType();
if (ct == null) {
ct = HttpUtils.getContentType(sv);
}
headers.put(CONTENT_TYPE, ct);
String disposition = "form-data";
String filename = item.getFilename();
if (filename != null) {
disposition = disposition + "; filename=\"" + filename + "\"";
}
disposition = disposition + "; name=\"" + item.getName() + "\"";
headers.put(CONTENT_DISPOSITION, disposition);
}

public boolean isFile() {
return item.getValue().getType() == Type.INPUT_STREAM;
}

public String getValue() {
return item.getValue().getAsString();
}

@Override
public InputStream getInputStream() throws IOException {
return new ByteArrayInputStream(bytes);
}

@Override
public String getContentType() {
return headers.get(CONTENT_TYPE);
}

@Override
public String getName() {
return item.getName();
}

@Override
public long getSize() {
return bytes.length;
}

@Override
public void write(String string) throws IOException {

}

@Override
public void delete() throws IOException {

}

@Override
public String getHeader(String string) {
/**
* support spring boot 2 StandardMultipartHttpServletRequest implementation to
* give CONTENT_DISPOSITION header details.
*/
return headers.getOrDefault(string, headers.get(string.toLowerCase()));
}

@Override
public Collection<String> getHeaders(String string) {
return Collections.singletonList(headers.get(string));
}

@Override
public Collection<String> getHeaderNames() {
return headers.keySet();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.intuit.karate.mock.servlet.test;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.springframework.http.HttpHeaders;

import com.intuit.karate.ScriptValue;
import com.intuit.karate.http.MultiPartItem;
import com.intuit.karate.mock.servlet.MockMultiPart;

/**
* @author nsehgal
*
* Test for different StandardMultipartHttpServletRequest implementation
* in spring. Below test checks both the implementation should return
* the CONTENT_DISPOSITION header details when asked via getHeader().
*
*/
public class MockMultiPartTest {

private MockMultiPart mockMultiPart = null;

private static final String CONTENT_DISPOSITION = "content-disposition";

@Before
public void init() {
ScriptValue NULL = new ScriptValue(null);
MultiPartItem item = new MultiPartItem("file", NULL);
item.setContentType("text/csv");
item.setFilename("test.csv");
mockMultiPart = new MockMultiPart(item);
}

@Test
public void testSpring2MultipartHeader() {
String headerValue = mockMultiPart.getHeader(HttpHeaders.CONTENT_DISPOSITION);
Assert.assertNotNull(headerValue);
Assert.assertEquals("form-data; filename=\"test.csv\"; name=\"file\"", headerValue);
}

@Test
public void testSpring1MultipartHeader() {
String headerValue = mockMultiPart.getHeader(CONTENT_DISPOSITION);
Assert.assertNotNull(headerValue);
Assert.assertEquals("form-data; filename=\"test.csv\"; name=\"file\"", headerValue);
}
}

0 comments on commit cecbb66

Please sign in to comment.