* =htp #20915 fix too small timing issue in DirectivesSpec * applying scalariform reformatting that keeps popping up
This commit is contained in:
parent
b4cd30cf94
commit
54ecf32d2b
7 changed files with 119 additions and 113 deletions
|
|
@ -14,6 +14,9 @@ import akka.stream.javadsl.Source;
|
|||
import akka.util.ByteString;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import scala.concurrent.duration.Duration;
|
||||
import scala.concurrent.duration.FiniteDuration;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.file.Files;
|
||||
|
|
@ -21,114 +24,117 @@ import java.util.Arrays;
|
|||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CompletionStage;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
public class FileUploadDirectivesExamplesTest extends JUnitRouteTest {
|
||||
|
||||
@Test
|
||||
public void testUploadedFile() {
|
||||
//#uploadedFile
|
||||
// function (FileInfo, File) => Route to process the file metadata and file itself
|
||||
BiFunction<FileInfo, File, Route> infoFileRoute =
|
||||
(info, file) -> {
|
||||
// do something with the file and file metadata ...
|
||||
file.delete();
|
||||
return complete(StatusCodes.OK);
|
||||
};
|
||||
@Test
|
||||
public void testUploadedFile() {
|
||||
//#uploadedFile
|
||||
// function (FileInfo, File) => Route to process the file metadata and file itself
|
||||
BiFunction<FileInfo, File, Route> infoFileRoute =
|
||||
(info, file) -> {
|
||||
// do something with the file and file metadata ...
|
||||
file.delete();
|
||||
return complete(StatusCodes.OK);
|
||||
};
|
||||
|
||||
|
||||
final Route route = uploadedFile("csv", infoFileRoute);
|
||||
final Route route = uploadedFile("csv", infoFileRoute);
|
||||
|
||||
Map<String, String> filenameMapping = new HashMap<>();
|
||||
filenameMapping.put("filename", "data.csv");
|
||||
Map<String, String> filenameMapping = new HashMap<>();
|
||||
filenameMapping.put("filename", "data.csv");
|
||||
|
||||
akka.http.javadsl.model.Multipart.FormData multipartForm =
|
||||
Multiparts.createStrictFormDataFromParts(Multiparts.createFormDataBodyPartStrict("csv",
|
||||
HttpEntities.create(ContentTypes.TEXT_PLAIN_UTF8,
|
||||
"1,5,7\n11,13,17"), filenameMapping));
|
||||
akka.http.javadsl.model.Multipart.FormData multipartForm =
|
||||
Multiparts.createStrictFormDataFromParts(Multiparts.createFormDataBodyPartStrict("csv",
|
||||
HttpEntities.create(ContentTypes.TEXT_PLAIN_UTF8,
|
||||
"1,5,7\n11,13,17"), filenameMapping));
|
||||
|
||||
// test:
|
||||
testRoute(route).run(HttpRequest.POST("/").withEntity(
|
||||
multipartForm.toEntity(HttpCharsets.UTF_8, BodyPartRenderer
|
||||
.randomBoundaryWithDefaults())))
|
||||
.assertStatusCode(StatusCodes.OK);
|
||||
//#
|
||||
// test:
|
||||
testRoute(route).run(HttpRequest.POST("/")
|
||||
.withEntity(
|
||||
multipartForm.toEntity(HttpCharsets.UTF_8,
|
||||
BodyPartRenderer
|
||||
.randomBoundaryWithDefaults())))
|
||||
.assertStatusCode(StatusCodes.OK);
|
||||
//#
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFileUpload() {
|
||||
//#fileUpload
|
||||
final Route route = extractRequestContext(ctx -> {
|
||||
// function (FileInfo, Source<ByteString,Object>) => Route to process the file contents
|
||||
BiFunction<FileInfo, Source<ByteString, Object>, Route> processUploadedFile =
|
||||
(metadata, byteSource) -> {
|
||||
CompletionStage<Integer> sumF = byteSource.via(Framing.delimiter(
|
||||
ByteString.fromString("\n"), 1024))
|
||||
.mapConcat(bs -> Arrays.asList(bs.utf8String().split(",")))
|
||||
.map(s -> Integer.parseInt(s))
|
||||
.runFold(0, (acc, n) -> acc + n, ctx.getMaterializer());
|
||||
return onSuccess(() -> sumF, sum -> complete("Sum: " + sum));
|
||||
};
|
||||
return fileUpload("csv", processUploadedFile);
|
||||
});
|
||||
|
||||
Map<String, String> filenameMapping = new HashMap<>();
|
||||
filenameMapping.put("filename", "primes.csv");
|
||||
|
||||
akka.http.javadsl.model.Multipart.FormData multipartForm =
|
||||
Multiparts.createStrictFormDataFromParts(
|
||||
Multiparts.createFormDataBodyPartStrict("csv",
|
||||
HttpEntities.create(ContentTypes.TEXT_PLAIN_UTF8,
|
||||
"2,3,5\n7,11,13,17,23\n29,31,37\n"), filenameMapping));
|
||||
|
||||
// test:
|
||||
testRoute(route).run(HttpRequest.POST("/").withEntity(
|
||||
multipartForm.toEntity(HttpCharsets.UTF_8, BodyPartRenderer.randomBoundaryWithDefaults())))
|
||||
.assertStatusCode(StatusCodes.OK).assertEntityAs(Unmarshaller.entityToString(), "Sum: 178");
|
||||
//#
|
||||
}
|
||||
|
||||
@Ignore("compileOnly")
|
||||
@Test
|
||||
public void testFileProcessing() {
|
||||
//#fileProcessing
|
||||
final Route route = extractRequestContext(ctx -> {
|
||||
// function (FileInfo, Source<ByteString,Object>) => Route to process the file contents
|
||||
BiFunction<FileInfo, Source<ByteString, Object>, Route> processUploadedFile =
|
||||
(metadata, byteSource) -> {
|
||||
CompletionStage<Integer> sumF = byteSource.via(Framing.delimiter(
|
||||
ByteString.fromString("\n"), 1024))
|
||||
.mapConcat(bs -> Arrays.asList(bs.utf8String().split(",")))
|
||||
.map(s -> Integer.parseInt(s))
|
||||
.runFold(0, (acc, n) -> acc + n, ctx.getMaterializer());
|
||||
return onSuccess(() -> sumF, sum -> complete("Sum: " + sum));
|
||||
};
|
||||
return fileUpload("csv", processUploadedFile);
|
||||
});
|
||||
|
||||
Map<String, String> filenameMapping = new HashMap<>();
|
||||
filenameMapping.put("filename", "primes.csv");
|
||||
|
||||
String prefix = "primes";
|
||||
String suffix = ".csv";
|
||||
|
||||
File tempFile = null;
|
||||
try {
|
||||
tempFile = File.createTempFile(prefix, suffix);
|
||||
tempFile.deleteOnExit();
|
||||
Files.write(tempFile.toPath(), Arrays.asList("2,3,5", "7,11,13,17,23", "29,31,37"), Charset.forName("UTF-8"));
|
||||
} catch (Exception e) {
|
||||
// ignore
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFileUpload() {
|
||||
//#fileUpload
|
||||
final Route route = extractRequestContext(ctx -> {
|
||||
// function (FileInfo, Source<ByteString,Object>) => Route to process the file contents
|
||||
BiFunction<FileInfo, Source<ByteString, Object>, Route> processUploadedFile =
|
||||
(metadata, byteSource) -> {
|
||||
CompletionStage<Integer> sumF = byteSource.via(Framing.delimiter(
|
||||
ByteString.fromString("\n"), 1024))
|
||||
.mapConcat(bs -> Arrays.asList(bs.utf8String().split(",")))
|
||||
.map(s -> Integer.parseInt(s))
|
||||
.runFold(0, (acc, n) -> acc + n, ctx.getMaterializer());
|
||||
return onSuccess(() -> sumF, sum -> complete("Sum: " + sum));
|
||||
};
|
||||
return fileUpload("csv", processUploadedFile);
|
||||
});
|
||||
|
||||
Map<String, String> filenameMapping = new HashMap<>();
|
||||
filenameMapping.put("filename", "primes.csv");
|
||||
akka.http.javadsl.model.Multipart.FormData multipartForm =
|
||||
Multiparts.createFormDataFromPath("csv", ContentTypes.TEXT_PLAIN_UTF8, tempFile.toPath());
|
||||
|
||||
akka.http.javadsl.model.Multipart.FormData multipartForm =
|
||||
Multiparts.createStrictFormDataFromParts(
|
||||
Multiparts.createFormDataBodyPartStrict("csv",
|
||||
HttpEntities.create(ContentTypes.TEXT_PLAIN_UTF8,
|
||||
"2,3,5\n7,11,13,17,23\n29,31,37\n"), filenameMapping));
|
||||
|
||||
// test:
|
||||
testRoute(route).run(HttpRequest.POST("/").withEntity(
|
||||
multipartForm.toEntity(HttpCharsets.UTF_8, BodyPartRenderer.randomBoundaryWithDefaults())))
|
||||
.assertStatusCode(StatusCodes.OK).assertEntityAs(Unmarshaller.entityToString(), "Sum: 178");
|
||||
//#
|
||||
}
|
||||
|
||||
@Ignore("compileOnly")
|
||||
@Test
|
||||
public void testFileProcessing() {
|
||||
//#fileProcessing
|
||||
final Route route = extractRequestContext(ctx -> {
|
||||
// function (FileInfo, Source<ByteString,Object>) => Route to process the file contents
|
||||
BiFunction<FileInfo, Source<ByteString, Object>, Route> processUploadedFile =
|
||||
(metadata, byteSource) -> {
|
||||
CompletionStage<Integer> sumF = byteSource.via(Framing.delimiter(
|
||||
ByteString.fromString("\n"), 1024))
|
||||
.mapConcat(bs -> Arrays.asList(bs.utf8String().split(",")))
|
||||
.map(s -> Integer.parseInt(s))
|
||||
.runFold(0, (acc, n) -> acc + n, ctx.getMaterializer());
|
||||
return onSuccess(() -> sumF, sum -> complete("Sum: " + sum));
|
||||
};
|
||||
return fileUpload("csv", processUploadedFile);
|
||||
});
|
||||
|
||||
Map<String, String> filenameMapping = new HashMap<>();
|
||||
filenameMapping.put("filename", "primes.csv");
|
||||
|
||||
String prefix = "primes";
|
||||
String suffix = ".csv";
|
||||
|
||||
File tempFile = null;
|
||||
try {
|
||||
tempFile = File.createTempFile(prefix, suffix);
|
||||
tempFile.deleteOnExit();
|
||||
Files.write(tempFile.toPath(), Arrays.asList("2,3,5", "7,11,13,17,23", "29,31,37"), Charset.forName("UTF-8"));
|
||||
} catch (Exception e) {
|
||||
// ignore
|
||||
}
|
||||
|
||||
|
||||
akka.http.javadsl.model.Multipart.FormData multipartForm =
|
||||
Multiparts.createFormDataFromPath("csv", ContentTypes.TEXT_PLAIN_UTF8, tempFile.toPath());
|
||||
|
||||
// test:
|
||||
testRoute(route).run(HttpRequest.POST("/").withEntity(
|
||||
multipartForm.toEntity(HttpCharsets.UTF_8, BodyPartRenderer.randomBoundaryWithDefaults())))
|
||||
.assertStatusCode(StatusCodes.OK).assertEntityAs(Unmarshaller.entityToString(), "Sum: 178");
|
||||
//#
|
||||
}
|
||||
// test:
|
||||
testRoute(route).run(HttpRequest.POST("/").withEntity(
|
||||
multipartForm.toEntity(HttpCharsets.UTF_8, BodyPartRenderer.randomBoundaryWithDefaults())))
|
||||
.assertStatusCode(StatusCodes.OK).assertEntityAs(Unmarshaller.entityToString(), "Sum: 178");
|
||||
//#
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -53,7 +53,7 @@ class ActorWithArgs(arg: String) extends Actor {
|
|||
//#actor-with-value-class-argument
|
||||
class Argument(val value: String) extends AnyVal
|
||||
class ValueClassActor(arg: Argument) extends Actor {
|
||||
def receive = {case _ => () }
|
||||
def receive = { case _ => () }
|
||||
}
|
||||
|
||||
object ValueClassActor {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue