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

The following examples show how to use com.meterware.httpunit.WebRequest#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: CoreFilesTest.java    From orion.server with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void testMoveFileNoOverwrite() throws Exception {
	String directoryPath = "testMoveFile/directory/path" + System.currentTimeMillis();
	String sourcePath = directoryPath + "/source.txt";
	String destName = "destination.txt";
	String destPath = directoryPath + "/" + destName;
	createDirectory(directoryPath);
	createFile(sourcePath, "This is the contents");
	JSONObject requestObject = new JSONObject();
	addSourceLocation(requestObject, sourcePath);
	WebRequest request = getPostFilesRequest(directoryPath, requestObject.toString(), "destination.txt");
	request.setHeaderField("X-Create-Options", "move");
	WebResponse response = webConversation.getResponse(request);
	assertEquals(HttpURLConnection.HTTP_CREATED, response.getResponseCode());
	JSONObject responseObject = new JSONObject(response.getText());
	checkFileMetadata(responseObject, destName, null, null, null, null, null, null, null, null);
	assertFalse(checkFileExists(sourcePath));
	assertTrue(checkFileExists(destPath));
}
 
Example 2
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 3
Source File: RemoteMetaStoreTests.java    From orion.server with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Get the list of workspaces for the test user.
 *
 * @throws IOException
 * @throws URISyntaxException
 * @throws JSONException
 * @throws SAXException
 */
@Test
public void testGetSites() throws IOException, URISyntaxException, JSONException, SAXException {
	WebConversation webConversation = new WebConversation();
	assertEquals(HttpURLConnection.HTTP_OK, login(webConversation, getOrionTestName(), getOrionTestName()));

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

	JSONObject jsonObject = new JSONObject(response.getText());
	JSONArray siteConfigurations = jsonObject.getJSONArray("SiteConfigurations");
	if (siteConfigurations.length() == 0) {
		System.out.println("Found zero Sites for user: " + getOrionTestName());
	} else {
		System.out.print("Found Sites for user: " + getOrionTestName() + " at locations: [ ");
		for (int i = 0; i < siteConfigurations.length(); i++) {
			JSONObject workspace = siteConfigurations.getJSONObject(i);
			System.out.print(workspace.getString("Location") + " ");
		}
		System.out.println("]");
	}
}
 
Example 4
Source File: RemoteMetaStoreTests.java    From orion.server with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Get the list of projects in the specified workspace for the specified user.
 *
 * @param workspace
 * @throws IOException
 * @throws URISyntaxException
 * @throws JSONException
 * @throws SAXException
 */
@Test
public void testGetProjects() throws IOException, URISyntaxException, JSONException, SAXException {
	WebConversation webConversation = new WebConversation();
	assertEquals(HttpURLConnection.HTTP_OK, login(webConversation, getOrionTestName(), getOrionTestName()));

	WebRequest request = new GetMethodWebRequest(getOrionServerURI("/workspace/" + getWorkspaceId(getOrionTestName(), "Orion Content")));
	request.setHeaderField(ProtocolConstants.HEADER_ORION_VERSION, "1");
	WebResponse response = webConversation.getResponse(request);
	assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());

	JSONObject jsonObject = new JSONObject(response.getText());
	JSONArray projects = jsonObject.getJSONArray("Projects");
	String name = jsonObject.getString("Name");
	if (projects.length() == 0) {
		System.out.println("Found zero Projects in workspace named: " + name);
	} else {
		System.out.print("Found Projects in workspace named: " + name + " at locations: [ ");
		for (int i = 0; i < projects.length(); i++) {
			JSONObject project = projects.getJSONObject(i);
			System.out.print(project.getString("Location") + " ");
		}
		System.out.println("]");
	}
}
 
Example 5
Source File: UsersTest.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
private void setAuthenticationAdmin(WebRequest request) {
	try {
		request.setHeaderField("Authorization", "Basic " + new String(Base64.encode("admin:admin".getBytes()), "UTF8"));
	} catch (UnsupportedEncodingException e) {
		// this should never happen
		e.printStackTrace();
	}
}
 
Example 6
Source File: GitStashTest.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
protected ServerStatus applyStash(String applyStashLocation) throws Exception {
	JSONObject body = new JSONObject();
	WebRequest request = new PutMethodWebRequest(toAbsoluteURI(applyStashLocation), IOUtilities.toInputStream(body.toString()), "application/json"); //$NON-NLS-1$

	request.setHeaderField(ProtocolConstants.HEADER_ORION_VERSION, "1"); //$NON-NLS-1$
	setAuthentication(request);
	return waitForTask(webConversation.getResponse(request));
}
 
