Skip to content

Commit

Permalink
Add a unit test that verifies the charset of the multipart is used
Browse files Browse the repository at this point in the history
  • Loading branch information
mallowlabs authored and raphaelbauer committed Nov 20, 2015
1 parent d4fc844 commit 7e663a0
Showing 1 changed file with 41 additions and 12 deletions.
Expand Up @@ -29,6 +29,7 @@

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Map;

import javax.servlet.ReadListener;
Expand Down Expand Up @@ -896,18 +897,7 @@ public void testGetUTF8ParameterInMultipart() throws Exception {
+ "\r\n"
+ "✓\r\n"
+ "------Ninja--\r\n";
final ByteArrayInputStream bais = new ByteArrayInputStream(body.getBytes(NinjaConstant.UTF_8));

ServletInputStream sis = new ServletInputStream(){
@Override
public boolean isFinished() { return false; }
@Override
public boolean isReady() { return false; }
@Override
public void setReadListener(ReadListener readListener) { }
@Override
public int read() throws IOException { return bais.read(); }
};
ServletInputStream sis = createHttpServletRequestInputStream(body.getBytes(NinjaConstant.UTF_8));

when(httpServletRequest.getContentType()).thenReturn("multipart/form-data; boundary=----Ninja");
when(httpServletRequest.getMethod()).thenReturn("POST");
Expand All @@ -920,4 +910,43 @@ public void setReadListener(ReadListener readListener) { }

assertEquals("✓", context.getParameter("utf8"));
}

@Test
public void testGetWindows1250ParameterInMultipart() throws Exception {
String body = "------Ninja\r\n"
+ "content-disposition: form-data; name=\"field1\"\r\n"
+ "content-type: text/plain; charset=windows-1250\r\n"
+ "content-transfer-encoding: quoted-printable\r\n"
+ "\r\n"
+ "Joe owes €100.\r\n"
+ "------Ninja--\r\n";
ServletInputStream sis = createHttpServletRequestInputStream(body.getBytes("windows-1250"));

when(httpServletRequest.getContentType()).thenReturn("multipart/form-data; boundary=----Ninja");
when(httpServletRequest.getMethod()).thenReturn("POST");
when(ninjaProperties.getIntegerWithDefault(NinjaConstant.UPLOADS_MAX_FILE_SIZE, -1)).thenReturn(1024);
when(ninjaProperties.getIntegerWithDefault(NinjaConstant.UPLOADS_MAX_TOTAL_SIZE, -1)).thenReturn(1024);

when(httpServletRequest.getInputStream()).thenReturn(sis);

context.init(servletContext, httpServletRequest, httpServletResponse);

assertEquals("Joe owes €100.", context.getParameter("field1"));
}

private ServletInputStream createHttpServletRequestInputStream(byte[] bytes) throws UnsupportedEncodingException {
final ByteArrayInputStream bais = new ByteArrayInputStream(bytes);

ServletInputStream sis = new ServletInputStream(){
@Override
public boolean isFinished() { return false; }
@Override
public boolean isReady() { return false; }
@Override
public void setReadListener(ReadListener readListener) { }
@Override
public int read() throws IOException { return bais.read(); }
};
return sis;
}
}

0 comments on commit 7e663a0

Please sign in to comment.