CAMEL-11299: restl-dsl server side should return response on OPTIONS for all the uri paths.
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/748c3bd9 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/748c3bd9 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/748c3bd9 Branch: refs/heads/camel-2.19.x Commit: 748c3bd95626b62bfcb909a6ae109c12ca0fb2f4 Parents: 988ea87 Author: Claus Ibsen <[email protected]> Authored: Mon May 22 16:02:24 2017 +0200 Committer: Claus Ibsen <[email protected]> Committed: Mon May 22 16:03:04 2017 +0200 ---------------------------------------------------------------------- .../support/RestConsumerContextPathMatcher.java | 27 ++++++- .../jetty/CamelContinuationServlet.java | 14 ++++ .../jetty/rest/RestJettyOptionsTest.java | 64 +++++++++++++++++ .../http/handlers/HttpServerChannelHandler.java | 2 +- .../http/handlers/HttpServerChannelHandler.java | 2 +- .../http/rest/RestNettyHttpOptionsTest.java | 64 +++++++++++++++++ .../restlet/RestRestletHttpOptionsTest.java | 67 ++++++++++++++++++ .../servlet/rest/OptionsMethodWebRequest.java | 41 +++++++++++ .../servlet/rest/RestServletOptionsTest.java | 74 ++++++++++++++++++++ .../component/undertow/UndertowConsumer.java | 2 +- .../rest/RestUndertowHttpOptionsTest.java | 66 +++++++++++++++++ 11 files changed, 419 insertions(+), 4 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/748c3bd9/camel-core/src/main/java/org/apache/camel/support/RestConsumerContextPathMatcher.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/support/RestConsumerContextPathMatcher.java b/camel-core/src/main/java/org/apache/camel/support/RestConsumerContextPathMatcher.java index f3c22af..86594db 100644 --- a/camel-core/src/main/java/org/apache/camel/support/RestConsumerContextPathMatcher.java +++ b/camel-core/src/main/java/org/apache/camel/support/RestConsumerContextPathMatcher.java @@ -139,6 +139,22 @@ public final class RestConsumerContextPathMatcher { } } + // we could not find a direct match, and if the request is OPTIONS then we need all candidates + if (answer == null && isOptionsMethod(requestMethod)) { + candidates.clear(); + candidates.addAll(consumerPaths); + + // then try again to see if we can find a direct match + it = candidates.iterator(); + while (it.hasNext()) { + ConsumerPath consumer = it.next(); + if (matchRestPath(requestPath, consumer.getConsumerPath(), false)) { + answer = consumer; + break; + } + } + } + // if there are no wildcards, then select the matching with the longest path boolean noWildcards = candidates.stream().allMatch(p -> countWildcards(p.getConsumerPath()) == 0); if (noWildcards) { @@ -192,7 +208,7 @@ public final class RestConsumerContextPathMatcher { } /** - * Matches the given request HTTP method with the configured HTTP method of the consumer + * Matches the given request HTTP method with the configured HTTP method of the consumer. * * @param method the request HTTP method * @param restrict the consumer configured HTTP restrict method @@ -207,6 +223,15 @@ public final class RestConsumerContextPathMatcher { } /** + * Is the request method OPTIONS + * + * @return <tt>true</tt> if matched, <tt>false</tt> otherwise + */ + private static boolean isOptionsMethod(String method) { + return "options".equalsIgnoreCase(method); + } + + /** * Matches the given request path with the configured consumer path * * @param requestPath the request path http://git-wip-us.apache.org/repos/asf/camel/blob/748c3bd9/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/CamelContinuationServlet.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/CamelContinuationServlet.java b/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/CamelContinuationServlet.java index c785689..4953570 100644 --- a/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/CamelContinuationServlet.java +++ b/components/camel-jetty-common/src/main/java/org/apache/camel/component/jetty/CamelContinuationServlet.java @@ -94,6 +94,20 @@ public class CamelContinuationServlet extends CamelServlet { return; } + // if its an OPTIONS request then return which method is allowed + if ("OPTIONS".equals(request.getMethod()) && !consumer.isOptionsEnabled()) { + String s; + if (consumer.getEndpoint().getHttpMethodRestrict() != null) { + s = "OPTIONS," + consumer.getEndpoint().getHttpMethodRestrict(); + } else { + // allow them all + s = "GET,HEAD,POST,PUT,DELETE,TRACE,OPTIONS,CONNECT,PATCH"; + } + response.addHeader("Allow", s); + response.setStatus(HttpServletResponse.SC_OK); + return; + } + if (consumer.getEndpoint().getHttpMethodRestrict() != null) { Iterator<?> it = ObjectHelper.createIterable(consumer.getEndpoint().getHttpMethodRestrict()).iterator(); boolean match = false; http://git-wip-us.apache.org/repos/asf/camel/blob/748c3bd9/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/RestJettyOptionsTest.java ---------------------------------------------------------------------- diff --git a/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/RestJettyOptionsTest.java b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/RestJettyOptionsTest.java new file mode 100644 index 0000000..7618601 --- /dev/null +++ b/components/camel-jetty9/src/test/java/org/apache/camel/component/jetty/rest/RestJettyOptionsTest.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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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.rest; + +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.jetty.BaseJettyTest; +import org.junit.Test; + +public class RestJettyOptionsTest extends BaseJettyTest { + + @Test + public void testJettyServerOptions() throws Exception { + Exchange exchange = template.request("http://localhost:" + getPort() + "/users/v1/customers", new Processor() { + @Override + public void process(Exchange exchange) throws Exception { + exchange.getIn().setHeader(Exchange.HTTP_METHOD, "OPTIONS"); + } + }); + + assertEquals(200, exchange.getOut().getHeader(Exchange.HTTP_RESPONSE_CODE)); + assertEquals("OPTIONS,GET", exchange.getOut().getHeader("ALLOW")); + assertEquals("", exchange.getOut().getBody(String.class)); + + exchange = fluentTemplate.to("http://localhost:" + getPort() + "/users/v1/123").withHeader(Exchange.HTTP_METHOD, "OPTIONS").send(); + assertEquals(200, exchange.getOut().getHeader(Exchange.HTTP_RESPONSE_CODE)); + assertEquals("OPTIONS,PUT", exchange.getOut().getHeader("ALLOW")); + assertEquals("", exchange.getOut().getBody(String.class)); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + // configure to use jetty on localhost with the given port + restConfiguration().component("jetty").host("localhost").port(getPort()); + + // use the rest DSL to define the rest services + rest("/users/") + .get("v1/customers") + .to("mock:customers") + .put("v1/{id}") + .to("mock:id"); + } + }; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/748c3bd9/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/handlers/HttpServerChannelHandler.java ---------------------------------------------------------------------- diff --git a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/handlers/HttpServerChannelHandler.java b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/handlers/HttpServerChannelHandler.java index cf8936a..b1d6456 100644 --- a/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/handlers/HttpServerChannelHandler.java +++ b/components/camel-netty-http/src/main/java/org/apache/camel/component/netty/http/handlers/HttpServerChannelHandler.java @@ -108,7 +108,7 @@ public class HttpServerChannelHandler extends ServerChannelHandler { HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK); response.setChunked(false); response.headers().set("Allow", s); - response.headers().set(Exchange.CONTENT_TYPE, "text/plain"); + // do not include content-type as that would indicate to the caller that we can only do text/plain response.headers().set(Exchange.CONTENT_LENGTH, 0); messageEvent.getChannel().write(response).syncUninterruptibly(); messageEvent.getChannel().close(); http://git-wip-us.apache.org/repos/asf/camel/blob/748c3bd9/components/camel-netty4-http/src/main/java/org/apache/camel/component/netty4/http/handlers/HttpServerChannelHandler.java ---------------------------------------------------------------------- diff --git a/components/camel-netty4-http/src/main/java/org/apache/camel/component/netty4/http/handlers/HttpServerChannelHandler.java b/components/camel-netty4-http/src/main/java/org/apache/camel/component/netty4/http/handlers/HttpServerChannelHandler.java index 6cd8d10..3034a56 100644 --- a/components/camel-netty4-http/src/main/java/org/apache/camel/component/netty4/http/handlers/HttpServerChannelHandler.java +++ b/components/camel-netty4-http/src/main/java/org/apache/camel/component/netty4/http/handlers/HttpServerChannelHandler.java @@ -104,7 +104,7 @@ public class HttpServerChannelHandler extends ServerChannelHandler { } HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK); response.headers().set("Allow", s); - response.headers().set(Exchange.CONTENT_TYPE, "text/plain"); + // do not include content-type as that would indicate to the caller that we can only do text/plain response.headers().set(Exchange.CONTENT_LENGTH, 0); ctx.writeAndFlush(response); return; http://git-wip-us.apache.org/repos/asf/camel/blob/748c3bd9/components/camel-netty4-http/src/test/java/org/apache/camel/component/netty4/http/rest/RestNettyHttpOptionsTest.java ---------------------------------------------------------------------- diff --git a/components/camel-netty4-http/src/test/java/org/apache/camel/component/netty4/http/rest/RestNettyHttpOptionsTest.java b/components/camel-netty4-http/src/test/java/org/apache/camel/component/netty4/http/rest/RestNettyHttpOptionsTest.java new file mode 100644 index 0000000..a6ea494 --- /dev/null +++ b/components/camel-netty4-http/src/test/java/org/apache/camel/component/netty4/http/rest/RestNettyHttpOptionsTest.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.netty4.http.rest; + +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.netty4.http.BaseNettyTest; +import org.junit.Test; + +public class RestNettyHttpOptionsTest extends BaseNettyTest { + + @Test + public void testNettyServerOptions() throws Exception { + Exchange exchange = template.request("http://localhost:" + getPort() + "/users/v1/customers", new Processor() { + @Override + public void process(Exchange exchange) throws Exception { + exchange.getIn().setHeader(Exchange.HTTP_METHOD, "OPTIONS"); + } + }); + + assertEquals(200, exchange.getOut().getHeader(Exchange.HTTP_RESPONSE_CODE)); + assertEquals("OPTIONS,GET", exchange.getOut().getHeader("ALLOW")); + assertEquals("", exchange.getOut().getBody(String.class)); + + exchange = fluentTemplate.to("http://localhost:" + getPort() + "/users/v1/123").withHeader(Exchange.HTTP_METHOD, "OPTIONS").send(); + assertEquals(200, exchange.getOut().getHeader(Exchange.HTTP_RESPONSE_CODE)); + assertEquals("OPTIONS,PUT", exchange.getOut().getHeader("ALLOW")); + assertEquals("", exchange.getOut().getBody(String.class)); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + // configure to use netty on localhost with the given port + restConfiguration().component("netty4-http").host("localhost").port(getPort()); + + // use the rest DSL to define the rest services + rest("/users/") + .get("v1/customers") + .to("mock:customers") + .put("v1/{id}") + .to("mock:id"); + } + }; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/748c3bd9/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletHttpOptionsTest.java ---------------------------------------------------------------------- diff --git a/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletHttpOptionsTest.java b/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletHttpOptionsTest.java new file mode 100644 index 0000000..e862573 --- /dev/null +++ b/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestRestletHttpOptionsTest.java @@ -0,0 +1,67 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.restlet; + +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.builder.RouteBuilder; +import org.junit.Ignore; +import org.junit.Test; + +/** + * @version + */ +@Ignore("Not supported by camel-restlet yet") +public class RestRestletHttpOptionsTest extends RestletTestSupport { + + @Test + public void testRestletServerOptions() throws Exception { + Exchange exchange = template.request("http://localhost:" + portNum + "/users/v1/customers", new Processor() { + @Override + public void process(Exchange exchange) throws Exception { + exchange.getIn().setHeader(Exchange.HTTP_METHOD, "OPTIONS"); + } + }); + + assertEquals(200, exchange.getOut().getHeader(Exchange.HTTP_RESPONSE_CODE)); + assertEquals("OPTIONS,GET", exchange.getOut().getHeader("ALLOW")); + assertEquals("", exchange.getOut().getBody(String.class)); + + exchange = fluentTemplate.to("http://localhost:" + portNum + "/users/v1/123").withHeader(Exchange.HTTP_METHOD, "OPTIONS").send(); + assertEquals(200, exchange.getOut().getHeader(Exchange.HTTP_RESPONSE_CODE)); + assertEquals("OPTIONS,PUT", exchange.getOut().getHeader("ALLOW")); + assertEquals("", exchange.getOut().getBody(String.class)); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + // configure to use restlet on localhost with the given port + restConfiguration().component("restlet").host("localhost").port(portNum); + + // use the rest DSL to define the rest services + rest("/users/") + .get("v1/customers") + .to("mock:customers") + .put("v1/{id}") + .to("mock:id"); + } + }; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/748c3bd9/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/rest/OptionsMethodWebRequest.java ---------------------------------------------------------------------- diff --git a/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/rest/OptionsMethodWebRequest.java b/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/rest/OptionsMethodWebRequest.java new file mode 100644 index 0000000..4cba44d --- /dev/null +++ b/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/rest/OptionsMethodWebRequest.java @@ -0,0 +1,41 @@ +/** + * 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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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.servlet.rest; + +import java.net.URL; + +import com.meterware.httpunit.GetMethodWebRequest; + +public class OptionsMethodWebRequest extends GetMethodWebRequest { + + public OptionsMethodWebRequest(String urlString) { + super(urlString); + } + + public OptionsMethodWebRequest(URL urlBase, String urlString) { + super(urlBase, urlString); + } + + public OptionsMethodWebRequest(URL urlBase, String urlString, String target) { + super(urlBase, urlString, target); + } + + @Override + public String getMethod() { + return "OPTIONS"; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/748c3bd9/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/rest/RestServletOptionsTest.java ---------------------------------------------------------------------- diff --git a/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/rest/RestServletOptionsTest.java b/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/rest/RestServletOptionsTest.java new file mode 100644 index 0000000..f6df318 --- /dev/null +++ b/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/rest/RestServletOptionsTest.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.servlet.rest; + +import com.meterware.httpunit.WebRequest; +import com.meterware.httpunit.WebResponse; +import com.meterware.servletunit.ServletUnitClient; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.servlet.ServletCamelRouterTestSupport; +import org.apache.camel.component.servlet.ServletRestHttpBinding; +import org.apache.camel.impl.JndiRegistry; +import org.junit.Test; + +public class RestServletOptionsTest extends ServletCamelRouterTestSupport { + + @Override + protected JndiRegistry createRegistry() throws Exception { + JndiRegistry jndi = super.createRegistry(); + jndi.bind("myBinding", new ServletRestHttpBinding()); + return jndi; + } + + @Test + public void testServletOptions() throws Exception { + WebRequest req = new OptionsMethodWebRequest(CONTEXT_URL + "/services/users/v1/customers"); + ServletUnitClient client = newClient(); + client.setExceptionsThrownOnErrorStatus(false); + WebResponse response = client.getResponse(req); + + assertEquals(200, response.getResponseCode()); + assertEquals("OPTIONS,GET", response.getHeaderField("ALLOW")); + assertEquals("", response.getText()); + + req = new OptionsMethodWebRequest(CONTEXT_URL + "/services/users/v1/123"); + response = client.getResponse(req); + + assertEquals(200, response.getResponseCode()); + assertEquals("OPTIONS,PUT", response.getHeaderField("ALLOW")); + assertEquals("", response.getText()); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + // configure to use servlet on localhost + restConfiguration().component("servlet").host("localhost").endpointProperty("httpBinding", "#myBinding"); + + // use the rest DSL to define the rest services + rest("/users/") + .get("v1/customers") + .to("mock:customers") + .put("v1/{id}") + .to("mock:id"); + } + }; + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/748c3bd9/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowConsumer.java ---------------------------------------------------------------------- diff --git a/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowConsumer.java b/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowConsumer.java index 817eb6e..1f89a54 100644 --- a/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowConsumer.java +++ b/components/camel-undertow/src/main/java/org/apache/camel/component/undertow/UndertowConsumer.java @@ -97,8 +97,8 @@ public class UndertowConsumer extends DefaultConsumer implements HttpHandler { } //return list of allowed methods in response headers httpExchange.setStatusCode(StatusCodes.OK); - httpExchange.getResponseHeaders().put(ExchangeHeaders.CONTENT_TYPE, MimeMappings.DEFAULT_MIME_MAPPINGS.get("txt")); httpExchange.getResponseHeaders().put(ExchangeHeaders.CONTENT_LENGTH, 0); + // do not include content-type as that would indicate to the caller that we can only do text/plain httpExchange.getResponseHeaders().put(Headers.ALLOW, allowedMethods); httpExchange.getResponseSender().close(); return; http://git-wip-us.apache.org/repos/asf/camel/blob/748c3bd9/components/camel-undertow/src/test/java/org/apache/camel/component/undertow/rest/RestUndertowHttpOptionsTest.java ---------------------------------------------------------------------- diff --git a/components/camel-undertow/src/test/java/org/apache/camel/component/undertow/rest/RestUndertowHttpOptionsTest.java b/components/camel-undertow/src/test/java/org/apache/camel/component/undertow/rest/RestUndertowHttpOptionsTest.java new file mode 100644 index 0000000..d353be4 --- /dev/null +++ b/components/camel-undertow/src/test/java/org/apache/camel/component/undertow/rest/RestUndertowHttpOptionsTest.java @@ -0,0 +1,66 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.undertow.rest; + +import org.apache.camel.Exchange; +import org.apache.camel.Processor; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.undertow.BaseUndertowTest; +import org.junit.Ignore; +import org.junit.Test; + +@Ignore("Not supported by camel-undertow yet") +public class RestUndertowHttpOptionsTest extends BaseUndertowTest { + + @Test + public void testUndertowServerOptions() throws Exception { + Exchange exchange = template.request("undertow:http://localhost:" + getPort() + "/users/v1/customers", new Processor() { + @Override + public void process(Exchange exchange) throws Exception { + exchange.getIn().setHeader(Exchange.HTTP_METHOD, "OPTIONS"); + } + }); + + assertEquals(200, exchange.getOut().getHeader(Exchange.HTTP_RESPONSE_CODE)); + assertEquals("OPTIONS,GET", exchange.getOut().getHeader("ALLOW")); + assertEquals("", exchange.getOut().getBody(String.class)); + + exchange = fluentTemplate.to("undertow:http://localhost:" + getPort() + "/users/v1/123").withHeader(Exchange.HTTP_METHOD, "OPTIONS").send(); + assertEquals(200, exchange.getOut().getHeader(Exchange.HTTP_RESPONSE_CODE)); + assertEquals("OPTIONS,PUT", exchange.getOut().getHeader("ALLOW")); + assertEquals("", exchange.getOut().getBody(String.class)); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + // configure to use undertow on localhost with the given port + restConfiguration().component("undertow").host("localhost").port(getPort()); + + // use the rest DSL to define the rest services + rest("/users/") + .get("v1/customers") + .to("mock:customers") + .put("v1/{id}") + .to("mock:id"); + } + }; + } + +}