Example 7
Source File: GitCommitTest.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
static WebRequest getGetGitCommitRequest(String location, boolean body, Integer page, Integer pageSize) {
	String requestURI = toAbsoluteURI(location);
	boolean firstParam = true;
	if (body) {
		if (firstParam) {
			requestURI += "?";
			firstParam = false;
		} else {
			requestURI += "&";
		}
		requestURI += "parts=body";
	}

	if (page != null) {
		if (firstParam) {
			requestURI += "?";
			firstParam = false;
		} else {
			requestURI += "&";
		}
		requestURI += "page=" + page.intValue();
	}

	if (pageSize != null) {
		if (firstParam) {
			requestURI += "?";
			firstParam = false;
		} else {
			requestURI += "&";
		}
		requestURI += "pageSize=" + pageSize.intValue();
	}

	WebRequest request = new GetMethodWebRequest(requestURI);
	request.setHeaderField(ProtocolConstants.HEADER_ORION_VERSION, "1");
	setAuthentication(request);
	return request;
}
 
Example 8
Source File: ResponseHeadersTest.java    From esigate with Apache License 2.0 5 votes vote down vote up
private String sendRequestAndExpectResponseHeader(String name, String value) throws Exception {
    WebConversation webConversation = new WebConversation();
    WebRequest req = new GetMethodWebRequest(APPLICATION_PATH + "nocache/ag1/response-headers.jsp");
    req.setHeaderField("X-response-header-" + name, value);
    WebResponse resp = webConversation.getResponse(req);
    String[] responseHeader = resp.getHeaderFields(name);
    if (responseHeader == null || responseHeader.length == 0) {
        return "";
    }
    String result = responseHeader[0];
    for (int i = 1; i < responseHeader.length; i++) {
        result += "\n" + responseHeader[i];
    }
    return result;
}
 
Example 9
Source File: WorkspaceServiceTest.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Tests creating a project that is stored at a non-default location on the server.
 */
@Test
public void testCreateProjectNonDefaultLocation() throws IOException, SAXException, JSONException {
	//create workspace
	createWorkspace(SimpleMetaStore.DEFAULT_WORKSPACE_NAME);

	String tmp = System.getProperty("java.io.tmpdir");
	File projectLocation = new File(new File(tmp), "Orion-testCreateProjectNonDefaultLocation");
	toDelete.add(EFS.getLocalFileSystem().getStore(projectLocation.toURI()));
	projectLocation.mkdir();

	//at first forbid all project locations
	ServletTestingSupport.allowedPrefixes = null;

	//create a project
	String projectName = "My Project";
	WebRequest request = getCreateProjectRequest(workspaceLocation, projectName, projectLocation.toString());
	request.setHeaderField(ProtocolConstants.HEADER_SLUG, projectName);
	request.setHeaderField(ProtocolConstants.HEADER_ORION_VERSION, "1");
	setAuthentication(request);
	WebResponse response = webConversation.getResponse(request);
	assertEquals(HttpURLConnection.HTTP_FORBIDDEN, response.getResponseCode());

	//now set the allowed prefixes and try again
	ServletTestingSupport.allowedPrefixes = projectLocation.toString();
	response = webConversation.getResponse(request);
	assertEquals(HttpURLConnection.HTTP_CREATED, response.getResponseCode());
	JSONObject project = new JSONObject(response.getText());
	assertEquals(projectName, project.getString(ProtocolConstants.KEY_NAME));
	String projectId = project.optString(ProtocolConstants.KEY_ID, null);
	assertNotNull(projectId);
}
 
