com.meterware.httpunit.PostMethodWebRequest Java Examples

The following examples show how to use com.meterware.httpunit.PostMethodWebRequest. 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: RemoteMetaStoreTests.java    From orion.server with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Create a workspace on the Orion server for the test user.
 *
 * @param webConversation
 * @param login
 * @param password
 * @param workspace
 * @param site
 * @return
 * @throws URISyntaxException
 * @throws IOException
 * @throws JSONException
 * @throws SAXException
 */
protected int createSite(WebConversation webConversation, String login, String password, String workspace, String site) throws URISyntaxException,
		IOException, JSONException, SAXException {
	assertEquals(HttpURLConnection.HTTP_OK, login(webConversation, login, password));

	JSONObject json = new JSONObject();
	json.put(SiteConfigurationConstants.KEY_WORKSPACE, getWorkspaceId(login, workspace));
	json.put(ProtocolConstants.KEY_NAME, site);
	json.put(SiteConfigurationConstants.KEY_HOST_HINT, site.toLowerCase().replaceAll(" ", "-"));
	WebRequest request = new PostMethodWebRequest(getOrionServerURI("/site"), IOUtilities.toInputStream(json.toString()), "application/json");
	request.setHeaderField(ProtocolConstants.HEADER_ORION_VERSION, "1");
	WebResponse response = webConversation.getResponse(request);
	assertEquals(HttpURLConnection.HTTP_CREATED, response.getResponseCode());

	JSONObject jsonObject = new JSONObject(response.getText());
	String location = jsonObject.getString("Location");
	String name = jsonObject.getString("Name");
	System.out.println("Created Site: " + name + " at Location: " + location);
	return response.getResponseCode();
}
 
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: GitResetTest.java    From orion.server with Eclipse Public License 1.0 6 votes vote down vote up
static WebRequest getPostGitIndexRequest(String location, String[] paths, String commit, String resetType) throws JSONException, UnsupportedEncodingException {
	String requestURI = toAbsoluteURI(location);
	JSONObject body = new JSONObject();
	if (resetType != null)
		body.put(GitConstants.KEY_RESET_TYPE, resetType);
	if (paths != null) {
		//			assertNull("Cannot mix paths and commit", commit);
		JSONArray jsonPaths = new JSONArray();
		for (String path : paths)
			jsonPaths.put(path);
		body.put(ProtocolConstants.KEY_PATH, jsonPaths);
	}
	if (commit != null)
		body.put(GitConstants.KEY_TAG_COMMIT, commit);
	WebRequest request = new PostMethodWebRequest(requestURI, IOUtilities.toInputStream(body.toString()), "UTF-8");
	request.setHeaderField(ProtocolConstants.HEADER_ORION_VERSION, "1");
	setAuthentication(request);
	return request;
}
 
Example #4
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 #5
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 #6
Source File: WorkspaceServiceTest.java    From orion.server with Eclipse Public License 1.0 6 votes vote down vote up
protected WebRequest getCopyMoveProjectRequest(URI workspaceLocationURI, String projectName, String sourceLocation, boolean isMove) throws UnsupportedEncodingException {
	workspaceLocation = addSchemeHostPort(workspaceLocationURI);
	JSONObject requestObject = new JSONObject();
	try {
		requestObject.put(ProtocolConstants.KEY_LOCATION, sourceLocation);
	} catch (JSONException e) {
		//should never happen
		Assert.fail("Invalid source location: " + sourceLocation);
	}
	InputStream source = IOUtilities.toInputStream(requestObject.toString());
	WebRequest request = new PostMethodWebRequest(workspaceLocation.toString(), source, "application/json");
	if (projectName != null)
		request.setHeaderField(ProtocolConstants.HEADER_SLUG, projectName);
	request.setHeaderField(ProtocolConstants.HEADER_ORION_VERSION, "1");
	request.setHeaderField(ProtocolConstants.HEADER_CREATE_OPTIONS, isMove ? "move" : "copy");
	setAuthentication(request);
	return request;
}
 
