@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()); }
@Test public void testRestletPostPojoError() throws Exception { getMockEndpoint("mock:input").expectedMessageCount(0); getMockEndpoint("mock:error").expectedMessageCount(1); String body = "This is not json"; try { template.sendBody("http://localhost:" + portNum + "/users/new", body); fail("Should have thrown an exception"); } catch (CamelExecutionException e) { HttpOperationFailedException cause = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause()); assertEquals(400, cause.getStatusCode()); assertEquals("Invalid json data", cause.getResponseBody()); } assertMockEndpointsSatisfied(); }
@Test public void testResponseBodyWhenError() throws Exception { try { template.requestBody("http://localhost:{{port}}/myapp/myservice", "bookid=123"); fail("Should have thrown an exception"); } catch (RuntimeCamelException e) { HttpOperationFailedException cause = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause()); assertEquals(500, cause.getStatusCode()); String body = context.getTypeConverter().convertTo(String.class, cause.getResponseBody()); assertTrue(body.indexOf("Damm") > -1); assertTrue(body.indexOf("IllegalArgumentException") > -1); assertNotNull(cause.getResponseHeaders()); String type = cause.getResponseHeaders().get(Exchange.CONTENT_TYPE); assertTrue(type.startsWith("text/plain")); } }
@Test public void test404() throws Exception { // give Jetty time to startup properly Thread.sleep(1000); try { template.sendBody(url, null); fail("Should have thrown exception"); } catch (Exception e) { HttpOperationFailedException cause = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause()); assertEquals(404, cause.getStatusCode()); assertEquals("http://127.0.0.1:" + getPort() + "/bar", cause.getUri()); assertEquals("Page not found", cause.getResponseBody()); assertNotNull(cause.getResponseHeaders()); } }
@Test public void testStreamCacheToFileShouldBeDeletedInCaseOfException() throws Exception { try { template.requestBody("direct:start", null, String.class); fail("Should have thrown an exception"); } catch (CamelExecutionException e) { HttpOperationFailedException hofe = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause()); String s = context.getTypeConverter().convertTo(String.class, hofe.getResponseBody()); assertEquals("Response body", responseBody, s); } // the temporary files should have been deleted File file = new File("target/cachedir"); String[] files = file.list(); assertEquals("There should be no files", 0, files.length); }
@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(); }
@Test public void testJettySuspend() throws Exception { 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()); } }
@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(503, cause.getStatusCode()); // should be approx 3-4 sec. assertTrue("Timeout should occur faster than " + taken, taken < 4500); } assertMockEndpointsSatisfied(); }
@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(503, cause.getStatusCode()); // should be approx 30-34 sec. assertTrue("Timeout should occur faster than " + taken, taken < 34000); } assertMockEndpointsSatisfied(2, TimeUnit.MINUTES); }
@Test public void testNoDataDefaultIsGet() throws Exception { HttpComponent component = context.getComponent("http", HttpComponent.class); HttpEndpoint endpoiont = (HttpEndpoint) component.createEndpoint("http://www.google.com"); MyHttpProducer producer = new MyHttpProducer(endpoiont, "GET", null); Exchange exchange = producer.createExchange(); exchange.getIn().setBody(null); try { producer.process(exchange); fail("Should have thrown HttpOperationFailedException"); } catch (HttpOperationFailedException e) { assertEquals(500, e.getStatusCode()); } producer.stop(); }
@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(); }
@Test public void testWithMethodPostInHeader() 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(""); exchange.getIn().setHeader(Exchange.HTTP_METHOD, POST); try { producer.process(exchange); fail("Should have thrown HttpOperationFailedException"); } catch (HttpOperationFailedException e) { assertEquals(500, e.getStatusCode()); } producer.stop(); }
@Test public void testWithMethodGetInHeader() throws Exception { HttpComponent component = context.getComponent("http", HttpComponent.class); HttpEndpoint endpoiont = (HttpEndpoint) component.createEndpoint("http://www.google.com"); MyHttpProducer producer = new MyHttpProducer(endpoiont, "GET", null); Exchange exchange = producer.createExchange(); exchange.getIn().setBody(""); exchange.getIn().setHeader(Exchange.HTTP_METHOD, GET); try { producer.process(exchange); fail("Should have thrown HttpOperationFailedException"); } catch (HttpOperationFailedException e) { assertEquals(500, e.getStatusCode()); } producer.stop(); }
@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(); }
@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(); }
@Test public void testWithQueryInHeaderOverrideEndpoint() throws Exception { HttpComponent component = context.getComponent("http", HttpComponent.class); HttpEndpoint endpoiont = (HttpEndpoint) component.createEndpoint("http://www.google.com?q=Donkey"); 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(); }
@Test public void testSparkGet() throws Exception { getMockEndpoint("mock:foo").expectedMessageCount(1); try { template.requestBodyAndHeader("http://localhost:" + getPort() + "/hello", null, "Accept", "text/plain", String.class); fail("Should fail"); } catch (CamelExecutionException e) { HttpOperationFailedException cause = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause()); assertEquals(404, cause.getStatusCode()); } String out2 = template.requestBodyAndHeader("http://localhost:" + getPort() + "/hello", null, "Accept", "application/json", String.class); assertEquals("{ \"reply\": \"Bye World\" }", out2); assertMockEndpointsSatisfied(); }
@Override public void configure() throws Exception { getContext().setTracing(true); // general error handler errorHandler(defaultErrorHandler() .maximumRedeliveries(5) .redeliveryDelay(2000) .retryAttemptedLogLevel(LoggingLevel.WARN)); // in case of a http exception then retry at most 3 times // and if exhausted then upload using ftp instead onException(HttpOperationFailedException.class).maximumRedeliveries(3) .handled(true) // use file instead of ftp as its easier to play with .to("file:target/ftp/upload"); // poll files every 5th second and send them to the HTTP server from("file:target/rider?delay=5000&readLock=none") .to("http://localhost:8765/rider"); }
@Override public void configure() throws Exception { getContext().setTracing(true); // general error handler errorHandler(defaultErrorHandler() .maximumRedeliveries(5) .redeliveryDelay(2000) .retryAttemptedLogLevel(LoggingLevel.WARN)); // in case of a http exception then retry at most 3 times // and if exhausted then upload using ftp instead onException(IOException.class, HttpOperationFailedException.class) .maximumRedeliveries(3) .handled(true) .to(ftp); // poll files send them to the HTTP server from(file).to(http); }
@Test public void testInvalidJson() throws Exception { try { String out = fluentTemplate().to("undertow:http://localhost:" + port1 + "/cafe/menu/items/1") .withHeader(Exchange.HTTP_METHOD, "PUT") .withBody("This is not JSON format") .request(String.class); fail("Expected exception to be thrown"); } catch (CamelExecutionException e) { HttpOperationFailedException httpException = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause()); assertEquals(400, httpException.getStatusCode()); assertEquals("text/plain", httpException.getResponseHeaders().get(Exchange.CONTENT_TYPE)); assertEquals("Invalid json data", httpException.getResponseBody()); } }
@Test public void testHttpJmsAsync() throws Exception { try { template.requestBody("http://0.0.0.0:" + getPort() + "/myservice", "Hello World", String.class); fail("Should have thrown exception"); } catch (CamelExecutionException e) { HttpOperationFailedException cause = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause()); assertEquals(503, cause.getStatusCode()); } }
@Test public void testRoutingSOAPFault() throws Exception { try { template.sendBody("http://localhost:" + getPort2() + "/" + getClass().getSimpleName() + "/CamelContext/RouterPort/", testDocLitFaultBody); fail("Should get an exception here."); } catch (RuntimeCamelException exception) { assertTrue("It should get the response error", exception.getCause() instanceof HttpOperationFailedException); assertEquals("Get a wrong response code", ((HttpOperationFailedException)exception.getCause()).getStatusCode(), 500); } }
@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()); }
@Test public void httpGetWhichReturnsHttp501ShouldThrowAnExceptionWithIgnoreResponseBody() throws Exception { Exchange reply = template.request("http4://" + localServer.getInetAddress().getHostName() + ":" + localServer.getLocalPort() + "/XXX?throwExceptionOnFailure=true&ignoreResponseBody=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()); }
@Test public void failsOnPrefixPath() throws Exception { try { template.requestBody("http://localhost:{{port}}/myapp", "Hello Camel!"); fail("Should fail, something is wrong"); } catch (CamelExecutionException ex) { HttpOperationFailedException cause = assertIsInstanceOf(HttpOperationFailedException.class, ex.getCause()); assertEquals(404, cause.getStatusCode()); } }
@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()); } }
@Test public void testDefaultJettyHttpBinding() throws Exception { Object out = template.requestBody("jetty:http://localhost:{{port}}/myapp/myservice?jettyHttpBindingRef=default", "Hello World"); assertEquals("Bye World", context.getTypeConverter().convertTo(String.class, out)); try { template.requestBody("jetty:http://localhost:{{port}}/myapp/myotherservice", "Hello World"); fail(); } catch (CamelExecutionException e) { assertNotNull(e.getCause()); assertTrue(e.getCause() instanceof HttpOperationFailedException); assertFalse("Not exactly the message the server returned.".equals(((HttpOperationFailedException) e.getCause()).getResponseBody())); } }
@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()); } }
@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); }
@Test public void testHttpRedirect() throws Exception { try { template.requestBody("jetty:http://localhost:{{port}}/test", "Hello World", String.class); fail("Should have thrown an exception"); } catch (RuntimeCamelException e) { HttpOperationFailedException cause = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause()); assertEquals(301, cause.getStatusCode()); assertEquals(true, cause.isRedirectError()); assertEquals(true, cause.hasRedirectLocation()); assertEquals("http://localhost:" + getPort() + "/test", cause.getUri()); assertEquals("http://localhost:" + getPort() + "/newtest", cause.getRedirectLocation()); } }
@Test public void testHttpOperationsFailedExceptionUri() throws Exception { try { template.requestBodyAndHeader("http://localhost:{{port}}/foo?bar=123", null, "foo", 123); fail("Should have thrown an exception"); } catch (RuntimeCamelException e) { HttpOperationFailedException cause = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause()); assertEquals(500, cause.getStatusCode()); assertEquals("http://localhost:" + getPort() + "/foo?bar=123", cause.getUri()); } }
@Test public void testHttpRedirect() throws Exception { try { template.requestBody("http://localhost:{{port}}/test", "Hello World", String.class); fail("Should have thrown an exception"); } catch (RuntimeCamelException e) { HttpOperationFailedException cause = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause()); assertEquals(301, cause.getStatusCode()); assertEquals(true, cause.isRedirectError()); assertEquals(true, cause.hasRedirectLocation()); assertEquals("http://localhost:" + getPort() + "/test", cause.getUri()); assertEquals("http://localhost:" + getPort() + "/newtest", cause.getRedirectLocation()); } }
@Test public void testAuthMethodPriorityNTLM() throws Exception { try { template.requestBody("http://localhost:{{port}}/test?authMethod=Basic&authMethodPriority=NTLM&authUsername=donald&authPassword=duck", "Hello World", String.class); fail("Should have thrown exception"); } catch (RuntimeCamelException e) { HttpOperationFailedException cause = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause()); assertEquals(401, cause.getStatusCode()); } }
@Test public void testHttpClientNoProxyException() throws Exception { try { template.requestBody("direct:cool", "Kaboom", String.class); fail("Should have thrown exception"); } catch (CamelExecutionException e) { HttpOperationFailedException cause = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause()); assertEquals(500, cause.getStatusCode()); assertNotNull(cause.getResponseBody()); assertTrue(cause.getResponseBody().contains("MyAppException")); } }
@Test public void testHttpClientProxyException() throws Exception { MyCoolService proxy = new ProxyBuilder(context).endpoint("direct:cool").build(MyCoolService.class); try { proxy.hello("Kaboom"); fail("Should have thrown exception"); } catch (UndeclaredThrowableException e) { HttpOperationFailedException cause = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause()); assertEquals(500, cause.getStatusCode()); assertNotNull(cause.getResponseBody()); assertTrue(cause.getResponseBody().contains("MyAppException")); } }
@Test public void testNoOk() throws Exception { byte[] data = "Hello World".getBytes(); try { template.requestBody("http://localhost:{{port}}/test?okStatusCodeRange=200-200", data, String.class); fail("Should have thrown exception"); } catch (CamelExecutionException e) { HttpOperationFailedException cause = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause()); assertEquals(209, cause.getStatusCode()); assertEquals("Not allowed", cause.getResponseBody()); } }
@Test public void testHttpBasicAuthInvalidPassword() throws Exception { try { template.requestBody("http://localhost:{{port}}/test?authMethod=Basic&authUsername=donald&authPassword=sorry", "Hello World", String.class); fail("Should have thrown exception"); } catch (RuntimeCamelException e) { HttpOperationFailedException cause = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause()); assertEquals(401, cause.getStatusCode()); } }
@Test public void testJettySuspendResume() throws Exception { 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); }
@Test public void testHttpRedirectNoLocation() throws Exception { try { template.requestBody("http://localhost:{{port}}/test", "Hello World", String.class); fail("Should have thrown an exception"); } catch (RuntimeCamelException e) { HttpOperationFailedException cause = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause()); assertEquals(302, cause.getStatusCode()); assertEquals(true, cause.isRedirectError()); assertEquals(false, cause.hasRedirectLocation()); assertEquals(null, cause.getRedirectLocation()); } }