client: Fix mediaType MIME code (FINERACT-1218)
This commit is contained in:
committed by
Petri Tuomola
parent
20daf59a3e
commit
5f42112278
@@ -33,24 +33,26 @@ public final class Parts {
|
|||||||
private Parts() {}
|
private Parts() {}
|
||||||
|
|
||||||
public static Part fromFile(File file) {
|
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);
|
return Part.createFormData("file", file.getName(), rb);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Part fromFile(String fileName, byte[] bytes) {
|
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);
|
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...
|
// 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
|
static MediaType mediaType(String fileName) {
|
||||||
// work :(
|
if (fileName == null) {
|
||||||
private static MediaType getMediaType(String fileName) {
|
return null;
|
||||||
|
}
|
||||||
int dotPos = fileName.lastIndexOf('.');
|
int dotPos = fileName.lastIndexOf('.');
|
||||||
if (dotPos == -1) {
|
if (dotPos == -1) {
|
||||||
return null;
|
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
|
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
|
||||||
switch (ext) {
|
switch (ext) {
|
||||||
case "jpg":
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
+6
-8
@@ -49,7 +49,7 @@ public class DocumentTest extends IntegrationTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Order(2)
|
@Order(2)
|
||||||
void createDocument() throws IOException {
|
void createDocument() {
|
||||||
String name = "Test";
|
String name = "Test";
|
||||||
Part part = Parts.fromFile(testFile);
|
Part part = Parts.fromFile(testFile);
|
||||||
String description = null;
|
String description = null;
|
||||||
@@ -63,7 +63,7 @@ public class DocumentTest extends IntegrationTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Order(3)
|
@Order(3)
|
||||||
void getDocument() throws IOException {
|
void getDocument() {
|
||||||
GetEntityTypeEntityIdDocumentsResponse doc = ok(fineract().documents.getDocument("clients", clientId, documentId));
|
GetEntityTypeEntityIdDocumentsResponse doc = ok(fineract().documents.getDocument("clients", clientId, documentId));
|
||||||
assertThat(doc.getName()).isEqualTo("Test");
|
assertThat(doc.getName()).isEqualTo("Test");
|
||||||
assertThat(doc.getFileName()).isEqualTo(testFile.getName());
|
assertThat(doc.getFileName()).isEqualTo(testFile.getName());
|
||||||
@@ -72,9 +72,8 @@ public class DocumentTest extends IntegrationTest {
|
|||||||
assertThat(doc.getParentEntityType()).isEqualTo("clients");
|
assertThat(doc.getParentEntityType()).isEqualTo("clients");
|
||||||
assertThat(doc.getParentEntityId()).isEqualTo(clientId);
|
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
|
// 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);
|
assertThat(doc.getSize()).isEqualTo(testFile.length() + 411);
|
||||||
// TODO huh?! MIME is always text/plain instead of image/jpeg... :(
|
assertThat(doc.getType()).isEqualTo("image/jpeg");
|
||||||
assertThat(doc.getType()).isEqualTo("text/plain");
|
|
||||||
// TODO doc.getStorageType() shouldn't be exposed by the API?!
|
// TODO doc.getStorageType() shouldn't be exposed by the API?!
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -82,15 +81,14 @@ public class DocumentTest extends IntegrationTest {
|
|||||||
@Order(4)
|
@Order(4)
|
||||||
void downloadFile() throws IOException {
|
void downloadFile() throws IOException {
|
||||||
ResponseBody r = ok(fineract().documents.downloadFile("clients", clientId, documentId));
|
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"
|
assertThat(r.contentType()).isEqualTo(MediaType.get("image/jpeg"));
|
||||||
// (as above)
|
|
||||||
assertThat(r.bytes().length).isEqualTo(testFile.length());
|
assertThat(r.bytes().length).isEqualTo(testFile.length());
|
||||||
// NOK: assertThat(r.contentLength()).isEqualTo(testFile.length());
|
// NOK: assertThat(r.contentLength()).isEqualTo(testFile.length());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Order(10)
|
@Order(10)
|
||||||
void updateDocument() throws IOException {
|
void updateDocument() {
|
||||||
String newName = "Test changed name";
|
String newName = "Test changed name";
|
||||||
String newDescription = getClass().getName();
|
String newDescription = getClass().getName();
|
||||||
ok(fineract().documents.updateDocument("clients", clientId, documentId, null, newName, newDescription));
|
ok(fineract().documents.updateDocument("clients", clientId, documentId, null, newName, newDescription));
|
||||||
|
|||||||
Reference in New Issue
Block a user