Search in sources :

Example 6 with HttpOperationFailedException

use of org.apache.camel.http.common.HttpOperationFailedException in project camel by apache.

the class HttpThrowExceptionOnFailureTest method httpGetWhichReturnsHttp501ShouldThrowAnException.

@Test
public void httpGetWhichReturnsHttp501ShouldThrowAnException() throws Exception {
    Exchange reply = template.request("http4://" + localServer.getInetAddress().getHostName() + ":" + localServer.getLocalPort() + "/XXX?throwExceptionOnFailure=true", new Processor() {

        public void process(Exchange exchange) throws Exception {
        }
    });
    Exception e = reply.getException();
    assertNotNull("Should have thrown an exception", e);
    HttpOperationFailedException cause = assertIsInstanceOf(HttpOperationFailedException.class, e);
    assertEquals(501, cause.getStatusCode());
}
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) Test(org.junit.Test)

Example 7 with HttpOperationFailedException

use of org.apache.camel.http.common.HttpOperationFailedException in project camel by apache.

the class HttpRedirectTest method httpRedirect.

@Test
public void httpRedirect() throws Exception {
    String uri = "http4://" + localServer.getInetAddress().getHostName() + ":" + localServer.getLocalPort() + "/test?httpClient.redirectsEnabled=false&httpClient.socketTimeout=60000&httpClient.connectTimeout=60000" + "&httpClient.staleConnectionCheckEnabled=false";
    Exchange out = template.request(uri, new Processor() {

        public void process(Exchange exchange) throws Exception {
        // no data
        }
    });
    assertNotNull(out);
    HttpOperationFailedException cause = out.getException(HttpOperationFailedException.class);
    assertNotNull(cause);
    assertEquals(HttpStatus.SC_MOVED_PERMANENTLY, cause.getStatusCode());
    assertEquals("http4://" + localServer.getInetAddress().getHostName() + ":" + localServer.getLocalPort() + "/someplaceelse", cause.getRedirectLocation());
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) HttpOperationFailedException(org.apache.camel.http.common.HttpOperationFailedException) IOException(java.io.IOException) HttpException(org.apache.http.HttpException) HttpOperationFailedException(org.apache.camel.http.common.HttpOperationFailedException) Test(org.junit.Test)

Example 8 with HttpOperationFailedException

use of org.apache.camel.http.common.HttpOperationFailedException in project camel by apache.

the class HttpRedirectTest method testHttpRedirectFromCamelRoute.

@Test
public void testHttpRedirectFromCamelRoute() throws Exception {
    MockEndpoint errorEndpoint = context.getEndpoint("mock:error", MockEndpoint.class);
    errorEndpoint.expectedMessageCount(1);
    MockEndpoint resultEndpoint = context.getEndpoint("mock:result", MockEndpoint.class);
    resultEndpoint.expectedMessageCount(0);
    try {
        template.requestBody("direct:start", "Hello World", String.class);
        fail("Should have thrown an exception");
    } catch (RuntimeCamelException e) {
        HttpOperationFailedException cause = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause());
        assertEquals(302, cause.getStatusCode());
    }
    errorEndpoint.assertIsSatisfied();
    resultEndpoint.assertIsSatisfied();
}
Also used : MockEndpoint(org.apache.camel.component.mock.MockEndpoint) HttpOperationFailedException(org.apache.camel.http.common.HttpOperationFailedException) RuntimeCamelException(org.apache.camel.RuntimeCamelException) Test(org.junit.Test)

Example 9 with HttpOperationFailedException

use of org.apache.camel.http.common.HttpOperationFailedException in project camel by apache.

the class HttpReturnFaultTest method testHttpFault.

@Test
public void testHttpFault() throws Exception {
    Exchange exchange = template.request("http://localhost:{{port}}/test", new Processor() {

        @Override
        public void process(Exchange exchange) throws Exception {
            exchange.getIn().setBody("Hello World!");
        }
    });
    assertTrue(exchange.isFailed());
    HttpOperationFailedException exception = exchange.getException(HttpOperationFailedException.class);
    assertNotNull(exception);
    assertEquals("This is a fault", exception.getResponseBody());
    assertEquals(500, exception.getStatusCode());
}
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) Test(org.junit.Test)

Example 10 with HttpOperationFailedException

use of org.apache.camel.http.common.HttpOperationFailedException in project camel by apache.

the class JettyAsyncDefaultContinuationTimeoutTest method testJettyAsyncTimeout.

@Test
public void testJettyAsyncTimeout() throws Exception {
    getMockEndpoint("mock:result").expectedBodiesReceived("Bye World");
    StopWatch watch = new StopWatch();
    try {
        template.requestBody("http://localhost:{{port}}/myservice", null, String.class);
        fail("Should have thrown an exception");
    } catch (CamelExecutionException e) {
        log.info("Timeout hit and client got reply with failure status code");
        long taken = watch.stop();
        HttpOperationFailedException cause = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause());
        assertEquals(504, cause.getStatusCode());
        // should be approx 30-34 sec.
        assertTrue("Timeout should occur faster than " + taken, taken < 34000);
    }
    assertMockEndpointsSatisfied(2, TimeUnit.MINUTES);
}
Also used : CamelExecutionException(org.apache.camel.CamelExecutionException) HttpOperationFailedException(org.apache.camel.http.common.HttpOperationFailedException) StopWatch(org.apache.camel.util.StopWatch) BaseJettyTest(org.apache.camel.component.jetty.BaseJettyTest) Test(org.junit.Test)

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