Search in sources :

Example 21 with HttpOperationFailedException

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

the class JettyHttpProderReturnFaultTest method testHttpFault.

@Test
public void testHttpFault() throws Exception {
    // these tests does not run well on Windows
    if (isPlatform("windows")) {
        return;
    }
    // give Jetty time to startup properly
    Thread.sleep(1000);
    Exchange exchange = template.request("jetty://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) BaseJettyTest(org.apache.camel.component.jetty.BaseJettyTest) Test(org.junit.Test)

Example 22 with HttpOperationFailedException

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

the class JettyAsyncContinuationTimeoutTest 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 3-4 sec.
        assertTrue("Timeout should occur faster than " + taken, taken < 4500);
    }
    assertMockEndpointsSatisfied();
}
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)

Example 23 with HttpOperationFailedException

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

the class JettyHttpProducerSuspendResumeTest method testJettySuspendResume.

@Test
public void testJettySuspendResume() throws Exception {
    // these tests does not run well on Windows
    if (isPlatform("windows")) {
        return;
    }
    context.getShutdownStrategy().setTimeout(50);
    String reply = template.requestBody(serverUri, "World", String.class);
    assertEquals("Bye World", reply);
    // now suspend jetty
    HttpConsumer consumer = (HttpConsumer) context.getRoute("route1").getConsumer();
    assertNotNull(consumer);
    // suspend
    consumer.suspend();
    try {
        template.requestBody(serverUri, "Moon", String.class);
        fail("Should throw exception");
    } catch (Exception e) {
        HttpOperationFailedException cause = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause());
        assertEquals(503, cause.getStatusCode());
    }
    // resume
    consumer.resume();
    // and send request which should be processed
    reply = template.requestBody(serverUri, "Moon", String.class);
    assertEquals("Bye Moon", reply);
}
Also used : HttpOperationFailedException(org.apache.camel.http.common.HttpOperationFailedException) HttpConsumer(org.apache.camel.http.common.HttpConsumer) HttpOperationFailedException(org.apache.camel.http.common.HttpOperationFailedException) BaseJettyTest(org.apache.camel.component.jetty.BaseJettyTest) Test(org.junit.Test)

Example 24 with HttpOperationFailedException

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

the class JettyHttpProducerSuspendTest method testJettySuspend.

@Test
public void testJettySuspend() throws Exception {
    // these tests does not run well on Windows
    if (isPlatform("windows")) {
        return;
    }
    context.getShutdownStrategy().setTimeout(50);
    String reply = template.requestBody(serverUri, "World", String.class);
    assertEquals("Bye World", reply);
    // now suspend jetty
    HttpConsumer consumer = (HttpConsumer) context.getRoute("route1").getConsumer();
    assertNotNull(consumer);
    // suspend
    consumer.suspend();
    try {
        template.requestBody(serverUri, "Moon", String.class);
        fail("Should throw exception");
    } catch (Exception e) {
        HttpOperationFailedException cause = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause());
        assertEquals(503, cause.getStatusCode());
    }
}
Also used : HttpOperationFailedException(org.apache.camel.http.common.HttpOperationFailedException) HttpConsumer(org.apache.camel.http.common.HttpConsumer) HttpOperationFailedException(org.apache.camel.http.common.HttpOperationFailedException) BaseJettyTest(org.apache.camel.component.jetty.BaseJettyTest) Test(org.junit.Test)

Example 25 with HttpOperationFailedException

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

the class HttpJavaBodyTest method testNotAllowed.

@Test
public void testNotAllowed() throws Exception {
    HttpCommonComponent jetty = context.getComponent("jetty", HttpCommonComponent.class);
    jetty.setAllowJavaSerializedObject(false);
    HttpComponent http = context.getComponent("http", HttpComponent.class);
    http.setAllowJavaSerializedObject(true);
    context.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            from("jetty:http://localhost:{{port}}/myapp/myservice").process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    String body = exchange.getIn().getBody(String.class);
                    assertNotNull(body);
                    assertEquals("Hello World", body);
                    MyCoolBean reply = new MyCoolBean(456, "Camel rocks");
                    exchange.getOut().setBody(reply);
                    exchange.getOut().setHeader(Exchange.CONTENT_TYPE, HttpConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT);
                }
            });
        }
    });
    context.start();
    MyCoolBean cool = new MyCoolBean(123, "Camel");
    try {
        template.requestBodyAndHeader("http://localhost:{{port}}/myapp/myservice", cool, Exchange.CONTENT_TYPE, HttpConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT, MyCoolBean.class);
        fail("Should fail");
    } catch (CamelExecutionException e) {
        HttpOperationFailedException cause = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause());
        assertEquals(415, cause.getStatusCode());
    }
}
Also used : Exchange(org.apache.camel.Exchange) CamelExecutionException(org.apache.camel.CamelExecutionException) Processor(org.apache.camel.Processor) RouteBuilder(org.apache.camel.builder.RouteBuilder) HttpCommonComponent(org.apache.camel.http.common.HttpCommonComponent) HttpOperationFailedException(org.apache.camel.http.common.HttpOperationFailedException) HttpComponent(org.apache.camel.component.http.HttpComponent) HttpOperationFailedException(org.apache.camel.http.common.HttpOperationFailedException) CamelExecutionException(org.apache.camel.CamelExecutionException) 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