Example 10
Source File: GitPullTest.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
static WebRequest getPostGitRemoteRequest(String location, boolean force) throws JSONException, UnsupportedEncodingException {
	String requestURI = toAbsoluteURI(location);
	JSONObject body = new JSONObject();
	body.put(GitConstants.KEY_PULL, Boolean.TRUE.toString());
	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 11
Source File: AdvancedFilesTest.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void testETagDeletedFile() throws JSONException, IOException, SAXException, CoreException {
	String fileName = "testfile.txt";

	//setup: create a file
	WebRequest request = getPostFilesRequest("", getNewFileJSON(fileName).toString(), fileName);
	WebResponse response = webConversation.getResponse(request);
	assertEquals(HttpURLConnection.HTTP_CREATED, response.getResponseCode());

	//obtain file metadata and ensure data is correct
	request = getGetFilesRequest(fileName + "?parts=meta");
	response = webConversation.getResponse(request);
	assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
	String etag = response.getHeaderField(ProtocolConstants.KEY_ETAG);
	assertNotNull(etag);

	//delete the file on disk
	IFileStore fileStore = EFS.getStore(makeLocalPathAbsolute(fileName));
	fileStore.delete(EFS.NONE, null);

	//now a PUT should fail
	request = getPutFileRequest(fileName, "something");
	request.setHeaderField(ProtocolConstants.HEADER_IF_MATCH, etag);
	try {
		response = webConversation.getResponse(request);
	} catch (IOException e) {
		//inexplicably HTTPUnit throws IOException on PRECON_FAILED rather than just giving us response
		assertTrue(e.getMessage().indexOf(Integer.toString(HttpURLConnection.HTTP_PRECON_FAILED)) > 0);
	}
}
 
Example 12
Source File: AbstractSimpleMetaStoreMigrationTests.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Verifies the sample content using the remote Orion API. Verifies the user has been
 * migrated successfully and all is good with the account.
 * @param directoryPath
 * @param fileName
 * @throws Exception
 */
protected void verifySampleFileContents(String directoryPath, String fileName) throws Exception {
	String location = directoryPath + "/" + fileName;
	String path = new Path(FILE_SERVLET_LOCATION).append(getTestBaseResourceURILocation()).append(location).toString();
	String requestURI = URIUtil.fromString(SERVER_LOCATION + path).toString();
	WebRequest request = new GetMethodWebRequest(requestURI);
	request.setHeaderField(ProtocolConstants.HEADER_ORION_VERSION, "1");
	setAuthentication(request);
	WebResponse response = webConversation.getResponse(request);
	assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
	assertEquals("Invalid file content", fileName, response.getText());
}
 
Example 13
Source File: GitTest.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
private static WebRequest getPostGitMergeRequest(String location, String commit, boolean squash) throws JSONException, UnsupportedEncodingException {
	String requestURI = toAbsoluteURI(location);
	JSONObject body = new JSONObject();
	body.put(GitConstants.KEY_MERGE, commit);
	body.put(GitConstants.KEY_SQUASH, squash);
	WebRequest request = new PostMethodWebRequest(requestURI, IOUtilities.toInputStream(body.toString()), "UTF-8");
	request.setHeaderField(ProtocolConstants.HEADER_ORION_VERSION, "1");
	setAuthentication(request);
	return request;
}
 
Example 14
Source File: GitDiffTest.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Creates a request to get the diff result for the given location.
 * @param location Either an absolute URI, or a workspace-relative URI
 * @param paths an array containing paths to be included in the diff, can be empty
 */
static WebRequest getGetGitDiffRequest(String location, String[] paths) {
	String requestURI = toAbsoluteURI(location);
	for (String path : paths) {
		requestURI = addParam(requestURI, "Path=" + path);
	}
	WebRequest request = new GetMethodWebRequest(requestURI);
	request.setHeaderField(ProtocolConstants.HEADER_ORION_VERSION, "1");
	setAuthentication(request);
	return request;
}
 
Example 15
Source File: GitBranchTest.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
private WebRequest getDeleteGitBranchRequest(String location) {
	String requestURI = toAbsoluteURI(location);
	WebRequest request = new DeleteMethodWebRequest(requestURI);
	request.setHeaderField(ProtocolConstants.HEADER_ORION_VERSION, "1");
	setAuthentication(request);
	return request;
}
 
Example 16
Source File: AbstractServerTest.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns a GET request for the given location.
 */
protected static WebRequest getGetRequest(String location) {
	String requestURI = toAbsoluteURI(location);
	WebRequest request = new GetMethodWebRequest(requestURI);
	request.setHeaderField(ProtocolConstants.HEADER_ORION_VERSION, "1");
	setAuthentication(request);
	return request;
}
 
Example 17
Source File: GitCommitTest.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
static WebRequest getPostGitCommitRequest(String location, String message, boolean amend, String committerName, String committerEmail, String authorName, String authorEmail) throws JSONException, UnsupportedEncodingException {
	String requestURI = toAbsoluteURI(location);
	JSONObject body = new JSONObject();
	body.put(GitConstants.KEY_COMMIT_MESSAGE, message);
	body.put(GitConstants.KEY_COMMIT_AMEND, Boolean.toString(amend));
	body.put(GitConstants.KEY_COMMITTER_NAME, committerName);
	body.put(GitConstants.KEY_COMMITTER_EMAIL, committerEmail);
	body.put(GitConstants.KEY_AUTHOR_NAME, authorName);
	body.put(GitConstants.KEY_AUTHOR_EMAIL, authorEmail);
	WebRequest request = new PostMethodWebRequest(requestURI, IOUtilities.toInputStream(body.toString()), "UTF-8");
	request.setHeaderField(ProtocolConstants.HEADER_ORION_VERSION, "1");
	setAuthentication(request);
	return request;
}
 
Example 18
Source File: GitTest.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
protected WebResponse getGetGitBlame(String blameUri) throws IOException, SAXException {
	assertBlameUri(blameUri);
	String requestURI = toAbsoluteURI(blameUri);
	WebRequest request = new GetMethodWebRequest(requestURI);
	request.setHeaderField(ProtocolConstants.HEADER_ORION_VERSION, "1");
	setAuthentication(request);
	return webConversation.getResponse(request);
}
 
Example 19
Source File: CoreSiteTest.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * @param locationUri
 * @param user If nonnull, value to use as username and password for auth.
 * @return Returns a request that will GET the site at the given URI.
 * @throws URISyntaxException 
 */
protected WebRequest getRetrieveSiteRequest(String locationUri, String user) throws URISyntaxException {
	WebRequest request = new GetMethodWebRequest(toAbsoluteURI(locationUri));
	request.setHeaderField(ProtocolConstants.HEADER_ORION_VERSION, "1");
	if (user == null)
		setAuthentication(request);
	else
		setAuthentication(request, user, user);
	return request;
}
 
Example 20
Source File: SimpleMetaStoreLiveMigrationTests.java    From orion.server with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Verify nothing is modified in the case that the workspace metadata file is corrupt.
 * @throws Exception
 */
@Test
public void testWorkspaceMetadataCorruption() throws Exception {
	int version = SimpleMetaStoreMigration.VERSION4;
	testUserId = testName.getMethodName();
	String workspaceName = SimpleMetaStore.DEFAULT_WORKSPACE_NAME;
	String workspaceId = SimpleMetaStoreUtil.encodeWorkspaceId(testUserId, workspaceName);
	List<String> workspaceIds = new ArrayList<String>();
	workspaceIds.add(workspaceId);
	List<String> projectNames = new ArrayList<String>();
	projectNames.add(testName.getMethodName().concat("Project"));

	// create metadata on disk
	JSONObject newUserJSON = createUserJson(version, testUserId, workspaceIds);
	createUserMetaData(newUserJSON, testUserId);
	JSONObject newWorkspaceJSON = createWorkspaceJson(version, testUserId, workspaceName, projectNames);
	createWorkspaceMetaData(version, newWorkspaceJSON, testUserId, workspaceName);
	File defaultContentLocation = getProjectDefaultContentLocation(testUserId, workspaceName, projectNames.get(0));
	JSONObject newProjectJSON = createProjectJson(version, testUserId, workspaceName, projectNames.get(0), defaultContentLocation);
	createProjectMetaData(version, newProjectJSON, testUserId, workspaceName, projectNames.get(0));

	// corrupt the workspace metadata on disk
	File userMetaFolder = SimpleMetaStoreUtil.readMetaUserFolder(getWorkspaceRoot(), testUserId);
	String encodedWorkspaceName = SimpleMetaStoreUtil.decodeWorkspaceNameFromWorkspaceId(workspaceId);
	File workspaceMetaFolder = SimpleMetaStoreUtil.readMetaFolder(userMetaFolder, encodedWorkspaceName);
	String workspaceJSON = "workspace.json";
	File corruptedWorkspaceJSON = new File(workspaceMetaFolder, workspaceJSON);
	try {
		FileOutputStream fileOutputStream = new FileOutputStream(corruptedWorkspaceJSON);
		Charset utf8 = Charset.forName("UTF-8");
		OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, utf8);
		outputStreamWriter.write("<!doctype html>\n");
		outputStreamWriter.flush();
		outputStreamWriter.close();
		fileOutputStream.close();
	} catch (IOException e) {
		fail("Count not create a test file in the Orion Project:" + e.getLocalizedMessage());
	}
	assertTrue(corruptedWorkspaceJSON.exists());
	assertTrue(corruptedWorkspaceJSON.isFile());

	// since we modified files on disk directly, force user cache update by reading all users
	OrionConfiguration.getMetaStore().readAllUsers();

	// verify the web request has failed
	WebRequest request = new GetMethodWebRequest(SERVER_LOCATION + "/workspace");
	request.setHeaderField(ProtocolConstants.HEADER_ORION_VERSION, "1");
	setAuthentication(request);
	WebResponse response = webConversation.getResponse(request);
	assertEquals(HttpURLConnection.HTTP_INTERNAL_ERROR, response.getResponseCode());

	// verify the user metadata on disk was not modified
	assertTrue(SimpleMetaStoreUtil.isMetaFile(userMetaFolder, SimpleMetaStore.USER));
	JSONObject userJSON = SimpleMetaStoreUtil.readMetaFile(userMetaFolder, SimpleMetaStore.USER);
	assertTrue(userJSON.has(SimpleMetaStore.ORION_VERSION));
	assertEquals("OrionVersion is incorrect", SimpleMetaStoreMigration.VERSION4, userJSON.getInt(SimpleMetaStore.ORION_VERSION));
}