Java Code Examples for com.meterware.httpunit.PostMethodWebRequest#setHeaderField()

The following examples show how to use com.meterware.httpunit.PostMethodWebRequest#setHeaderField() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TransferTest.java    From orion.server with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void testImportAndUnzip() throws CoreException, IOException, SAXException, URISyntaxException {
	//create a directory to upload to
	String directoryPath = "sample/directory/path" + System.currentTimeMillis();
	createDirectory(directoryPath);

	//start the import
	URL entry = ServerTestsActivator.getContext().getBundle().getEntry("testData/importTest/client.zip");
	File source = new File(FileLocator.toFileURL(entry).getPath());
	long length = source.length();
	String importPath = getImportRequestPath(directoryPath);
	PostMethodWebRequest request = new PostMethodWebRequest(importPath);
	request.setHeaderField("X-Xfer-Content-Length", Long.toString(length));
	setAuthentication(request);
	WebResponse postResponse = webConversation.getResponse(request);
	assertEquals(HttpURLConnection.HTTP_OK, postResponse.getResponseCode());
	String location = postResponse.getHeaderField("Location");
	assertNotNull(location);
	URI importURI = URIUtil.fromString(importPath);
	location = importURI.resolve(location).toString();
	doImport(source, length, location);

	//assert the file has been unzipped in the workspace
	assertTrue(checkFileExists(directoryPath + "/org.eclipse.e4.webide/static/js/navigate-tree/navigate-tree.js"));
}
 
Example 2
Source File: TransferTest.java    From orion.server with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Tests attempting to import and unzip a file that is not a zip file.
 */
@Test
public void testImportUnzipNonZipFile() throws CoreException, IOException, SAXException {
	//create a directory to upload to
	String directoryPath = "sample/testImportWithPost/path" + System.currentTimeMillis();
	createDirectory(directoryPath);

	//start the import
	URL entry = ServerTestsActivator.getContext().getBundle().getEntry("testData/importTest/junk.zip");
	File source = new File(FileLocator.toFileURL(entry).getPath());
	long length = source.length();
	InputStream in = new BufferedInputStream(new FileInputStream(source));
	PostMethodWebRequest request = new PostMethodWebRequest(getImportRequestPath(directoryPath), in, "application/zip");
	request.setHeaderField("Content-Length", "" + length);
	request.setHeaderField("Content-Type", "application/zip");
	setAuthentication(request);
	WebResponse postResponse = webConversation.getResponse(request);
	assertEquals(HttpURLConnection.HTTP_BAD_REQUEST, postResponse.getResponseCode());
}
 
Example 3
Source File: TransferTest.java    From orion.server with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void testImportWithPost() throws CoreException, IOException, SAXException {
	//create a directory to upload to
	String directoryPath = "sample/testImportWithPost/path" + System.currentTimeMillis();
	createDirectory(directoryPath);

	//start the import
	URL entry = ServerTestsActivator.getContext().getBundle().getEntry("testData/importTest/client.zip");
	File source = new File(FileLocator.toFileURL(entry).getPath());
	long length = source.length();
	InputStream in = new BufferedInputStream(new FileInputStream(source));
	PostMethodWebRequest request = new PostMethodWebRequest(getImportRequestPath(directoryPath), in, "application/zip");
	request.setHeaderField("Content-Length", "" + length);
	request.setHeaderField("Content-Type", "application/zip");
	setAuthentication(request);
	WebResponse postResponse = webConversation.getResponse(request);
	assertEquals(HttpURLConnection.HTTP_CREATED, postResponse.getResponseCode());
	String location = postResponse.getHeaderField("Location");
	assertNotNull(location);
	String type = postResponse.getHeaderField("Content-Type");
	assertNotNull(type);
	assertTrue(type.contains("text/html"));

	//assert the file has been unzipped in the workspace
	assertTrue(checkFileExists(directoryPath + "/org.eclipse.e4.webide/static/js/navigate-tree/navigate-tree.js"));
}
 
Example 4
Source File: SearchTest.java    From orion.server with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Setup a project with some files that we can use for search tests.
 * @throws CoreException 
 * @throws IOException 
 * @throws SAXException 
 */
