http://git-wip-us.apache.org/repos/asf/camel/blob/a59becd7/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpBridgeMultipartRouteTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpBridgeMultipartRouteTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpBridgeMultipartRouteTest.java new file mode 100644 index 0000000..d5a289d --- /dev/null +++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpBridgeMultipartRouteTest.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 java.io.File; + +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.HttpEndpoint; +import org.apache.camel.impl.DefaultHeaderFilterStrategy; +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.junit.Test; + +public class HttpBridgeMultipartRouteTest extends BaseJettyTest { + + private int port1; + private int port2; + + private static class MultipartHeaderFilterStrategy extends DefaultHeaderFilterStrategy { + public MultipartHeaderFilterStrategy() { + initialize(); + } + + protected void initialize() { + setLowerCase(true); + setOutFilterPattern("(?i)(Camel|org\\.apache\\.camel)[\\.|a-z|A-z|0-9]*"); + } + } + + @Test + public void testHttpClient() throws Exception { + File jpg = new File("src/test/resources/java.jpg"); + String body = "TEST"; + Part[] parts = new Part[] {new StringPart("body", body), new FilePart(jpg.getName(), jpg)}; + + PostMethod method = new PostMethod("http://localhost:" + port2 + "/test/hello"); + MultipartRequestEntity requestEntity = new MultipartRequestEntity(parts, method.getParams()); + method.setRequestEntity(requestEntity); + + HttpClient client = new HttpClient(); + client.executeMethod(method); + + String responseBody = method.getResponseBodyAsString(); + assertEquals(body, responseBody); + + String numAttachments = method.getResponseHeader("numAttachments").getValue(); + assertEquals(numAttachments, "1"); + } + + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + public void configure() { + port1 = getPort(); + port2 = getNextPort(); + + errorHandler(noErrorHandler()); + + Processor serviceProc = new Processor() { + public void process(Exchange exchange) throws Exception { + Message in = exchange.getIn(); + // put the number of attachments in a response header + exchange.getOut().setHeader("numAttachments", in.getAttachments().size()); + exchange.getOut().setBody(in.getHeader("body")); + } + }; + + HttpEndpoint epOut = getContext().getEndpoint("http://localhost:" + port1 + "?bridgeEndpoint=true&throwExceptionOnFailure=false", HttpEndpoint.class); + epOut.setHeaderFilterStrategy(new MultipartHeaderFilterStrategy()); + + from("jetty:http://localhost:" + port2 + "/test/hello?enableMultipartFilter=false") + .to(epOut); + + from("jetty://http://localhost:" + port1 + "?matchOnUriPrefix=true").process(serviceProc); + } + }; + } + +} \ 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/HttpBridgeRouteTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpBridgeRouteTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpBridgeRouteTest.java new file mode 100644 index 0000000..5303e6a --- /dev/null +++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpBridgeRouteTest.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.ByteArrayInputStream; + +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.RuntimeCamelException; +import org.apache.camel.builder.RouteBuilder; +import org.junit.Test; + +public class HttpBridgeRouteTest extends BaseJettyTest { + + private int port1; + private int port2; + + @Test + public void testHttpClient() throws Exception { + String response = template.requestBodyAndHeader("http://localhost:" + port2 + "/test/hello", + new ByteArrayInputStream("This is a test".getBytes()), "Content-Type", "application/xml", String.class); + assertEquals("Get a wrong response", "/", response); + + response = template.requestBody("http://localhost:" + port1 + "/hello/world", "hello", String.class); + assertEquals("Get a wrong response", "/hello/world", response); + + try { + template.requestBody("http://localhost:" + port2 + "/hello/world", "hello", String.class); + fail("Expect exception here!"); + } catch (Exception ex) { + assertTrue("We should get a RuntimeCamelException", ex instanceof RuntimeCamelException); + } + } + + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + public void configure() { + port1 = getPort(); + port2 = getNextPort(); + + errorHandler(noErrorHandler()); + + Processor serviceProc = new Processor() { + public void process(Exchange exchange) throws Exception { + // get the request URL and copy it to the request body + String uri = exchange.getIn().getHeader(Exchange.HTTP_URI, String.class); + exchange.getOut().setBody(uri); + } + }; + from("jetty:http://localhost:" + port2 + "/test/hello") + .to("http://localhost:" + port1 + "?throwExceptionOnFailure=false&bridgeEndpoint=true"); + + from("jetty://http://localhost:" + port1 + "?matchOnUriPrefix=true").process(serviceProc); + } + }; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/a59becd7/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpCharacterEncodingTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpCharacterEncodingTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpCharacterEncodingTest.java new file mode 100644 index 0000000..f27d4ec --- /dev/null +++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpCharacterEncodingTest.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.builder.RouteBuilder; +import org.junit.Test; + +public class HttpCharacterEncodingTest extends BaseJettyTest { + + @Test + public void testSendToJetty() throws Exception { + Exchange exchange = template.send("http://localhost:{{port}}/myapp/myservice", new Processor() { + + public void process(Exchange exchange) throws Exception { + exchange.getIn().setBody("Hello World Thai Elephant \u0E08"); + exchange.getIn().setHeader("Content-Type", "text/html; charset=utf-8"); + } + + }); + // convert the response to a String + String body = exchange.getOut().getBody(String.class); + assertEquals("Response message is Thai Elephant \u0E08", body); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + public void configure() throws Exception { + 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); + + // for unit testing make sure we got right message + assertEquals("Hello World Thai Elephant \u0E08", body); + + // send a html response + exchange.getOut().setHeader("Content-Type", "text/html; charset=utf-8"); + exchange.getOut().setBody("Response message is Thai Elephant \u0E08"); + } + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/a59becd7/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpClientRouteEnableChunkedTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpClientRouteEnableChunkedTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpClientRouteEnableChunkedTest.java new file mode 100644 index 0000000..7d8ba72 --- /dev/null +++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpClientRouteEnableChunkedTest.java @@ -0,0 +1,84 @@ +/** + * 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.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.junit.Test; + +public class HttpClientRouteEnableChunkedTest extends BaseJettyTest { + + @Test + public void testHttpRouteWithOption() throws Exception { + testHttpClient("direct:start2"); + } + + private void testHttpClient(String uri) throws Exception { + System.getProperties().put("HTTPClient.dontChunkRequests", "yes"); + + MockEndpoint mockEndpoint = getMockEndpoint("mock:a"); + mockEndpoint.expectedBodiesReceived("<b>Hello World</b>"); + + template.requestBodyAndHeader(uri, new ByteArrayInputStream("This is a test".getBytes()), "Content-Type", "application/xml"); + + 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); + + // should get the Content-Length + assertEquals("Should get the transfer-encoding as chunked", "chunked", headers.get("Transfer-Encoding")); + // remove the system property + System.getProperties().remove("HTTPClient.dontChunkRequests"); + } + + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + public void configure() { + errorHandler(noErrorHandler()); + + from("direct:start2").to("http://localhost:{{port}}/hello").to("mock:a"); + + Processor proc = new Processor() { + public void process(Exchange exchange) throws Exception { + ByteArrayInputStream bis = new ByteArrayInputStream("<b>Hello World</b>".getBytes()); + exchange.getOut().setBody(bis); + } + }; + + from("jetty:http://localhost:{{port}}/hello").process(proc); + } + }; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/a59becd7/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpClientRouteTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpClientRouteTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpClientRouteTest.java new file mode 100644 index 0000000..848cc70 --- /dev/null +++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpClientRouteTest.java @@ -0,0 +1,147 @@ +/** + * 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.InputStream; +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.junit.Test; + +public class HttpClientRouteTest extends BaseJettyTest { + + private int port1; + private int port2; + + @Test + public void testHttpRouteWithMessageHeader() throws Exception { + testHttpClient("direct:start"); + } + + @Test + public void testHttpRouteWithOption() throws Exception { + testHttpClient("direct:start2"); + } + + private void testHttpClient(String uri) throws Exception { + System.getProperties().put("HTTPClient.dontChunkRequests", "yes"); + + MockEndpoint mockEndpoint = getMockEndpoint("mock:a"); + mockEndpoint.expectedBodiesReceived("<b>Hello World</b>"); + + template.requestBodyAndHeader(uri, new ByteArrayInputStream("This is a test".getBytes()), "Content-Type", "application/xml"); + + 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 testHttpRouteWithQuery() throws Exception { + MockEndpoint mockEndpoint = getMockEndpoint("mock:a"); + mockEndpoint.expectedBodiesReceived("@ query"); + + template.sendBody("direct:start3", null); + mockEndpoint.assertIsSatisfied(); + } + + @Test + public void testHttpRouteWithQueryByHeader() throws Exception { + MockEndpoint mockEndpoint = getMockEndpoint("mock:a"); + mockEndpoint.expectedBodiesReceived("test"); + + template.sendBody("direct:start4", "test"); + mockEndpoint.assertIsSatisfied(); + } + + @Test + public void testHttpRouteWithHttpURI() throws Exception { + Exchange exchange = template.send("http://localhost:" + port2 + "/querystring", new Processor() { + @Override + public void process(Exchange exchange) throws Exception { + exchange.getIn().setBody(""); + exchange.getIn().setHeader(Exchange.HTTP_URI, "http://localhost:" + port2 + "/querystring?id=test"); + } + }); + assertEquals("Get a wrong response.", "test", exchange.getOut().getBody(String.class)); + } + + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + public void configure() { + port1 = getPort(); + port2 = getNextPort(); + + errorHandler(noErrorHandler()); + + Processor clientProc = new Processor() { + public void process(Exchange exchange) throws Exception { + assertIsInstanceOf(InputStream.class, exchange.getIn().getBody()); + } + }; + + from("direct:start").to("http://localhost:" + port1 + "/hello").process(clientProc).convertBodyTo(String.class).to("mock:a"); + from("direct:start2").to("http://localhost:" + port2 + "/hello").to("mock:a"); + from("direct:start3").to("http://localhost:" + port2 + "/Query%20/test?myQuery=%40%20query").to("mock:a"); + from("direct:start4").setHeader(Exchange.HTTP_QUERY, simple("id=${body}")).to("http://localhost:" + port2 + "/querystring").to("mock:a"); + + Processor proc = new Processor() { + public void process(Exchange exchange) throws Exception { + ByteArrayInputStream bis = new ByteArrayInputStream("<b>Hello World</b>".getBytes()); + exchange.getOut().setBody(bis); + } + }; + from("jetty:http://localhost:" + port1 + "/hello").process(proc).setHeader(Exchange.HTTP_CHUNKED).constant(false); + + from("jetty:http://localhost:" + port2 + "/hello?chunked=false").process(proc); + + from("jetty:http://localhost:" + port2 + "/Query%20/test").process(new Processor() { + public void process(Exchange exchange) throws Exception { + exchange.getOut().setBody(exchange.getIn().getHeader("myQuery", String.class)); + } + }); + + from("jetty:http://localhost:" + port2 + "/querystring").process(new Processor() { + public void process(Exchange exchange) throws Exception { + String result = exchange.getIn().getHeader("id", String.class); + if (result == null) { + result = "No id header"; + } + exchange.getOut().setBody(result); + } + }); + } + }; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/a59becd7/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpConverterTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpConverterTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpConverterTest.java new file mode 100644 index 0000000..eef0641 --- /dev/null +++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpConverterTest.java @@ -0,0 +1,131 @@ +/** + * 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.InputStream; +import javax.servlet.ServletInputStream; +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.HttpConverter; +import org.apache.camel.component.http.HttpMessage; +import org.junit.Test; + +/** + * @version + */ +public class HttpConverterTest extends BaseJettyTest { + + @Override + public boolean isUseRouteBuilder() { + return false; + } + + @Test + public void testToServletRequestAndResponse() throws Exception { + context.addRoutes(new RouteBuilder() { + @Override + public void configure() throws Exception { + from("jetty://http://localhost:{{port}}/test") + // add this node to make sure the convert can work within DefaultMessageImpl + .convertBodyTo(String.class) + .process(new Processor() { + public void process(Exchange exchange) throws Exception { + HttpServletRequest request = exchange.getIn(HttpServletRequest.class); + assertNotNull("We should get request object here", request); + HttpServletResponse response = exchange.getIn(HttpServletResponse.class); + assertNotNull("We should get response object here", response); + String s = exchange.getIn().getBody(String.class); + assertEquals("Hello World", s); + } + }).transform(constant("Bye World")); + } + }); + context.start(); + + String out = template.requestBody("http://localhost:{{port}}/test", "Hello World", String.class); + assertEquals("Bye World", out); + } + + @Test + public void testToServletInputStream() throws Exception { + context.addRoutes(new RouteBuilder() { + @Override + public void configure() throws Exception { + from("jetty://http://localhost:{{port}}/test"). + process(new Processor() { + public void process(Exchange exchange) throws Exception { + HttpMessage msg = exchange.getIn(HttpMessage.class); + + ServletInputStream sis = HttpConverter.toServletInputStream(msg); + assertNotNull(sis); + // The ServletInputStream should be cached and you can't read message here + assertTrue(sis.available() == 0); + String s = msg.getBody(String.class); + + assertEquals("Hello World", s); + } + }).transform(constant("Bye World")); + } + }); + context.start(); + + String out = template.requestBody("http://localhost:{{port}}/test", "Hello World", String.class); + assertEquals("Bye World", out); + } + + @Test + public void testToInputStream() throws Exception { + context.addRoutes(new RouteBuilder() { + @Override + public void configure() throws Exception { + from("jetty://http://localhost:{{port}}/test"). + process(new Processor() { + public void process(Exchange exchange) throws Exception { + HttpMessage msg = exchange.getIn(HttpMessage.class); + + InputStream sis = msg.getBody(InputStream.class); + assertNotNull(sis); + String s = exchange.getContext().getTypeConverter().convertTo(String.class, sis); + + assertEquals("Hello World", s); + } + }).transform(constant("Bye World")); + } + }); + context.start(); + + String out = template.requestBody("http://localhost:{{port}}/test", "Hello World", String.class); + assertEquals("Bye World", out); + } + + @Test + public void testNulls() throws Exception { + HttpMessage msg = null; + assertNull(HttpConverter.toInputStream(msg, null)); + assertNull(HttpConverter.toServletInputStream(msg)); + assertNull(HttpConverter.toServletRequest(msg)); + assertNull(HttpConverter.toServletResponse(msg)); + + HttpServletRequest req = null; + assertNull(HttpConverter.toInputStream(req, null)); + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/a59becd7/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpEndpointURLTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpEndpointURLTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpEndpointURLTest.java new file mode 100644 index 0000000..5bbde5c --- /dev/null +++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpEndpointURLTest.java @@ -0,0 +1,30 @@ +/** + * 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.test.junit4.CamelTestSupport; +import org.junit.Test; + +public class HttpEndpointURLTest extends CamelTestSupport { + + @Test + public void testHttpEndpointURLWithIPv6() { + JettyHttpEndpoint endpoint = (JettyHttpEndpoint)context.getEndpoint("jetty://http://[2a00:8a00:6000:40::1413]:30300/test?test=true"); + assertEquals("http://[2a00:8a00:6000:40::1413]:30300/test?test=true", endpoint.getHttpUri().toString()); + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/a59becd7/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpEndpointUriEncodingIssueTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpEndpointUriEncodingIssueTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpEndpointUriEncodingIssueTest.java new file mode 100644 index 0000000..f15e080 --- /dev/null +++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpEndpointUriEncodingIssueTest.java @@ -0,0 +1,68 @@ +/** + * 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; + +/** + * + */ +public class HttpEndpointUriEncodingIssueTest extends BaseJettyTest { + + @Test + public void testEndpointUriEncodingIssue() throws Exception { + String uri = "http://localhost:{{port}}/myapp/mytest?columns=totalsens,upsens&username=apiuser"; + String out = template.requestBody(uri, null, String.class); + + assertEquals("We got totalsens,upsens columns", out); + } + + @Test + public void testEndpointUriWithDanishCharEncodingIssue() throws Exception { + String uri = "http://localhost:{{port}}/myapp/mytest?columns=claus,s\u00F8ren&username=apiuser"; + String out = template.requestBody(uri, null, String.class); + + assertEquals("We got claus,s\u00F8ren columns", out); + } + + @Test + public void testEndpointHeaderUriEncodingIssue() throws Exception { + String uri = "http://localhost:{{port}}/myapp/mytest?columns=totalsens,upsens&username=apiuser"; + String out = template.requestBodyAndHeader("http://localhost/dummy", null, Exchange.HTTP_URI, uri, String.class); + + assertEquals("We got totalsens,upsens columns", out); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("jetty:http://localhost:{{port}}/myapp/mytest").process(new Processor() { + public void process(Exchange exchange) throws Exception { + String columns = exchange.getIn().getHeader("columns", String.class); + exchange.getOut().setBody("We got " + columns + " columns"); + } + }); + } + }; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/a59becd7/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpFilterCamelHeadersTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpFilterCamelHeadersTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpFilterCamelHeadersTest.java new file mode 100644 index 0000000..4527062 --- /dev/null +++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpFilterCamelHeadersTest.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.Map; + +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.impl.JndiRegistry; +import org.junit.Test; + +/** + * @version + */ +public class HttpFilterCamelHeadersTest extends BaseJettyTest { + + @Test + public void testFilterCamelHeaders() throws Exception { + Exchange out = template.send("http://localhost:{{port}}/test/filter", new Processor() { + public void process(Exchange exchange) throws Exception { + exchange.getIn().setBody("Claus"); + exchange.getIn().setHeader("bar", 123); + } + }); + + assertNotNull(out); + assertEquals("Hi Claus", out.getOut().getBody(String.class)); + + // there should be no internal Camel headers + // except for the response code + Map<String, Object> headers = out.getOut().getHeaders(); + for (String key : headers.keySet()) { + if (!key.equalsIgnoreCase(Exchange.HTTP_RESPONSE_CODE)) { + assertTrue("Should not contain any Camel internal headers", !key.toLowerCase().startsWith("camel")); + } else { + assertEquals(200, headers.get(Exchange.HTTP_RESPONSE_CODE)); + } + } + } + + @Override + protected JndiRegistry createRegistry() throws Exception { + JndiRegistry jndi = super.createRegistry(); + jndi.bind("foo", new MyFooBean()); + return jndi; + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("jetty:http://localhost:{{port}}/test/filter").beanRef("foo"); + } + }; + } + + public static class MyFooBean { + + public String hello(String name) { + return "Hi " + name; + } + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/a59becd7/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpGZipEncodingTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpGZipEncodingTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpGZipEncodingTest.java new file mode 100644 index 0000000..28efbfc --- /dev/null +++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpGZipEncodingTest.java @@ -0,0 +1,100 @@ +/** + * 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 javax.servlet.http.HttpServletRequest; + +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.builder.ExpressionBuilder; +import org.apache.camel.builder.RouteBuilder; +import org.junit.Test; + +public class HttpGZipEncodingTest extends BaseJettyTest { + + private int port1; + private int port2; + + @Test + public void testHttpProducerWithGzip() throws Exception { + String response = template.requestBodyAndHeader("http://localhost:" + port1 + "/gzip", + new ByteArrayInputStream("<Hello>World</Hello>".getBytes()), Exchange.CONTENT_ENCODING, "gzip", String.class); + assertEquals("The response is wrong", "<b>Hello World</b>", response); + } + + @Test + public void testGzipProxy() throws Exception { + String response = + template.requestBodyAndHeader("http://localhost:" + port2 + "/route", + new ByteArrayInputStream("<Hello>World</Hello>".getBytes()), Exchange.CONTENT_ENCODING, "gzip", String.class); + assertEquals("The response is wrong", "<b>Hello World</b>", response); + } + + @Test + public void testGzipProducerWithGzipData() throws Exception { + String response = template.requestBodyAndHeader("direct:gzip", + new ByteArrayInputStream("<Hello>World</Hello>".getBytes()), Exchange.CONTENT_ENCODING, "gzip", String.class); + assertEquals("The response is wrong", "<b>Hello World</b>", response); + } + + @Test + public void testGzipGet() throws Exception { + String response = template.requestBodyAndHeader("http://localhost:" + port1 + "/gzip", + null, "Accept-Encoding", "gzip", String.class); + assertEquals("The response is wrong", "<b>Hello World for gzip</b>", response); + } + + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + public void configure() { + port1 = getPort(); + port2 = getNextPort(); + + errorHandler(noErrorHandler()); + + from("direct:gzip") + .marshal().gzip() + .setProperty(Exchange.SKIP_GZIP_ENCODING, ExpressionBuilder.constantExpression(Boolean.TRUE)) + .to("http://localhost:" + port1 + "/gzip").unmarshal().gzip(); + + from("jetty:http://localhost:" + port1 + "/gzip").process(new Processor() { + public void process(Exchange exchange) throws Exception { + // check the request method + HttpServletRequest request = exchange.getIn().getHeader(Exchange.HTTP_SERVLET_REQUEST, HttpServletRequest.class); + if ("POST".equals(request.getMethod())) { + String requestBody = exchange.getIn().getBody(String.class); + assertEquals("Get a wrong request string", "<Hello>World</Hello>", requestBody); + } + exchange.getOut().setHeader(Exchange.CONTENT_ENCODING, "gzip"); + // check the Accept Encoding header + String header = exchange.getIn().getHeader("Accept-Encoding", String.class); + if (header != null && header.indexOf("gzip") > -1) { + exchange.getOut().setBody("<b>Hello World for gzip</b>"); + } else { + exchange.getOut().setBody("<b>Hello World</b>"); + } + } + }); + + from("jetty:http://localhost:" + port2 + "/route?bridgeEndpoint=true") + .to("http://localhost:" + port1 + "/gzip?bridgeEndpoint=true"); + } + }; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/a59becd7/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpHeaderCaseTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpHeaderCaseTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpHeaderCaseTest.java new file mode 100644 index 0000000..03e8829 --- /dev/null +++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpHeaderCaseTest.java @@ -0,0 +1,73 @@ +/** + * 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.LinkedHashMap; +import java.util.Map; + +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.builder.RouteBuilder; +import org.apache.commons.httpclient.HttpClient; +import org.apache.commons.httpclient.HttpMethod; +import org.apache.commons.httpclient.methods.PostMethod; +import org.junit.Test; + +public class HttpHeaderCaseTest extends BaseJettyTest { + + @Test + public void testHttpHeaderCase() throws Exception { + HttpClient client = new HttpClient(); + HttpMethod method = new PostMethod("http://localhost:" + getPort() + "/myapp/mytest"); + + method.setRequestHeader("clientHeader", "fooBAR"); + method.setRequestHeader("OTHER", "123"); + method.setRequestHeader("beer", "Carlsberg"); + + client.executeMethod(method); + + assertEquals("Bye World", method.getResponseBodyAsString()); + assertEquals("aBc123", method.getResponseHeader("MyCaseHeader").getValue()); + assertEquals("456DEf", method.getResponseHeader("otherCaseHeader").getValue()); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + public void configure() throws Exception { + from("jetty:http://localhost:{{port}}/myapp/mytest").process(new Processor() { + public void process(Exchange exchange) throws Exception { + + // headers received should be in case as well + Map<String, Object> map = new LinkedHashMap<String, Object>(); + map.putAll(exchange.getIn().getHeaders()); + + assertEquals("123", map.get("OTHER")); + assertEquals(null, map.get("other")); + assertEquals("Carlsberg", map.get("beer")); + assertEquals(null, map.get("Beer")); + + exchange.getOut().setBody("Bye World"); + exchange.getOut().setHeader("MyCaseHeader", "aBc123"); + exchange.getOut().setHeader("otherCaseHeader", "456DEf"); + } + }); + } + }; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/a59becd7/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpHeaderTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpHeaderTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpHeaderTest.java new file mode 100644 index 0000000..c3deb50 --- /dev/null +++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpHeaderTest.java @@ -0,0 +1,95 @@ +/** + * 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 java.util.Map.Entry; +import javax.servlet.ServletRequest; + +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.builder.RouteBuilder; +import org.junit.Test; + +public class HttpHeaderTest extends BaseJettyTest { + + @Test + public void testHttpHeaders() throws Exception { + String result = template.requestBody("direct:start", "hello", String.class); + assertEquals("Should send a right http header to the server.", "Find the key!", result); + } + + @Test + public void testServerHeader() throws Exception { + Exchange ex = template.request("http://localhost:{{port}}/server/mytest", new Processor() { + @Override + public void process(Exchange exchange) throws Exception { + // Do nothing here + } + }); + + assertNotNull(ex.getOut().getHeader("Server")); + assertNull(ex.getOut().getHeader("Date")); + + ex = template.request("http://localhost:{{port2}}/server/mytest", new Processor() { + @Override + public void process(Exchange exchange) throws Exception { + // Do nothing here + } + }); + + assertNull(ex.getOut().getHeader("Server")); + assertNotNull(ex.getOut().getHeader("Date")); + } + + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + public void configure() throws Exception { + from("direct:start").setHeader("SOAPAction", constant("http://xxx.com/interfaces/ticket")) + .setHeader("Content-Type", constant("text/xml; charset=utf-8")) + .setHeader(Exchange.HTTP_PROTOCOL_VERSION, constant("HTTP/1.0")) + .to("http://localhost:{{port}}/myapp/mytest"); + + from("jetty:http://localhost:{{port}}/myapp/mytest").process(new Processor() { + public void process(Exchange exchange) throws Exception { + Map<String, Object> headers = exchange.getIn().getHeaders(); + ServletRequest request = exchange.getIn().getHeader(Exchange.HTTP_SERVLET_REQUEST, ServletRequest.class); + assertNotNull(request); + assertEquals("Get a wong http protocol version", request.getProtocol(), "HTTP/1.0"); + for (Entry<String, Object> entry : headers.entrySet()) { + if ("SOAPAction".equals(entry.getKey()) && "http://xxx.com/interfaces/ticket".equals(entry.getValue())) { + exchange.getOut().setBody("Find the key!"); + return; + } + } + exchange.getOut().setBody("Cannot find the key!"); + } + }); + + from("jetty:http://localhost:{{port}}/server/mytest").transform(constant("Response!")); + + //The setting only effect on a new server endpoint + from("jetty:http://localhost:{{port2}}/server/mytest?sendServerVersion=false&sendDateHeader=true").transform(constant("Response!")); + + + } + }; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/a59becd7/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpMethodRestrictTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpMethodRestrictTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpMethodRestrictTest.java new file mode 100644 index 0000000..8ff3990 --- /dev/null +++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpMethodRestrictTest.java @@ -0,0 +1,74 @@ +/** + * 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.commons.httpclient.HttpClient; +import org.apache.commons.httpclient.methods.GetMethod; +import org.apache.commons.httpclient.methods.PostMethod; +import org.apache.commons.httpclient.methods.StringRequestEntity; +import org.junit.Test; + +public class HttpMethodRestrictTest extends BaseJettyTest { + + private String getUrl() { + return "http://localhost:" + getPort() + "/methodRestrict"; + } + + @Test + public void testProperHttpMethod() throws Exception { + HttpClient httpClient = new HttpClient(); + PostMethod httpPost = new PostMethod(getUrl()); + + StringRequestEntity reqEntity = new StringRequestEntity("This is a test", null, null); + 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", "This is a test response", result); + } + + @Test + public void testImproperHttpMethod() throws Exception { + HttpClient httpClient = new HttpClient(); + GetMethod httpGet = new GetMethod(getUrl()); + int status = httpClient.executeMethod(httpGet); + + assertEquals("Get a wrong response status", 405, status); + } + + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + public void configure() throws Exception { + + from("jetty://http://localhost:{{port}}/methodRestrict?httpMethodRestrict=POST").process(new Processor() { + public void process(Exchange exchange) throws Exception { + Message in = exchange.getIn(); + String request = in.getBody(String.class); + exchange.getOut().setBody(request + " response"); + } + }); + } + }; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/a59becd7/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpOperationsFailedExceptionUriTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpOperationsFailedExceptionUriTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpOperationsFailedExceptionUriTest.java new file mode 100644 index 0000000..cc48438 --- /dev/null +++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpOperationsFailedExceptionUriTest.java @@ -0,0 +1,57 @@ +/** + * 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 HttpOperationsFailedExceptionUriTest extends BaseJettyTest { + + @Test + public void testHttpOperationsFailedExceptionUri() throws Exception { + try { + template.requestBodyAndHeader("http://localhost:{{port}}/foo?bar=123", null, "foo", 123); + fail("Should have thrown an exception"); + } catch (RuntimeCamelException e) { + HttpOperationFailedException cause = assertIsInstanceOf(HttpOperationFailedException.class, e.getCause()); + assertEquals(500, cause.getStatusCode()); + assertEquals("http://localhost:" + getPort() + "/foo?bar=123", cause.getUri()); + } + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("jetty://http://localhost:{{port}}/foo") + .process(new Processor() { + public void process(Exchange exchange) throws Exception { + exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, 500); + } + }); + } + }; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/a59becd7/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpPollingConsumerTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpPollingConsumerTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpPollingConsumerTest.java new file mode 100644 index 0000000..41f1c59 --- /dev/null +++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpPollingConsumerTest.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.net.SocketTimeoutException; + +import org.apache.camel.RuntimeCamelException; +import org.apache.camel.builder.RouteBuilder; +import org.junit.Test; + +/** + * @version + */ +public class HttpPollingConsumerTest extends BaseJettyTest { + + @Test + public void testReceive() throws Exception { + String body = consumer.receiveBody("http://localhost:{{port}}/test", String.class); + assertEquals("Bye World", body); + } + + @Test + public void testReceiveTimeout() throws Exception { + String body = consumer.receiveBody("http://localhost:{{port}}/test", 5000, String.class); + assertEquals("Bye World", body); + } + + @Test + public void testReceiveTimeoutTriggered() throws Exception { + try { + consumer.receiveBody("http://localhost:{{port}}/test", 250, String.class); + fail("Should have thrown an exception"); + } catch (RuntimeCamelException e) { + assertIsInstanceOf(SocketTimeoutException.class, e.getCause()); + } + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("jetty://http://localhost:{{port}}/test") + .delay(2000).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/HttpPollingGetTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpPollingGetTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpPollingGetTest.java new file mode 100644 index 0000000..9e3a3cf --- /dev/null +++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpPollingGetTest.java @@ -0,0 +1,76 @@ +/** + * 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.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.junit.Test; + +/** + * @version + */ +public class HttpPollingGetTest extends BaseJettyTest { + + protected String expectedText = "<html"; + + @Test + public void testHttpPollingGet() throws Exception { + MockEndpoint mockEndpoint = resolveMandatoryEndpoint("mock:results", MockEndpoint.class); + mockEndpoint.expectedMinimumMessageCount(1); + + 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.debug("Headers: " + headers); + assertTrue("Should be more than one header but was: " + headers, headers.size() > 0); + + String body = in.getBody(String.class); + + log.debug("Body: " + body); + assertNotNull("Should have a body!", body); + assertTrue("body should contain: " + expectedText, body.contains(expectedText)); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + public void configure() { + from("http://localhost:{{port}}/myservice?delay=5000").to("mock:results"); + + from("jetty:http://localhost:{{port}}/myservice").process(new Processor() { + public void process(Exchange exchange) throws Exception { + exchange.getOut().setBody("<html>Bye World</html>"); + } + }); + } + }; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/a59becd7/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpProducerByteTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpProducerByteTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpProducerByteTest.java new file mode 100644 index 0000000..a03986d --- /dev/null +++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpProducerByteTest.java @@ -0,0 +1,44 @@ +/** + * 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 HttpProducerByteTest extends BaseJettyTest { + + @Test + public void testHttpProducerByteTest() throws Exception { + byte[] data = "Hello World".getBytes(); + String out = template.requestBody("http://localhost:{{port}}/test", data, String.class); + assertEquals("Bye World", out); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("jetty://http://localhost:{{port}}/test").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/HttpProducerConcurrentTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpProducerConcurrentTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpProducerConcurrentTest.java new file mode 100644 index 0000000..a9361eb --- /dev/null +++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpProducerConcurrentTest.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.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; + +import org.apache.camel.builder.RouteBuilder; +import org.junit.Test; + +/** + * HTTP producer concurrent test. + * + * @version + */ +public class HttpProducerConcurrentTest extends BaseJettyTest { + + @Test + public void testNoConcurrentProducers() throws Exception { + doSendMessages(1, 1); + } + + @Test + public void testConcurrentProducers() throws Exception { + doSendMessages(10, 5); + } + + private void doSendMessages(int files, int poolSize) throws Exception { + getMockEndpoint("mock:result").expectedMessageCount(files); + getMockEndpoint("mock:result").assertNoDuplicates(body()); + + ExecutorService executor = Executors.newFixedThreadPool(poolSize); + // we access the responses Map below only inside the main thread, + // so no need for a thread-safe Map implementation + Map<Integer, Future<String>> responses = new HashMap<Integer, Future<String>>(); + for (int i = 0; i < files; i++) { + final int index = i; + Future<String> out = executor.submit(new Callable<String>() { + public String call() throws Exception { + return template.requestBody("http://localhost:{{port}}/echo", "" + index, String.class); + } + }); + responses.put(index, out); + } + + assertMockEndpointsSatisfied(); + + assertEquals(files, responses.size()); + + // get all responses + Set<String> unique = new HashSet<String>(); + for (Future<String> future : responses.values()) { + unique.add(future.get()); + } + + // should be 'files' unique responses + assertEquals("Should be " + files + " unique responses", files, unique.size()); + executor.shutdownNow(); + } + + + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + public void configure() throws Exception { + // expose a echo service + from("jetty:http://localhost:{{port}}/echo") + .transform(body().append(body())).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/HttpProducerJMXBeansIssueTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpProducerJMXBeansIssueTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpProducerJMXBeansIssueTest.java new file mode 100644 index 0000000..15a71a2 --- /dev/null +++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/HttpProducerJMXBeansIssueTest.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 org.apache.camel.Endpoint; +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.Producer; +import org.apache.camel.builder.RouteBuilder; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * @version + */ +public class HttpProducerJMXBeansIssueTest extends BaseJettyTest { + + private static final Logger LOG = LoggerFactory.getLogger(HttpProducerJMXBeansIssueTest.class); + + @Override + public void setUp() throws Exception { + // to enable the JMX connector + enableJMX(); + System.setProperty("org.apache.camel.jmx.createRmiConnector", "True"); + super.setUp(); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("jetty:http://localhost:{{port}}/leak").transform(constant("Bye World")); + + from("direct:leak").process(new Processor() { + public void process(Exchange exchange) throws Exception { + LOG.debug("URL is: " + exchange.getIn().getHeader("url")); + } + }).recipientList(header("url")); + } + }; + } + + @Test + public void testNothing() { + // do nothing as this test is manual + } + + // @Test + // TODO: disabled as this is a manual test + public void testSendAlot() throws Exception { + Endpoint ep = context.getEndpoint("direct:leak"); + Producer p = ep.createProducer(); + p.start(); + + for (int i = 0; i < 10000; i++) { + Exchange ex = ep.createExchange(); + ex.getIn().setHeader("url", "http://localhost:{{port}}/leak?id=" + i); + p.process(ex); + } + + p.stop(); + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/a59becd7/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/a59becd7/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/a59becd7/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/a59becd7/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"); + } + }); + } + }; + } + +}