I made controller for uploading zip files, and when I tested it through Postman, everything worked as intended.
Now I'm trying to create test for this controller, using custom test framework.
Part of my controller method for extracting zip:
try (final ZipInputStream zis = new ZipInputStream(file.getInputStream())) {
byte[] buffer = new byte[1024];
ZipEntry entry;
// until here everything works, next line throws EOFException
while ((entry = zis.getNextEntry()) != null) {
String entryName = entry.getName();
StringBuilder stringBuilder = new StringBuilder();
int read;
while ((read = zis.read(buffer, 0, buffer.length)) != -1) {
stringBuilder.append(new String(buffer,0, read ));
}
}
} catch (IOException e) {
throw new GeneralServerException(e);
}
Everything works fine up to line: ((entry = zis.getNextEntry()) != null):
Caused by: java.io.EOFException
at java.util.zip.ZipInputStream.readFully(ZipInputStream.java:405) ~[?:1.8.0_181]
at java.util.zip.ZipInputStream.readLOC(ZipInputStream.java:321) ~[?:1.8.0_181]
at java.util.zip.ZipInputStream.getNextEntry(ZipInputStream.java:122) ~[?:1.8.0_181]
... 81 more
And this is part of my class for creating multipart request:
private static final String FILE_PART_PATTERN = "\r\nContent-Disposition: form-data; name=\"file\"; filename=\"%s\";\r\nContent-Type: application/zip\r\n\r\n%s\r\n--";
@Override
public String getValue() {
try {
String fileContent = IOUtils.toString(getInputStream());
String rawPart = String.format(FILE_PART_PATTERN, getName(), fileContent);
return rawPart;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
And this is how I create file part of multipart request in test:
FilePart file = new FilePart("file", getClass().getResourceAsStream("/user_agreements.zip"));
I don't understand where the problem is and why this doesn't work. Method for extracting zips works fine when tested via Postman.