private void createTestData() throws Exception {
	//create a directory to upload to
	String directoryPath = "sample/directory/path" + System.currentTimeMillis();
	createDirectory(directoryPath);

	//start the import
	URL entry = ServerTestsActivator.getContext().getBundle().getEntry("testData/searchTest/files.zip");
	File source = new File(FileLocator.toFileURL(entry).getPath());
	long length = source.length();
	IPath path = new Path("/xfer/import").append(getTestBaseResourceURILocation()).append(directoryPath);
	PostMethodWebRequest request = new PostMethodWebRequest(URIUtil.fromString(SERVER_LOCATION + path.toString()).toString());
	request.setHeaderField("X-Xfer-Content-Length", Long.toString(length));
	setAuthentication(request);
	WebResponse postResponse = webConversation.getResponse(request);
	assertEquals(HttpURLConnection.HTTP_OK, postResponse.getResponseCode());
	String location = postResponse.getHeaderField("Location");
	assertNotNull(location);

	doImport(source, length, location);
}
 
Example 5
Source File: TransferTest.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void testImportFile() throws CoreException, IOException, SAXException, URISyntaxException {
	//create a directory to upload to
	String directoryPath = "sample/directory/path" + System.currentTimeMillis();
	createDirectory(directoryPath);

	//start the import
	URL entry = ServerTestsActivator.getContext().getBundle().getEntry("testData/importTest/client.zip");
	File source = new File(FileLocator.toFileURL(entry).getPath());
	long length = source.length();
	String importPath = getImportRequestPath(directoryPath);
	PostMethodWebRequest request = new PostMethodWebRequest(importPath);
	request.setHeaderField("X-Xfer-Content-Length", Long.toString(length));
	request.setHeaderField("X-Xfer-Options", "raw");

	request.setHeaderField("Slug", "client.zip");
	setAuthentication(request);
	WebResponse postResponse = webConversation.getResponse(request);
	assertEquals(HttpURLConnection.HTTP_OK, postResponse.getResponseCode());
	String location = postResponse.getHeaderField("Location");
	assertNotNull(location);
	URI importURI = URIUtil.fromString(importPath);
	location = importURI.resolve(location).toString();

	doImport(source, length, location);

	//assert the file is present in the workspace
	assertTrue(checkFileExists(directoryPath + "/client.zip"));
	//assert that imported file has same content as original client.zip
	assertTrue(checkContentEquals(source, directoryPath + "/client.zip"));
}
 
Example 6
Source File: TransferTest.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void testImportDBCSFilename() throws CoreException, IOException, SAXException, URISyntaxException {
	//create a directory to upload to
	String directoryPath = "sample/directory/path" + System.currentTimeMillis();
	createDirectory(directoryPath);

	//start the import
	// file with DBCS character in the filename.
	String filename = "\u3042.txt";
	File source = null;
	try {
		source = createTempFile(filename, "No content");
		long length = source.length();
		String importPath = getImportRequestPath(directoryPath);
		PostMethodWebRequest request = new PostMethodWebRequest(importPath);
		request.setHeaderField("X-Xfer-Content-Length", Long.toString(length));
		request.setHeaderField("X-Xfer-Options", "raw");

		request.setHeaderField("Slug", Slug.encode(filename));
		setAuthentication(request);
		WebResponse postResponse = webConversation.getResponse(request);
		assertEquals(HttpURLConnection.HTTP_OK, postResponse.getResponseCode());
		String location = postResponse.getHeaderField("Location");
		assertNotNull(location);
		URI importURI = URIUtil.fromString(importPath);
		location = importURI.resolve(location).toString();

		doImport(source, length, location, "text/plain");

		//assert the file is present in the workspace
		assertTrue(checkFileExists(directoryPath + File.separator + filename));
		//assert that imported file has same content as original client.zip
		assertTrue(checkContentEquals(source, directoryPath + File.separator + filename));
	} finally {
		// Delete the temp file
		FileUtils.deleteQuietly(source);
	}
}
 
