Search in sources :

Example 16 with HttpOperationFailedException

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));
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) HttpOperationFailedException(org.apache.camel.http.common.HttpOperationFailedException) HttpOperationFailedException(org.apache.camel.http.common.HttpOperationFailedException) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 17 with HttpOperationFailedException

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();
}
Also used : Exchange(org.apache.camel.Exchange) HttpOperationFailedException(org.apache.camel.http.common.HttpOperationFailedException) Test(org.junit.Test)

Example 18 with HttpOperationFailedException

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();
}
Also used : Exchange(org.apache.camel.Exchange) HttpOperationFailedException(org.apache.camel.http.common.HttpOperationFailedException) Test(org.junit.Test)

Example 19 with HttpOperationFailedException

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();
}
Also used : Exchange(org.apache.camel.Exchange) HttpOperationFailedException(org.apache.camel.http.common.HttpOperationFailedException) Test(org.junit.Test)

Example 20 with HttpOperationFailedException

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;
}
Also used : HttpOperationFailedException(org.apache.camel.http.common.HttpOperationFailedException) RuntimeCamelException(org.apache.camel.RuntimeCamelException) IOException(java.io.IOException) HttpOperationFailedException(org.apache.camel.http.common.HttpOperationFailedException)

Aggregations

HttpOperationFailedException (org.apache.camel.http.common.HttpOperationFailedException)28 Test (org.junit.Test)22 Exchange (org.apache.camel.Exchange)17 Processor (org.apache.camel.Processor)10 BaseJettyTest (org.apache.camel.component.jetty.BaseJettyTest)6 IOException (java.io.IOException)4 CamelExecutionException (org.apache.camel.CamelExecutionException)4 HttpConsumer (org.apache.camel.http.common.HttpConsumer)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 URISyntaxException (java.net.URISyntaxException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 CamelExchangeException (org.apache.camel.CamelExchangeException)2 Predicate (org.apache.camel.Predicate)2 RuntimeCamelException (org.apache.camel.RuntimeCamelException)2 RouteBuilder (org.apache.camel.builder.RouteBuilder)2 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)2 StopWatch (org.apache.camel.util.StopWatch)2 File (java.io.File)1