Skip to content
Permalink
Browse files
Merge pull request #73 from arturobernalg/feature/FILEUPLOAD-320
FILEUPLOAD-320 - Use charset constant int the test V1
  • Loading branch information
jochenw committed Apr 27, 2021
2 parents 5d3e1d8 + 7be9057 commit d2dbb56218e0e96681eaa6d6be5436ee71542e4f
Showing 5 changed files with 16 additions and 11 deletions.
@@ -19,6 +19,7 @@
import static org.junit.jupiter.api.Assertions.*;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
@@ -84,7 +85,7 @@ public void testMoveFile() throws Exception {
"This is the content of the file\n" +
"\r\n" +
"-----1234--\r\n";
final byte[] contentBytes = content.getBytes("US-ASCII");
final byte[] contentBytes = content.getBytes(StandardCharsets.US_ASCII);
final HttpServletRequest request = new MockHttpServletRequest(contentBytes, Constants.CONTENT_TYPE);
final List<FileItem> items = myUpload.parseRequest(request);
assertNotNull(items);
@@ -23,6 +23,7 @@

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.stream.Stream;

@@ -220,7 +221,7 @@ public void testFILEUPLOAD62(final FileUpload upload) throws Exception {
"...contents of file2.gif...\r\n" +
"--BbC04y--\r\n" +
"--AaB03x--";
final List<FileItem> fileItems = Util.parseUpload(upload, request.getBytes("US-ASCII"), contentType);
final List<FileItem> fileItems = Util.parseUpload(upload, request.getBytes(StandardCharsets.US_ASCII), contentType);
assertEquals(3, fileItems.size());
final FileItem item0 = fileItems.get(0);
assertEquals("field1", item0.getFieldName());
@@ -19,6 +19,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;

@@ -63,7 +64,7 @@ public void parseParameterMap()
"\r\n" +
"value2\r\n" +
"-----1234--\r\n";
final byte[] bytes = text.getBytes("US-ASCII");
final byte[] bytes = text.getBytes(StandardCharsets.US_ASCII);
final HttpServletRequest request = new MockJakSrvltHttpRequest(bytes, Constants.CONTENT_TYPE);

final JakSrvltFileUpload upload = new JakSrvltFileUpload(new DiskFileItemFactory());
@@ -86,18 +87,18 @@ public void parseImpliedUtf8()
final String text = "-----1234\r\n" +
"Content-Disposition: form-data; name=\"utf8Html\"\r\n" +
"\r\n" +
"Thís ís the coñteñt of the fíle\n" +
"Thís ís the coñteñt of the fíle\n" +
"\r\n" +
"-----1234--\r\n";

final byte[] bytes = text.getBytes("UTF-8");
final byte[] bytes = text.getBytes(StandardCharsets.UTF_8);
final HttpServletRequest request = new MockJakSrvltHttpRequest(bytes, Constants.CONTENT_TYPE);

final DiskFileItemFactory fileItemFactory = new DiskFileItemFactory();
fileItemFactory.setDefaultCharset("UTF-8");
final JakSrvltFileUpload upload = new JakSrvltFileUpload(fileItemFactory);
final List<FileItem> fileItems = upload.parseRequest(request);
final FileItem fileItem = fileItems.get(0);
assertTrue(fileItem.getString().contains("coñteñt"), fileItem.getString());
assertTrue(fileItem.getString().contains("coñteñt"), fileItem.getString());
}
}
@@ -19,6 +19,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;

@@ -68,7 +69,7 @@ public void parseParameterMap()
"\r\n" +
"value2\r\n" +
"-----1234--\r\n";
final byte[] bytes = text.getBytes("US-ASCII");
final byte[] bytes = text.getBytes(StandardCharsets.US_ASCII);
final ActionRequest request = new MockPortletActionRequest(bytes, Constants.CONTENT_TYPE);

final Map<String, List<FileItem>> mappedParameters = upload.parseParameterMap(request);
@@ -24,6 +24,7 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;

import org.junit.jupiter.api.Test;

@@ -134,15 +135,15 @@ public void badLength() throws Exception {
// The non-ASCII characters should just be ignored
@Test
public void nonASCIIcharacter() throws Exception {
assertEncoded("f","Zg=À="); // A-grave
assertEncoded("f","Zg=À="); // A-grave
assertEncoded("f","Zg=\u0100=");
}

private static void assertEncoded(final String clearText, final String encoded) throws Exception {
final byte[] expected = clearText.getBytes(US_ASCII_CHARSET);
final byte[] expected = clearText.getBytes(StandardCharsets.US_ASCII);

final ByteArrayOutputStream out = new ByteArrayOutputStream(encoded.length());
final byte[] encodedData = encoded.getBytes(US_ASCII_CHARSET);
final byte[] encodedData = encoded.getBytes(StandardCharsets.US_ASCII);
Base64Decoder.decode(encodedData, out);
final byte[] actual = out.toByteArray();

@@ -151,7 +152,7 @@ private static void assertEncoded(final String clearText, final String encoded)

private static void assertIOException(final String messageText, final String encoded) throws UnsupportedEncodingException {
final ByteArrayOutputStream out = new ByteArrayOutputStream(encoded.length());
final byte[] encodedData = encoded.getBytes(US_ASCII_CHARSET);
final byte[] encodedData = encoded.getBytes(StandardCharsets.US_ASCII);
try {
Base64Decoder.decode(encodedData, out);
fail("Expected IOException");

0 comments on commit d2dbb56

Please sign in to comment.