use of org.apache.camel.http.common.HttpOperationFailedException in project camel by apache.
the class HttpPostWithBodyTest method testHttpPostWithError.
@Ignore
@Test
public void testHttpPostWithError() throws Exception {
Exchange exchange = template.send("direct:start", new Processor() {
public void process(Exchange exchange) throws Exception {
exchange.getIn().setBody("q=test1234");
}
});
assertNotNull("exchange", exchange);
assertTrue("The exchange should be failed", exchange.isFailed());
// get the ex message
HttpOperationFailedException exception = (HttpOperationFailedException) exchange.getException();
assertNotNull("exception", exception);
int statusCode = exception.getStatusCode();
assertTrue("The response code should not be 200", statusCode != 200);
String reason = exception.getStatusText();
assertNotNull("Should have a body!", reason);
assertTrue("body should contain: " + expectedText, reason.contains(expectedText));
}
use of org.apache.camel.http.common.HttpOperationFailedException in project camel by apache.
the class HttpProducerSelectMethodTest method testDataDefaultIsPost.
@Test
public void testDataDefaultIsPost() throws Exception {
HttpComponent component = context.getComponent("http", HttpComponent.class);
HttpEndpoint endpoiont = (HttpEndpoint) component.createEndpoint("http://www.google.com");
MyHttpProducer producer = new MyHttpProducer(endpoiont, "POST", null);
Exchange exchange = producer.createExchange();
exchange.getIn().setBody("This is some data to post");
try {
producer.process(exchange);
fail("Should have thrown HttpOperationFailedException");
} catch (HttpOperationFailedException e) {
assertEquals(500, e.getStatusCode());
}
producer.stop();
}
use of org.apache.camel.http.common.HttpOperationFailedException in project camel by apache.
the class HttpProducerSelectMethodTest method testWithEndpointQuery.
@Test
public void testWithEndpointQuery() throws Exception {
HttpComponent component = context.getComponent("http", HttpComponent.class);
HttpEndpoint endpoiont = (HttpEndpoint) component.createEndpoint("http://www.google.com?q=Camel");
MyHttpProducer producer = new MyHttpProducer(endpoiont, "GET", "q=Camel");
Exchange exchange = producer.createExchange();
// no body should be GET
exchange.getIn().setBody(null);
try {
producer.process(exchange);
fail("Should have thrown HttpOperationFailedException");
} catch (HttpOperationFailedException e) {
assertEquals(500, e.getStatusCode());
}
producer.stop();
}
use of org.apache.camel.http.common.HttpOperationFailedException in project camel by apache.
the class HttpProducerSelectMethodTest method testWithQueryInHeader.
@Test
public void testWithQueryInHeader() throws Exception {
HttpComponent component = context.getComponent("http", HttpComponent.class);
HttpEndpoint endpoiont = (HttpEndpoint) component.createEndpoint("http://www.google.com");
MyHttpProducer producer = new MyHttpProducer(endpoiont, "GET", "q=Camel");
Exchange exchange = producer.createExchange();
// no body should be GET
exchange.getIn().setBody(null);
exchange.getIn().setHeader(Exchange.HTTP_QUERY, "q=Camel");
try {
producer.process(exchange);
fail("Should have thrown HttpOperationFailedException");
} catch (HttpOperationFailedException e) {
assertEquals(500, e.getStatusCode());
}
producer.stop();
}
use of org.apache.camel.http.common.HttpOperationFailedException in project camel by apache.
the class DefaultJettyHttpBinding method populateHttpOperationFailedException.
protected Exception populateHttpOperationFailedException(Exchange exchange, JettyContentExchange httpExchange, int responseCode) throws IOException {
HttpOperationFailedException answer;
String uri = httpExchange.getUrl();
Map<String, String> headers = getSimpleMap(httpExchange.getResponseHeaders());
Object responseBody = extractResponseBody(exchange, httpExchange);
if (transferException && responseBody != null && responseBody instanceof Exception) {
// if the response was a serialized exception then use that
return (Exception) responseBody;
}
// make a defensive copy of the response body in the exception so its detached from the cache
String copy = null;
if (responseBody != null) {
copy = exchange.getContext().getTypeConverter().convertTo(String.class, exchange, responseBody);
}
if (responseCode >= 300 && responseCode < 400) {
Collection<String> loc = httpExchange.getResponseHeaders().get("location");
if (loc != null && !loc.isEmpty()) {
String locationHeader = loc.iterator().next();
answer = new HttpOperationFailedException(uri, responseCode, null, locationHeader, headers, copy);
} else {
// no redirect location
answer = new HttpOperationFailedException(uri, responseCode, null, null, headers, copy);
}
} else {
// internal server error (error code 500)
answer = new HttpOperationFailedException(uri, responseCode, null, null, headers, copy);
}
return answer;
}
Aggregations