|
3 | 3 | import org.junit.Test; |
4 | 4 |
|
5 | 5 | import java.io.*; |
| 6 | +import java.nio.file.Path; |
6 | 7 |
|
| 8 | +import static httpserver.fileutils.FileHelpers.tempDir; |
| 9 | +import static httpserver.fileutils.FileHelpers.tempFileOptions; |
| 10 | +import static java.nio.file.Files.write; |
7 | 11 | import static org.junit.Assert.*; |
8 | 12 |
|
9 | 13 | public class SocketHandlerTest { |
10 | | - private final String tempdir; |
11 | | - |
12 | | - public SocketHandlerTest() { |
13 | | - this.tempdir = System.getProperty("java.io.tmpdir"); |
14 | | - } |
15 | 14 |
|
16 | 15 | @Test |
17 | 16 | public void writesRequestedFileContentsToOutputStreamForGET() throws Exception { |
18 | | - File tempFile = File.createTempFile("temp-", "-testfile"); |
19 | | - tempFile.deleteOnExit(); |
20 | | - String fullPath = tempFile.toString(); |
21 | | - String relativePath = fullPath.substring(tempdir.length()); |
22 | | - FileOutputStream fileOutputStream = new FileOutputStream(fullPath); |
23 | | - fileOutputStream.write("Test file contents for GET request.".getBytes()); |
24 | | - |
25 | | - byte[] request = ("GET " + relativePath + " HTTP/1.1\r\nHost: 127.0.0.1:5000\r\n\r\n").getBytes(); |
| 17 | + Path root = tempDir(); |
| 18 | + Path file = tempFileOptions(root, "aaa"); |
| 19 | + write(file, "Test file contents for GET request.".getBytes()); |
| 20 | + |
| 21 | + Path relativePath = root.relativize(file); |
| 22 | + byte[] request = ("GET " + relativePath.toString() + " HTTP/1.1\r\nHost: 127.0.0.1:5000\r\n\r\n").getBytes(); |
| 23 | + |
26 | 24 | InputStream inputStream = new ByteArrayInputStream(request); |
27 | 25 | ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
28 | | - SocketHandler socketHandler = new SocketHandler(tempdir); |
| 26 | + SocketHandler socketHandler = new SocketHandler(root); |
29 | 27 |
|
30 | 28 | socketHandler.process(inputStream, outputStream); |
31 | 29 |
|
|
0 commit comments