http://git-wip-us.apache.org/repos/asf/camel/blob/8b2a8877/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpProducerQueryParamTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpProducerQueryParamTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpProducerQueryParamTest.java new file mode 100644 index 0000000..b98a554 --- /dev/null +++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpProducerQueryParamTest.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 java.util.Map; + +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.builder.RouteBuilder; +import org.junit.Test; + +/** + * @version + */ +public class HttpProducerQueryParamTest extends BaseJettyTest { + + private String url = "http://0.0.0.0:" + getPort() + "/cheese"; + + @Test + public void testQueryParameters() throws Exception { + Exchange exchange = template.request(url + "?quote=Camel%20rocks", null); + assertNotNull(exchange); + + String body = exchange.getOut().getBody(String.class); + Map<?, ?> headers = exchange.getOut().getHeaders(); + + assertEquals("Bye World", body); + assertEquals("Carlsberg", headers.get("beer")); + } + + @Test + public void testQueryParametersWithHeader() throws Exception { + Exchange exchange = template.request(url, new Processor() { + public void process(Exchange exchange) throws Exception { + exchange.getIn().setHeader(Exchange.HTTP_QUERY, "quote=Camel rocks"); + } + }); + assertNotNull(exchange); + + String body = exchange.getOut().getBody(String.class); + Map<?, ?> headers = exchange.getOut().getHeaders(); + + assertEquals("Bye World", body); + assertEquals("Carlsberg", headers.get("beer")); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("jetty:" + url).process(new Processor() { + public void process(Exchange exchange) throws Exception { + String quote = exchange.getIn().getHeader("quote", String.class); + assertEquals("Camel rocks", quote); + + exchange.getOut().setBody("Bye World"); + exchange.getOut().setHeader("beer", "Carlsberg"); + } + }); + } + }; + } +} \ No newline at end of file
http://git-wip-us.apache.org/repos/asf/camel/blob/8b2a8877/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpProducerSOTimeoutTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpProducerSOTimeoutTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpProducerSOTimeoutTest.java new file mode 100644 index 0000000..bf39a54 --- /dev/null +++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpProducerSOTimeoutTest.java @@ -0,0 +1,72 @@ +/** + * 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.net.SocketTimeoutException; + +import org.apache.camel.RuntimeCamelException; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.junit.Test; + +/** + * Unit test for using http client SO timeout + * + * @version + */ +public class HttpProducerSOTimeoutTest extends BaseJettyTest { + + @Test + public void testSendWithSOTimeoutNoTimeout() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:result"); + mock.expectedMessageCount(1); + + String out = template.requestBody("http://localhost:{{port}}/myservice?httpClient.soTimeout=5000", null, String.class); + assertEquals("Bye World", out); + + assertMockEndpointsSatisfied(); + } + + @Test + public void testSendWithSOTimeoutTimeout() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:result"); + mock.expectedMessageCount(1); + + try { + // we use a timeout of 1 second + template.requestBody("http://localhost:{{port}}/myservice?httpClient.soTimeout=1000", null, String.class); + fail("Should throw an exception"); + } catch (RuntimeCamelException e) { + assertIsInstanceOf(SocketTimeoutException.class, e.getCause()); + } + + assertMockEndpointsSatisfied(); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("jetty://http://localhost:{{port}}/myservice") + // but we wait for 2 sec before reply is sent back + .delay(2000) + .transform().constant("Bye World").to("mock:result"); + } + }; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/8b2a8877/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpProducerSendEmptyHeaderTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpProducerSendEmptyHeaderTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpProducerSendEmptyHeaderTest.java new file mode 100644 index 0000000..10b795c --- /dev/null +++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpProducerSendEmptyHeaderTest.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.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.junit.Test; + +/** + * + */ +public class HttpProducerSendEmptyHeaderTest extends BaseJettyTest { + + @Test + public void testHttpProducerSendEmptyHeader() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:result"); + mock.expectedMessageCount(1); + + // Jetty 8 treats an empty header as "" while Jetty 9 treats it as null + String expectedValue = isJetty8() ? "" : null; + mock.expectedHeaderReceived("foo", expectedValue); + + template.sendBodyAndHeader("http://localhost:{{port}}/myapp/mytest", "Hello World", "foo", ""); + + assertMockEndpointsSatisfied(); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + allowNullHeaders(); + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("jetty:http://localhost:{{port}}/myapp/mytest") + .convertBodyTo(String.class) + .to("mock:result"); + } + }; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/8b2a8877/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpProducerTwoParametersWithSameKeyTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpProducerTwoParametersWithSameKeyTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpProducerTwoParametersWithSameKeyTest.java new file mode 100644 index 0000000..0b03809 --- /dev/null +++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpProducerTwoParametersWithSameKeyTest.java @@ -0,0 +1,103 @@ +/** + * 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.ArrayList; +import java.util.List; + +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.builder.RouteBuilder; +import org.junit.Test; + +/** + * + */ +public class HttpProducerTwoParametersWithSameKeyTest extends BaseJettyTest { + + @Test + public void testTwoParametersWithSameKey() throws Exception { + Exchange out = template.request("http://localhost:{{port}}/myapp?from=me&to=foo&to=bar", null); + + assertNotNull(out); + assertFalse("Should not fail", out.isFailed()); + assertEquals("OK", out.getOut().getBody(String.class)); + assertEquals("yes", out.getOut().getHeader("bar")); + + List<?> foo = out.getOut().getHeader("foo", List.class); + assertNotNull(foo); + assertEquals(2, foo.size()); + assertEquals("123", foo.get(0)); + assertEquals("456", foo.get(1)); + } + + @Test + public void testTwoHeadersWithSameKeyHeader() throws Exception { + Exchange out = template.request("http://localhost:{{port}}/myapp", new Processor() { + public void process(Exchange exchange) throws Exception { + exchange.getIn().setBody(null); + exchange.getIn().setHeader("from", "me"); + List<String> list = new ArrayList<String>(); + list.add("foo"); + list.add("bar"); + exchange.getIn().setHeader("to", list); + } + }); + + assertNotNull(out); + assertFalse("Should not fail", out.isFailed()); + assertEquals("OK", out.getOut().getBody(String.class)); + assertEquals("yes", out.getOut().getHeader("bar")); + + List<?> foo = out.getOut().getHeader("foo", List.class); + assertNotNull(foo); + assertEquals(2, foo.size()); + assertEquals("123", foo.get(0)); + assertEquals("456", foo.get(1)); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("jetty://http://localhost:{{port}}/myapp").process(new Processor() { + public void process(Exchange exchange) throws Exception { + String from = exchange.getIn().getHeader("from", String.class); + assertEquals("me", from); + + List<?> to = exchange.getIn().getHeader("to", List.class); + assertNotNull(to); + assertEquals(2, to.size()); + assertEquals("foo", to.get(0)); + assertEquals("bar", to.get(1)); + + // response + exchange.getOut().setBody("OK"); + // use multiple values for the foo header in the reply + List<Integer> list = new ArrayList<Integer>(); + list.add(123); + list.add(456); + exchange.getOut().setHeader("foo", list); + exchange.getOut().setHeader("bar", "yes"); + } + }); + } + }; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/8b2a8877/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpProxyRouteContentTypeTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpProxyRouteContentTypeTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpProxyRouteContentTypeTest.java new file mode 100644 index 0000000..7485a93 --- /dev/null +++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpProxyRouteContentTypeTest.java @@ -0,0 +1,52 @@ +/** + * 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.util.ExchangeHelper; +import org.junit.Test; + +public class HttpProxyRouteContentTypeTest extends BaseJettyTest { + + @Test + public void testHttpProxyWithContentType() throws Exception { + + String out = template.requestBodyAndHeader("http://localhost:{{port}}/hello", "test", "Content-Type", "application/xml", String.class); + + assertEquals("Get a wrong response ", "application/xml", out); + } + + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + public void configure() { + from("jetty://http://localhost:{{port}}/hello").to("http://localhost:{{port}}/bye?throwExceptionOnFailure=false&bridgeEndpoint=true"); + + from("jetty://http://localhost:{{port}}/bye").process(new Processor() { + + public void process(Exchange exchange) throws Exception { + + exchange.getOut().setBody(ExchangeHelper.getContentType(exchange)); + } + + }); + } + }; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/8b2a8877/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpProxyRouteTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpProxyRouteTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpProxyRouteTest.java new file mode 100644 index 0000000..e0188f6 --- /dev/null +++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpProxyRouteTest.java @@ -0,0 +1,99 @@ +/** + * 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.Message; +import org.apache.camel.Processor; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.util.StopWatch; +import org.apache.camel.util.TimeUtils; +import org.junit.Test; + +public class HttpProxyRouteTest extends BaseJettyTest { + + private int size = 10; + + @Test + public void testHttpProxy() throws Exception { + log.info("Sending " + size + " messages to a http endpoint which is proxied/bridged"); + + StopWatch watch = new StopWatch(); + for (int i = 0; i < size; i++) { + String out = template.requestBody("http://localhost:{{port}}?foo=" + i, null, String.class); + assertEquals("Bye " + i, out); + } + + log.info("Time taken: " + TimeUtils.printDuration(watch.taken())); + } + + @Test + public void testHttpProxyWithDifferentPath() throws Exception { + String out = template.requestBody("http://localhost:{{port}}/proxy", null, String.class); + assertEquals("/otherEndpoint", out); + + out = template.requestBody("http://localhost:{{port}}/proxy/path", null, String.class); + assertEquals("/otherEndpoint/path", out); + } + + @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); + } + + @Test + public void testHttpProxyFormHeader() throws Exception { + String out = template.requestBodyAndHeader("http://localhost:{{port}}/form", "username=abc&pass=password", Exchange.CONTENT_TYPE, "application/x-www-form-urlencoded", String.class); + assertEquals("Get a wrong response message", "username=abc&pass=password", out); + } + + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + public void configure() { + from("jetty://http://localhost:{{port}}") + .to("http://localhost:{{port}}/bye?throwExceptionOnFailure=false&bridgeEndpoint=true"); + + from("jetty://http://localhost:{{port}}/proxy?matchOnUriPrefix=true") + .to("http://localhost:{{port}}/otherEndpoint?throwExceptionOnFailure=false&bridgeEndpoint=true"); + + from("jetty://http://localhost:{{port}}/bye").transform(header("foo").prepend("Bye ")); + + from("jetty://http://localhost:{{port}}/otherEndpoint?matchOnUriPrefix=true").transform(header(Exchange.HTTP_URI)); + + from("jetty://http://localhost:{{port}}/proxyServer") + .to("http://localhost:{{port2}}/host?bridgeEndpoint=true"); + + from("jetty://http://localhost:{{port2}}/host").transform(header("host")); + + // check the from request + from("jetty://http://localhost:{{port}}/form?bridgeEndpoint=true") + .process(new Processor() { + @Override + public void process(Exchange exchange) throws Exception { + // just take out the message body and send it back + Message in = exchange.getIn(); + String request = in.getBody(String.class); + exchange.getOut().setBody(request); + } + + }); + } + }; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/8b2a8877/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpRedirectNoLocationTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpRedirectNoLocationTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpRedirectNoLocationTest.java new file mode 100644 index 0000000..b37b448 --- /dev/null +++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpRedirectNoLocationTest.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.Processor; +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 HttpRedirectNoLocationTest extends BaseJettyTest { + + @Test + public void testHttpRedirectNoLocation() throws Exception { + try { + template.requestBody("http://localhost:{{port}}/test", "Hello World", String.class); + fail("Should have thrown an exception"); + } catch (RuntimeCamelException e) { + HttpOperationFailedException cause = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause()); + assertEquals(302, cause.getStatusCode()); + assertEquals(true, cause.isRedirectError()); + assertEquals(false, cause.hasRedirectLocation()); + assertEquals(null, cause.getRedirectLocation()); + } + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("jetty://http://localhost:{{port}}/test") + .process(new Processor() { + public void process(Exchange exchange) throws Exception { + exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, 302); + } + }); + } + }; + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/8b2a8877/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpRedirectTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpRedirectTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpRedirectTest.java new file mode 100644 index 0000000..cd26dae --- /dev/null +++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpRedirectTest.java @@ -0,0 +1,89 @@ +/** + * 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.apache.camel.component.mock.MockEndpoint; +import org.junit.Test; + +/** + * @version + */ +public class HttpRedirectTest extends BaseJettyTest { + + @Test + public void testHttpRedirect() throws Exception { + try { + template.requestBody("http://localhost:{{port}}/test", "Hello World", String.class); + fail("Should have thrown an exception"); + } catch (RuntimeCamelException e) { + HttpOperationFailedException cause = assertIsInstanceOf(HttpOperationFailedException.class, + e.getCause()); + assertEquals(301, cause.getStatusCode()); + assertEquals(true, cause.isRedirectError()); + assertEquals(true, cause.hasRedirectLocation()); + assertEquals("http://localhost:" + getPort() + "/test", cause.getUri()); + assertEquals("http://localhost:" + getPort() + "/newtest", cause.getRedirectLocation()); + } + } + + @Test + public void testHttpRedirectFromCamelRoute() throws Exception { + MockEndpoint errorEndpoint = context.getEndpoint("mock:error", MockEndpoint.class); + errorEndpoint.expectedMessageCount(1); + MockEndpoint resultEndpoint = context.getEndpoint("mock:result", MockEndpoint.class); + resultEndpoint.expectedMessageCount(0); + try { + template.requestBody("direct:start", "Hello World", String.class); + fail("Should have thrown an exception"); + } catch (RuntimeCamelException e) { + HttpOperationFailedException cause = assertIsInstanceOf(HttpOperationFailedException.class, + e.getCause()); + assertEquals(302, cause.getStatusCode()); + } + errorEndpoint.assertIsSatisfied(); + resultEndpoint.assertIsSatisfied(); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("jetty://http://localhost:{{port}}/test").process(new Processor() { + public void process(Exchange exchange) throws Exception { + exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, 301); + exchange.getOut().setHeader("location", "http://localhost:" + getPort() + "/newtest"); + } + }); + from("jetty://http://localhost:{{port}}/remove").process(new Processor() { + public void process(Exchange exchange) throws Exception { + exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, 302); + } + }); + + from("direct:start").onException(HttpOperationFailedException.class).to("mock:error").end() + .to("http://localhost:{{port}}/remove?throwExceptionOnFailure=true").to("mock:result"); + + } + }; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/8b2a8877/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpRequestResponseTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpRequestResponseTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpRequestResponseTest.java new file mode 100644 index 0000000..7c962c2 --- /dev/null +++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpRequestResponseTest.java @@ -0,0 +1,81 @@ +/** + * 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 javax.servlet.http.HttpServletResponse; + +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.junit.Test; + +/** + * Unit test for request response in message + */ +public class HttpRequestResponseTest extends BaseJettyTest { + + @Test + public void testHttpServletRequestResponse() 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("Written by servlet response<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 + from("jetty:http://localhost:{{port}}/myapp/myservice").process(new MyBookService()); + // END SNIPPET: e1 + } + }; + } + + 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); + + // we have access to the HttpServletResponse here and we can grab it if we need it + HttpServletResponse res = exchange.getIn().getBody(HttpServletResponse.class); + assertNotNull(res); + + // and they should also be on HttpMessage + HttpMessage msg = (HttpMessage) exchange.getIn(); + assertNotNull(msg.getRequest()); + assertNotNull(msg.getResponse()); + + // and we can use servlet response to write to output stream also + res.getOutputStream().print("Written by servlet response"); + + // 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/8b2a8877/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpReturnDataNotInputStreamConvertableTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpReturnDataNotInputStreamConvertableTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpReturnDataNotInputStreamConvertableTest.java new file mode 100644 index 0000000..c057e18 --- /dev/null +++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpReturnDataNotInputStreamConvertableTest.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.jetty; + +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.builder.RouteBuilder; +import org.junit.Test; + +/** + * @version + */ +public class HttpReturnDataNotInputStreamConvertableTest extends BaseJettyTest { + + @Test + public void testHttpReturnDataNotInputStreamConvertableTest() throws Exception { + String out = template.requestBody("http://localhost:{{port}}/test", "Hello World", String.class); + assertEquals("This is the response", out); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("jetty://http://localhost:{{port}}/test") + .process(new Processor() { + public void process(Exchange exchange) throws Exception { + exchange.getOut().setBody(new MyResponseBean()); + } + }); + } + }; + } + + private static class MyResponseBean { + @Override + public String toString() { + return "This is the response"; + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/8b2a8877/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpReturnFaultTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpReturnFaultTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpReturnFaultTest.java new file mode 100644 index 0000000..86f2e85 --- /dev/null +++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpReturnFaultTest.java @@ -0,0 +1,61 @@ +/** + * 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.HttpOperationFailedException; +import org.junit.Test; + +/** + * @version + */ +public class HttpReturnFaultTest extends BaseJettyTest { + + @Test + public void testHttpFault() throws Exception { + Exchange exchange = template.request("http://localhost:{{port}}/test", new Processor() { + @Override + public void process(Exchange exchange) throws Exception { + exchange.getIn().setBody("Hello World!"); + } + + }); + assertTrue(exchange.isFailed()); + HttpOperationFailedException exception = exchange.getException(HttpOperationFailedException.class); + assertNotNull(exception); + assertEquals("This is a fault", exception.getResponseBody()); + assertEquals(500, exception.getStatusCode()); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("jetty://http://localhost:{{port}}/test") + .process(new Processor() { + public void process(Exchange exchange) throws Exception { + exchange.getOut().setFault(true); + exchange.getOut().setBody("This is a fault"); + } + }); + } + }; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/8b2a8877/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpRoundtripHeaderTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpRoundtripHeaderTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpRoundtripHeaderTest.java new file mode 100644 index 0000000..16a9201 --- /dev/null +++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpRoundtripHeaderTest.java @@ -0,0 +1,126 @@ +/** + * 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.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.List; +import java.util.Map; + +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.component.mock.MockEndpoint; +import org.apache.camel.impl.DefaultHeaderFilterStrategy; +import org.apache.camel.util.IOHelper; + +import org.junit.Test; + +public class HttpRoundtripHeaderTest extends BaseJettyTest { + protected final String uri = "http://localhost:" + getPort() + "/WhichWillGetCloseException"; + protected final String jettyUri = "jetty:" + uri; + protected final String outputText = ":output"; + protected String inputText = "input"; + protected String expectedText = inputText + outputText; + + // http://issues.apache.org/activemq/browse/CAMEL-324 + @Test + public void testHttpRoundTripHeaders() throws Exception { + MockEndpoint mockEndpoint = resolveMandatoryEndpoint("mock:results", MockEndpoint.class); + mockEndpoint.expectedMessageCount(1); + + InputStream answer = (InputStream) template.requestBody(uri, inputText); + + verifyMockGotExpectedText(mockEndpoint, expectedText); + + // read the response data + String lastLine = readLastLine(answer); + + assertNotNull("last response line", lastLine); + assertEquals("response matches: " + expectedText, expectedText, lastLine); + } + + @Test + public void testHttpRoundTripHeadersWithNoIgnoredHeaders() throws Exception { + MockEndpoint mockEndpoint = resolveMandatoryEndpoint("mock:results", MockEndpoint.class); + mockEndpoint.expectedMessageCount(1); + + JettyHttpEndpoint endpoint = context.getEndpoint(jettyUri, JettyHttpEndpoint.class); + // override the default set of ignored headers which includes Content-Length + ((DefaultHeaderFilterStrategy)endpoint.getHeaderFilterStrategy()).setOutFilter(null); + + // read the response data + InputStream answer = (InputStream) template.requestBody(uri, inputText); + verifyMockGotExpectedText(mockEndpoint, expectedText); + + String lastLine = readLastLine(answer); + assertNotNull("last response line", lastLine); + + // Content-Length from request will truncate the output to just the inputText + assertEquals("response matches: " + inputText, inputText, lastLine); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + public void configure() { + Processor processor = new Processor() { + public void process(Exchange exchange) { + String input = (String) exchange.getIn().getBody(); + // append some text to invalidate Context-Length + // for the http reply + exchange.getIn().setBody(input + outputText); + } + }; + + // the unmarshaller does a copy from in message to out + // including all headers + from(jettyUri).unmarshal().string().process(processor).to("mock:results"); + } + }; + } + + private void verifyMockGotExpectedText(MockEndpoint mockEndpoint, String expected) throws InterruptedException { + mockEndpoint.assertIsSatisfied(); + List<Exchange> list = mockEndpoint.getReceivedExchanges(); + Exchange exchange = list.get(0); + assertNotNull("exchange", exchange); + Message in = exchange.getIn(); + assertNotNull("in", in); + Map<String, Object> headers = in.getHeaders(); + assertTrue("no headers are propagated", !headers.isEmpty()); + assertEquals("body has expectedText:" + expected, expected, in.getBody()); + } + + private String readLastLine(InputStream answer) throws IOException { + String lastLine = null; + BufferedReader reader = IOHelper.buffered(new InputStreamReader(answer)); + while (true) { + String line = reader.readLine(); + if (line == null) { + break; + } + lastLine = line; + log.info("Read: " + line); + } + reader.close(); + return lastLine; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/8b2a8877/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpRouteTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpRouteTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpRouteTest.java new file mode 100644 index 0000000..adb78c7 --- /dev/null +++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpRouteTest.java @@ -0,0 +1,269 @@ +/** + * 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.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.util.List; +import java.util.Map; +import javax.servlet.http.HttpSession; + +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.component.http.HttpMessage; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.converter.stream.InputStreamCache; +import org.apache.commons.httpclient.HttpClient; +import org.apache.commons.httpclient.NameValuePair; +import org.apache.commons.httpclient.methods.GetMethod; +import org.apache.commons.httpclient.methods.PostMethod; +import org.apache.commons.httpclient.methods.PutMethod; +import org.apache.commons.httpclient.methods.StringRequestEntity; +import org.junit.Test; + +/** + * @version + */ +public class HttpRouteTest extends BaseJettyTest { + protected static final String POST_MESSAGE = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> " + + "<test>Hello World</test>"; + protected String expectedBody = "<hello>world!</hello>"; + + private int port1; + private int port2; + private int port3; + private int port4; + + @Test + public void testEndpoint() throws Exception { + MockEndpoint mockEndpoint = getMockEndpoint("mock:a"); + mockEndpoint.expectedBodiesReceived(expectedBody); + + invokeHttpEndpoint(); + + mockEndpoint.assertIsSatisfied(); + List<Exchange> list = mockEndpoint.getReceivedExchanges(); + Exchange exchange = list.get(0); + assertNotNull("exchange", exchange); + + Message in = exchange.getIn(); + assertNotNull("in", in); + + Map<String, Object> headers = in.getHeaders(); + + log.info("Headers: " + headers); + + assertTrue("Should be more than one header but was: " + headers, headers.size() > 0); + } + + @Test + public void testHelloEndpoint() throws Exception { + ByteArrayOutputStream os = new ByteArrayOutputStream(); + InputStream is = new URL("http://localhost:" + port2 + "/hello").openStream(); + int c; + while ((c = is.read()) >= 0) { + os.write(c); + } + + String data = new String(os.toByteArray()); + assertEquals("<b>Hello World</b>", data); + } + + @Test + public void testEchoEndpoint() throws Exception { + String out = template.requestBody("http://localhost:" + port1 + "/echo", "HelloWorld", String.class); + assertEquals("Get a wrong output " , "HelloWorld", out); + } + + @Test + public void testPostParameter() throws Exception { + NameValuePair[] data = {new NameValuePair("request", "PostParameter"), + new NameValuePair("others", "bloggs")}; + HttpClient client = new HttpClient(); + PostMethod post = new PostMethod("http://localhost:" + port1 + "/parameter"); + post.setRequestBody(data); + client.executeMethod(post); + InputStream response = post.getResponseBodyAsStream(); + String out = context.getTypeConverter().convertTo(String.class, response); + assertEquals("Get a wrong output " , "PostParameter", out); + } + + @Test + public void testPostXMLMessage() throws Exception { + HttpClient client = new HttpClient(); + PostMethod post = new PostMethod("http://localhost:" + port1 + "/postxml"); + StringRequestEntity entity = new StringRequestEntity(POST_MESSAGE, "application/xml", "UTF-8"); + post.setRequestEntity(entity); + client.executeMethod(post); + InputStream response = post.getResponseBodyAsStream(); + String out = context.getTypeConverter().convertTo(String.class, response); + assertEquals("Get a wrong output " , "OK", out); + } + + @Test + public void testPostParameterInURI() throws Exception { + HttpClient client = new HttpClient(); + PostMethod post = new PostMethod("http://localhost:" + port1 + "/parameter?request=PostParameter&others=bloggs"); + StringRequestEntity entity = new StringRequestEntity(POST_MESSAGE, "application/xml", "UTF-8"); + post.setRequestEntity(entity); + client.executeMethod(post); + InputStream response = post.getResponseBodyAsStream(); + String out = context.getTypeConverter().convertTo(String.class, response); + assertEquals("Get a wrong output " , "PostParameter", out); + } + + @Test + public void testPutParameterInURI() throws Exception { + HttpClient client = new HttpClient(); + PutMethod put = new PutMethod("http://localhost:" + port1 + "/parameter?request=PutParameter&others=bloggs"); + StringRequestEntity entity = new StringRequestEntity(POST_MESSAGE, "application/xml", "UTF-8"); + put.setRequestEntity(entity); + client.executeMethod(put); + InputStream response = put.getResponseBodyAsStream(); + String out = context.getTypeConverter().convertTo(String.class, response); + assertEquals("Get a wrong output " , "PutParameter", out); + } + + @Test + public void testDisableStreamCache() throws Exception { + String response = + template.requestBodyAndHeader("http://localhost:" + port3 + "/noStreamCache", + new ByteArrayInputStream("This is a test".getBytes()), "Content-Type", "application/xml", String.class); + + assertEquals("Get a wrong output ", "OK", response); + } + + @Test + public void testRequestBufferSize() throws Exception { + InputStream in = this.getClass().getResourceAsStream("/META-INF/LICENSE.txt"); + int fileSize = in.available(); + String response = + template.requestBodyAndHeader("http://localhost:" + port4 + "/requestBufferSize", + in, Exchange.CONTENT_TYPE, "application/txt", String.class); + assertEquals("Got a wrong response.", fileSize, response.length()); + } + + @Test + public void testResponseCode() throws Exception { + HttpClient client = new HttpClient(); + GetMethod get = new GetMethod("http://localhost:" + port1 + "/responseCode"); + client.executeMethod(get); + // just make sure we get the right + assertEquals("Get a wrong status code.", 400, get.getStatusCode()); + } + + + protected void invokeHttpEndpoint() throws IOException { + template.requestBodyAndHeader("http://localhost:" + port1 + "/test", expectedBody, "Content-Type", "application/xml"); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + public void configure() { + port1 = getPort(); + port2 = getNextPort(); + port3 = getNextPort(); + port4 = getNextPort(); + + + // enable stream cache + context.setStreamCaching(true); + + from("jetty:http://localhost:" + port1 + "/test").to("mock:a"); + + Processor proc = new Processor() { + public void process(Exchange exchange) throws Exception { + try { + HttpMessage message = (HttpMessage)exchange.getIn(); + HttpSession session = message.getRequest().getSession(); + assertNotNull("we should get session here", session); + } catch (Exception e) { + exchange.getOut().setFault(true); + exchange.getOut().setBody(e); + } + exchange.getOut().setBody("<b>Hello World</b>"); + } + }; + + from("jetty:http://localhost:" + port1 + "/responseCode").setHeader(Exchange.HTTP_RESPONSE_CODE, simple("400")); + + Processor printProcessor = new Processor() { + public void process(Exchange exchange) throws Exception { + Message out = exchange.getOut(); + out.copyFrom(exchange.getIn()); + log.info("The body's object is " + exchange.getIn().getBody()); + log.info("Process body = " + exchange.getIn().getBody(String.class)); + InputStreamCache cache = out.getBody(InputStreamCache.class); + cache.reset(); + } + }; + from("jetty:http://localhost:" + port2 + "/hello?sessionSupport=true").process(proc); + + from("jetty:http://localhost:" + port1 + "/echo").process(printProcessor).process(printProcessor); + + Processor procParameters = new Processor() { + public void process(Exchange exchange) throws Exception { + // As the request input stream is cached by DefaultHttpBinding, + // HttpServletRequest can't get the parameters of post message + String value = exchange.getIn().getHeader("request", String.class); + if (value != null) { + assertNotNull("The value of the parameter should not be null", value); + exchange.getOut().setBody(value); + } else { + exchange.getOut().setBody("Can't get a right parameter"); + } + } + }; + + from("jetty:http://localhost:" + port1 + "/parameter").process(procParameters); + + from("jetty:http://localhost:" + port1 + "/postxml").process(new Processor() { + public void process(Exchange exchange) throws Exception { + String value = exchange.getIn().getBody(String.class); + assertEquals("The response message is wrong", value, POST_MESSAGE); + exchange.getOut().setBody("OK"); + } + }); + + from("jetty:http://localhost:" + port3 + "/noStreamCache?disableStreamCache=true").noStreamCaching().process(new Processor() { + public void process(Exchange exchange) throws Exception { + InputStream is = (InputStream)exchange.getIn().getBody(); + assertTrue("It should be a raw inputstream", is instanceof org.eclipse.jetty.server.HttpInput); + String request = exchange.getIn().getBody(String.class); + assertEquals("Got a wrong request", "This is a test", request); + exchange.getOut().setBody("OK"); + } + }); + + from("jetty:http://localhost:" + port4 + "/requestBufferSize").process(new Processor() { + public void process(Exchange exchange) throws Exception { + String string = exchange.getIn().getBody(String.class); + exchange.getOut().setBody(string); + } + }); + } + }; + } +} + + http://git-wip-us.apache.org/repos/asf/camel/blob/8b2a8877/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpSendFileTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpSendFileTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpSendFileTest.java new file mode 100644 index 0000000..89cfbd0 --- /dev/null +++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpSendFileTest.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 java.io.File; +import java.io.InputStream; + +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.Test; + +/** + * @version + */ +public class HttpSendFileTest extends BaseJettyTest { + + @Test + public void testSendImage() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:result"); + mock.expectedMinimumMessageCount(1); + mock.message(0).body().isInstanceOf(InputStream.class); + mock.message(0).header("Content-Type").isEqualTo("image/jpeg"); + + Exchange out = template.send("http://localhost:{{port}}/myapp/myservice", new Processor() { + public void process(Exchange exchange) throws Exception { + exchange.getIn().setBody(new File("src/test/data/logo.jpeg")); + exchange.getIn().setHeader("Content-Type", "image/jpeg"); + } + }); + + assertMockEndpointsSatisfied(); + + assertEquals("OK", out.getOut().getBody(String.class)); + assertEquals("text/plain", out.getOut().getHeader("Content-Type")); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + public void configure() throws Exception { + from("jetty:http://localhost:{{port}}/myapp/myservice") + .to("mock:result") + .process(new Processor() { + public void process(Exchange exchange) throws Exception { + String body = exchange.getIn().getBody(String.class); + assertNotNull("Body should not be null", body); + } + }) + .transform(constant("OK")).setHeader("Content-Type", constant("text/plain")); + } + }; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/8b2a8877/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpStreamCacheFileIssueTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpStreamCacheFileIssueTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpStreamCacheFileIssueTest.java new file mode 100644 index 0000000..d86b37b --- /dev/null +++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpStreamCacheFileIssueTest.java @@ -0,0 +1,90 @@ +/** + * 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.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.converter.stream.CachedOutputStream; +import org.junit.Before; +import org.junit.Test; + +/** + * @version + */ +public class HttpStreamCacheFileIssueTest extends BaseJettyTest { + + private String body = "12345678901234567890123456789012345678901234567890"; + + @Override + @Before + public void setUp() throws Exception { + deleteDirectory("target/cachedir"); + createDirectory("target/cachedir"); + super.setUp(); + } + + @Test + public void testStreamCacheToFileShouldBeDeletedInCaseOfStop() throws Exception { + getMockEndpoint("mock:result").expectedMessageCount(1); + + String out = template.requestBody("direct:start", "Hello World", String.class); + assertEquals(body, out); + + // the temporary files should have been deleted + File file = new File("target/cachedir"); + String[] files = file.list(); + assertEquals("There should be no files", 0, files.length); + + assertMockEndpointsSatisfied(); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + // enable stream caching and use a low threshold so its forced to write to file + context.getProperties().put(CachedOutputStream.TEMP_DIR, "target/cachedir"); + context.getProperties().put(CachedOutputStream.THRESHOLD, "16"); + context.setStreamCaching(true); + + // use a route so we got an unit of work + from("direct:start") + .to("http://localhost:{{port}}/myserver") + .process(new Processor() { + public void process(Exchange exchange) throws Exception { + // there should be a temp cache file + File file = new File("target/cachedir"); + String[] files = file.list(); + assertTrue("There should be a temp cache file", files.length > 0); + } + }) + // TODO: CAMEL-3839: need to convert the body to a String as the tmp file will be deleted + // before the producer template can convert the result back + .convertBodyTo(String.class) + .to("mock:result"); + + from("jetty://http://localhost:{{port}}/myserver") + .transform().constant(body); + } + }; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/8b2a8877/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpStreamCacheFileResponseTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpStreamCacheFileResponseTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpStreamCacheFileResponseTest.java new file mode 100644 index 0000000..f9432f3 --- /dev/null +++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpStreamCacheFileResponseTest.java @@ -0,0 +1,72 @@ +/** + * 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.BufferedInputStream; +import java.io.ByteArrayInputStream; +import java.io.File; + +import org.apache.camel.builder.RouteBuilder; +import org.junit.Before; +import org.junit.Test; + +/** + * @version + */ +public class HttpStreamCacheFileResponseTest extends BaseJettyTest { + + private String body = "12345678901234567890123456789012345678901234567890"; + private String body2 = "Bye " + body; + + @Override + @Before + public void setUp() throws Exception { + deleteDirectory("target/cachedir"); + createDirectory("target/cachedir"); + super.setUp(); + } + + @Test + public void testStreamCacheToFileShouldBeDeletedInCaseOfResponse() throws Exception { + String out = template.requestBody("http://localhost:{{port}}/myserver", body, String.class); + assertEquals(body2, out); + + // the temporary files should have been deleted + File file = new File("target/cachedir"); + String[] files = file.list(); + assertEquals("There should be no files", 0, files.length); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + // enable stream caching and use a low threshold so its forced to write to file + context.getStreamCachingStrategy().setSpoolDirectory("target/cachedir"); + context.getStreamCachingStrategy().setSpoolThreshold(16); + context.setStreamCaching(true); + + from("jetty://http://localhost:{{port}}/myserver") + // wrap the response in 2 input streams so it will force caching to disk + .transform().constant(new BufferedInputStream(new ByteArrayInputStream(body2.getBytes()))) + .to("log:reply"); + } + }; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/8b2a8877/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpStreamCacheFileStopIssueTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpStreamCacheFileStopIssueTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpStreamCacheFileStopIssueTest.java new file mode 100644 index 0000000..0eb40e8 --- /dev/null +++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpStreamCacheFileStopIssueTest.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.jetty; + +import java.io.File; + +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.converter.stream.CachedOutputStream; +import org.junit.Before; +import org.junit.Test; + +/** + * @version + */ +public class HttpStreamCacheFileStopIssueTest extends BaseJettyTest { + + private String body = "12345678901234567890123456789012345678901234567890"; + + @Override + @Before + public void setUp() throws Exception { + deleteDirectory("target/cachedir"); + createDirectory("target/cachedir"); + super.setUp(); + } + + @Test + public void testStreamCacheToFileShouldBeDeletedInCaseOfStop() throws Exception { + getMockEndpoint("mock:result").expectedMessageCount(0); + + String out = template.requestBody("direct:start", "Hello World", String.class); + assertEquals(body, out); + + // the temporary files should have been deleted + File file = new File("target/cachedir"); + String[] files = file.list(); + assertEquals("There should be no files", 0, files.length); + + assertMockEndpointsSatisfied(); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + // enable stream caching and use a low threshold so its forced to write to file + context.getProperties().put(CachedOutputStream.TEMP_DIR, "target/cachedir"); + context.getProperties().put(CachedOutputStream.THRESHOLD, "16"); + context.setStreamCaching(true); + + // use a route so we got an unit of work + from("direct:start") + .to("http://localhost:{{port}}/myserver") + .process(new Processor() { + public void process(Exchange exchange) throws Exception { + // there should be a temp cache file + File file = new File("target/cachedir"); + String[] files = file.list(); + assertTrue("There should be a temp cache file", files.length > 0); + } + }) + // TODO: CAMEL-3839: need to convert the body to a String as the tmp file will be deleted + // before the producer template can convert the result back + .convertBodyTo(String.class) + // mark the exchange to stop continue routing + .stop() + .to("mock:result"); + + from("jetty://http://localhost:{{port}}/myserver") + .transform().constant(body); + } + }; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/8b2a8877/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpStreamCacheFileTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpStreamCacheFileTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpStreamCacheFileTest.java new file mode 100644 index 0000000..c72b418 --- /dev/null +++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpStreamCacheFileTest.java @@ -0,0 +1,101 @@ +/** + * 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.CamelExecutionException; +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.http.HttpOperationFailedException; +import org.apache.camel.converter.stream.CachedOutputStream; +import org.junit.Before; +import org.junit.Test; + +/** + * @version + */ +public class HttpStreamCacheFileTest extends BaseJettyTest { + + private String body = "12345678901234567890123456789012345678901234567890"; + + @Override + @Before + public void setUp() throws Exception { + deleteDirectory("target/cachedir"); + createDirectory("target/cachedir"); + super.setUp(); + } + + @Test + public void testStreamCacheToFileShouldBeDeletedInCaseOfResponse() throws Exception { + String out = template.requestBody("direct:start", "Hello World", String.class); + assertEquals("Bye World", out); + + // the temporary files should have been deleted + File file = new File("target/cachedir"); + String[] files = file.list(); + assertEquals("There should be no files", 0, files.length); + } + + @Test + public void testStreamCacheToFileShouldBeDeletedInCaseOfException() throws Exception { + try { + template.requestBody("direct:start", null, String.class); + fail("Should have thrown an exception"); + } catch (CamelExecutionException e) { + HttpOperationFailedException hofe = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause()); + String s = context.getTypeConverter().convertTo(String.class, hofe.getResponseBody()); + assertEquals("Response body", body, s); + } + + // the temporary files should have been deleted + File file = new File("target/cachedir"); + String[] files = file.list(); + assertEquals("There should be no files", 0, files.length); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + // enable stream caching and use a low threshold so its forced to write to file + context.getProperties().put(CachedOutputStream.TEMP_DIR, "target/cachedir"); + context.getProperties().put(CachedOutputStream.THRESHOLD, "16"); + context.setStreamCaching(true); + + // use a route so we got an unit of work + from("direct:start").to("http://localhost:{{port}}/myserver"); + + from("jetty://http://localhost:{{port}}/myserver") + .process(new Processor() { + public void process(Exchange exchange) throws Exception { + if (exchange.getIn().getBody() == null) { + exchange.getOut().setBody(body); + exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, 500); + } else { + exchange.getOut().setBody("Bye World"); + } + } + }); + } + }; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/8b2a8877/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpToFileTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpToFileTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpToFileTest.java new file mode 100644 index 0000000..7d07f31 --- /dev/null +++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpToFileTest.java @@ -0,0 +1,81 @@ +/** + * 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.Exchange; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.converter.IOConverter; +import org.junit.Before; +import org.junit.Test; + +/** + * Unit testing demonstrating how to store incoming requests as files and serving a response back. + */ +public class HttpToFileTest extends BaseJettyTest { + + @Test + public void testToJettyAndSaveToFile() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:result"); + mock.expectedBodiesReceived("Hello World"); + + Object out = template.requestBody("http://localhost:{{port}}/myworld", "Hello World"); + + String response = context.getTypeConverter().convertTo(String.class, out); + assertEquals("Response from Jetty", "We got the file", response); + + assertMockEndpointsSatisfied(); + + // give file some time to save + Thread.sleep(500); + + File file = new File("target/myworld/hello.txt"); + assertTrue("File should exists", file.exists()); + + String content = IOConverter.toString(file, null); + assertEquals("File content", "Hello World", content); + } + + @Override + @Before + public void setUp() throws Exception { + deleteDirectory("target/myworld"); + super.setUp(); + } + + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + public void configure() throws Exception { + // put the incoming data on the seda queue and return a fixed response that we got the file + from("jetty:http://localhost:{{port}}/myworld") + .convertBodyTo(String.class) + .to("seda:in") + .setBody(constant("We got the file")); + + // store the content from the queue as a file + from("seda:in") + .setHeader(Exchange.FILE_NAME, constant("hello.txt")) + .convertBodyTo(String.class) + .to("file://target/myworld/") + .to("mock:result"); + } + }; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/8b2a8877/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpTwoEndpointTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpTwoEndpointTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpTwoEndpointTest.java new file mode 100644 index 0000000..1d20aef --- /dev/null +++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpTwoEndpointTest.java @@ -0,0 +1,67 @@ +/** + * 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.junit.Test; + +/** + * @version + */ +public class HttpTwoEndpointTest extends BaseJettyTest { + + @Test + public void testTwoEndpoints() throws Exception { + Exchange a = template.request("direct:a", null); + assertNotNull(a); + + Exchange b = template.request("direct:b", null); + assertNotNull(b); + + assertEquals("Bye cheese", a.getOut().getBody(String.class)); + assertEquals(246, a.getOut().getHeader("foo", Integer.class).intValue()); + + assertEquals("Bye cake", b.getOut().getBody(String.class)); + assertEquals(912, b.getOut().getHeader("foo", Integer.class).intValue()); + + assertEquals(5, context.getEndpoints().size()); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:a").to("http://localhost:{{port}}/myapp?foo=123&bar=cheese"); + + from("direct:b").to("http://localhost:{{port}}/myapp?foo=456&bar=cake"); + + from("jetty://http://localhost:{{port}}/myapp").process(new Processor() { + public void process(Exchange exchange) throws Exception { + int foo = exchange.getIn().getHeader("foo", Integer.class); + String bar = exchange.getIn().getHeader("bar", String.class); + + exchange.getOut().setHeader("foo", foo * 2); + exchange.getOut().setBody("Bye " + bar); + } + }); + } + }; + } +}