CAMEL-9442 - Add tests for camel-http, porting from camel-http4
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/0cf8e048 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/0cf8e048 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/0cf8e048 Branch: refs/heads/camel-2.16.x Commit: 0cf8e0484d96c23d28b0a5d56466e692397e2dc0 Parents: 2314e21 Author: Tadayoshi Sato <sato.tadayo...@gmail.com> Authored: Wed Feb 24 15:56:16 2016 +0900 Committer: Claus Ibsen <davscl...@apache.org> Committed: Thu Feb 25 10:41:49 2016 +0100 ---------------------------------------------------------------------- components/camel-http/pom.xml | 8 +- .../camel/component/http/BaseHttpTest.java | 73 +++++++++++++ .../component/http/HttpBridgeEndpointTest.java | 109 +++++++++++++++++++ .../handler/BasicRawQueryValidationHandler.java | 38 +++++++ .../http/handler/BasicValidationHandler.java | 93 ++++++++++++++++ 5 files changed, 320 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/0cf8e048/components/camel-http/pom.xml ---------------------------------------------------------------------- diff --git a/components/camel-http/pom.xml b/components/camel-http/pom.xml index 50020bf..e247ddb 100644 --- a/components/camel-http/pom.xml +++ b/components/camel-http/pom.xml @@ -73,7 +73,7 @@ <!-- testing --> <dependency> <groupId>org.apache.camel</groupId> - <artifactId>camel-test</artifactId> + <artifactId>camel-test</artifactId> <scope>test</scope> </dependency> @@ -87,6 +87,12 @@ <artifactId>slf4j-log4j12</artifactId> <scope>test</scope> </dependency> + <dependency> + <groupId>org.eclipse.jetty</groupId> + <artifactId>jetty-server</artifactId> + <version>${jetty9-version}</version> + <scope>test</scope> + </dependency> </dependencies> </project> http://git-wip-us.apache.org/repos/asf/camel/blob/0cf8e048/components/camel-http/src/test/java/org/apache/camel/component/http/BaseHttpTest.java ---------------------------------------------------------------------- diff --git a/components/camel-http/src/test/java/org/apache/camel/component/http/BaseHttpTest.java b/components/camel-http/src/test/java/org/apache/camel/component/http/BaseHttpTest.java new file mode 100644 index 0000000..7453b9e --- /dev/null +++ b/components/camel-http/src/test/java/org/apache/camel/component/http/BaseHttpTest.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.http; + +import java.util.Map; + +import javax.servlet.http.HttpServletResponse; + +import org.apache.camel.Exchange; +import org.apache.camel.Message; +import org.apache.camel.test.junit4.CamelTestSupport; +import org.eclipse.jetty.server.Handler; +import org.eclipse.jetty.server.handler.ContextHandler; +import org.eclipse.jetty.server.handler.ContextHandlerCollection; +import org.eclipse.jetty.server.handler.HandlerCollection; + +/** + * Ported from {@link org.apache.camel.component.http4.BaseHttpTest}. + * + * @version + */ +public abstract class BaseHttpTest extends CamelTestSupport { + + protected void assertExchange(Exchange exchange) { + assertNotNull(exchange); + + assertTrue(exchange.hasOut()); + Message out = exchange.getOut(); + assertHeaders(out.getHeaders()); + assertBody(out.getBody(String.class)); + } + + protected void assertHeaders(Map<String, Object> headers) { + assertEquals(HttpServletResponse.SC_OK, headers.get(Exchange.HTTP_RESPONSE_CODE)); + assertEquals("12", headers.get("Content-Length")); + assertNotNull("Should have Content-Type header", headers.get("Content-Type")); + } + + protected void assertBody(String body) { + assertEquals(getExpectedContent(), body); + } + + protected String getExpectedContent() { + return "camel rocks!"; + } + + protected ContextHandler contextHandler(String context, Handler handler) { + ContextHandler contextHandler = new ContextHandler(context); + contextHandler.setHandler(handler); + return contextHandler; + } + + protected HandlerCollection handlers(Handler... handlers) { + HandlerCollection collection = new ContextHandlerCollection(); + collection.setHandlers(handlers); + return collection; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/0cf8e048/components/camel-http/src/test/java/org/apache/camel/component/http/HttpBridgeEndpointTest.java ---------------------------------------------------------------------- diff --git a/components/camel-http/src/test/java/org/apache/camel/component/http/HttpBridgeEndpointTest.java b/components/camel-http/src/test/java/org/apache/camel/component/http/HttpBridgeEndpointTest.java new file mode 100644 index 0000000..3e021e9 --- /dev/null +++ b/components/camel-http/src/test/java/org/apache/camel/component/http/HttpBridgeEndpointTest.java @@ -0,0 +1,109 @@ +/** + * 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.http; + +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.component.http.handler.BasicRawQueryValidationHandler; +import org.apache.camel.component.http.handler.BasicValidationHandler; +import org.apache.camel.test.AvailablePortFinder; +import org.eclipse.jetty.server.Server; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +/** + * Tests ported from {@link org.apache.camel.component.http4.HttpBridgeEndpointTest}. + * + * @version + */ +public class HttpBridgeEndpointTest extends BaseHttpTest { + + private static final int PORT = AvailablePortFinder.getNextAvailable(); + private Server localServer; + + @Before + @Override + public void setUp() throws Exception { + localServer = new Server(PORT); + localServer.setHandler(handlers( + contextHandler("/", new BasicValidationHandler("GET", null, null, getExpectedContent())), + contextHandler("/query", new BasicRawQueryValidationHandler("GET", "x=%3B", null, getExpectedContent())) + )); + localServer.start(); + + super.setUp(); + } + + @After + @Override + public void tearDown() throws Exception { + super.tearDown(); + + if (localServer != null) { + localServer.stop(); + } + } + + @Test + public void notBridgeEndpoint() throws Exception { + Exchange exchange = template.request("http://host/?bridgeEndpoint=false", new Processor() { + public void process(Exchange exchange) throws Exception { + exchange.getIn().setHeader(Exchange.HTTP_URI, "http://localhost:" + PORT + "/"); + } + }); + + assertExchange(exchange); + } + + @Test + public void bridgeEndpoint() throws Exception { + Exchange exchange = template.request("http://localhost:" + PORT + "/?bridgeEndpoint=true", new Processor() { + public void process(Exchange exchange) throws Exception { + exchange.getIn().setHeader(Exchange.HTTP_URI, "http://host:8080/"); + } + }); + + assertExchange(exchange); + } + + @Test + public void bridgeEndpointWithQuery() throws Exception { + Exchange exchange = template.request("http://localhost:" + PORT + "/query?bridgeEndpoint=true", new Processor() { + public void process(Exchange exchange) throws Exception { + exchange.getIn().setHeader(Exchange.HTTP_URI, "http://host:8080/"); + exchange.getIn().setHeader(Exchange.HTTP_QUERY, "x=%3B"); + } + }); + + assertExchange(exchange); + } + + @Test + public void bridgeEndpointWithRawQueryAndQuery() throws Exception { + Exchange exchange = template.request("http://localhost:" + PORT + "/query?bridgeEndpoint=true", new Processor() { + public void process(Exchange exchange) throws Exception { + exchange.getIn().setHeader(Exchange.HTTP_URI, "http://host:8080/"); + exchange.getIn().setHeader(Exchange.HTTP_RAW_QUERY, "x=%3B"); + exchange.getIn().setHeader(Exchange.HTTP_QUERY, "x=;"); + } + }); + + assertExchange(exchange); + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/0cf8e048/components/camel-http/src/test/java/org/apache/camel/component/http/handler/BasicRawQueryValidationHandler.java ---------------------------------------------------------------------- diff --git a/components/camel-http/src/test/java/org/apache/camel/component/http/handler/BasicRawQueryValidationHandler.java b/components/camel-http/src/test/java/org/apache/camel/component/http/handler/BasicRawQueryValidationHandler.java new file mode 100644 index 0000000..1f8019b --- /dev/null +++ b/components/camel-http/src/test/java/org/apache/camel/component/http/handler/BasicRawQueryValidationHandler.java @@ -0,0 +1,38 @@ +/** + * 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.http.handler; + +import javax.servlet.http.HttpServletRequest; + +/** + * Similar to {@link BasicValidationHandler} but validates the raw query instead. + */ +public class BasicRawQueryValidationHandler extends BasicValidationHandler { + + public BasicRawQueryValidationHandler(String expectedMethod, String expectedQuery, Object expectedContent, String responseContent) { + super(expectedMethod, expectedQuery, expectedContent, responseContent); + } + + protected boolean validateQuery(HttpServletRequest request) { + String query = request.getQueryString(); + if (expectedQuery != null && !expectedQuery.equals(query)) { + return false; + } + return true; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/0cf8e048/components/camel-http/src/test/java/org/apache/camel/component/http/handler/BasicValidationHandler.java ---------------------------------------------------------------------- diff --git a/components/camel-http/src/test/java/org/apache/camel/component/http/handler/BasicValidationHandler.java b/components/camel-http/src/test/java/org/apache/camel/component/http/handler/BasicValidationHandler.java new file mode 100644 index 0000000..a709272 --- /dev/null +++ b/components/camel-http/src/test/java/org/apache/camel/component/http/handler/BasicValidationHandler.java @@ -0,0 +1,93 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.http.handler; + +import java.io.IOException; +import java.io.PrintWriter; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.eclipse.jetty.server.Request; +import org.eclipse.jetty.server.handler.AbstractHandler; + +/** + * + * @version + */ +public class BasicValidationHandler extends AbstractHandler { + + protected String expectedMethod; + protected String expectedQuery; + protected Object expectedContent; + protected String responseContent; + + public BasicValidationHandler(String expectedMethod, String expectedQuery, + Object expectedContent, String responseContent) { + this.expectedMethod = expectedMethod; + this.expectedQuery = expectedQuery; + this.expectedContent = expectedContent; + this.responseContent = responseContent; + } + + @Override + public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) + throws IOException, ServletException { + + baseRequest.setHandled(true); + + if (expectedMethod != null && !expectedMethod.equals(request.getMethod())) { + response.setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED); + return; + } + + if (!validateQuery(request)) { + response.setStatus(HttpServletResponse.SC_BAD_REQUEST); + return; + } + + if (expectedContent != null) { + StringBuilder content = new StringBuilder(); + String line = null; + while ((line = request.getReader().readLine()) != null) { + content.append(line); + } + + if (!expectedContent.equals(content.toString())) { + response.setStatus(HttpServletResponse.SC_BAD_REQUEST); + return; + } + } + + response.setStatus(HttpServletResponse.SC_OK); + if (responseContent != null) { + response.setContentType("text/plain; charset=utf-8"); + PrintWriter out = response.getWriter(); + out.print(responseContent); + } + } + + protected boolean validateQuery(HttpServletRequest request) { + String query = request.getQueryString(); + if (expectedQuery != null && !expectedQuery.equals(query)) { + return false; + } + return true; + } + +}