Example 7
Source File: TransferTest.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void testImportFileMultiPart() throws CoreException, IOException, SAXException, URISyntaxException {
	//create a directory to upload to
	String directoryPath = "sample/directory/path" + System.currentTimeMillis();
	createDirectory(directoryPath);

	URL entry = ServerTestsActivator.getContext().getBundle().getEntry("testData/importTest/client.zip");
	File source = new File(FileLocator.toFileURL(entry).getPath());

	// current server implementation cannot handle binary data, thus base64-encode client.zip before import
	File expected = EFS.getStore(makeLocalPathAbsolute(directoryPath + "/expected.txt")).toLocalFile(EFS.NONE, null);
	byte[] expectedContent = Base64.encode(FileUtils.readFileToByteArray(source));
	FileUtils.writeByteArrayToFile(expected, expectedContent);

	//start the import
	long length = expectedContent.length;
	String importPath = getImportRequestPath(directoryPath);
	PostMethodWebRequest request = new PostMethodWebRequest(importPath);
	request.setHeaderField("X-Xfer-Content-Length", Long.toString(length));
	request.setHeaderField("X-Xfer-Options", "raw");
	request.setHeaderField("Slug", "actual.txt");
	setAuthentication(request);
	WebResponse postResponse = webConversation.getResponse(request);
	assertEquals(HttpURLConnection.HTTP_OK, postResponse.getResponseCode());
	String location = postResponse.getHeaderField("Location");
	assertNotNull(location);
	URI importURI = URIUtil.fromString(importPath);
	location = importURI.resolve(location).toString();

	// perform the upload
	doImport(expected, length, location, "multipart/mixed;boundary=foobar");

	//assert the file is present in the workspace
	assertTrue(checkFileExists(directoryPath + "/actual.txt"));
	//assert that actual.txt has same content as expected.txt
	assertTrue(checkContentEquals(expected, directoryPath + "/actual.txt"));
}
 
Example 8
Source File: TransferTest.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void testImportWithPostZeroByteFile() throws CoreException, IOException, SAXException, URISyntaxException {
	//create a directory to upload to
	String directoryPath = "sample/directory/path" + System.currentTimeMillis();
	createDirectory(directoryPath);

	//start the import
	URL entry = ServerTestsActivator.getContext().getBundle().getEntry("testData/importTest/zeroByteFile.txt");
	File source = new File(FileLocator.toFileURL(entry).getPath());
	long length = source.length();
	assertEquals(length, 0);
	String importPath = getImportRequestPath(directoryPath);
	PostMethodWebRequest request = new PostMethodWebRequest(importPath);
	request.setHeaderField("X-Xfer-Content-Length", Long.toString(length));
	request.setHeaderField("X-Xfer-Options", "raw");

	request.setHeaderField("Slug", "zeroByteFile.txt");
	setAuthentication(request);
	WebResponse postResponse = webConversation.getResponse(request);
	assertEquals(HttpURLConnection.HTTP_OK, postResponse.getResponseCode());
	String location = postResponse.getHeaderField("Location");
	assertNotNull(location);
	URI importURI = URIUtil.fromString(importPath);
	location = importURI.resolve(location).toString();

	doImport(source, length, location, "text/plain");

	//assert the file is present in the workspace
	assertTrue(checkFileExists(directoryPath + "/zeroByteFile.txt"));
}
 
Example 9
Source File: TransferTest.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void testImportFilesWithoutOverride() throws CoreException, IOException, SAXException {
	//create a directory to upload to
	String directoryPath = "sample/directory/path" + System.currentTimeMillis();
	createDirectory(directoryPath);
	//create a file that is not overwritten
	String fileContents = "This is the file contents";
	createFile(directoryPath + "/client.zip", fileContents);
	//start the import
	URL entry = ServerTestsActivator.getContext().getBundle().getEntry("testData/importTest/client.zip");
	File source = new File(FileLocator.toFileURL(entry).getPath());
	long length = source.length();
	InputStream in = new BufferedInputStream(new FileInputStream(source));
	PostMethodWebRequest request = new PostMethodWebRequest(getImportRequestPath(directoryPath), in, "application/zip");
	request.setHeaderField("Content-Length", "" + length);
	request.setHeaderField("Content-Type", "application/octet-stream");
	request.setHeaderField("Slug", "client.zip");
	request.setHeaderField("X-Xfer-Options", "raw,no-overwrite");
	setAuthentication(request);
	WebResponse postResponse = webConversation.getResponse(request);
	//assert the server rejects the override
	assertEquals(HttpURLConnection.HTTP_BAD_REQUEST, postResponse.getResponseCode());
	//assert the file is still present in the workspace
	assertTrue(checkFileExists(directoryPath + "/client.zip"));
	//assert that imported file was not overwritten
	assertTrue(checkContentEquals(createTempFile("expectedFile", fileContents), directoryPath + "/client.zip"));
}
 
