http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/750916a9/juneau-microservice/juneau-microservice-test/src/test/java/org/apache/juneau/rest/test/RestHooksInitTest.java ---------------------------------------------------------------------- diff --git a/juneau-microservice/juneau-microservice-test/src/test/java/org/apache/juneau/rest/test/RestHooksInitTest.java b/juneau-microservice/juneau-microservice-test/src/test/java/org/apache/juneau/rest/test/RestHooksInitTest.java new file mode 100644 index 0000000..0743682 --- /dev/null +++ b/juneau-microservice/juneau-microservice-test/src/test/java/org/apache/juneau/rest/test/RestHooksInitTest.java @@ -0,0 +1,112 @@ +// *************************************************************************************************************************** +// * 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.juneau.rest.test; + +import static org.apache.juneau.rest.test.TestUtils.*; + +import java.util.*; + +import org.apache.juneau.rest.client.*; +import org.junit.*; + +/** + * Validates the behavior of the @RestHook(INIT/POST_INIT/POST_INIT_CHILD_FIRST) annotations. + */ +public class RestHooksInitTest extends RestTestcase { + + private static String URL = "/testRestHooksInit"; + + //==================================================================================================== + // @RestHook(INIT) + //==================================================================================================== + @Test + public void testInit() throws Exception { + RestClient client = TestMicroservice.DEFAULT_CLIENT; + String e; + Object r; + + r = client.doGet(URL + "/super/init").getResponse(List.class); + e = "['super-1a','super-1b','super-1c','super-2a']"; + assertObjectEquals(e, r); + + r = client.doGet(URL + "/sub/init").getResponse(List.class); + e = "['sub-1a','sub-1b','sub-1c','super-2a','sub-2b']"; + assertObjectEquals(e, r); + + r = client.doGet(URL + "/sub/child/init").getResponse(List.class); + e = "['super-1a','super-1b','child-1c','super-2a','child-2b']"; + assertObjectEquals(e, r); + } + + //==================================================================================================== + // @RestHook(POST_INIT) + //==================================================================================================== + @Test + public void testPostInit() throws Exception { + RestClient client = TestMicroservice.DEFAULT_CLIENT; + String e; + Object r; + + r = client.doGet(URL + "/super/postInit").getResponse(List.class); + e = "['super-1a','super-1b','super-1c','super-2a']"; + assertObjectEquals(e, r); + + r = client.doGet(URL + "/sub/postInit").getResponse(List.class); + e = "['sub-1a','sub-1b','sub-1c','super-2a','sub-2b']"; + assertObjectEquals(e, r); + + r = client.doGet(URL + "/sub/child/postInit").getResponse(List.class); + e = "['super-1a','super-1b','child-1c','super-2a','child-2b']"; + assertObjectEquals(e, r); + } + + //==================================================================================================== + // @RestHook(POST_INIT_CHILD_FIRST) + //==================================================================================================== + @Test + public void testPostInitChildFirst() throws Exception { + RestClient client = TestMicroservice.DEFAULT_CLIENT; + String e; + Object r; + + r = client.doGet(URL + "/super/postInitChildFirst").getResponse(List.class); + e = "['super-1a','super-1b','super-1c','super-2a']"; + assertObjectEquals(e, r); + + r = client.doGet(URL + "/sub/postInitChildFirst").getResponse(List.class); + e = "['sub-1a','sub-1b','sub-1c','super-2a','sub-2b']"; + assertObjectEquals(e, r); + + r = client.doGet(URL + "/sub/child/postInitChildFirst").getResponse(List.class); + e = "['super-1a','super-1b','child-1c','super-2a','child-2b']"; + assertObjectEquals(e, r); + } + + //==================================================================================================== + // @RestHook(POST_INIT/POST_INIT_CHILD_FIRST) orders + //==================================================================================================== + @Test + public void testPostInitChildFirstOrder() throws Exception { + RestClient client = TestMicroservice.DEFAULT_CLIENT; + String e; + Object r; + + r = client.doGet(URL + "/sub/postInitOrder").getResponse(String.class); + e = "'CHILD'"; + assertObjectEquals(e, r); + + r = client.doGet(URL + "/sub/postInitChildFirstOrder").getResponse(String.class); + e = "'PARENT'"; + assertObjectEquals(e, r); + } +}
http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/750916a9/juneau-microservice/juneau-microservice-test/src/test/java/org/apache/juneau/rest/test/RestHooksTest.java ---------------------------------------------------------------------- diff --git a/juneau-microservice/juneau-microservice-test/src/test/java/org/apache/juneau/rest/test/RestHooksTest.java b/juneau-microservice/juneau-microservice-test/src/test/java/org/apache/juneau/rest/test/RestHooksTest.java new file mode 100644 index 0000000..0e85392 --- /dev/null +++ b/juneau-microservice/juneau-microservice-test/src/test/java/org/apache/juneau/rest/test/RestHooksTest.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.juneau.rest.test; + +import static org.apache.juneau.rest.test.TestUtils.*; +import static org.junit.Assert.*; + +import java.util.*; + +import org.apache.http.*; +import org.apache.juneau.rest.client.*; +import org.junit.*; + +/** + * Validates the behavior of the @RestHook(START/PRE/POST) annotations. + */ +public class RestHooksTest extends RestTestcase { + + private static String URL = "/testRestHooks"; + + //==================================================================================================== + // @RestHook(START) + //==================================================================================================== + @Test + public void testStart() throws Exception { + RestClient client = TestMicroservice.DEFAULT_CLIENT; + String e; + Object r; + + r = client.doGet(URL + "/start").getResponse(Map.class); + e = "{'1':'true','2':'true','3':'true','4':'true'}"; + assertObjectEquals(e, r); + } + + //==================================================================================================== + // @RestHook(START) + //==================================================================================================== + @Test + public void testPre() throws Exception { + RestClient client = TestMicroservice.DEFAULT_CLIENT; + String e; + Object r; + + r = client.doGet(URL + "/pre").getResponse(Map.class); + e = "{'1':'true','2':'true','3':'true','4':'true'}"; + assertObjectEquals(e, r); + } + + //==================================================================================================== + // @RestHook(POST) + //==================================================================================================== + @Test + public void testPost() throws Exception { + RestClient client = TestMicroservice.DEFAULT_CLIENT; + + HttpResponse res = client.doGet(URL + "/post").getResponse(); + assertEquals("true", res.getFirstHeader("post1-called").getValue()); + assertEquals("true", res.getFirstHeader("post2-called").getValue()); + assertEquals("true", res.getFirstHeader("post3-called").getValue()); + assertEquals("true", res.getFirstHeader("post4-called").getValue()); + } +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/750916a9/juneau-microservice/juneau-microservice-test/src/test/java/org/apache/juneau/rest/test/RestTestcase.java ---------------------------------------------------------------------- diff --git a/juneau-microservice/juneau-microservice-test/src/test/java/org/apache/juneau/rest/test/RestTestcase.java b/juneau-microservice/juneau-microservice-test/src/test/java/org/apache/juneau/rest/test/RestTestcase.java new file mode 100644 index 0000000..983180e --- /dev/null +++ b/juneau-microservice/juneau-microservice-test/src/test/java/org/apache/juneau/rest/test/RestTestcase.java @@ -0,0 +1,89 @@ +// *************************************************************************************************************************** +// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * +// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * +// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * +// * with the License. You may obtain a copy of the License at * +// * * +// * http://www.apache.org/licenses/LICENSE-2.0 * +// * * +// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * +// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * +// * specific language governing permissions and limitations under the License. * +// *************************************************************************************************************************** +package org.apache.juneau.rest.test; + +import java.io.*; +import java.util.*; + +import org.apache.juneau.parser.*; +import org.apache.juneau.rest.client.*; +import org.apache.juneau.serializer.*; +import org.junit.*; + +/** + * Superclass of REST testcases that start up the REST test microservice before running the tests locally. + * + * @author James Bognar ([email protected]) + */ +public class RestTestcase { + + private static boolean microserviceStarted; + + // Reusable RestClients keyed by label that live for the duration of a testcase class. + private static Map<String,RestClient> clients = new LinkedHashMap<String,RestClient>(); + + // Reusable object cache that lives for the duration of a testcase class. + private static Map<String,Object> cache = new LinkedHashMap<String,Object>(); + + @BeforeClass + public static void setUp() { + microserviceStarted = TestMicroservice.startMicroservice(); + } + + /** + * Creates a REST client against the test microservice using the specified serializer and parser. + * Client is automatically closed on tear-down. + */ + protected RestClient getClient(String label, Serializer serializer, Parser parser) { + if (! clients.containsKey(label)) + clients.put(label, TestMicroservice.client(serializer, parser).pooled().build()); + return clients.get(label); + } + + /** + * Same as {@link #getClient(String, Serializer, Parser)} but sets the debug flag on the client. + */ + protected RestClient getDebugClient(String label, Serializer serializer, Parser parser) { + if (! clients.containsKey(label)) + clients.put(label, TestMicroservice.client(serializer, parser).debug().build()); + return clients.get(label); + } + + protected void addClientToLifecycle(RestClient c) { + clients.put(UUID.randomUUID().toString(), c); + } + + @SuppressWarnings("unchecked") + protected <T> T getCached(String label, Class<T> c) { + return (T)cache.get(label); + } + + protected void cache(String label, Object o) { + cache.put(label, o); + } + + @AfterClass + public static void tearDown() { + if (microserviceStarted) + TestMicroservice.stopMicroservice(); + for (RestClient rc : clients.values()) { + try { + rc.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + clients.clear(); + cache.clear(); + } +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/750916a9/juneau-microservice/juneau-microservice-test/src/test/java/org/apache/juneau/rest/test/RestUtilsTest.java ---------------------------------------------------------------------- diff --git a/juneau-microservice/juneau-microservice-test/src/test/java/org/apache/juneau/rest/test/RestUtilsTest.java b/juneau-microservice/juneau-microservice-test/src/test/java/org/apache/juneau/rest/test/RestUtilsTest.java new file mode 100644 index 0000000..9a210f9 --- /dev/null +++ b/juneau-microservice/juneau-microservice-test/src/test/java/org/apache/juneau/rest/test/RestUtilsTest.java @@ -0,0 +1,189 @@ +// *************************************************************************************************************************** +// * 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.juneau.rest.test; + +import static org.apache.juneau.internal.StringUtils.*; +import static org.apache.juneau.rest.RestUtils.*; +import static org.junit.Assert.*; + +import org.junit.*; + +public class RestUtilsTest extends RestTestcase { + + //==================================================================================================== + // decode(String) + //==================================================================================================== + @Test + public void testDecode() throws Exception { + assertNull(urlDecode(null)); + assertEquals("foo/bar baz bing", urlDecode("foo%2Fbar+baz++bing")); + } + + //==================================================================================================== + // encode(String) + //==================================================================================================== + @Test + public void testEncode() throws Exception { + assertNull(urlEncode(null)); + assertEquals("foo%2Fbar+baz++bing", urlEncode("foo/bar baz bing")); + assertEquals("foobar", urlEncode("foobar")); + assertEquals("+", urlEncode(" ")); + assertEquals("%2F", urlEncode("/")); + } + + //==================================================================================================== + // trimPathInfo(String,String) + //==================================================================================================== + @Test + public void testGetServletURI() throws Exception { + String e, sp, cp; + + e = "http://hostname"; + sp = ""; + cp = ""; + + for (String s : new String[]{ + "http://hostname", + "http://hostname/foo", + "http://hostname?foo", + "http://hostname/?foo"}) + assertEquals(e, trimPathInfo(new StringBuffer(s), cp, sp).toString()); + + for (String s : new String[]{ + "http:/hostname?foo"}) { + try { + trimPathInfo(new StringBuffer(s), cp, sp); + fail("Exception expected - " + s); + } catch (RuntimeException ex) {} + } + + + e = "http://hostname"; + sp = "/"; + cp = "/"; + + for (String s : new String[]{ + "http://hostname", + "http://hostname/foo", + "http://hostname?foo", + "http://hostname/?foo"}) + assertEquals(e, trimPathInfo(new StringBuffer(s), cp, sp).toString()); + + e = "http://hostname/foo"; + sp = "/foo"; + cp = "/"; + + for (String s : new String[]{ + "http://hostname/foo", + "http://hostname/foo/bar", + "http://hostname/foo?bar"}) + assertEquals(e, trimPathInfo(new StringBuffer(s), cp, sp).toString()); + + for (String s : new String[]{ + "http://hostname/foo2", + "http://hostname/fo2", + "http://hostname?foo", + "http://hostname/fo?bar", + "http:/hostname/foo"}) { + try { + trimPathInfo(new StringBuffer(s), cp, sp); + fail("Exception expected - " + s); + } catch (RuntimeException ex) {} + } + + e = "http://hostname/foo/bar"; + sp = "/foo/bar"; + cp = "/"; + + for (String s : new String[]{ + "http://hostname/foo/bar", + "http://hostname/foo/bar/baz", + "http://hostname/foo/bar?baz"}) + assertEquals(e, trimPathInfo(new StringBuffer(s), cp, sp).toString()); + + for (String s : new String[]{ + "http://hostname/foo2/bar", + "http://hostname/foo/bar2" + }) { + try { + trimPathInfo(new StringBuffer(s), cp, sp); + fail("Exception expected - " + s); + } catch (RuntimeException ex) {} + } + + e = "http://hostname/foo/bar"; + sp = "/bar"; + cp = "/foo"; + + for (String s : new String[]{ + "http://hostname/foo/bar", + "http://hostname/foo/bar/baz", + "http://hostname/foo/bar?baz"}) + assertEquals(e, trimPathInfo(new StringBuffer(s), cp, sp).toString()); + + for (String s : new String[]{ + "http://hostname/foo2/bar", + "http://hostname/foo/bar2" + }) { + try { + trimPathInfo(new StringBuffer(s), cp, sp); + fail("Exception expected - " + s); + } catch (RuntimeException ex) {} + } + } + + //==================================================================================================== + // trimSlashes(String) + //==================================================================================================== + @Test + public void testTrimSlashes() throws Exception { + assertNull(trimSlashes(null)); + assertEquals("", trimSlashes("")); + assertEquals("", trimSlashes("/")); + assertEquals("", trimSlashes("//")); + assertEquals("foo/bar", trimSlashes("foo/bar")); + assertEquals("foo/bar", trimSlashes("foo/bar//")); + assertEquals("foo/bar", trimSlashes("/foo/bar//")); + assertEquals("foo/bar", trimSlashes("//foo/bar//")); + } + + //==================================================================================================== + // trimTrailingSlashes(String) + //==================================================================================================== + @Test + public void testTrimTrailingSlashes() throws Exception { + assertNull(trimTrailingSlashes((String)null)); + assertEquals("", trimTrailingSlashes("")); + assertEquals("", trimTrailingSlashes("/")); + assertEquals("", trimTrailingSlashes("//")); + assertEquals("foo/bar", trimTrailingSlashes("foo/bar")); + assertEquals("foo/bar", trimTrailingSlashes("foo/bar//")); + assertEquals("/foo/bar", trimTrailingSlashes("/foo/bar//")); + assertEquals("//foo/bar", trimTrailingSlashes("//foo/bar//")); + } + + //==================================================================================================== + // trimTrailingSlashes(StringBuffer) + //==================================================================================================== + @Test + public void testTrimTrailingSlashes2() throws Exception { + assertNull(trimTrailingSlashes((StringBuffer)null)); + assertEquals("", trimTrailingSlashes(new StringBuffer("")).toString()); + assertEquals("", trimTrailingSlashes(new StringBuffer("/")).toString()); + assertEquals("", trimTrailingSlashes(new StringBuffer("//")).toString()); + assertEquals("foo/bar", trimTrailingSlashes(new StringBuffer("foo/bar")).toString()); + assertEquals("foo/bar", trimTrailingSlashes(new StringBuffer("foo/bar//")).toString()); + assertEquals("/foo/bar", trimTrailingSlashes(new StringBuffer("/foo/bar//")).toString()); + assertEquals("//foo/bar", trimTrailingSlashes(new StringBuffer("//foo/bar//")).toString()); + } +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/750916a9/juneau-microservice/juneau-microservice-test/src/test/java/org/apache/juneau/rest/test/SerializersTest.java ---------------------------------------------------------------------- diff --git a/juneau-microservice/juneau-microservice-test/src/test/java/org/apache/juneau/rest/test/SerializersTest.java b/juneau-microservice/juneau-microservice-test/src/test/java/org/apache/juneau/rest/test/SerializersTest.java new file mode 100644 index 0000000..ec17178 --- /dev/null +++ b/juneau-microservice/juneau-microservice-test/src/test/java/org/apache/juneau/rest/test/SerializersTest.java @@ -0,0 +1,130 @@ +// *************************************************************************************************************************** +// * 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.juneau.rest.test; + +import static javax.servlet.http.HttpServletResponse.*; +import static org.apache.juneau.rest.test.TestUtils.*; +import static org.junit.Assert.*; + +import org.apache.juneau.rest.client.*; +import org.junit.*; + +public class SerializersTest extends RestTestcase { + + private static String URL = "/testSerializers"; + private static boolean debug = false; + private RestClient client = TestMicroservice.DEFAULT_CLIENT; + + + //==================================================================================================== + // Serializer defined on class. + //==================================================================================================== + @Test + public void testSerializerOnClass() throws Exception { + String url = URL + "/testSerializerOnClass"; + + String r = client.doGet(url).accept("text/a").getResponseAsString(); + assertEquals("text/a - test1", r); + + try { + client.doGet(url + "?noTrace=true").accept("text/b").getResponseAsString(); + fail("Exception expected"); + } catch (RestCallException e) { + checkErrorResponse(debug, e, SC_NOT_ACCEPTABLE, + "Unsupported media-type in request header 'Accept': 'text/b'", + "Supported media-types: ['text/a',"); + } + + r = client.doGet(url).accept("text/json").getResponseAsString(); + assertEquals("\"test1\"", r); + } + + //==================================================================================================== + // Serializer defined on method. + //==================================================================================================== + @Test + public void testSerializerOnMethod() throws Exception { + String url = URL + "/testSerializerOnMethod"; + + try { + client.doGet(url + "?noTrace=true").accept("text/a").getResponseAsString(); + fail("Exception expected"); + } catch (RestCallException e) { + checkErrorResponse(debug, e, SC_NOT_ACCEPTABLE, + "Unsupported media-type in request header 'Accept': 'text/a'", + "Supported media-types: ['text/b']" + ); + } + + try { + client.doGet(url + "?noTrace=true").accept("text/json").getResponseAsString(); + fail("Exception expected"); + } catch (RestCallException e) { + checkErrorResponse(debug, e, SC_NOT_ACCEPTABLE, + "Unsupported media-type in request header 'Accept': 'text/json'", + "Supported media-types: ['text/b']" + ); + } + } + + //==================================================================================================== + // Serializer overridden on method. + //==================================================================================================== + @Test + public void testSerializerOverriddenOnMethod() throws Exception { + String url = URL + "/testSerializerOverriddenOnMethod"; + + String r = client.doGet(url).accept("text/a").getResponseAsString(); + assertEquals("text/c - test3", r); + + r = client.doGet(url).accept("text/b").getResponseAsString(); + assertEquals("text/b - test3", r); + + r = client.doGet(url).accept("text/json").getResponseAsString(); + assertEquals("\"test3\"", r); + } + + //==================================================================================================== + // Serializer with different Accept than Content-Type. + //==================================================================================================== + @Test + public void testSerializerWithDifferentMediaTypes() throws Exception { + String url = URL + "/testSerializerWithDifferentMediaTypes"; + + String r = client.doGet(url).accept("text/a").getResponseAsString(); + assertEquals("text/d - test4", r); + + r = client.doGet(url).accept("text/d").getResponseAsString(); + assertEquals("text/d - test4", r); + + r = client.doGet(url).accept("text/json").getResponseAsString(); + assertEquals("\"test4\"", r); + } + + //==================================================================================================== + // Check for valid 406 error response. + //==================================================================================================== + @Test + public void test406() throws Exception { + String url = URL + "/test406"; + + try { + client.doGet(url + "?noTrace=true").accept("text/bad").getResponseAsString(); + fail("Exception expected"); + } catch (RestCallException e) { + checkErrorResponse(debug, e, SC_NOT_ACCEPTABLE, + "Unsupported media-type in request header 'Accept': 'text/bad'", + "Supported media-types: ['text/a"); + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/750916a9/juneau-microservice/juneau-microservice-test/src/test/java/org/apache/juneau/rest/test/StaticFilesTest.java ---------------------------------------------------------------------- diff --git a/juneau-microservice/juneau-microservice-test/src/test/java/org/apache/juneau/rest/test/StaticFilesTest.java b/juneau-microservice/juneau-microservice-test/src/test/java/org/apache/juneau/rest/test/StaticFilesTest.java new file mode 100644 index 0000000..42c4c9b --- /dev/null +++ b/juneau-microservice/juneau-microservice-test/src/test/java/org/apache/juneau/rest/test/StaticFilesTest.java @@ -0,0 +1,53 @@ +// *************************************************************************************************************************** +// * 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.juneau.rest.test; + +import static org.junit.Assert.*; + +import org.apache.juneau.rest.client.*; +import org.junit.*; + +public class StaticFilesTest extends RestTestcase { + + private static String URL = "/testStaticFiles"; + + //==================================================================================================== + // Tests the @RestResource(staticFiles) annotation. + //==================================================================================================== + @Test + public void testXdocs() throws Exception { + RestClient client = TestMicroservice.DEFAULT_CLIENT_PLAINTEXT; + String r; + String url = URL + "/xdocs"; + + r = client.doGet(url + "/test.txt").getResponseAsString(); + assertTrue(r.endsWith("OK-1")); + r = client.doGet(url + "/xdocs/test.txt").getResponseAsString(); + assertTrue(r.endsWith("OK-2")); + + // For security reasons, paths containing ".." should always return 404. + try { + client.doGet(url + "/xdocs/../test.txt?noTrace=true").connect(); + fail("404 exception expected"); + } catch (RestCallException e) { + assertEquals(404, e.getResponseCode()); + } + + try { + client.doGet(url + "/xdocs/%2E%2E/test.txt?noTrace=true").connect(); + fail("404 exception expected"); + } catch (RestCallException e) { + assertEquals(404, e.getResponseCode()); + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/750916a9/juneau-microservice/juneau-microservice-test/src/test/java/org/apache/juneau/rest/test/TestMicroservice.java ---------------------------------------------------------------------- diff --git a/juneau-microservice/juneau-microservice-test/src/test/java/org/apache/juneau/rest/test/TestMicroservice.java b/juneau-microservice/juneau-microservice-test/src/test/java/org/apache/juneau/rest/test/TestMicroservice.java new file mode 100644 index 0000000..03e50e3 --- /dev/null +++ b/juneau-microservice/juneau-microservice-test/src/test/java/org/apache/juneau/rest/test/TestMicroservice.java @@ -0,0 +1,136 @@ +// *************************************************************************************************************************** +// * 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.juneau.rest.test; + +import java.net.*; +import java.security.*; +import java.util.*; + +import javax.net.ssl.*; + +import org.apache.http.conn.ssl.*; +import org.apache.http.impl.client.*; +import org.apache.juneau.microservice.*; +import org.apache.juneau.parser.*; +import org.apache.juneau.plaintext.*; +import org.apache.juneau.rest.client.*; +import org.apache.juneau.serializer.*; + +/** + * Utility class for starting up the tests microservice. + * @author james.bognar + */ +public class TestMicroservice { + + static RestMicroservice microservice; + static URI microserviceURI; + + // Reusable HTTP clients that get created and shut down with the microservice. + public static RestClient DEFAULT_CLIENT, DEFAULT_CLIENT_DEBUG; + public static RestClient DEFAULT_CLIENT_PLAINTEXT; + + /** + * Starts the microservice. + * @return <jk>true</jk> if the service started, <jk>false</jk> if it's already started. + * If this returns <jk>false</jk> then don't call stopMicroservice()!. + */ + public static boolean startMicroservice() { + if (microservice != null) + return false; + try { + Locale.setDefault(Locale.US); + microservice = new RestMicroservice() + .setConfig("juneau-microservice-test.cfg", false) + .setManifestContents( + "Test-Entry: test-value" + ); + microserviceURI = microservice.start().getURI(); + DEFAULT_CLIENT = client().build(); + DEFAULT_CLIENT_DEBUG = client().debug().build(); + DEFAULT_CLIENT_PLAINTEXT = client(PlainTextSerializer.class, PlainTextParser.class).build(); + return true; + } catch (Throwable e) { + System.err.println(e); // NOT DEBUG + return false; + } + } + + /** + * Returns the URI of the microservice. + * @return The URI of the microservice. + */ + public static URI getURI() { + if (microservice == null) + startMicroservice(); + return microserviceURI; + } + + /** + * Stops the microservice. + */ + public static void stopMicroservice() { + try { + microservice.stop(); + microservice = null; + DEFAULT_CLIENT.closeQuietly(); + DEFAULT_CLIENT_PLAINTEXT.closeQuietly(); + + } catch (Exception e) { + System.err.println(e); // NOT DEBUG + } + } + + /** + * Create a new HTTP client. + */ + public static RestClientBuilder client() { + try { + return new RestClientBuilder() + .rootUrl(microserviceURI) + .noTrace() + ; + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + /** + * Create a new HTTP client using the specified serializer and parser. + */ + public static RestClientBuilder client(Serializer s, Parser p) { + return client().serializer(s).parser(p); + } + + /** + * Create a new HTTP client using the specified serializer and parser. + */ + public static RestClientBuilder client(Class<? extends Serializer> s, Class<? extends Parser> p) { + return client().serializer(s).parser(p); + } + + // TODO - Why is this needed? + static SSLConnectionSocketFactory getSSLSocketFactory() throws Exception { + SSLContext sslContext = SSLContext.getInstance("SSL"); + TrustManager tm = new SimpleX509TrustManager(true); + sslContext.init(null, new TrustManager[]{tm}, new SecureRandom()); + return new SSLConnectionSocketFactory(sslContext, new NoopHostnameVerifier()); + } + + public static CloseableHttpClient createHttpClient() { + try { + return HttpClients.custom().setSSLSocketFactory(getSSLSocketFactory()).setRedirectStrategy(new LaxRedirectStrategy()).build(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } +}
