Exemplo n.º 1
0
/** @author <a href="mailto:[email protected]">Matej Lazar</a> */
public class TestFileUpload {

  private static final String HOST = "localhost";
  private static final int PORT = TermdServer.getNextPort();

  private static Logger log = LoggerFactory.getLogger(TestFileUpload.class);

  @BeforeClass
  public static void setUP() throws Exception {
    TermdServer.startServer(HOST, PORT);
  }

  @AfterClass
  public static void tearDown() {
    TermdServer.stopServer();
  }

  @Test
  public void uploadFile() throws Exception {
    Path pwd = Paths.get("").toAbsolutePath();
    Path fileUploadPath = Paths.get(pwd.toString(), "/test-upload.txt");
    URL url = new URL("http://" + HOST + ":" + PORT + "/upload" + fileUploadPath);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("PUT");

    connection.setDoOutput(true);
    connection.setDoInput(true);

    String fileContent = "The quick brown fox jumps over the lazy dog.";
    byte[] fileContentBytes = fileContent.getBytes();
    connection.setRequestProperty("Content-Length", "" + Integer.toString(fileContentBytes.length));

    try (OutputStream outputStream = connection.getOutputStream()) {
      outputStream.write(fileContentBytes);
    }

    Assert.assertEquals(connection.getResponseMessage(), 200, connection.getResponseCode());

    assertFileWasUploaded(fileUploadPath, fileContent);

    fileUploadPath.toFile().delete();
  }

  private void assertFileWasUploaded(Path fileUploadPath, String expectedFileContent)
      throws FileNotFoundException {
    String actualFileContent = new Scanner(fileUploadPath.toFile()).useDelimiter("\\Z").next();
    log.info("Content written to file: {}", actualFileContent);
    Assert.assertEquals(expectedFileContent, actualFileContent);
  }
}
Exemplo n.º 2
0
 @AfterClass
 public static void tearDown() {
   TermdServer.stopServer();
 }
Exemplo n.º 3
0
 @BeforeClass
 public static void setUP() throws Exception {
   TermdServer.startServer(HOST, PORT);
 }