Example #7
Source File: GitPushTest.java    From orion.server with Eclipse Public License 1.0 6 votes vote down vote up
static WebRequest getPostGitRemoteRequest(String location, String srcRef, boolean tags, boolean force, String name, String kh, byte[] privk, byte[] pubk, byte[] p) throws JSONException, UnsupportedEncodingException {
	String requestURI = toAbsoluteURI(location);
	JSONObject body = new JSONObject();

	body.put(ProtocolConstants.KEY_NAME, name);
	if (kh != null)
		body.put(GitConstants.KEY_KNOWN_HOSTS, kh);
	if (privk != null)
		body.put(GitConstants.KEY_PRIVATE_KEY, new String(privk));
	if (pubk != null)
		body.put(GitConstants.KEY_PUBLIC_KEY, new String(pubk));
	if (p != null)
		body.put(GitConstants.KEY_PASSPHRASE, new String(p));

	if (srcRef != null)
		body.put(GitConstants.KEY_PUSH_SRC_REF, srcRef);
	body.put(GitConstants.KEY_PUSH_TAGS, tags);
	body.put(GitConstants.KEY_FORCE, force);
	WebRequest request = new PostMethodWebRequest(requestURI, IOUtilities.toInputStream(body.toString()), "UTF-8");
	request.setHeaderField(ProtocolConstants.HEADER_ORION_VERSION, "1");
	setAuthentication(request);
	return request;
}
 
Example #8
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 #9
Source File: RemoteMetaStoreTests.java    From orion.server with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Create a folder in a project on the Orion server for the test user.
 *
 * @param webConversation
 * @param login
 * @param password
 * @param workspace
 * @param project
 * @return
 * @throws IOException
 * @throws JSONException
 * @throws URISyntaxException
 * @throws SAXException
 */
protected int createFolder(WebConversation webConversation, String login, String password, String workspace, String project) throws IOException,
		JSONException, URISyntaxException, SAXException {
	assertEquals(HttpURLConnection.HTTP_OK, login(webConversation, login, password));

	JSONObject jsonObject = new JSONObject();
	jsonObject.put("Directory", "true");
	jsonObject.put("Name", "folder");
	jsonObject.put("LocalTimeStamp", "0");
	String parent = "/file/" + getWorkspaceId(login, workspace) + "/" + project;
	WebRequest request = new PostMethodWebRequest(getOrionServerURI(parent), IOUtilities.toInputStream(jsonObject.toString()), "application/json");
	request.setHeaderField(ProtocolConstants.HEADER_ORION_VERSION, "1");
	WebResponse response = webConversation.getResponse(request);
	assertEquals(HttpURLConnection.HTTP_CREATED, response.getResponseCode());

	System.out.println("Created Folder: " + parent + "/folder");
	return response.getResponseCode();
}
 
Example #10
Source File: RemoteMetaStoreTests.java    From orion.server with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Create a workspace on the Orion server for the test user.
 *
 * @param webConversation
 * @param login
 * @param password
 * @param workspace
 * @return
 * @throws URISyntaxException
 * @throws IOException
 * @throws JSONException
 * @throws SAXException
 */
protected int createWorkspace(WebConversation webConversation, String login, String password, String workspace) throws URISyntaxException, IOException,
		JSONException, SAXException {
	assertEquals(HttpURLConnection.HTTP_OK, login(webConversation, login, password));

	WebRequest request = new PostMethodWebRequest(getOrionServerURI("/workspace"));
	request.setHeaderField(ProtocolConstants.HEADER_ORION_VERSION, "1");
	request.setHeaderField(ProtocolConstants.HEADER_SLUG, workspace);
	WebResponse response = webConversation.getResponse(request);
	assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

	JSONObject jsonObject = new JSONObject(response.getText());
	String location = jsonObject.getString("Location");
	String name = jsonObject.getString("Name");
	System.out.println("Created Workspace: " + name + " at Location: " + location);
	return response.getResponseCode();
}
 
Example #11
Source File: GitApplyPatchTest.java    From orion.server with Eclipse Public License 1.0 6 votes vote down vote up
private static WebRequest getPostGitDiffRequest(String location, String patch) throws UnsupportedEncodingException {
	String requestURI = toAbsoluteURI(location);

	String boundary = new UniversalUniqueIdentifier().toBase64String();
	StringBuilder sb = new StringBuilder();
	sb.append("--" + boundary + EOL);
	sb.append("Content-Disposition: form-data; name=\"radio\"" + EOL);
	sb.append(EOL);
	sb.append("fileRadio" + EOL);
	sb.append("--" + boundary + EOL);
	sb.append("Content-Disposition: form-data; name=\"uploadedfile\"; filename=\"\"" + EOL);
	sb.append(ProtocolConstants.HEADER_CONTENT_TYPE + ": plain/text" + EOL + EOL); //$NON-NLS-1$
	sb.append(patch);
	// see GitDiffHandlerV1.readPatch(ServletInputStream, String)
	sb.append(EOL + "--" + boundary + "--" + EOL);
	patch = sb.toString();

	WebRequest request = new PostMethodWebRequest(requestURI, IOUtilities.toInputStream(patch), "UTF-8");
	request.setHeaderField(ProtocolConstants.HEADER_ORION_VERSION, "1");
	request.setHeaderField(ProtocolConstants.HEADER_CONTENT_TYPE, "multipart/related; boundary=" + boundary); //$NON-NLS-1$
	setAuthentication(request);
	return request;
}
 
