http://git-wip-us.apache.org/repos/asf/camel/blob/a59becd7/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettyHttpGetWithParamAsExchangeHeaderTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettyHttpGetWithParamAsExchangeHeaderTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettyHttpGetWithParamAsExchangeHeaderTest.java new file mode 100644 index 0000000..acca07d --- /dev/null +++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettyHttpGetWithParamAsExchangeHeaderTest.java @@ -0,0 +1,128 @@ +/** + * 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.jetty; + +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; + +/** + * Unit test to verify that we can have URI options for external system (endpoint is lenient) + */ +public class JettyHttpGetWithParamAsExchangeHeaderTest extends BaseJettyTest { + + private String serverUri = "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 + 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("jetty:" + serverUri).to("mock:result"); + } + }; + } +}
http://git-wip-us.apache.org/repos/asf/camel/blob/a59becd7/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettyHttpGetWithParamTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettyHttpGetWithParamTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettyHttpGetWithParamTest.java new file mode 100644 index 0000000..c2aeecb --- /dev/null +++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettyHttpGetWithParamTest.java @@ -0,0 +1,93 @@ +/** + * 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.jetty; + +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.http.HttpMessage; +import org.apache.camel.component.mock.MockEndpoint; +import org.junit.Test; + +/** + * Unit test to verify that we can have URI options for external system (endpoint is lenient) + */ +public class JettyHttpGetWithParamTest extends BaseJettyTest { + + private String serverUri = "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", "Hello World"); + + assertMockEndpointsSatisfied(); + } + + @Test + 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, "Hello World", Exchange.HTTP_QUERY, "one=uno&two=dos"); + + assertMockEndpointsSatisfied(); + } + + @Test + public void testHttpGetFromOtherRoute() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:result"); + mock.expectedBodiesReceived("Bye World"); + mock.expectedHeaderReceived("one", "eins"); + mock.expectedHeaderReceived("two", "zwei"); + + template.requestBodyAndHeader("direct:start", "Hello World", "parameters", "one=uno&two=dos"); + + assertMockEndpointsSatisfied(); + } + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + public void configure() throws Exception { + from("jetty:" + serverUri).process(processor).to("mock:result"); + from("direct:start") + .setHeader(Exchange.HTTP_METHOD, constant("GET")) + .setHeader(Exchange.HTTP_URI, simple(serverUri + "?${in.headers.parameters}")) + .to("http://example"); + } + }; + } + + private static class MyParamsProcessor implements Processor { + public void process(Exchange exchange) throws Exception { + HttpMessage message = (HttpMessage)exchange.getIn(); + assertNotNull(message.getRequest()); + assertEquals("uno", message.getRequest().getParameter("one")); + assertEquals("dos", message.getRequest().getParameter("two")); + + 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/a59becd7/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettyHttpHeadersTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettyHttpHeadersTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettyHttpHeadersTest.java new file mode 100644 index 0000000..31b6e45 --- /dev/null +++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettyHttpHeadersTest.java @@ -0,0 +1,54 @@ +/** + * 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.jetty; + +import org.apache.camel.Exchange; +import org.apache.camel.builder.RouteBuilder; +import org.junit.Test; + +public class JettyHttpHeadersTest extends BaseJettyTest { + + @Test + public void testHttpHeaders() throws Exception { + getMockEndpoint("mock:input").expectedBodiesReceived("Hello World"); + getMockEndpoint("mock:input").expectedHeaderReceived("beer", "yes"); + getMockEndpoint("mock:input").expectedHeaderReceived(Exchange.HTTP_METHOD, "POST"); + getMockEndpoint("mock:input").expectedHeaderReceived(Exchange.HTTP_URL, "http://localhost:" + getPort() + "/foo"); + getMockEndpoint("mock:input").expectedHeaderReceived(Exchange.HTTP_URI, "/foo"); + getMockEndpoint("mock:input").expectedHeaderReceived(Exchange.HTTP_QUERY, "beer=yes"); + getMockEndpoint("mock:input").expectedHeaderReceived(Exchange.HTTP_PATH, ""); + + String out = template.requestBody("http://localhost:{{port}}/foo?beer=yes", "Hello World", String.class); + assertEquals("Bye World", out); + + assertMockEndpointsSatisfied(); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("jetty:http://0.0.0.0:{{port}}/foo") + .to("mock:input") + .transform().constant("Bye World"); + } + }; + } + + +} http://git-wip-us.apache.org/repos/asf/camel/blob/a59becd7/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettyImageFileTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettyImageFileTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettyImageFileTest.java new file mode 100644 index 0000000..ed61bda --- /dev/null +++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettyImageFileTest.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.jetty; + +import java.io.File; + +import org.apache.camel.Endpoint; +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.util.MessageHelper; +import org.junit.Test; + +/** + * Unit test for exposing a http server that returns images + */ +public class JettyImageFileTest extends BaseJettyTest { + + private void sendImageContent(boolean usingGZip) throws Exception { + Endpoint endpoint = context.getEndpoint("http://localhost:{{port}}/myapp/myservice"); + Exchange exchange = endpoint.createExchange(); + if (usingGZip) { + exchange.getIn().setHeader(Exchange.CONTENT_ENCODING, "gzip"); + } + template.send(endpoint, exchange); + + assertNotNull(exchange.getOut().getBody()); + assertEquals("Get a wrong content-type ", MessageHelper.getContentType(exchange.getOut()), "image/jpeg"); + } + + @Test + public void testImageContentType() throws Exception { + sendImageContent(false); + } + + @Test + public void testImageContentWithGZip() throws Exception { + sendImageContent(true); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + public void configure() throws Exception { + from("jetty:http://localhost:{{port}}/myapp/myservice").process(new MyImageService()); + } + }; + } + + public class MyImageService implements Processor { + public void process(Exchange exchange) throws Exception { + exchange.getOut().setBody(new File("src/test/data/logo.jpeg")); + exchange.getOut().setHeader("Content-Type", "image/jpeg"); + } + } + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/a59becd7/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettyOnExceptionHandledTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettyOnExceptionHandledTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettyOnExceptionHandledTest.java new file mode 100644 index 0000000..e2fb3c7 --- /dev/null +++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettyOnExceptionHandledTest.java @@ -0,0 +1,59 @@ +/** + * 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.jetty; + +import org.apache.camel.Exchange; +import org.apache.camel.builder.RouteBuilder; +import org.junit.Test; + +/** + * @version + */ +public class JettyOnExceptionHandledTest extends BaseJettyTest { + + @Test + public void testJettyOnException() throws Exception { + Exchange reply = template.request("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("jetty://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/a59becd7/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettyResponseBodyWhenErrorTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettyResponseBodyWhenErrorTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettyResponseBodyWhenErrorTest.java new file mode 100644 index 0000000..80de7d6 --- /dev/null +++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettyResponseBodyWhenErrorTest.java @@ -0,0 +1,64 @@ +/** + * 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.jetty; + +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.RuntimeCamelException; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.http.HttpOperationFailedException; +import org.junit.Test; + +/** + * Unit test for HttpOperationFailedException should contain response body + */ +public class JettyResponseBodyWhenErrorTest extends BaseJettyTest { + + @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 = assertThrowable(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")); + } + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + public void configure() throws Exception { + errorHandler(noErrorHandler()); + from("jetty:http://localhost:{{port}}/myapp/myservice").process(new MyBookService()); + } + }; + } + + public class MyBookService implements Processor { + public void process(Exchange exchange) throws Exception { + throw new IllegalArgumentException("Damm"); + } + } + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/a59becd7/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettyResponseBufferSizeTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettyResponseBufferSizeTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettyResponseBufferSizeTest.java new file mode 100644 index 0000000..84baa2c --- /dev/null +++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettyResponseBufferSizeTest.java @@ -0,0 +1,40 @@ +/** + * 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.jetty; + +import org.apache.camel.builder.RouteBuilder; +import org.junit.Test; + +public class JettyResponseBufferSizeTest extends BaseJettyTest { + + @Test + public void testSimple() throws Exception { + String result = template.requestBody("http://localhost:{{port}}/myapp", "Camel", String.class); + assertEquals("Hello Camel", result); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + public void configure() throws Exception { + from("jetty:http://localhost:{{port}}/myapp?responseBufferSize=65536") + .transform(body().prepend("Hello ")); + } + }; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/a59becd7/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettyRouteTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettyRouteTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettyRouteTest.java new file mode 100644 index 0000000..f7c4464 --- /dev/null +++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettyRouteTest.java @@ -0,0 +1,80 @@ +/** + * 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.jetty; + +import javax.servlet.http.HttpServletRequest; + +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.builder.RouteBuilder; +import org.junit.Test; + +/** + * Unit test for wiki demonstration. + */ +public class JettyRouteTest extends BaseJettyTest { + + @Test + public void testSendToJetty() throws Exception { + Object response = template.requestBody("http://localhost:{{port}}/myapp/myservice", "bookid=123"); + // convert the response to a String + String body = context.getTypeConverter().convertTo(String.class, response); + assertEquals("<html><body>Book 123 is Camel in Action</body></html>", body); + } + + @Test + public void testHttpProxyHostHeader() throws Exception { + String out = template.requestBody("http://localhost:{{port}}/proxyServer", null, String.class); + assertEquals("Get a wrong host header", "localhost:" + getPort2(), out); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + public void configure() throws Exception { + // START SNIPPET: e1 + from("jetty:http://localhost:{{port}}/myapp/myservice").process(new MyBookService()); + // END SNIPPET: e1 + + from("jetty://http://localhost:{{port}}/proxyServer") + .to("http://localhost:{{port2}}/host?bridgeEndpoint=true"); + + from("jetty://http://localhost:{{port2}}/host").transform(header("host")); + } + }; + } + + // START SNIPPET: e2 + public class MyBookService implements Processor { + public void process(Exchange exchange) throws Exception { + // just get the body as a string + String body = exchange.getIn().getBody(String.class); + + // we have access to the HttpServletRequest here and we can grab it if we need it + HttpServletRequest req = exchange.getIn().getBody(HttpServletRequest.class); + assertNotNull(req); + + // for unit testing + assertEquals("bookid=123", body); + + // send a html response + exchange.getOut().setBody("<html><body>Book 123 is Camel in Action</body></html>"); + } + } + // END SNIPPET: e2 + +} http://git-wip-us.apache.org/repos/asf/camel/blob/a59becd7/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettyRouteWithSocketPropertiesTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettyRouteWithSocketPropertiesTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettyRouteWithSocketPropertiesTest.java new file mode 100644 index 0000000..86ef152 --- /dev/null +++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettyRouteWithSocketPropertiesTest.java @@ -0,0 +1,79 @@ +/** + * 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.jetty; + +import java.util.HashMap; +import java.util.Map; + +import javax.servlet.http.HttpServletRequest; + +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.builder.RouteBuilder; +import org.junit.Test; + +/** + * Unit test for wiki demonstration. + */ +public class JettyRouteWithSocketPropertiesTest extends BaseJettyTest { + + @Test + public void testSendToJetty() throws Exception { + Object response = template.requestBody("http://localhost:{{port}}/myapp/myservice", "bookid=123"); + // convert the response to a String + String body = context.getTypeConverter().convertTo(String.class, response); + assertEquals("<html><body>Book 123 is Camel in Action</body></html>", body); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + public void configure() throws Exception { + // START SNIPPET: e1 + // define socket connector properties + Map<String, Object> properties = new HashMap<String, Object>(); + properties.put("acceptors", 4); + properties.put("statsOn", "false"); + properties.put("soLingerTime", "5000"); + + JettyHttpComponent jetty = getContext().getComponent("jetty", JettyHttpComponent.class); + jetty.setSocketConnectorProperties(properties); + // END SNIPPET: e1 + + from("jetty:http://localhost:{{port}}/myapp/myservice").process(new MyBookService()); + } + }; + } + + public class MyBookService implements Processor { + public void process(Exchange exchange) throws Exception { + // just get the body as a string + String body = exchange.getIn().getBody(String.class); + + // we have access to the HttpServletRequest here and we can grab it if we need it + HttpServletRequest req = exchange.getIn().getBody(HttpServletRequest.class); + assertNotNull(req); + + // for unit testing + assertEquals("bookid=123", body); + + // send a html response + exchange.getOut().setBody("<html><body>Book 123 is Camel in Action</body></html>"); + } + } + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/a59becd7/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettyRouteWithUnknownSocketPropertiesTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettyRouteWithUnknownSocketPropertiesTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettyRouteWithUnknownSocketPropertiesTest.java new file mode 100644 index 0000000..8b5fedd --- /dev/null +++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettyRouteWithUnknownSocketPropertiesTest.java @@ -0,0 +1,63 @@ +/** + * 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.jetty; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.camel.builder.RouteBuilder; +import org.eclipse.jetty.server.Server; +import org.junit.Test; + +public class JettyRouteWithUnknownSocketPropertiesTest extends BaseJettyTest { + + @Override + public boolean isUseRouteBuilder() { + return false; + } + + @Test + public void testUnknownProperty() throws Exception { + if (!Server.getVersion().startsWith("8")) { + // SocketConnector props do not work for jetty 9 + return; + } + context.addRoutes(new RouteBuilder() { + @Override + public void configure() throws Exception { + // define socket connector properties + Map<String, Object> properties = new HashMap<String, Object>(); + properties.put("acceptors", 4); + properties.put("statsOn", "false"); + properties.put("soLingerTime", "5000"); + properties.put("doesNotExist", 2000); + + JettyHttpComponent jetty = getContext().getComponent("jetty", JettyHttpComponent.class); + jetty.setSocketConnectorProperties(properties); + + from("jetty:http://localhost:{{port}}/myapp/myservice").to("log:foo"); + } + }); + try { + context.start(); + fail("Should have thrown exception"); + } catch (IllegalArgumentException e) { + assertTrue(e.getMessage().endsWith("Unknown parameters=[{doesNotExist=2000}]")); + } + } + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/a59becd7/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettyRouteWithUnknownSslSocketPropertiesTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettyRouteWithUnknownSslSocketPropertiesTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettyRouteWithUnknownSslSocketPropertiesTest.java new file mode 100644 index 0000000..72d913d --- /dev/null +++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettyRouteWithUnknownSslSocketPropertiesTest.java @@ -0,0 +1,63 @@ +/** + * 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.jetty; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.camel.builder.RouteBuilder; +import org.eclipse.jetty.server.Server; +import org.junit.Test; + +public class JettyRouteWithUnknownSslSocketPropertiesTest extends BaseJettyTest { + + @Override + public boolean isUseRouteBuilder() { + return false; + } + + @Test + public void testUnknownProperty() throws Exception { + if (!Server.getVersion().startsWith("8")) { + // SocketConnector props do not work for jetty 9 + return; + } + context.addRoutes(new RouteBuilder() { + @Override + public void configure() throws Exception { + // define socket connector properties + Map<String, Object> properties = new HashMap<String, Object>(); + properties.put("acceptors", 4); + properties.put("statsOn", "false"); + properties.put("soLingerTime", "5000"); + properties.put("doesNotExist", 2000); + + JettyHttpComponent jetty = getContext().getComponent("jetty", JettyHttpComponent.class); + jetty.setSslSocketConnectorProperties(properties); + + from("jetty:https://localhost:{{port}}/myapp/myservice").to("log:foo"); + } + }); + try { + context.start(); + fail("Should have thrown exception"); + } catch (IllegalArgumentException e) { + assertTrue("Actual message: " + e.getMessage(), e.getMessage().endsWith("Unknown parameters=[{doesNotExist=2000}]")); + } + } + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/a59becd7/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettySessionSupportTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettySessionSupportTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettySessionSupportTest.java new file mode 100644 index 0000000..0af49a0 --- /dev/null +++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettySessionSupportTest.java @@ -0,0 +1,70 @@ +/** + * 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.jetty; + +import org.apache.camel.builder.RouteBuilder; +import org.junit.Test; + +/** + * @version + */ +public class JettySessionSupportTest extends BaseJettyTest { + + @Override + public boolean isUseRouteBuilder() { + return false; + } + + @Test + public void testJettySessionSupportInvalid() throws Exception { + context.addRoutes(new RouteBuilder() { + @Override + public void configure() throws Exception { + from("jetty:http://localhost:{{port}}/hello").to("mock:foo"); + + from("jetty:http://localhost:{{port}}/bye?sessionSupport=true").to("mock:bar"); + } + }); + try { + context.start(); + fail("Should have thrown an exception"); + } catch (IllegalStateException e) { + assertEquals("Server has already been started. Cannot enabled sessionSupport on http:localhost:" + getPort(), e.getMessage()); + } + } + + @Test + public void testJettySessionSupportOk() throws Exception { + context.addRoutes(new RouteBuilder() { + @Override + public void configure() throws Exception { + from("jetty:http://localhost:{{port}}/hello?sessionSupport=true").transform(simple("Bye ${body}")); + } + }); + context.start(); + + try { + String reply = template.requestBody("http://localhost:{{port}}/hello", "World", String.class); + assertEquals("Bye World", reply); + + reply = template.requestBody("http://localhost:{{port}}/hello", "Moon", String.class); + assertEquals("Bye Moon", reply); + } finally { + context.stop(); + } + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/a59becd7/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettySimplifiedHandle404Test.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettySimplifiedHandle404Test.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettySimplifiedHandle404Test.java new file mode 100644 index 0000000..8d2eeed --- /dev/null +++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettySimplifiedHandle404Test.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.jetty; + +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; + +/** + * Based on end user on forum how to get the 404 error code in his enrich aggregator + * + * @version + */ +public class JettySimplifiedHandle404Test extends BaseJettyTest { + + @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()); + + // START SNIPPET: e1 + // We set throwExceptionOnFailure to false to let Camel return any response from the remove HTTP server without thrown + // HttpOperationFailedException in case of failures. + // This allows us to handle all responses in the aggregation strategy where we can check the HTTP response code + // and decide what to do. As this is based on an unit test we assert the code is 404 + from("direct:start").enrich("http://localhost:{{port}}/myserver?throwExceptionOnFailure=false&user=Camel", 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"); + + // this is our jetty server where we simulate the 404 + from("jetty://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); + } + }); + // END SNIPPET: e1 + } + }; + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/a59becd7/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettySimulateInOnlyTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettySimulateInOnlyTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettySimulateInOnlyTest.java new file mode 100644 index 0000000..1d5000d --- /dev/null +++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettySimulateInOnlyTest.java @@ -0,0 +1,120 @@ +/** + * 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.jetty; + +import org.apache.camel.Exchange; +import org.apache.camel.ExchangePattern; +import org.apache.camel.Processor; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.junit.Test; + +/** + * @version + */ +public class JettySimulateInOnlyTest extends BaseJettyTest { + + private static String route = ""; + + @Override + public boolean isUseRouteBuilder() { + return false; + } + + @Test + public void testSimulateInOnlyUsingWireTap() throws Exception { + context.addRoutes(new RouteBuilder() { + @Override + public void configure() throws Exception { + // START SNIPPET: e1 + from("jetty://http://localhost:{{port}}/myserver") + // turn the route to in only as we do not want jetty to wait for the response + // we can do this using the wiretap EIP pattern + .wireTap("direct:continue") + // and then construct a canned empty response + .transform(constant("OK")); + + from("direct:continue") + .delay(1500) + .process(new Processor() { + public void process(Exchange exchange) throws Exception { + route += "B"; + } + }). + to("mock:result"); + // END SNIPPET: e1 + } + }); + context.start(); + + route = ""; + + MockEndpoint mock = getMockEndpoint("mock:result"); + mock.expectedMessageCount(1); + mock.expectedHeaderReceived("foo", "bar"); + + String reply = template.requestBody("http://localhost:{{port}}/myserver?foo=bar", null, String.class); + route += "A"; + assertEquals("OK", reply); + + assertMockEndpointsSatisfied(); + + assertEquals("AB", route); + } + + @Test + public void testSimulateInOnly() throws Exception { + context.addRoutes(new RouteBuilder() { + @Override + public void configure() throws Exception { + from("jetty://http://localhost:{{port}}/myserver") + // turn the route to in only as we do not want jetty to wait for the response + // we can do this by changing the MEP and sending to a seda endpoint to spin off + // a new thread continue doing the routing + .setExchangePattern(ExchangePattern.InOnly) + .to("seda:continue") + // and then construct a canned empty response + .transform(constant("OK")); + + from("seda:continue") + .delay(1000) + .process(new Processor() { + public void process(Exchange exchange) throws Exception { + route += "B"; + } + }). + to("mock:result"); + } + }); + context.start(); + + route = ""; + + MockEndpoint mock = getMockEndpoint("mock:result"); + mock.expectedMessageCount(1); + mock.expectedHeaderReceived("foo", "bar"); + + String reply = template.requestBody("http://localhost:{{port}}/myserver?foo=bar", null, String.class); + route += "A"; + assertEquals("OK", reply); + + assertMockEndpointsSatisfied(); + + assertEquals("AB", route); + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/a59becd7/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettySteveIssueTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettySteveIssueTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettySteveIssueTest.java new file mode 100644 index 0000000..d66eadb --- /dev/null +++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettySteveIssueTest.java @@ -0,0 +1,51 @@ +/** + * 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.jetty; + +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.junit.Test; + +/** + * Unit test based on Steve request for CAMEL-877. + */ +public class JettySteveIssueTest extends BaseJettyTest { + + private String serverUri = "http://localhost:" + getPort() + "/myservice"; + + @Test + public void testSendX() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:result"); + mock.expectedBodiesReceived("<html><body>foo</body></html>"); + mock.expectedHeaderReceived("x", "foo"); + + template.requestBody(serverUri + "?x=foo", null, Object.class); + + assertMockEndpointsSatisfied(); + } + + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + public void configure() throws Exception { + from("jetty:" + serverUri) + .setBody().simple("<html><body>${in.header.x}</body></html>") + .to("mock:result"); + } + }; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/a59becd7/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettyStreamCacheIssueTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettyStreamCacheIssueTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettyStreamCacheIssueTest.java new file mode 100644 index 0000000..719c992 --- /dev/null +++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettyStreamCacheIssueTest.java @@ -0,0 +1,66 @@ +/** + * 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.jetty; + +import org.apache.camel.CamelContext; +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.builder.RouteBuilder; +import org.junit.Assert; +import org.junit.Test; + +public class JettyStreamCacheIssueTest extends BaseJettyTest { + + @Override + protected CamelContext createCamelContext() throws Exception { + CamelContext context = super.createCamelContext(); + // ensure we overflow and spool to disk + context.getStreamCachingStrategy().setSpoolThreshold(5000); + context.setStreamCaching(true); + return context; + } + + @Test + public void testStreamCache() throws Exception { + StringBuffer sb = new StringBuffer(); + for (int i = 0; i < 10000; i++) { + sb.append("0123456789"); + } + String input = sb.toString(); + + String out = template.requestBody("direct:input", input, String.class); + assertEquals(input, out); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:input").to("jetty:http://localhost:" + getPort() + "/input"); + + from("jetty:http://localhost:" + getPort() + "/input").process(new Processor() { + @Override + public void process(final Exchange exchange) throws Exception { + Assert.assertFalse(exchange.hasOut()); + } + }); + } + }; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/a59becd7/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettySuspendResumeTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettySuspendResumeTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettySuspendResumeTest.java new file mode 100644 index 0000000..31e05cc --- /dev/null +++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettySuspendResumeTest.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.jetty; + +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.http.HttpConsumer; +import org.apache.camel.component.http.HttpOperationFailedException; +import org.junit.Test; + +/** + * @version + */ +public class JettySuspendResumeTest extends BaseJettyTest { + + private String serverUri = "http://localhost:" + getPort() + "/cool"; + + @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); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("jetty://" + serverUri).id("route1") + .transform(body().prepend("Bye ")); + } + }; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/a59becd7/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettySuspendTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettySuspendTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettySuspendTest.java new file mode 100644 index 0000000..3935c71 --- /dev/null +++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettySuspendTest.java @@ -0,0 +1,64 @@ +/** + * 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.jetty; + +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.http.HttpConsumer; +import org.apache.camel.component.http.HttpOperationFailedException; +import org.junit.Test; + +/** + * @version + */ +public class JettySuspendTest extends BaseJettyTest { + + private String serverUri = "http://localhost:" + getPort() + "/cool"; + + @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()); + } + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("jetty://" + serverUri).id("route1") + .transform(body().prepend("Bye ")); + } + }; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/a59becd7/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettySuspendWhileInProgressTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettySuspendWhileInProgressTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettySuspendWhileInProgressTest.java new file mode 100644 index 0000000..cacbe95 --- /dev/null +++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettySuspendWhileInProgressTest.java @@ -0,0 +1,85 @@ +/** + * 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.jetty; + +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; + +import org.apache.camel.RuntimeCamelException; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.http.HttpOperationFailedException; +import org.junit.Test; + +/** + * @version + */ +public class JettySuspendWhileInProgressTest extends BaseJettyTest { + + private String serverUri = "http://localhost:" + getPort() + "/cool"; + + @Test + public void testJettySuspendWhileInProgress() throws Exception { + context.getShutdownStrategy().setTimeout(50); + + // send a request/reply and have future handle so we can shutdown while in progress + Future<String> reply = template.asyncRequestBodyAndHeader(serverUri, null, "name", "Camel", String.class); + + // shutdown camel while in progress, wait 2 sec so the first req has been received in Camel route + Executors.newSingleThreadExecutor().execute(new Runnable() { + public void run() { + try { + Thread.sleep(2000); + context.stop(); + } catch (Exception e) { + // ignore + } + } + }); + + // wait a bit more before sending next + Thread.sleep(5000); + + // now send a new req/reply + Future<String> reply2 = template.asyncRequestBodyAndHeader(serverUri, null, "name", "Tiger", String.class); + + // the first should wait to have its reply returned + assertEquals("Bye Camel", reply.get(20, TimeUnit.SECONDS)); + + // the 2nd should have a 503 returned as we are shutting down + try { + reply2.get(20, TimeUnit.SECONDS); + fail("Should throw exception"); + } catch (Exception e) { + RuntimeCamelException rce = assertIsInstanceOf(RuntimeCamelException.class, e.getCause()); + HttpOperationFailedException hofe = assertIsInstanceOf(HttpOperationFailedException.class, rce.getCause()); + assertEquals(503, hofe.getStatusCode()); + } + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + public void configure() throws Exception { + from("jetty://" + serverUri) + .log("Got data will wait 10 sec with reply") + .delay(10000) + .transform(simple("Bye ${header.name}")); + } + }; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/a59becd7/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettyWithXPathChoiceTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettyWithXPathChoiceTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettyWithXPathChoiceTest.java new file mode 100644 index 0000000..836533d --- /dev/null +++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/JettyWithXPathChoiceTest.java @@ -0,0 +1,82 @@ +/** + * 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.jetty; + +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.junit.Before; +import org.junit.Test; + +import static org.apache.camel.component.mock.MockEndpoint.expectsMessageCount; + +public class JettyWithXPathChoiceTest extends BaseJettyTest { + protected MockEndpoint x; + protected MockEndpoint y; + protected MockEndpoint z; + + @Test + public void testSendToFirstWhen() throws Exception { + String body = "<one/>"; + expectsMessageCount(0, y, z); + + sendBody(body); + + assertMockEndpointsSatisfied(); + + x.reset(); + y.reset(); + z.reset(); + + body = "<two/>"; + expectsMessageCount(0, x, z); + + sendBody(body); + + assertMockEndpointsSatisfied(); + } + + private void sendBody(String body) { + template.sendBody("http://localhost:{{port}}/myworld", body); + } + + @Override + @Before + public void setUp() throws Exception { + super.setUp(); + + x = getMockEndpoint("mock:x"); + y = getMockEndpoint("mock:y"); + z = getMockEndpoint("mock:z"); + } + + protected RouteBuilder createRouteBuilder() { + return new RouteBuilder() { + public void configure() { + from("jetty:http://localhost:{{port}}/myworld") + // use stream caching + .streamCaching() + .choice() + .when().xpath("/one").to("mock:x") + .when().xpath("/two").to("mock:y") + .otherwise().to("mock:z") + .end(); + + } + }; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/a59becd7/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/MultiPartFormTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/MultiPartFormTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/MultiPartFormTest.java new file mode 100644 index 0000000..7c81101 --- /dev/null +++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/MultiPartFormTest.java @@ -0,0 +1,115 @@ +/** + * 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.jetty; + +import java.io.File; + +import javax.activation.DataHandler; + +import org.apache.camel.Exchange; +import org.apache.camel.Message; +import org.apache.camel.Processor; +import org.apache.camel.builder.RouteBuilder; +import org.apache.commons.httpclient.HttpClient; +import org.apache.commons.httpclient.methods.PostMethod; +import org.apache.commons.httpclient.methods.RequestEntity; +import org.apache.commons.httpclient.methods.multipart.FilePart; +import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity; +import org.apache.commons.httpclient.methods.multipart.Part; +import org.apache.commons.httpclient.methods.multipart.StringPart; +import org.apache.commons.httpclient.params.HttpMethodParams; +import org.junit.Test; + +public class MultiPartFormTest extends BaseJettyTest { + private RequestEntity createMultipartRequestEntity() throws Exception { + File file = new File("src/main/resources/META-INF/NOTICE.txt"); + + Part[] parts = {new StringPart("comment", "A binary file of some kind"), + new FilePart(file.getName(), file)}; + + return new MultipartRequestEntity(parts, new HttpMethodParams()); + + } + + @Test + public void testSendMultiPartForm() throws Exception { + HttpClient httpclient = new HttpClient(); + + PostMethod httppost = new PostMethod("http://localhost:" + getPort() + "/test"); + + httppost.setRequestEntity(createMultipartRequestEntity()); + + int status = httpclient.executeMethod(httppost); + + assertEquals("Get a wrong response status", 200, status); + String result = httppost.getResponseBodyAsString(); + + assertEquals("Get a wrong result", "A binary file of some kind", result); + + } + + @Test + public void testSendMultiPartFormFromCamelHttpComponnent() throws Exception { + String result = template.requestBody("http://localhost:" + getPort() + "/test", createMultipartRequestEntity(), String.class); + assertEquals("Get a wrong result", "A binary file of some kind", result); + } + + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + public void configure() throws Exception { + // START SNIPPET: e1 + // Set the jetty temp directory which store the file for multi + // part form + // camel-jetty will clean up the file after it handled the + // request. + // The option works rightly from Camel 2.4.0 + getContext().getProperties().put("CamelJettyTempDir", "target"); + + from("jetty://http://localhost:{{port}}/test").process(new Processor() { + + public void process(Exchange exchange) throws Exception { + Message in = exchange.getIn(); + assertEquals("Get a wrong attachement size", 1, in.getAttachments().size()); + // The file name is attachment id + DataHandler data = in.getAttachment("NOTICE.txt"); + + assertNotNull("Should get the DataHandle NOTICE.txt", data); + // This assert is wrong, but the correct content-type + // (application/octet-stream) + // will not be returned until Jetty makes it available - + // currently the content-type + // returned is just the default for FileDataHandler (for + // the implentation being used) + // assertEquals("Get a wrong content type", + // "text/plain", data.getContentType()); + assertEquals("Got the wrong name", "NOTICE.txt", data.getName()); + + assertTrue("We should get the data from the DataHandle", data.getDataSource() + .getInputStream().available() > 0); + + // The other form date can be get from the message + // header + exchange.getOut().setBody(in.getHeader("comment")); + } + + }); + // END SNIPPET: e1 + } + }; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/a59becd7/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/MultiPartFormWithCustomFilterTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/MultiPartFormWithCustomFilterTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/MultiPartFormWithCustomFilterTest.java new file mode 100644 index 0000000..846f8df --- /dev/null +++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/MultiPartFormWithCustomFilterTest.java @@ -0,0 +1,152 @@ +/** + * 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.jetty; + +import java.io.File; +import java.io.IOException; + +import javax.activation.DataHandler; +import javax.servlet.FilterChain; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletResponse; + +import org.apache.camel.Exchange; +import org.apache.camel.Message; +import org.apache.camel.Processor; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.impl.JndiRegistry; +import org.apache.commons.httpclient.HttpClient; +import org.apache.commons.httpclient.methods.PostMethod; +import org.apache.commons.httpclient.methods.multipart.FilePart; +import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity; +import org.apache.commons.httpclient.methods.multipart.Part; +import org.apache.commons.httpclient.methods.multipart.StringPart; +import org.eclipse.jetty.servlets.MultiPartFilter; +import org.junit.Test; + +public class MultiPartFormWithCustomFilterTest extends BaseJettyTest { + + private static class MyMultipartFilter extends MultiPartFilter { + @Override + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { + // set a marker attribute to show that this filter class was used + ((HttpServletResponse)response).addHeader("MyMultipartFilter", "true"); + + super.doFilter(request, response, chain); + } + } + + @Test + public void testSendMultiPartForm() throws Exception { + HttpClient httpclient = new HttpClient(); + File file = new File("src/main/resources/META-INF/NOTICE.txt"); + PostMethod httppost = new PostMethod("http://localhost:" + getPort() + "/test"); + Part[] parts = { + new StringPart("comment", "A binary file of some kind"), + new FilePart(file.getName(), file) + }; + + MultipartRequestEntity reqEntity = new MultipartRequestEntity(parts, httppost.getParams()); + httppost.setRequestEntity(reqEntity); + + int status = httpclient.executeMethod(httppost); + + assertEquals("Get a wrong response status", 200, status); + + String result = httppost.getResponseBodyAsString(); + assertEquals("Get a wrong result", "A binary file of some kind", result); + assertNotNull("Did not use custom multipart filter", httppost.getResponseHeader("MyMultipartFilter")); + } + + @Test + public void testSendMultiPartFormOverrideEnableMultpartFilterFalse() throws Exception { + HttpClient httpclient = new HttpClient(); + + File file = new File("src/main/resources/META-INF/NOTICE.txt"); + + PostMethod httppost = new PostMethod("http://localhost:" + getPort() + "/test2"); + Part[] parts = { + new StringPart("comment", "A binary file of some kind"), + new FilePart(file.getName(), file) + }; + + MultipartRequestEntity reqEntity = new MultipartRequestEntity(parts, httppost.getParams()); + httppost.setRequestEntity(reqEntity); + + int status = httpclient.executeMethod(httppost); + + assertEquals("Get a wrong response status", 200, status); + assertNotNull("Did not use custom multipart filter", httppost.getResponseHeader("MyMultipartFilter")); + } + + @Override + protected JndiRegistry createRegistry() throws Exception { + JndiRegistry jndi = super.createRegistry(); + jndi.bind("myMultipartFilter", new MyMultipartFilter()); + return jndi; + } + + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + public void configure() throws Exception { + // START SNIPPET: e1 + // Set the jetty temp directory which store the file for multi part form + // camel-jetty will clean up the file after it handled the request. + // The option works rightly from Camel 2.4.0 + getContext().getProperties().put("CamelJettyTempDir", "target"); + + from("jetty://http://localhost:{{port}}/test?multipartFilterRef=myMultipartFilter").process(new Processor() { + public void process(Exchange exchange) throws Exception { + Message in = exchange.getIn(); + assertEquals("Get a wrong attachement size", 1, in.getAttachments().size()); + // The file name is attachment id + DataHandler data = in.getAttachment("NOTICE.txt"); + + assertNotNull("Should get the DataHandle NOTICE.txt", data); + // This assert is wrong, but the correct content-type (application/octet-stream) + // will not be returned until Jetty makes it available - currently the content-type + // returned is just the default for FileDataHandler (for the implentation being used) + //assertEquals("Get a wrong content type", "text/plain", data.getContentType()); + assertEquals("Got the wrong name", "NOTICE.txt", data.getName()); + + assertTrue("We should get the data from the DataHandle", data.getDataSource() + .getInputStream().available() > 0); + + // The other form date can be get from the message header + exchange.getOut().setBody(in.getHeader("comment")); + } + }); + // END SNIPPET: e1 + + // Test to ensure that setting a multipartFilterRef overrides the enableMultipartFilter=false parameter + from("jetty://http://localhost:{{port}}/test2?multipartFilterRef=myMultipartFilter&enableMultipartFilter=false").process(new Processor() { + public void process(Exchange exchange) throws Exception { + Message in = exchange.getIn(); + assertEquals("Get a wrong attachement size", 1, in.getAttachments().size()); + DataHandler data = in.getAttachment("NOTICE.txt"); + + assertNotNull("Should get the DataHandle NOTICE.txt", data); + // The other form date can be get from the message header + exchange.getOut().setBody(in.getHeader("comment")); + } + }); + } + }; + } +}