Skip to content

Commit

Permalink
Update tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
gk-brown committed Oct 3, 2024
1 parent 1990d3a commit e56e1d4
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 14 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

subprojects {
group = 'org.httprpc'
version = '4.4.2'
version = '4.4.3'

apply plugin: 'java-library'

Expand Down
26 changes: 23 additions & 3 deletions kilo-test/src/main/java/org/httprpc/kilo/test/TestService.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

package org.httprpc.kilo.test;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.MultipartConfig;
import jakarta.servlet.annotation.WebServlet;
import org.httprpc.kilo.Creates;
Expand Down Expand Up @@ -302,9 +303,28 @@ public Coordinates testPostCoordinates(Coordinates coordinates) {

@RequestMethod("POST")
@ResourcePath("form-data")
public Map<String, Object> testPostFormData(Void body) {
// TODO Read parts
return null;
public Map<String, Object> testPostFormData(Void body) throws IOException, ServletException {
String string = null;
List<String> strings = null;
Integer number = null;
Set<Integer> numbers = null;

var fileSize = 0;
var totalFileSize = 0;

for (var part : getRequest().getParts()) {
// TODO
var name = part.getName();
}

return mapOf(
entry("string", string),
entry("strings", strings),
entry("number", number),
entry("numbers", numbers),
entry("fileSize", fileSize),
entry("totalFileSize", totalFileSize)
);
}

@RequestMethod("POST")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@
@ServicePath("test")
public interface TestServiceProxy {
interface FormData {
@Required
String getString();
List<Integer> getNumbers();
List<String> getStrings();
Integer getNumber();
Date getDate();
Path getFile();
List<Path> getFiles();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.time.Period;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import static org.httprpc.kilo.util.Collections.*;
Expand Down Expand Up @@ -365,44 +366,61 @@ public void testCoordinatesPost() throws IOException {

@Test
public void testFormDataPost() throws IOException {
var now = new Date();

var textURL = getClass().getResource("test.txt");
var imageURL = getClass().getResource("test.jpg");

var body = mapOf(
entry("string", "abc"),
entry("numbers", listOf(1, 2, 3)),
entry("date", new Date()),
entry("string", "héllo&gøod+bye?"),
entry("strings", listOf("a", "b", "c")),
entry("number", 123),
entry("date", now),
entry("file", textURL.getPath()),
entry("files", listOf(textURL.getPath(), imageURL.getPath()))
);

var webServiceProxy = new WebServiceProxy("POST", baseURI.resolve("test/form-data"));

webServiceProxy.setRequestHandler(new WebServiceProxy.FormDataRequestHandler());
webServiceProxy.setBody(body);

var result = webServiceProxy.invoke();
var result = (Map<?, ?>)webServiceProxy.invoke();

// TODO
assertEquals(result.get("string"), "héllo&gøod+bye?");
assertEquals(result.get("strings"), listOf("a", "b", "c"));
assertEquals(result.get("number"), 123);
assertEquals(result.get("date"), now.getTime());
assertEquals(result.get("fileSize"), 0);
assertEquals(result.get("totalFileSize"), 0);
}

@Test
public void testFormDataPostProxy() throws IOException {
var testServiceProxy = WebServiceProxy.of(TestServiceProxy.class, baseURI);

var now = new Date();

var textURL = getClass().getResource("test.txt");
var imageURL = getClass().getResource("test.jpg");

var formData = BeanAdapter.coerce(mapOf(
entry("string", "abc"),
entry("numbers", listOf(1, 2, 3)),
entry("date", new Date()),
entry("string", "héllo&gøod+bye?"),
entry("strings", listOf("a", "b", "c")),
entry("number", 123),
entry("date", now),
entry("file", textURL.getPath()),
entry("files", listOf(textURL.getPath(), imageURL.getPath()))
), TestServiceProxy.FormData.class);

var result = testServiceProxy.testFormDataPost(formData);

// TODO
assertEquals(result.get("string"), "héllo&gøod+bye?");
assertEquals(result.get("strings"), listOf("a", "b", "c"));
assertEquals(result.get("number"), 123);
assertEquals(result.get("date"), now.getTime());
assertEquals(result.get("fileSize"), 0);
assertEquals(result.get("totalFileSize"), 0);
}

@Test
Expand Down

0 comments on commit e56e1d4

Please sign in to comment.