use of org.apache.commons.httpclient.Header in project cloudstack by apache.
the class BigSwitchApiTest method testExecuteCreateObjectException.
@Test(expected = BigSwitchBcfApiException.class)
public void testExecuteCreateObjectException() throws BigSwitchBcfApiException, IOException {
NetworkData network = new NetworkData();
when(_client.executeMethod((HttpMethod) any())).thenThrow(new HttpException());
_method = mock(PostMethod.class);
when(_method.getStatusCode()).thenReturn(HttpStatus.SC_INTERNAL_SERVER_ERROR);
Header header = mock(Header.class);
when(header.getValue()).thenReturn("text/html");
when(_method.getResponseHeader("Content-type")).thenReturn(header);
when(_method.getResponseBodyAsString()).thenReturn("Off to timbuktu, won't be back later.");
try {
_api.executeCreateObject(network, "/", Collections.<String, String>emptyMap());
} finally {
verify(_method, times(1)).releaseConnection();
}
}
use of org.apache.commons.httpclient.Header in project cloudstack by apache.
the class BigSwitchApiTest method testExecuteUpdateObjectOK.
@Test
public void testExecuteUpdateObjectOK() throws BigSwitchBcfApiException, IOException {
NetworkData network = new NetworkData();
_method = mock(PutMethod.class);
when(_method.getResponseHeader("X-BSN-BVS-HASH-MATCH")).thenReturn(new Header("X-BSN-BVS-HASH-MATCH", UUID.randomUUID().toString()));
when(_method.getStatusCode()).thenReturn(HttpStatus.SC_OK);
String hash = _api.executeUpdateObject(network, "/", Collections.<String, String>emptyMap());
verify(_method, times(1)).releaseConnection();
verify(_client, times(1)).executeMethod(_method);
assertNotEquals(hash, "");
assertNotEquals(hash, BigSwitchBcfApi.HASH_CONFLICT);
assertNotEquals(hash, BigSwitchBcfApi.HASH_IGNORE);
}
use of org.apache.commons.httpclient.Header in project maven-plugins by apache.
the class ClassicJiraDownloader method download.
/**
* Downloads the given link using the configured HttpClient, possibly following redirects.
*
* @param cl the HttpClient
* @param link the URL to JIRA
*/
private void download(final HttpClient cl, final String link) {
InputStream in = null;
OutputStream out = null;
try {
GetMethod gm = new GetMethod(link);
getLog().info("Downloading from JIRA at: " + link);
gm.setFollowRedirects(true);
cl.executeMethod(gm);
StatusLine sl = gm.getStatusLine();
if (sl == null) {
getLog().error("Unknown error validating link: " + link);
return;
}
// if we get a redirect, do so
if (gm.getStatusCode() == HttpStatus.SC_MOVED_TEMPORARILY) {
Header locationHeader = gm.getResponseHeader("Location");
if (locationHeader == null) {
getLog().warn("Site sent redirect, but did not set Location header");
} else {
String newLink = locationHeader.getValue();
getLog().debug("Following redirect to " + newLink);
download(cl, newLink);
}
}
if (gm.getStatusCode() == HttpStatus.SC_OK) {
in = gm.getResponseBodyAsStream();
if (!output.getParentFile().exists()) {
output.getParentFile().mkdirs();
}
// write the response to file
out = new FileOutputStream(output);
IOUtil.copy(in, out);
out.close();
out = null;
in.close();
in = null;
getLog().debug("Downloading from JIRA was successful");
} else {
getLog().warn("Downloading from JIRA failed. Received: [" + gm.getStatusCode() + "]");
}
} catch (HttpException e) {
if (getLog().isDebugEnabled()) {
getLog().error("Error downloading issues from JIRA:", e);
} else {
getLog().error("Error downloading issues from JIRA url: " + e.getLocalizedMessage());
}
} catch (IOException e) {
if (getLog().isDebugEnabled()) {
getLog().error("Error downloading issues from JIRA:", e);
} else {
getLog().error("Error downloading issues from JIRA. Cause is " + e.getLocalizedMessage());
}
} finally {
IOUtil.close(out);
IOUtil.close(in);
}
}
use of org.apache.commons.httpclient.Header 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.Header 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;
}
Aggregations