Example 10
Source File: TransferTest.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void testImportFilesWithOverride() throws CoreException, IOException, SAXException {
	//create a directory to upload to
	String directoryPath = "sample/directory/path" + System.currentTimeMillis();
	createDirectory(directoryPath);
	//create a file that is to be overwritten
	String fileContents = "This is the file contents";
	createFile(directoryPath + "/client.zip", fileContents);
	//start the import
	URL entry = ServerTestsActivator.getContext().getBundle().getEntry("testData/importTest/client.zip");
	File source = new File(FileLocator.toFileURL(entry).getPath());
	long length = source.length();
	InputStream in = new BufferedInputStream(new FileInputStream(source));
	PostMethodWebRequest request = new PostMethodWebRequest(getImportRequestPath(directoryPath), in, "application/zip");
	request.setHeaderField("Content-Length", "" + length);
	request.setHeaderField("Content-Type", "application/octet-stream");
	request.setHeaderField("Slug", "client.zip");
	request.setHeaderField("X-Xfer-Options", "raw,overwrite-older");
	setAuthentication(request);
	WebResponse postResponse = webConversation.getResponse(request);
	//assert the server accepts the override
	assertEquals(HttpURLConnection.HTTP_CREATED, postResponse.getResponseCode());
	String location = postResponse.getHeaderField("Location");
	assertNotNull(location);
	String type = postResponse.getHeaderField("Content-Type");
	assertNotNull(type);
	//assert the file is still present in the workspace
	assertTrue(checkFileExists(directoryPath + "/client.zip"));
	//assert that imported file was overwritten
	assertFalse(checkContentEquals(createTempFile("expectedFile", fileContents), directoryPath + "/client.zip"));
	assertTrue(checkContentEquals(source, directoryPath + "/client.zip"));
}
 
Example 11
Source File: TransferTest.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void testImportAndUnzipWithoutOverride() throws CoreException, IOException, SAXException {
	//create a directory to upload to
	String directoryPath = "sample/directory/path" + System.currentTimeMillis();
	String filePath = "/org.eclipse.e4.webide/static/js/navigate-tree";
	createDirectory(directoryPath + filePath);
	//create a file that is not overwritten
	String fileContents = "This is the file contents";
	createFile(directoryPath + filePath + "/navigate-tree.js", fileContents);
	//start the import
	URL entry = ServerTestsActivator.getContext().getBundle().getEntry("testData/importTest/client.zip");
	File source = new File(FileLocator.toFileURL(entry).getPath());
	long length = source.length();
	InputStream in = new BufferedInputStream(new FileInputStream(source));
	PostMethodWebRequest request = new PostMethodWebRequest(getImportRequestPath(directoryPath), in, "application/zip");
	request.setHeaderField("Content-Length", "" + length);
	request.setHeaderField("Content-Type", "application/octet-stream");
	request.setHeaderField("Slug", "client.zip");
	request.setHeaderField("X-Xfer-Options", "no-overwrite");
	setAuthentication(request);
	WebResponse postResponse = webConversation.getResponse(request);
	//assert the server rejects the override
	assertEquals(HttpURLConnection.HTTP_BAD_REQUEST, postResponse.getResponseCode());
	//assert the unzip still occurred in the workspace
	assertTrue(checkFileExists(directoryPath + "/org.eclipse.e4.webide/static/images/unit_test/add-test-config.png"));
	//assert that imported file was not overwritten
	assertTrue(checkContentEquals(createTempFile("expectedFile", fileContents), directoryPath + filePath + "/navigate-tree.js"));
}
 
