client: Fix mediaType MIME code (FINERACT-1218)

This commit is contained in:
Michael Vorburger
2020-10-24 21:01:32 +03:00
committed by Petri Tuomola
parent 20daf59a3e
commit 5f42112278
3 changed files with 60 additions and 14 deletions
@@ -33,24 +33,26 @@ public final class Parts {
private Parts() {}
public static Part fromFile(File file) {
RequestBody rb = RequestBody.create(file, getMediaType(file.getName()));
RequestBody rb = RequestBody.create(file, mediaType(file.getName()));
return Part.createFormData("file", file.getName(), rb);
}
public static Part fromFile(String fileName, byte[] bytes) {
RequestBody rb = RequestBody.create(bytes, getMediaType(fileName));
RequestBody rb = RequestBody.create(bytes, mediaType(fileName));
return Part.createFormData("file", fileName, rb);
}
// package local, for unit testing
// TODO this logic should be on the Server, not have to be done by the client...
// There actually does seem to be some code related to MIME type guessing in Fineract, but test shows it doesn't
// work :(
private static MediaType getMediaType(String fileName) {
static MediaType mediaType(String fileName) {
if (fileName == null) {
return null;
}
int dotPos = fileName.lastIndexOf('.');
if (dotPos == -1) {
return null;
}
String ext = fileName.substring(dotPos);
String ext = fileName.substring(dotPos + 1);
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
switch (ext) {
case "jpg":
@@ -0,0 +1,46 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.client.util;
import com.google.common.truth.Truth;
import okhttp3.MediaType;
import org.junit.jupiter.api.Test;
public class PartsTest {
@Test
void validMediaType() {
Truth.assertThat(Parts.mediaType("test.jpg")).isEqualTo(MediaType.get("image/jpeg"));
}
@Test
void dotMediaType() {
Truth.assertThat(Parts.mediaType("test.")).isNull();
}
@Test
void emptyMediaType() {
Truth.assertThat(Parts.mediaType("")).isNull();
}
@Test
void nullMediaType() {
Truth.assertThat(Parts.mediaType(null)).isNull();
}
}
@@ -49,7 +49,7 @@ public class DocumentTest extends IntegrationTest {
@Test
@Order(2)
void createDocument() throws IOException {
void createDocument() {
String name = "Test";
Part part = Parts.fromFile(testFile);
String description = null;
@@ -63,7 +63,7 @@ public class DocumentTest extends IntegrationTest {
@Test
@Order(3)
void getDocument() throws IOException {
void getDocument() {
GetEntityTypeEntityIdDocumentsResponse doc = ok(fineract().documents.getDocument("clients", clientId, documentId));
assertThat(doc.getName()).isEqualTo("Test");
assertThat(doc.getFileName()).isEqualTo(testFile.getName());
@@ -72,9 +72,8 @@ public class DocumentTest extends IntegrationTest {
assertThat(doc.getParentEntityType()).isEqualTo("clients");
assertThat(doc.getParentEntityId()).isEqualTo(clientId);
// TODO huh?! It's more than uploaded file; seems like a bug - it's including create body, not just file size
assertThat(doc.getSize()).isEqualTo(testFile.length() + 385);
// TODO huh?! MIME is always text/plain instead of image/jpeg... :(
assertThat(doc.getType()).isEqualTo("text/plain");
assertThat(doc.getSize()).isEqualTo(testFile.length() + 411);
assertThat(doc.getType()).isEqualTo("image/jpeg");
// TODO doc.getStorageType() shouldn't be exposed by the API?!
}
@@ -82,15 +81,14 @@ public class DocumentTest extends IntegrationTest {
@Order(4)
void downloadFile() throws IOException {
ResponseBody r = ok(fineract().documents.downloadFile("clients", clientId, documentId));
assertThat(r.contentType()).isEqualTo(MediaType.get("text/plain")); // TODO wrong, bug; needs to be "image/jpeg"
// (as above)
assertThat(r.contentType()).isEqualTo(MediaType.get("image/jpeg"));
assertThat(r.bytes().length).isEqualTo(testFile.length());
// NOK: assertThat(r.contentLength()).isEqualTo(testFile.length());
}
@Test
@Order(10)
void updateDocument() throws IOException {
void updateDocument() {
String newName = "Test changed name";
String newDescription = getClass().getName();
ok(fineract().documents.updateDocument("clients", clientId, documentId, null, newName, newDescription));