Updated Branches: refs/heads/master 22eb2803e -> d1bffa439
CAMEL-6327: More work on new camel-netty-http component. Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/d1bffa43 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/d1bffa43 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/d1bffa43 Branch: refs/heads/master Commit: d1bffa439772f2b2bdb37b1de39bed4cbb91d9fb Parents: 22eb280 Author: Claus Ibsen <davscl...@apache.org> Authored: Wed May 8 15:20:01 2013 +0200 Committer: Claus Ibsen <davscl...@apache.org> Committed: Wed May 8 15:20:01 2013 +0200 ---------------------------------------------------------------------- .../NettyHttpGetWithParamAsExchangeHeaderTest.java | 127 +++++++++++++++ .../netty/http/NettyHttpGetWithParamTest.java | 78 +++++++++ .../netty/http/NettyHttpHandle404Test.java | 92 +++++++++++ .../http/NettyHttpOnExceptionHandledTest.java | 56 +++++++ .../netty/http/NettyHttpSuspendResumeTest.java | 71 ++++++++ 5 files changed, 424 insertions(+), 0 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/d1bffa43/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpGetWithParamAsExchangeHeaderTest.java ---------------------------------------------------------------------- diff --git a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpGetWithParamAsExchangeHeaderTest.java b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpGetWithParamAsExchangeHeaderTest.java new file mode 100644 index 0000000..ebb15a2 --- /dev/null +++ b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpGetWithParamAsExchangeHeaderTest.java @@ -0,0 +1,127 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.netty.http; + +import org.apache.camel.Exchange; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.junit.Ignore; +import org.junit.Test; + +public class NettyHttpGetWithParamAsExchangeHeaderTest extends BaseNettyTest { + + private String serverUri = "netty-http:http://localhost:" + getPort() + "/myservice"; + + @Test + public void testHttpGetWithParamsViaURI() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:result"); + mock.expectedMessageCount(1); + mock.expectedHeaderReceived("one", "einz"); + mock.expectedHeaderReceived("two", "twei"); + mock.expectedHeaderReceived(Exchange.HTTP_METHOD, "GET"); + + template.requestBody(serverUri + "?one=einz&two=twei", null, Object.class); + + assertMockEndpointsSatisfied(); + } + + @Test + public void testHttpGetWithUTF8EncodedParamsViaURI() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:result"); + mock.expectedMessageCount(1); + mock.expectedHeaderReceived("message", "Keine g\u00FCltige GPS-Daten!"); + mock.expectedHeaderReceived(Exchange.HTTP_METHOD, "GET"); + + template.requestBody(serverUri + "?message=Keine%20g%C3%BCltige%20GPS-Daten!", null, Object.class); + + assertMockEndpointsSatisfied(); + } + + @Test + @Ignore + public void testHttpGetWithISO8859EncodedParamsViaURI() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:result"); + mock.expectedMessageCount(1); + mock.expectedHeaderReceived("message", "Keine g\u00C6ltige GPS-Daten!"); + mock.expectedHeaderReceived(Exchange.HTTP_METHOD, "GET"); + + template.requestBody(serverUri + "?message=Keine+g%C6ltige+GPS-Daten%21", null, Object.class); + + assertMockEndpointsSatisfied(); + } + + @Test + public void testHttpGetWithSpaceInParams() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:result"); + mock.expectedMessageCount(1); + mock.expectedHeaderReceived("message", " World"); + mock.expectedHeaderReceived(Exchange.HTTP_METHOD, "GET"); + + // parameter starts with a space using %2B as decimal encoded + template.requestBody(serverUri + "?message=%2BWorld", null, Object.class); + + assertMockEndpointsSatisfied(); + } + + @Test + public void testHttpGetWithSpaceAsPlusInParams() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:result"); + mock.expectedMessageCount(1); + mock.expectedHeaderReceived("message", " World"); + mock.expectedHeaderReceived(Exchange.HTTP_METHOD, "GET"); + + // parameter starts with a space using + decoded + template.requestBody(serverUri + "?message=+World", null, Object.class); + + assertMockEndpointsSatisfied(); + } + + @Test + @Ignore("HTTP_QUERY not yet supported") + public void testHttpGetWithParamsViaHeader() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:result"); + mock.expectedMessageCount(1); + mock.expectedHeaderReceived("one", "uno"); + mock.expectedHeaderReceived("two", "dos"); + mock.expectedHeaderReceived(Exchange.HTTP_METHOD, "GET"); + + template.requestBodyAndHeader(serverUri, null, Exchange.HTTP_QUERY, "one=uno&two=dos"); + + assertMockEndpointsSatisfied(); + } + + @Test + public void testHttpPost() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:result"); + mock.expectedMessageCount(1); + mock.expectedBodiesReceived("Hello World"); + mock.expectedHeaderReceived(Exchange.HTTP_METHOD, "POST"); + + template.requestBody(serverUri, "Hello World"); + + assertMockEndpointsSatisfied(); + } + + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + public void configure() throws Exception { + from(serverUri).to("mock:result"); + } + }; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/d1bffa43/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpGetWithParamTest.java ---------------------------------------------------------------------- diff --git a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpGetWithParamTest.java b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpGetWithParamTest.java new file mode 100644 index 0000000..0f67128 --- /dev/null +++ b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpGetWithParamTest.java @@ -0,0 +1,78 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.netty.http; + +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.junit.Ignore; +import org.junit.Test; + +public class NettyHttpGetWithParamTest extends BaseNettyTest { + + private String serverUri = "netty-http:http://localhost:" + getPort() + "/myservice"; + private MyParamsProcessor processor = new MyParamsProcessor(); + + @Test + public void testHttpGetWithParamsViaURI() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:result"); + mock.expectedBodiesReceived("Bye World"); + mock.expectedHeaderReceived("one", "eins"); + mock.expectedHeaderReceived("two", "zwei"); + + template.requestBody(serverUri + "?one=uno&two=dos", (Object) null); + + assertMockEndpointsSatisfied(); + } + + @Test + @Ignore("HTTP_QUERY not supported") + public void testHttpGetWithParamsViaHeader() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:result"); + mock.expectedBodiesReceived("Bye World"); + mock.expectedHeaderReceived("one", "eins"); + mock.expectedHeaderReceived("two", "zwei"); + + template.requestBodyAndHeader(serverUri, null, Exchange.HTTP_QUERY, "one=uno&two=dos"); + + assertMockEndpointsSatisfied(); + } + + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + public void configure() throws Exception { + from(serverUri).process(processor).to("mock:result"); + } + }; + } + + private static class MyParamsProcessor implements Processor { + public void process(Exchange exchange) throws Exception { + NettyHttpMessage message = exchange.getIn(NettyHttpMessage.class); + assertNotNull(message.getHttpRequest()); + + String uri = message.getHttpRequest().getUri(); + assertTrue(uri.endsWith("one=uno&two=dos")); + + exchange.getOut().setBody("Bye World"); + exchange.getOut().setHeader("one", "eins"); + exchange.getOut().setHeader("two", "zwei"); + } + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/d1bffa43/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpHandle404Test.java ---------------------------------------------------------------------- diff --git a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpHandle404Test.java b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpHandle404Test.java new file mode 100644 index 0000000..b6571e11 --- /dev/null +++ b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpHandle404Test.java @@ -0,0 +1,92 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.netty.http; + +import java.nio.charset.Charset; + +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.processor.aggregate.AggregationStrategy; +import org.junit.Test; + +public class NettyHttpHandle404Test extends BaseNettyTest { + + public String getProducerUrl() { + return "netty-http:http://localhost:{{port}}/myserver?user=Camel"; + } + + @Test + public void testSimulate404() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:result"); + mock.expectedBodiesReceived("Page not found"); + mock.expectedHeaderReceived(Exchange.HTTP_RESPONSE_CODE, 404); + + template.sendBody("direct:start", "Hello World"); + + assertMockEndpointsSatisfied(); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + // disable error handling + errorHandler(noErrorHandler()); + + from("direct:start").enrich("direct:tohttp", new AggregationStrategy() { + public Exchange aggregate(Exchange original, Exchange resource) { + // get the response code + Integer code = resource.getIn().getHeader(Exchange.HTTP_RESPONSE_CODE, Integer.class); + assertEquals(404, code.intValue()); + return resource; + } + }).to("mock:result"); + + // use this sub route as indirection to handle the HttpOperationFailedException + // and set the data back as data on the exchange to not cause the exception to be thrown + from("direct:tohttp") + .doTry() + .to(getProducerUrl()) + .doCatch(NettyHttpOperationFailedException.class) + .process(new Processor() { + public void process(Exchange exchange) { + // copy the caused exception values to the exchange as we want the response in the regular exchange + // instead as an exception that will get thrown and thus the route breaks + NettyHttpOperationFailedException cause = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, NettyHttpOperationFailedException.class); + exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, cause.getStatusCode()); + exchange.getOut().setBody(cause.getResponse().getContent().toString(Charset.defaultCharset())); + } + }) + .end(); + + + // this is our jetty server where we simulate the 404 + from("netty-http:http://localhost:{{port}}/myserver") + .process(new Processor() { + public void process(Exchange exchange) throws Exception { + exchange.getOut().setBody("Page not found"); + exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, 404); + } + }); + } + }; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/d1bffa43/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpOnExceptionHandledTest.java ---------------------------------------------------------------------- diff --git a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpOnExceptionHandledTest.java b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpOnExceptionHandledTest.java new file mode 100644 index 0000000..51d1250 --- /dev/null +++ b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpOnExceptionHandledTest.java @@ -0,0 +1,56 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.netty.http; + +import org.apache.camel.Exchange; +import org.apache.camel.builder.RouteBuilder; +import org.junit.Test; + +public class NettyHttpOnExceptionHandledTest extends BaseNettyTest { + + @Test + public void testOnExceptionHandled() throws Exception { + Exchange reply = template.request("netty-http:http://localhost:{{port}}/myserver?throwExceptionOnFailure=false", null); + + assertNotNull(reply); + assertEquals("Dude something went wrong", reply.getOut().getBody(String.class)); + assertEquals(500, reply.getOut().getHeader(Exchange.HTTP_RESPONSE_CODE)); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + // START SNIPPET: e1 + from("netty-http:http://localhost:{{port}}/myserver") + // use onException to catch all exceptions and return a custom reply message + .onException(Exception.class) + .handled(true) + // create a custom failure response + .transform(constant("Dude something went wrong")) + // we must remember to set error code 500 as handled(true) + // otherwise would let Camel thing its a OK response (200) + .setHeader(Exchange.HTTP_RESPONSE_CODE, constant(500)) + .end() + // now just force an exception immediately + .throwException(new IllegalArgumentException("I cannot do this")); + // END SNIPPET: e1 + } + }; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/d1bffa43/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpSuspendResumeTest.java ---------------------------------------------------------------------- diff --git a/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpSuspendResumeTest.java b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpSuspendResumeTest.java new file mode 100644 index 0000000..255aff6 --- /dev/null +++ b/components/camel-netty-http/src/test/java/org/apache/camel/component/netty/http/NettyHttpSuspendResumeTest.java @@ -0,0 +1,71 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.netty.http; + +import org.apache.camel.builder.RouteBuilder; +import org.junit.Ignore; +import org.junit.Test; + +@Ignore +public class NettyHttpSuspendResumeTest extends BaseNettyTest { + + private String serverUri = "netty-http:http://localhost:" + getPort() + "/cool?chunked=false"; + + @Test + public void testNettySuspendResume() throws Exception { + context.getShutdownStrategy().setTimeout(50); + + String reply = template.requestBody(serverUri, "World", String.class); + assertEquals("Bye World", reply); + + // now suspend netty + NettyHttpConsumer consumer = (NettyHttpConsumer) context.getRoute("route1").getConsumer(); + assertNotNull(consumer); + + // suspend + consumer.suspend(); + + try { + template.requestBody(serverUri, "Moon", String.class); + fail("Should throw exception"); + } catch (Exception e) { + NettyHttpOperationFailedException cause = assertIsInstanceOf(NettyHttpOperationFailedException.class, e.getCause()); + assertEquals(503, cause.getStatusCode()); + } + + // resume + consumer.resume(); + + Thread.sleep(2000); + + // and send request which should be processed + reply = template.requestBody(serverUri, "Moon", String.class); + assertEquals("Bye Moon", reply); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from(serverUri) + .transform(body().prepend("Bye ")); + } + }; + } + +}