Example 12
Source File: TransferTest.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void testImportAndUnzipWithOverride() throws CoreException, IOException, SAXException {
	//create a directory to upload to
	String directoryPath = "sample/directory/path" + System.currentTimeMillis();
	String filePath = "/org.eclipse.e4.webide/static/js/navigate-tree";
	createDirectory(directoryPath + filePath);
	//create a file that is to be overwritten
	String fileContents = "This is the file contents";
	createFile(directoryPath + filePath + "/navigate-tree.js", fileContents);
	//start the import
	URL entry = ServerTestsActivator.getContext().getBundle().getEntry("testData/importTest/client.zip");
	File source = new File(FileLocator.toFileURL(entry).getPath());
	long length = source.length();
	InputStream in = new BufferedInputStream(new FileInputStream(source));
	PostMethodWebRequest request = new PostMethodWebRequest(getImportRequestPath(directoryPath), in, "application/zip");
	request.setHeaderField("Content-Length", "" + length);
	request.setHeaderField("Content-Type", "application/octet-stream");
	request.setHeaderField("Slug", "client.zip");
	request.setHeaderField("X-Xfer-Options", "overwrite-older");
	setAuthentication(request);
	WebResponse postResponse = webConversation.getResponse(request);
	//assert the server accepts the override
	assertEquals(HttpURLConnection.HTTP_CREATED, postResponse.getResponseCode());
	String location = postResponse.getHeaderField("Location");
	assertNotNull(location);
	String type = postResponse.getHeaderField("Content-Type");
	assertNotNull(type);
	//assert the unzip still occurred in the workspace
	assertTrue(checkFileExists(directoryPath + "/org.eclipse.e4.webide/static/images/unit_test/add-test-config.png"));
	//assert that imported file was overwritten
	assertFalse(checkContentEquals(createTempFile("expectedFile", fileContents), directoryPath + filePath + "/navigate-tree.js"));
}
 
Example 13
Source File: TransferTest.java    From orion.server with Eclipse Public License 1.0 4 votes vote down vote up
@Test
public void testImportEmojiFilename() throws CoreException, IOException, SAXException, URISyntaxException {
	//create a directory to upload to
	String directoryPath = "sample/directory/path" + System.currentTimeMillis();
	createDirectory(directoryPath);

	//start the import
	// file with Emoji characters in the filename.
	// U+1F60A: SMILING FACE WITH SMILING EYES ("\ud83d\ude0a")
	// U+1F431: CAT FACE ("\ud83d\udc31")
	// U+1F435: MONKEY FACE ("\ud83d\udc35")
	String filename = "\ud83d\ude0a\ud83d\udc31\ud83d\udc35.txt";
	String contents = "Emoji characters: \ud83d\ude0a\ud83d\udc31\ud83d\udc35";
	File source = null;
	try {
		source = createTempFile(filename, contents);
		long length = source.length();

		String importPath = getImportRequestPath(directoryPath);
		PostMethodWebRequest request = new PostMethodWebRequest(importPath);
		request.setHeaderField("X-Xfer-Content-Length", Long.toString(length));
		request.setHeaderField("X-Xfer-Options", "raw");

		request.setHeaderField("Slug", Slug.encode(filename));
		setAuthentication(request);
		WebResponse postResponse = webConversation.getResponse(request);
		assertEquals(HttpURLConnection.HTTP_OK, postResponse.getResponseCode());
		String location = postResponse.getHeaderField("Location");
		assertNotNull(location);
		URI importURI = URIUtil.fromString(importPath);
		location = importURI.resolve(location).toString();

		doImport(source, length, location, "text/plain");

		//assert the file is present in the workspace
		assertTrue(checkFileExists(directoryPath + File.separator + filename));
		//assert that imported file has same content as original client.zip
		assertTrue(checkContentEquals(source, directoryPath + File.separator + filename));
	} finally {
		// Delete the temp file
		FileUtils.deleteQuietly(source);
	}
}