Example #12
Source File: GitFetchTest.java    From orion.server with Eclipse Public License 1.0 6 votes vote down vote up
static WebRequest getPostGitRemoteRequest(String location, boolean fetch, boolean force, String name, String kh, byte[] privk, byte[] pubk, byte[] p) throws JSONException, UnsupportedEncodingException {
	String requestURI = toAbsoluteURI(location);
	JSONObject body = new JSONObject();
	body.put(ProtocolConstants.KEY_NAME, name);
	if (kh != null)
		body.put(GitConstants.KEY_KNOWN_HOSTS, kh);
	if (privk != null)
		body.put(GitConstants.KEY_PRIVATE_KEY, new String(privk));
	if (pubk != null)
		body.put(GitConstants.KEY_PUBLIC_KEY, new String(pubk));
	if (p != null)
		body.put(GitConstants.KEY_PASSPHRASE, new String(p));

	body.put(GitConstants.KEY_FETCH, Boolean.toString(fetch));
	body.put(GitConstants.KEY_FORCE, force);
	WebRequest request = new PostMethodWebRequest(requestURI, IOUtilities.toInputStream(body.toString()), "UTF-8");
	request.setHeaderField(ProtocolConstants.HEADER_ORION_VERSION, "1");
	setAuthentication(request);
	return request;
}
 
Example #13
Source File: CXFFilterTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testPostInvokeServices() throws Exception {
    newClient();

    WebRequest req = new PostMethodWebRequest(CONTEXT_URL + "/services/Greeter",
            getClass().getResourceAsStream("GreeterMessage.xml"),
            "text/xml; charset=UTF-8");

    WebResponse response = newClient().getResponse(req);

    assertEquals("text/xml", response.getContentType());
    assertEquals(StandardCharsets.UTF_8.name(), response.getCharacterSet());

    Document doc = StaxUtils.read(response.getInputStream());
    assertNotNull(doc);

    addNamespace("h", "http://apache.org/hello_world_soap_http/types");

    assertValid("/s:Envelope/s:Body", doc);
    assertValid("//h:sayHiResponse", doc);
}
 
Example #14
Source File: ExternalServicesServletTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testPostInvokeServices() throws Exception {
    newClient();

    WebRequest req = new PostMethodWebRequest(CONTEXT_URL + "/greeter",
            getClass().getResourceAsStream("GreeterMessage.xml"),
            "text/xml; charset=UTF-8");

    WebResponse response = newClient().getResponse(req);

    assertEquals("text/xml", response.getContentType());
    assertEquals(StandardCharsets.UTF_8.name(), response.getCharacterSet());

    Document doc = StaxUtils.read(response.getInputStream());
    assertNotNull(doc);

    addNamespace("h", "http://apache.org/hello_world_soap_http/types");

    assertValid("/s:Envelope/s:Body", doc);
    assertValid("//h:sayHiResponse", doc);
}
 
Example #15
Source File: CXFServletTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
private void invoke(String encoding) throws Exception {
    WebRequest req = new PostMethodWebRequest(CONTEXT_URL + "/services/greeter",
        getClass().getResourceAsStream("GreeterMessage.xml"),
        "text/xml; charset=" + encoding);

    ServletUnitClient client = newClient();
    WebResponse response = client.getResponse(req);
    client.setExceptionsThrownOnErrorStatus(false);

    assertEquals("text/xml", response.getContentType());
    assertTrue(encoding.equalsIgnoreCase(response.getCharacterSet()));

    Document doc = StaxUtils.read(response.getInputStream());
    assertNotNull(doc);

    addNamespace("h", "http://apache.org/hello_world_soap_http/types");

    assertValid("/s:Envelope/s:Body", doc);
    assertValid("//h:sayHiResponse", doc);
}
 
Example #16
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 #17
Source File: ViewToolsIT.java    From velocity-tools with Apache License 2.0 5 votes vote down vote up
public @Test void testJsonTool() throws Exception
{
    String json = "{\"foo\":\"bar\"}";
    WebConversation conv = new WebConversation();
    WebRequest req = new PostMethodWebRequest(ROOT_URL+"post_json.vm", new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8)), "application/json");
    WebResponse resp = conv.getResponse(req);
    checkText(resp, "foo", "bar");
}
 
Example #18
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 #19
Source File: RequestHeadersTest.java    From esigate with Apache License 2.0 5 votes vote down vote up
/**
 * We never change request body (unlike for responses) The only case when the request body may be changed is when it
 * has a chunked "Transfer-Encoding" but in this case the "Content-Length" is not set
 * 
 * @throws Exception
 */
