-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
+ 2MB and 2GB tests
- Loading branch information
Showing
9 changed files
with
433 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
embedded/file-server/src/test/java/examples/ByteCountingOutputStream.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// | ||
// ======================================================================== | ||
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others. | ||
// | ||
// This program and the accompanying materials are made available under the | ||
// terms of the Eclipse Public License v. 2.0 which is available at | ||
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 | ||
// which is available at https://www.apache.org/licenses/LICENSE-2.0. | ||
// | ||
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 | ||
// ======================================================================== | ||
// | ||
|
||
package examples; | ||
|
||
import java.io.OutputStream; | ||
|
||
public class ByteCountingOutputStream extends OutputStream | ||
{ | ||
private long count = 0; | ||
|
||
public long getCount() | ||
{ | ||
return count; | ||
} | ||
|
||
@Override | ||
public void write(int b) | ||
{ | ||
count++; | ||
} | ||
|
||
@Override | ||
public void write(byte[] b) | ||
{ | ||
count += b.length; | ||
} | ||
|
||
@Override | ||
public void write(byte[] b, int off, int len) | ||
{ | ||
count += len; | ||
} | ||
} |
109 changes: 109 additions & 0 deletions
109
embedded/file-server/src/test/java/examples/ResourceHandlerFromFileSystemTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
// | ||
// ======================================================================== | ||
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others. | ||
// | ||
// This program and the accompanying materials are made available under the | ||
// terms of the Eclipse Public License v. 2.0 which is available at | ||
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 | ||
// which is available at https://www.apache.org/licenses/LICENSE-2.0. | ||
// | ||
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 | ||
// ======================================================================== | ||
// | ||
|
||
package examples; | ||
|
||
import java.net.HttpURLConnection; | ||
import java.nio.file.Path; | ||
|
||
import org.eclipse.jetty.server.Server; | ||
import org.eclipse.jetty.toolchain.test.FS; | ||
import org.eclipse.jetty.toolchain.test.MavenPaths; | ||
import org.eclipse.jetty.util.component.LifeCycle; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static examples.StaticFileGen.GB; | ||
import static examples.StaticFileGen.MB; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertNull; | ||
|
||
public class ResourceHandlerFromFileSystemTest | ||
{ | ||
private final long exampleSize = 2 * MB; | ||
private final long largeSize = 2 * GB; | ||
private Server server; | ||
private String exampleSha; | ||
private String largeSha; | ||
|
||
@BeforeEach | ||
public void startServer() throws Exception | ||
{ | ||
Path resourcesRoot = MavenPaths.targetTestDir(ResourceHandlerFromFileSystemTest.class.getSimpleName()); | ||
FS.ensureDirExists(resourcesRoot); | ||
|
||
exampleSha = StaticFileGen.generate(resourcesRoot.resolve("example.png"), exampleSize); | ||
largeSha = StaticFileGen.generate(resourcesRoot.resolve("large.mkv"), largeSize); | ||
|
||
server = ResourceHandlerFromFileSystem.newServer(0, resourcesRoot); | ||
server.start(); | ||
} | ||
|
||
@AfterEach | ||
public void stopServer() | ||
{ | ||
LifeCycle.stop(server); | ||
} | ||
|
||
/** | ||
* Get small file | ||
*/ | ||
@Test | ||
public void testGetSmall() throws Exception | ||
{ | ||
HttpURLConnection http = (HttpURLConnection)server.getURI().resolve("/example.png").toURL().openConnection(); | ||
http.connect(); | ||
dumpRequestResponse(http); | ||
assertEquals(HttpURLConnection.HTTP_OK, http.getResponseCode()); | ||
String contentLengthResponse = http.getHeaderField("Content-Length"); | ||
assertNotNull(contentLengthResponse); | ||
long contentLengthLong = Long.parseLong(contentLengthResponse); | ||
assertEquals(2 * MB, contentLengthLong); | ||
assertEquals("image/png", http.getHeaderField("Content-Type")); | ||
|
||
StaticFileGen.verify(http.getInputStream(), exampleSize, exampleSha); | ||
} | ||
|
||
/** | ||
* Get large file | ||
*/ | ||
@Test | ||
public void testGetLarge() throws Exception | ||
{ | ||
HttpURLConnection http = (HttpURLConnection)server.getURI().resolve("/large.mkv").toURL().openConnection(); | ||
http.connect(); | ||
dumpRequestResponse(http); | ||
assertEquals(HttpURLConnection.HTTP_OK, http.getResponseCode()); | ||
String contentLengthResponse = http.getHeaderField("Content-Length"); | ||
assertNotNull(contentLengthResponse); | ||
long contentLengthLong = Long.parseLong(contentLengthResponse); | ||
assertEquals(2 * GB, contentLengthLong); | ||
assertNull(http.getHeaderField("Content-Type"), "Not a recognized mime-type by Jetty"); | ||
|
||
StaticFileGen.verify(http.getInputStream(), largeSize, largeSha); | ||
} | ||
|
||
private static void dumpRequestResponse(HttpURLConnection http) | ||
{ | ||
System.out.println(); | ||
System.out.println("----"); | ||
System.out.printf("%s %s HTTP/1.1%n", http.getRequestMethod(), http.getURL()); | ||
System.out.println("----"); | ||
System.out.printf("%s%n", http.getHeaderField(null)); | ||
http.getHeaderFields().entrySet().stream() | ||
.filter(entry -> entry.getKey() != null) | ||
.forEach((entry) -> System.out.printf("%s: %s%n", entry.getKey(), http.getHeaderField(entry.getKey()))); | ||
} | ||
} |
Oops, something went wrong.