use of org.apache.commons.httpclient.NameValuePair in project zm-mailbox by Zimbra.
the class TestPreAuthServlet method testShouldNotAllowPreAuthGetCookieReuse.
public void testShouldNotAllowPreAuthGetCookieReuse() throws Exception {
Account account = TestUtil.getAccount("user1");
AuthToken authToken = new ZimbraAuthToken(account);
System.out.println(authToken.isRegistered());
HttpClient client = new HttpClient();
Server localServer = Provisioning.getInstance().getLocalServer();
String protoHostPort = "http://localhost:" + localServer.getIntAttr(Provisioning.A_zimbraMailPort, 0);
String url = protoHostPort + PRE_AUTH_URL;
//allow first request
HttpMethod method = new GetMethod(url);
NameValuePair[] queryStringPairArray = new NameValuePair[] { new NameValuePair("isredirect", "1"), new NameValuePair("authtoken", authToken.getEncoded()) };
method.setQueryString(queryStringPairArray);
int respCode = HttpClientUtil.executeMethod(client, method);
//reject second request
method = new GetMethod(url);
method.setQueryString(queryStringPairArray);
respCode = HttpClientUtil.executeMethod(client, method);
Assert.assertEquals(400, respCode);
}
use of org.apache.commons.httpclient.NameValuePair in project cloudstack by apache.
the class BigSwitchBcfApi method executeRetrieveObject.
@SuppressWarnings("unchecked")
protected <T> T executeRetrieveObject(final Type returnObjectType, final String uri, final Map<String, String> parameters) throws BigSwitchBcfApiException {
checkInvariants();
GetMethod gm = (GetMethod) createMethod("get", uri, _port);
setHttpHeader(gm);
if (parameters != null && !parameters.isEmpty()) {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(parameters.size());
for (Entry<String, String> e : parameters.entrySet()) {
nameValuePairs.add(new NameValuePair(e.getKey(), e.getValue()));
}
gm.setQueryString(nameValuePairs.toArray(new NameValuePair[0]));
}
executeMethod(gm);
String hash = checkResponse(gm, "BigSwitch HTTP get failed: ");
T returnValue;
try {
// CAUTIOUS: Safety margin of 2048 characters - extend if needed.
returnValue = (T) gson.fromJson(gm.getResponseBodyAsString(2048), returnObjectType);
} catch (IOException e) {
S_LOGGER.error("IOException while retrieving response body", e);
throw new BigSwitchBcfApiException(e);
} finally {
gm.releaseConnection();
}
if (returnValue instanceof ControlClusterStatus) {
if (HASH_CONFLICT.equals(hash)) {
isMaster = true;
((ControlClusterStatus) returnValue).setTopologySyncRequested(true);
} else if (!HASH_IGNORE.equals(hash) && !isMaster) {
isMaster = true;
((ControlClusterStatus) returnValue).setTopologySyncRequested(true);
}
}
return returnValue;
}
use of org.apache.commons.httpclient.NameValuePair in project sling by apache.
the class HttpTestBase method getContent.
/** retrieve the contents of given URL and assert its content type
* @param expectedContentType use CONTENT_TYPE_DONTCARE if must not be checked
* @param httpMethod supports just GET and POST methods
* @throws IOException
* @throws HttpException */
public String getContent(String url, String expectedContentType, List<NameValuePair> params, int expectedStatusCode, String httpMethod) throws IOException {
HttpMethodBase method = null;
if (HTTP_METHOD_GET.equals(httpMethod)) {
method = new GetMethod(url);
} else if (HTTP_METHOD_POST.equals(httpMethod)) {
method = new PostMethod(url);
} else {
fail("Http Method not supported in this test suite, method: " + httpMethod);
}
if (params != null) {
final NameValuePair[] nvp = new NameValuePair[0];
method.setQueryString(params.toArray(nvp));
}
final int status = httpClient.executeMethod(method);
final String content = getResponseBodyAsStream(method, 0);
assertEquals("Expected status " + expectedStatusCode + " for " + url + " (content=" + content + ")", expectedStatusCode, status);
final Header h = method.getResponseHeader("Content-Type");
if (expectedContentType == null) {
if (h != null) {
fail("Expected null Content-Type, got " + h.getValue());
}
} else if (CONTENT_TYPE_DONTCARE.equals(expectedContentType)) {
// no check
} else if (h == null) {
fail("Expected Content-Type that starts with '" + expectedContentType + " but got no Content-Type header at " + url);
} else {
assertTrue("Expected Content-Type that starts with '" + expectedContentType + "' for " + url + ", got '" + h.getValue() + "'", h.getValue().startsWith(expectedContentType));
}
return content.toString();
}
use of org.apache.commons.httpclient.NameValuePair in project sling by apache.
the class SelectorAuthenticationResponseCodeTest method assertPostStatus.
// TODO - move this method into commons.testing
protected HttpMethod assertPostStatus(String url, int expectedStatusCode, List<NameValuePair> postParams, List<Header> headers, String assertMessage) throws IOException {
final PostMethod post = new PostMethod(url);
post.setFollowRedirects(false);
if (headers != null) {
for (Header header : headers) {
post.addRequestHeader(header);
}
}
if (postParams != null) {
final NameValuePair[] nvp = {};
post.setRequestBody(postParams.toArray(nvp));
}
final int status = H.getHttpClient().executeMethod(post);
if (assertMessage == null) {
assertEquals(expectedStatusCode, status);
} else {
assertEquals(assertMessage, expectedStatusCode, status);
}
return post;
}
use of org.apache.commons.httpclient.NameValuePair in project sling by apache.
the class AccessPrivilegesInfoTest method testSLING_1090.
/**
* Test the fix for SLING-1090
*/
@Test
public void testSLING_1090() throws Exception {
testUserId = H.createTestUser();
//grant jcr: removeChildNodes to the root node
ArrayList<NameValuePair> postParams = new ArrayList<NameValuePair>();
postParams.add(new NameValuePair("principalId", testUserId));
postParams.add(new NameValuePair("privilege@jcr:read", "granted"));
postParams.add(new NameValuePair("privilege@jcr:removeChildNodes", "granted"));
Credentials adminCreds = new UsernamePasswordCredentials("admin", "admin");
H.assertAuthenticatedPostStatus(adminCreds, HttpTest.HTTP_BASE_URL + "/.modifyAce.html", HttpServletResponse.SC_OK, postParams, null);
//create a node as a child of the root folder
testFolderUrl = H.getTestClient().createNode(HttpTest.HTTP_BASE_URL + "/testFolder" + random.nextInt() + SlingPostConstants.DEFAULT_CREATE_SUFFIX, null);
String postUrl = testFolderUrl + ".modifyAce.html";
//grant jcr:removeNode to the test node
postParams = new ArrayList<NameValuePair>();
postParams.add(new NameValuePair("principalId", testUserId));
postParams.add(new NameValuePair("privilege@jcr:read", "granted"));
postParams.add(new NameValuePair("privilege@jcr:removeNode", "granted"));
H.assertAuthenticatedPostStatus(adminCreds, postUrl, HttpServletResponse.SC_OK, postParams, null);
//fetch the JSON for the test page to verify the settings.
String getUrl = testFolderUrl + ".privileges-info.json";
Credentials testUserCreds = new UsernamePasswordCredentials(testUserId, "testPwd");
String json = H.getAuthenticatedContent(testUserCreds, getUrl, HttpTest.CONTENT_TYPE_JSON, null, HttpServletResponse.SC_OK);
assertNotNull(json);
JsonObject jsonObj = JsonUtil.parseObject(json);
assertEquals(true, jsonObj.getBoolean("canDelete"));
}
Aggregations