public void testContentLength() throws Exception {
    // This header will be set by http client automatically for POST
    // requests so we will find it in the request even if it is not
    // forwarded
    WebConversation webConversation = new WebConversation();
    WebRequest req =
            new PostMethodWebRequest(APPLICATION_PATH + "nocache/ag1/request-headers.jsp?name=Content-Length");
    WebResponse resp = webConversation.getResponse(req);
    String result = resp.getText();
    assertEquals("Content-Length request header should be set for POST requests", "content-length: 0",
            result.toLowerCase());
}
 
Example #20
Source File: RequestHeadersTest.java    From esigate with Apache License 2.0 5 votes vote down vote up
/**
 * Bug fix: Content-type Header value placed in Content-Encoding Header.
 * https://github.com/esigate/esigate/issues/159
 * 
 * @throws Exception
 */
public void testContentEncodingWithContentType() throws Exception {
    WebConversation webConversation = new WebConversation();
    PostMethodWebRequest req =
            new PostMethodWebRequest(APPLICATION_PATH + "nocache/ag1/request-headers.jsp?name=Content-Encoding",
                    IOUtils.toInputStream("test"), "text/html; charset=utf-8");
    String resp = webConversation.getResponse(req).getText();
    assertEquals("Content-Encoding request header should be empty", "", resp.toLowerCase());
}
 
Example #21
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 #22
Source File: AggregatorTest.java    From esigate with Apache License 2.0 5 votes vote down vote up
/**
 * Test for a post on a local page
 * 
 * @see <a href="https://github.com/esigate/esigate/issues/97">POST requests do not work with local providers
 *      #97</a>
 * 
 * @throws Exception
 */
public void testLocalPost() throws Exception {
    // Post request with a e-acute urlencoded using UTF-8 charset
    PostMethodWebRequest req =
            new PostMethodWebRequest(APPLICATION_PATH + "local/post.jsp", new ByteArrayInputStream(
                    "myField=%C3%A9".getBytes("UTF-8")), "application/x-www-form-urlencoded");
    WebResponse resp = webConversation.getResponse(req);
    assertEquals("Status should be 200", HttpServletResponse.SC_OK, resp.getResponseCode());
    assertEqualsIgnoreCarriageReturn(getResource("post.jsp"), resp.getText());
}
 
Example #23
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 #24
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 #25
Source File: AggregatorTest.java    From esigate with Apache License 2.0 5 votes vote down vote up
public void testRawPostWithQueryString() throws Exception {
    PostMethodWebRequest req =
            new PostMethodWebRequest(APPLICATION_PATH + "post_raw.jsp?param=smile", new ByteArrayInputStream(
                    "Hello !".getBytes("UTF-8")), "raw/post-data");
    WebResponse resp = webConversation.getResponse(req);
    assertEquals("Response body did not match", "Posted body data : Hello !smile", resp.getText());
}
 
Example #26
Source File: AggregatorTest.java    From esigate with Apache License 2.0 5 votes vote down vote up
public void testRawPost() throws Exception {
    PostMethodWebRequest req =
            new PostMethodWebRequest(APPLICATION_PATH + "post_raw.jsp", new ByteArrayInputStream(
                    "Hello smile!".getBytes("UTF-8")), "raw/post-data");
    WebResponse resp = webConversation.getResponse(req);
    assertEquals("Response body did not match", "Posted body data : Hello smile!", resp.getText());
}
 
Example #27
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 #28
Source File: AggregatorTest.java    From esigate with Apache License 2.0 5 votes vote down vote up
public void testPost() throws Exception {
    // Post request with a e-acute urlencoded using UTF-8 charset
    PostMethodWebRequest req =
            new PostMethodWebRequest(APPLICATION_PATH + "post.jsp", new ByteArrayInputStream(
                    "myField=%C3%A9".getBytes("UTF-8")), "application/x-www-form-urlencoded");
    WebResponse resp = webConversation.getResponse(req);
    assertEquals("Status should be 200", HttpServletResponse.SC_OK, resp.getResponseCode());
    assertEquals(getResource("post.jsp"), resp.getText());
}
 
Example #29
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 #30
Source File: GitDiffTest.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
private static WebRequest getPostGitDiffRequest(String location, String str) throws JSONException, UnsupportedEncodingException {
	String requestURI = toAbsoluteURI(location);
	JSONObject body = new JSONObject();
	body.put(GitConstants.KEY_COMMIT_NEW, str);
	str = body.toString();

	WebRequest request = new PostMethodWebRequest(requestURI, IOUtilities.toInputStream(str), "UTF-8");
	request.setHeaderField(ProtocolConstants.HEADER_ORION_VERSION, "1");
	setAuthentication(request);
	return request;
}