http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/1b4f98a0/org.apache.juneau.server.test/src/main/java/org/apache/juneau/server/TestTransforms.java ---------------------------------------------------------------------- diff --git a/org.apache.juneau.server.test/src/main/java/org/apache/juneau/server/TestTransforms.java b/org.apache.juneau.server.test/src/main/java/org/apache/juneau/server/TestTransforms.java new file mode 100755 index 0000000..d5673e7 --- /dev/null +++ b/org.apache.juneau.server.test/src/main/java/org/apache/juneau/server/TestTransforms.java @@ -0,0 +1,115 @@ +/*************************************************************************************************************************** + * 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.server; + + +import org.apache.juneau.*; +import org.apache.juneau.parser.*; +import org.apache.juneau.serializer.*; +import org.apache.juneau.server.annotation.*; +import org.apache.juneau.transform.*; + +/** + * JUnit automated testcase resource. + */ +@RestResource( + path="/testTransforms", + transforms={TestTransforms.TransformA2.class} +) +public class TestTransforms extends TestTransformsParent { + private static final long serialVersionUID = 1L; + + //==================================================================================================== + // Test class transform overrides parent class transform + // Should return "A2-1". + //==================================================================================================== + @RestMethod(name="GET", path="/testClassTransformOverridesParentClassTransform") + public A testClassTransformOverridesParentClassTransform() { + return new A(); + } + @RestMethod(name="PUT", path="/testClassTransformOverridesParentClassTransform") + public A test1b(@Content A a) { + return a; + } + @RestMethod(name="PUT", path="/testClassTransformOverridesParentClassTransform/{a}") + public A test1c(@Attr A a) { + return a; + } + + //==================================================================================================== + // Test method transform overrides class transform + // Should return "A3-1". + //==================================================================================================== + @RestMethod(name="GET", path="/testMethodTransformOverridesClassTransform", transforms={TransformA3.class}) + public A test2a() { + return new A(); + } + @RestMethod(name="PUT", path="/testMethodTransformOverridesClassTransform", transforms={TransformA3.class}) + public A test2b(@Content A a) { + return a; + } + @RestMethod(name="PUT", path="/testMethodTransformOverridesClassTransform/{a}", transforms={TransformA3.class}) + public A test2c(@Attr A a) { + return a; + } + + + public static class A { + public int f1; + } + + public static class TransformA1 extends PojoTransform<A,String> { + @Override /* PojoTransform */ + public String transform(A a) throws SerializeException { + return "A1-" + a.f1; + } + @Override /* PojoTransform */ + public A normalize(String in, ClassMeta<?> hint) throws ParseException { + if (! in.startsWith("A1")) + throw new RuntimeException("Invalid input for TransformA1!"); + A a = new A(); + a.f1 = Integer.parseInt(in.substring(3)); + return a; + } + } + + public static class TransformA2 extends PojoTransform<A,String> { + @Override /* PojoTransform */ + public String transform(A a) throws SerializeException { + return "A2-" + a.f1; + } + @Override /* PojoTransform */ + public A normalize(String in, ClassMeta<?> hint) throws ParseException { + if (! in.startsWith("A2")) + throw new RuntimeException("Invalid input for TransformA2!"); + A a = new A(); + a.f1 = Integer.parseInt(in.substring(3)); + return a; + } + } + + public static class TransformA3 extends PojoTransform<A,String> { + @Override /* PojoTransform */ + public String transform(A a) throws SerializeException { + return "A3-" + a.f1; + } + @Override /* PojoTransform */ + public A normalize(String in, ClassMeta<?> hint) throws ParseException { + if (! in.startsWith("A3")) + throw new RuntimeException("Invalid input for TransformA3!"); + A a = new A(); + a.f1 = Integer.parseInt(in.substring(3)); + return a; + } + } +}
http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/1b4f98a0/org.apache.juneau.server.test/src/main/java/org/apache/juneau/server/TestTransformsParent.java ---------------------------------------------------------------------- diff --git a/org.apache.juneau.server.test/src/main/java/org/apache/juneau/server/TestTransformsParent.java b/org.apache.juneau.server.test/src/main/java/org/apache/juneau/server/TestTransformsParent.java new file mode 100755 index 0000000..986083d --- /dev/null +++ b/org.apache.juneau.server.test/src/main/java/org/apache/juneau/server/TestTransformsParent.java @@ -0,0 +1,25 @@ +/*************************************************************************************************************************** + * 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.server; + +import org.apache.juneau.server.annotation.*; + +/** + * JUnit automated testcase resource. + */ +@RestResource( + transforms={TestTransforms.TransformA1.class} +) +public class TestTransformsParent extends RestServletDefault { + private static final long serialVersionUID = 1L; +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/1b4f98a0/org.apache.juneau.server.test/src/main/java/org/apache/juneau/server/TestUris.java ---------------------------------------------------------------------- diff --git a/org.apache.juneau.server.test/src/main/java/org/apache/juneau/server/TestUris.java b/org.apache.juneau.server.test/src/main/java/org/apache/juneau/server/TestUris.java new file mode 100755 index 0000000..b2db727 --- /dev/null +++ b/org.apache.juneau.server.test/src/main/java/org/apache/juneau/server/TestUris.java @@ -0,0 +1,120 @@ +/*************************************************************************************************************************** + * 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.server; + +import org.apache.juneau.*; +import org.apache.juneau.server.annotation.*; + +@RestResource( + path="/testuris", + children={ + TestUris.Child.class + } +) +public class TestUris extends RestServletDefault { + private static final long serialVersionUID = 1L; + + @RestMethod(name="GET", path="/*") + public ObjectMap test1(RestRequest req) throws Exception { + return getPathInfoObject(req).append("testMethod", "root.test1"); + } + + @RestMethod(name="GET", path="/test2/*") + public ObjectMap test2(RestRequest req) throws Exception { + return getPathInfoObject(req).append("testMethod", "root.test2"); + } + + @RestMethod(name="GET", path="/test3%2Ftest3/*") + public ObjectMap test3(RestRequest req) throws Exception { + return getPathInfoObject(req).append("testMethod", "root.test3"); + } + + @RestMethod(name="GET", path="/test4/test4/*") + public ObjectMap test4(RestRequest req) throws Exception { + return getPathInfoObject(req).append("testMethod", "root.test4"); + } + + @RestResource( + path="/child", + children={ + GrandChild.class + } + ) + public static class Child extends RestServletDefault { + private static final long serialVersionUID = 1L; + + @RestMethod(name="GET", path="/*") + public ObjectMap test1(RestRequest req) throws Exception { + return getPathInfoObject(req).append("testMethod", "child.test1"); + } + + @RestMethod(name="GET", path="/test2/*") + public ObjectMap test2(RestRequest req) throws Exception { + return getPathInfoObject(req).append("testMethod", "child.test2"); + } + + @RestMethod(name="GET", path="/test3%2Ftest3/*") + public ObjectMap test3(RestRequest req) throws Exception { + return getPathInfoObject(req).append("testMethod", "child.test3"); + } + + @RestMethod(name="GET", path="/test4/test4/*") + public ObjectMap test4(RestRequest req) throws Exception { + return getPathInfoObject(req).append("testMethod", "child.test4"); + } + } + + @RestResource( + path="/grandchild" + ) + public static class GrandChild extends RestServletDefault { + private static final long serialVersionUID = 1L; + + @RestMethod(name="GET", path="/*") + public ObjectMap test1(RestRequest req) throws Exception { + return getPathInfoObject(req).append("testMethod", "grandchild.test1"); + } + + @RestMethod(name="GET", path="/test2/*") + public ObjectMap test2(RestRequest req) throws Exception { + return getPathInfoObject(req).append("testMethod", "grandchild.test2"); + } + + @RestMethod(name="GET", path="/test3%2Ftest3/*") + public ObjectMap test3(RestRequest req) throws Exception { + return getPathInfoObject(req).append("testMethod", "grandchild.test3"); + } + + @RestMethod(name="GET", path="/test4/test4/*") + public ObjectMap test4(RestRequest req) throws Exception { + return getPathInfoObject(req).append("testMethod", "grandchild.test4"); + } + } + + static ObjectMap getPathInfoObject(RestRequest req) throws Exception { + ObjectMap m = new ObjectMap(); + m.put("contextPath", req.getContextPath()); + m.put("pathInfo", req.getPathInfo()); + m.put("pathRemainder", req.getPathRemainder()); + m.put("pathTranslated", req.getPathTranslated()); + m.put("requestParentURI", req.getRequestParentURI()); + m.put("requestURI", req.getRequestURI()); + m.put("requestURL", req.getRequestURL()); + m.put("servletPath", req.getServletPath()); + m.put("servletURI", req.getServletURI()); + m.put("testURL1", req.getURL("testURL")); + m.put("testURL2", req.getURL("/testURL")); + m.put("testURL3", req.getURL("http://testURL")); + return m; + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/1b4f98a0/org.apache.juneau.server.test/src/main/java/org/apache/juneau/server/TestUrlContent.java ---------------------------------------------------------------------- diff --git a/org.apache.juneau.server.test/src/main/java/org/apache/juneau/server/TestUrlContent.java b/org.apache.juneau.server.test/src/main/java/org/apache/juneau/server/TestUrlContent.java new file mode 100755 index 0000000..ad0c64d --- /dev/null +++ b/org.apache.juneau.server.test/src/main/java/org/apache/juneau/server/TestUrlContent.java @@ -0,0 +1,58 @@ +/*************************************************************************************************************************** + * 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.server; + +import org.apache.juneau.json.*; +import org.apache.juneau.plaintext.*; +import org.apache.juneau.server.annotation.*; + +/** + * JUnit automated testcase resource. + */ +@RestResource( + path="/testUrlContent", + serializers={PlainTextSerializer.class}, + parsers={JsonParser.class} +) +public class TestUrlContent extends RestServlet { + private static final long serialVersionUID = 1L; + + @RestMethod(name="GET", path="/testString") + public String testString(@Content String content) { + return String.format("class=%s, value=%s", content.getClass().getName(), content.toString()); + } + + @RestMethod(name="GET", path="/testEnum") + public String testEnum(@Content TestEnum content) { + return String.format("class=%s, value=%s", content.getClass().getName(), content.toString()); + } + + public static enum TestEnum { + X1 + } + + @RestMethod(name="GET", path="/testBean") + public String testBean(@Content TestBean content) throws Exception { + return String.format("class=%s, value=%s", content.getClass().getName(), JsonSerializer.DEFAULT_LAX.serialize(content)); + } + + public static class TestBean { + public int f1; + public String f2; + } + + @RestMethod(name="GET", path="/testInt") + public String testString(@Content Integer content) { + return String.format("class=%s, value=%s", content.getClass().getName(), content.toString()); + } +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/1b4f98a0/org.apache.juneau.server.test/src/main/java/org/apache/juneau/server/xdocs/test.txt ---------------------------------------------------------------------- diff --git a/org.apache.juneau.server.test/src/main/java/org/apache/juneau/server/xdocs/test.txt b/org.apache.juneau.server.test/src/main/java/org/apache/juneau/server/xdocs/test.txt new file mode 100755 index 0000000..17116ed --- /dev/null +++ b/org.apache.juneau.server.test/src/main/java/org/apache/juneau/server/xdocs/test.txt @@ -0,0 +1 @@ +OK-1 \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/1b4f98a0/org.apache.juneau.server.test/src/main/java/org/apache/juneau/server/xdocs/xdocs/test.txt ---------------------------------------------------------------------- diff --git a/org.apache.juneau.server.test/src/main/java/org/apache/juneau/server/xdocs/xdocs/test.txt b/org.apache.juneau.server.test/src/main/java/org/apache/juneau/server/xdocs/xdocs/test.txt new file mode 100755 index 0000000..0e35120 --- /dev/null +++ b/org.apache.juneau.server.test/src/main/java/org/apache/juneau/server/xdocs/xdocs/test.txt @@ -0,0 +1 @@ +OK-2 \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/1b4f98a0/org.apache.juneau.server.test/src/test/java/org/apache/juneau/server/CT_JacocoDummy.java ---------------------------------------------------------------------- diff --git a/org.apache.juneau.server.test/src/test/java/org/apache/juneau/server/CT_JacocoDummy.java b/org.apache.juneau.server.test/src/test/java/org/apache/juneau/server/CT_JacocoDummy.java new file mode 100755 index 0000000..8c7ebf7 --- /dev/null +++ b/org.apache.juneau.server.test/src/test/java/org/apache/juneau/server/CT_JacocoDummy.java @@ -0,0 +1,37 @@ +/*************************************************************************************************************************** + * 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.server; + +import java.lang.reflect.*; + +import org.junit.*; + +public class CT_JacocoDummy { + + //==================================================================================================== + // Dummy code to add test coverage in Jacoco. + //==================================================================================================== + @Test + public void accessPrivateConstructorsOnStaticUtilityClasses() throws Exception { + + Class<?>[] classes = new Class[] { + RestUtils.class + }; + + for (Class<?> c : classes) { + Constructor<?> c1 = c.getDeclaredConstructor(); + c1.setAccessible(true); + c1.newInstance(); + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/1b4f98a0/org.apache.juneau.server.test/src/test/java/org/apache/juneau/server/CT_RestUtils.java ---------------------------------------------------------------------- diff --git a/org.apache.juneau.server.test/src/test/java/org/apache/juneau/server/CT_RestUtils.java b/org.apache.juneau.server.test/src/test/java/org/apache/juneau/server/CT_RestUtils.java new file mode 100755 index 0000000..e97ee08 --- /dev/null +++ b/org.apache.juneau.server.test/src/test/java/org/apache/juneau/server/CT_RestUtils.java @@ -0,0 +1,188 @@ +/*************************************************************************************************************************** + * 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.server; + +import static org.apache.juneau.server.RestUtils.*; +import static org.junit.Assert.*; + +import org.junit.*; + +public class CT_RestUtils { + + //==================================================================================================== + // decode(String) + //==================================================================================================== + @Test + public void testDecode() throws Exception { + assertNull(decode(null)); + assertEquals("foo/bar baz bing", decode("foo%2Fbar+baz++bing")); + } + + //==================================================================================================== + // encode(String) + //==================================================================================================== + @Test + public void testEncode() throws Exception { + assertNull(encode(null)); + assertEquals("foo%2Fbar+baz++bing", encode("foo/bar baz bing")); + assertEquals("foobar", encode("foobar")); + assertEquals("+", encode(" ")); + assertEquals("%2F", encode("/")); + } + + //==================================================================================================== + // 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/1b4f98a0/org.apache.juneau.server.test/src/test/java/org/apache/juneau/server/CT_TestAcceptCharset.java ---------------------------------------------------------------------- diff --git a/org.apache.juneau.server.test/src/test/java/org/apache/juneau/server/CT_TestAcceptCharset.java b/org.apache.juneau.server.test/src/test/java/org/apache/juneau/server/CT_TestAcceptCharset.java new file mode 100755 index 0000000..9162208 --- /dev/null +++ b/org.apache.juneau.server.test/src/test/java/org/apache/juneau/server/CT_TestAcceptCharset.java @@ -0,0 +1,123 @@ +/*************************************************************************************************************************** + * 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.server; + +import static javax.servlet.http.HttpServletResponse.*; +import static org.apache.juneau.server.TestUtils.*; +import static org.junit.Assert.*; + +import java.io.*; + +import org.apache.juneau.client.*; +import org.apache.juneau.internal.*; +import org.junit.*; + +public class CT_TestAcceptCharset { + + boolean debug = false; + + //==================================================================================================== + // Test that Q-values are being resolved correctly. + //==================================================================================================== + @Test + public void testQValues() throws Exception { + RestClient client = new TestRestClient().setHeader("Accept", "text/plain"); + + check1(client, "utf-8", "utf-8"); + check1(client, "iso-8859-1", "iso-8859-1"); + check1(client, "bad,utf-8", "utf-8"); + check1(client, "utf-8,bad", "utf-8"); + check1(client, "bad;q=0.9,utf-8;q=0.1", "utf-8"); + check1(client, "bad;q=0.1,utf-8;q=0.9", "utf-8"); + check1(client, "utf-8,iso-8859-1", "utf-8"); + check1(client, "iso-8859-1,utf-8", "utf-8"); + check1(client, "utf-8;q=0.9,iso-8859-1;q=0.1", "utf-8"); + check1(client, "utf-8;q=0.1,iso-8859-1;q=0.9", "iso-8859-1"); + check1(client, "*", "utf-8"); + check1(client, "bad,iso-8859-1;q=0.5,*;q=0.1", "iso-8859-1"); + check1(client, "bad,iso-8859-1;q=0.1,*;q=0.5", "utf-8"); + + client.closeQuietly(); + } + + private void check1(RestClient client, String requestCharset, String responseCharset) throws Exception { + RestCall r; + InputStream is; + String url = "/testAcceptCharset/testQValues"; + r = client.doGet(url).setHeader("Accept-Charset", requestCharset).connect(); + assertTrue(r.getResponse().getFirstHeader("Content-Type").getValue().toLowerCase().contains(responseCharset)); + is = r.getInputStream(); + assertEquals("foo", IOUtils.read(new InputStreamReader(is, responseCharset))); + } + + //==================================================================================================== + // Validate various Accept-Charset variations. + //==================================================================================================== + @Test + public void testCharsetOnResponse() throws Exception { + RestClient client = new TestRestClient().setAccept("text/plain").setContentType("text/plain"); + String url = "/testAcceptCharset/testCharsetOnResponse"; + String r; + + r = client.doPut(url, new StringReader("")).getResponseAsString(); + assertEquals("utf-8/utf-8", r.toLowerCase()); + + r = client.doPut(url, new StringReader("")).setHeader("Accept-Charset", "Shift_JIS").getResponseAsString(); + assertEquals("utf-8/shift_jis", r.toLowerCase()); + + try { + r = client.doPut(url+"?noTrace=true", new StringReader("")).setHeader("Accept-Charset", "BAD").getResponseAsString(); + fail("Exception expected"); + } catch (RestCallException e) { + checkErrorResponse(debug, e, SC_NOT_ACCEPTABLE, "No supported charsets in header 'Accept-Charset': 'BAD'"); + } + + r = client.doPut(url, new StringReader("")).setHeader("Accept-Charset", "UTF-8").getResponseAsString(); + assertEquals("utf-8/utf-8", r.toLowerCase()); + + r = client.doPut(url, new StringReader("")).setHeader("Accept-Charset", "bad,iso-8859-1").getResponseAsString(); + assertEquals("utf-8/iso-8859-1", r.toLowerCase()); + + r = client.doPut(url, new StringReader("")).setHeader("Accept-Charset", "bad;q=0.9,iso-8859-1;q=0.1").getResponseAsString(); + assertEquals("utf-8/iso-8859-1", r.toLowerCase()); + + r = client.doPut(url, new StringReader("")).setHeader("Accept-Charset", "bad;q=0.1,iso-8859-1;q=0.9").getResponseAsString(); + assertEquals("utf-8/iso-8859-1", r.toLowerCase()); + + client.setHeader("Accept-Charset", "utf-8"); + + r = client.doPut(url, new StringReader("")).setHeader("Content-Type", "text/plain").getResponseAsString(); + assertEquals("utf-8/utf-8", r.toLowerCase()); + + r = client.doPut(url, new StringReader("")).setHeader("Content-Type", "text/plain;charset=utf-8").getResponseAsString(); + assertEquals("utf-8/utf-8", r.toLowerCase()); + + r = client.doPut(url, new StringReader("")).setHeader("Content-Type", "text/plain;charset=UTF-8").getResponseAsString(); + assertEquals("utf-8/utf-8", r.toLowerCase()); + + r = client.doPut(url, new StringReader("")).setHeader("Content-Type", "text/plain;charset=iso-8859-1").getResponseAsString(); + assertEquals("iso-8859-1/utf-8", r.toLowerCase()); + + r = client.doPut(url, new StringReader("")).setHeader("Content-Type", "text/plain;charset=Shift_JIS").getResponseAsString(); + assertEquals("shift_jis/utf-8", r.toLowerCase()); + + try { + r = client.doPut(url + "?noTrace=true&Content-Type=text/plain;charset=BAD", new StringReader("")).getResponseAsString(); + fail("Exception expected"); + } catch (RestCallException e) { + checkErrorResponse(debug, e, SC_UNSUPPORTED_MEDIA_TYPE, "Unsupported charset in header 'Content-Type': 'text/plain;charset=BAD'"); + } + + client.closeQuietly(); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/1b4f98a0/org.apache.juneau.server.test/src/test/java/org/apache/juneau/server/CT_TestBeanContextProperties.java ---------------------------------------------------------------------- diff --git a/org.apache.juneau.server.test/src/test/java/org/apache/juneau/server/CT_TestBeanContextProperties.java b/org.apache.juneau.server.test/src/test/java/org/apache/juneau/server/CT_TestBeanContextProperties.java new file mode 100755 index 0000000..3358d26 --- /dev/null +++ b/org.apache.juneau.server.test/src/test/java/org/apache/juneau/server/CT_TestBeanContextProperties.java @@ -0,0 +1,37 @@ +/*************************************************************************************************************************** + * 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.server; + +import static org.junit.Assert.*; + +import org.apache.juneau.client.*; +import org.apache.juneau.json.*; +import org.junit.*; + +public class CT_TestBeanContextProperties { + + boolean debug = false; + + //==================================================================================================== + // Validate that filters defined on class filter to underlying bean context. + //==================================================================================================== + @Test + public void testClassTransforms() throws Exception { + RestClient client = new TestRestClient(JsonSerializer.class, JsonParser.class); + String r; + r = client.doGet("/testBeanContext/testClassTransforms/2001-07-04T15:30:45Z?d2=2001-07-05T15:30:45Z").setHeader("X-D3", "2001-07-06T15:30:45Z").getResponseAsString(); + assertEquals("d1=2001-07-04T15:30:45Z,d2=2001-07-05T15:30:45Z,d3=2001-07-06T15:30:45Z", r); + + client.closeQuietly(); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/1b4f98a0/org.apache.juneau.server.test/src/test/java/org/apache/juneau/server/CT_TestCallbackStrings.java ---------------------------------------------------------------------- diff --git a/org.apache.juneau.server.test/src/test/java/org/apache/juneau/server/CT_TestCallbackStrings.java b/org.apache.juneau.server.test/src/test/java/org/apache/juneau/server/CT_TestCallbackStrings.java new file mode 100755 index 0000000..c1891d8 --- /dev/null +++ b/org.apache.juneau.server.test/src/test/java/org/apache/juneau/server/CT_TestCallbackStrings.java @@ -0,0 +1,50 @@ +/*************************************************************************************************************************** + * 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.server; + +import static org.junit.Assert.*; + +import org.apache.juneau.client.*; +import org.junit.*; + +public class CT_TestCallbackStrings { + + //==================================================================================================== + // Basic tests using &Content parameter + //==================================================================================================== + @Test + public void test() throws Exception { + RestClient c = new TestRestClient().setAccept("text/json+simple"); + String r; + + r = c.doCallback("GET /testCallback").getResponseAsString(); + assertEquals("{method:'GET',headers:{},content:''}", r); + + r = c.doCallback("GET /testCallback some sample content").getResponseAsString(); + assertEquals("{method:'GET',headers:{},content:'some sample content'}", r); + + r = c.doCallback("GET {Foo-X:123,Foo-Y:'abc'} /testCallback").getResponseAsString(); + assertEquals("{method:'GET',headers:{'Foo-X':'123','Foo-Y':'abc'},content:''}", r); + + r = c.doCallback("GET { Foo-X : 123, Foo-Y : 'abc' } /testCallback").getResponseAsString(); + assertEquals("{method:'GET',headers:{'Foo-X':'123','Foo-Y':'abc'},content:''}", r); + + r = c.doCallback("GET {Foo-X:123,Foo-Y:'abc'} /testCallback some sample content ").getResponseAsString(); + assertEquals("{method:'GET',headers:{'Foo-X':'123','Foo-Y':'abc'},content:'some sample content'}", r); + + r = c.doCallback("PUT {Foo-X:123,Foo-Y:'abc'} /testCallback some sample content ").getResponseAsString(); + assertEquals("{method:'PUT',headers:{'Foo-X':'123','Foo-Y':'abc'},content:'some sample content'}", r); + + c.closeQuietly(); + } +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/1b4f98a0/org.apache.juneau.server.test/src/test/java/org/apache/juneau/server/CT_TestCharsetEncodings.java ---------------------------------------------------------------------- diff --git a/org.apache.juneau.server.test/src/test/java/org/apache/juneau/server/CT_TestCharsetEncodings.java b/org.apache.juneau.server.test/src/test/java/org/apache/juneau/server/CT_TestCharsetEncodings.java new file mode 100755 index 0000000..3b34af6 --- /dev/null +++ b/org.apache.juneau.server.test/src/test/java/org/apache/juneau/server/CT_TestCharsetEncodings.java @@ -0,0 +1,96 @@ +/*************************************************************************************************************************** + * 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.server; + +import static javax.servlet.http.HttpServletResponse.*; +import static org.apache.juneau.server.TestUtils.*; +import static org.junit.Assert.*; + +import java.io.*; + +import org.apache.juneau.client.*; +import org.apache.juneau.internal.*; +import org.junit.*; + + +public class CT_TestCharsetEncodings { + + private static boolean debug = false; + + /** + * Basic tests to ensure that the correct charsets are found and used + * under a variety of scenarios. + */ + @Test + public void test() throws Exception { + String url = "/testCharsetEncodings"; + RestClient client = new TestRestClient().setAccept("text/s").setContentType("text/p"); + InputStream is; + String r; + + r = client.doPut(url, new StringReader("foo")).getResponseAsString(); + if (debug) System.err.println(r); + assertEquals("utf-8/foo/utf-8", r); + + is = client.doPut(url, new StringReader("foo")).getInputStream(); + r = IOUtils.read(new InputStreamReader(is, "utf-8")); + if (debug) System.err.println(r); + assertEquals("utf-8/foo/utf-8", r); + + client.setHeader("Accept-Charset", "utf-8").setContentType("text/p;charset=utf-8"); + is = client.doPut(url, new StringReader("foo")).getInputStream(); + r = IOUtils.read(new InputStreamReader(is, "utf-8")); + if (debug) System.err.println(r); + assertEquals("utf-8/foo/utf-8", r); + + client.setHeader("Accept-Charset", "Shift_JIS").setContentType("text/p;charset=shift_jis"); + is = client.doPut(url, new StringReader("foo")).getInputStream(); + r = IOUtils.read(new InputStreamReader(is, "Shift_JIS")); + if (debug) System.err.println(r); + assertEquals("shift_jis/foo/shift_jis", r); + + try { + client.setHeader("Accept-Charset", "BAD").setContentType("text/p;charset=sjis"); + is = client.doPut(url + "?noTrace=true", new StringReader("foo")).getInputStream(); + r = IOUtils.read(new InputStreamReader(is, "sjis")); + if (debug) System.err.println(r); + fail("Exception expected"); + } catch (RestCallException e) { + checkErrorResponse(debug, e, SC_NOT_ACCEPTABLE, "No supported charsets in header 'Accept-Charset': 'BAD'"); + } + + client.setAccept("text/s").setHeader("Accept-Charset", "utf-8").setContentType("text/p"); + is = client.doPut(url+"?Content-Type=text/p", new StringReader("foo")).getInputStream(); + r = IOUtils.read(new InputStreamReader(is, "utf-8")); + if (debug) System.err.println(r); + assertEquals("utf-8/foo/utf-8", r); + + client.setAccept("text/s").setContentType("text/bad").setHeader("Accept-Charset", "utf-8"); + is = client.doPut(url+"?Content-Type=text/p;charset=utf-8", new StringReader("foo")).getInputStream(); + r = IOUtils.read(new InputStreamReader(is, "utf-8")); + if (debug) System.err.println(r); + assertEquals("utf-8/foo/utf-8", r); + + try { + client.setAccept("text/s").setContentType("text/p").setHeader("Accept-Charset", "utf-8"); + is = client.doPut(url+"?Content-Type=text/p;charset=BAD&noTrace=true", new StringReader("foo")).getInputStream(); + r = IOUtils.read(new InputStreamReader(is, "utf-8")); + if (debug) System.err.println(r); + assertEquals("utf-8/foo/utf-8", r); + fail("Exception expected"); + } catch (RestCallException e) { + checkErrorResponse(debug, e, SC_UNSUPPORTED_MEDIA_TYPE, "Unsupported charset in header 'Content-Type': 'text/p;charset=BAD'"); + } + client.closeQuietly(); + } +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/1b4f98a0/org.apache.juneau.server.test/src/test/java/org/apache/juneau/server/CT_TestClientVersion.java ---------------------------------------------------------------------- diff --git a/org.apache.juneau.server.test/src/test/java/org/apache/juneau/server/CT_TestClientVersion.java b/org.apache.juneau.server.test/src/test/java/org/apache/juneau/server/CT_TestClientVersion.java new file mode 100644 index 0000000..253bcae --- /dev/null +++ b/org.apache.juneau.server.test/src/test/java/org/apache/juneau/server/CT_TestClientVersion.java @@ -0,0 +1,90 @@ +/*************************************************************************************************************************** + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + ***************************************************************************************************************************/ +package org.apache.juneau.server; + +import static org.junit.Assert.*; + +import org.apache.juneau.client.*; +import org.apache.juneau.plaintext.*; +import org.junit.*; + +public class CT_TestClientVersion { + + private static String URL = "/testClientVersion"; + + //==================================================================================================== + // Basic tests - default X-Client-Version header. + //==================================================================================================== + @Test + public void testDefaultHeader() throws Exception { + RestClient c = new TestRestClient(PlainTextSerializer.class, PlainTextParser.class); + String url = URL + "/defaultHeader"; + + assertEquals("no-version", c.doGet(url).getResponseAsString()); + + for (String s : "0, 0.0, 0.1, .1, .9, .99".split("\\s*,\\s*")) { + c.setClientVersion(s); + assertEquals("[0.0,1.0)", c.doGet(url).getResponseAsString()); + } + + for (String s : "1, 1.0, 1.0.0, 1.0.1".split("\\s*,\\s*")) { + c.setClientVersion(s); + assertEquals("[1.0,1.0]", c.doGet(url).getResponseAsString()); + } + + for (String s : "1.1, 1.1.1, 1.2, 1.9.9".split("\\s*,\\s*")) { + c.setClientVersion(s); + assertEquals("[1.1,2)", c.doGet(url).getResponseAsString()); + } + + for (String s : "2, 2.0, 2.1, 9, 9.9".split("\\s*,\\s*")) { + c.setClientVersion(s); + assertEquals("2", c.doGet(url).getResponseAsString()); + } + + c.closeQuietly(); + } + + //==================================================================================================== + // Basic tests - Custom-Client-Version header. + //==================================================================================================== + @Test + public void testCustomHeader() throws Exception { + RestClient c = new TestRestClient(PlainTextSerializer.class, PlainTextParser.class); + String url = URL + "/customHeader"; + + assertEquals("no-version", c.doGet(url).getResponseAsString()); + + for (String s : "0, 0.0, 0.1, .1, .9, .99".split("\\s*,\\s*")) { + c.setHeader("Custom-Client-Version", s); + assertEquals("[0.0,1.0)", c.doGet(url).getResponseAsString()); + } + + for (String s : "1, 1.0, 1.0.0, 1.0.1".split("\\s*,\\s*")) { + c.setHeader("Custom-Client-Version", s); + assertEquals("[1.0,1.0]", c.doGet(url).getResponseAsString()); + } + + for (String s : "1.1, 1.1.1, 1.2, 1.9.9".split("\\s*,\\s*")) { + c.setHeader("Custom-Client-Version", s); + assertEquals("[1.1,2)", c.doGet(url).getResponseAsString()); + } + + for (String s : "2, 2.0, 2.1, 9, 9.9".split("\\s*,\\s*")) { + c.setHeader("Custom-Client-Version", s); + assertEquals("2", c.doGet(url).getResponseAsString()); + } + + c.closeQuietly(); + } +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/1b4f98a0/org.apache.juneau.server.test/src/test/java/org/apache/juneau/server/CT_TestConfig.java ---------------------------------------------------------------------- diff --git a/org.apache.juneau.server.test/src/test/java/org/apache/juneau/server/CT_TestConfig.java b/org.apache.juneau.server.test/src/test/java/org/apache/juneau/server/CT_TestConfig.java new file mode 100755 index 0000000..d2e4844 --- /dev/null +++ b/org.apache.juneau.server.test/src/test/java/org/apache/juneau/server/CT_TestConfig.java @@ -0,0 +1,58 @@ +/*************************************************************************************************************************** + * 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.server; + +import static org.apache.juneau.server.TestUtils.*; +import static org.junit.Assert.*; + +import org.apache.juneau.client.*; +import org.apache.juneau.ini.*; +import org.apache.juneau.json.*; +import org.junit.*; + +public class CT_TestConfig { + + private static String URL = "/testConfig"; + + //==================================================================================================== + // Basic tests + //==================================================================================================== + @Test + public void test() throws Exception { + RestClient c = new TestRestClient(JsonSerializer.class, JsonParser.class).setAccept("text/json+simple"); + + ConfigFile cf = c.doGet(URL).getResponse(ConfigFileImpl.class); + + assertObjectEquals("{int1:'1',int2:'1,2,3',int3:'$C{Test/int1, -1}',int4:'$C{Test/int3, -1}',int5:'$C{XXX, -1}',boolean1:'true',boolean2:'true,true',path:'$E{PATH}',mainClass:'$MF{Main-Class}',importPackage:'$MF{Import-Package}'}", cf.get("Test")); + + assertEquals("'1'", c.doGet(URL + "/Test%2Fint1/" + getName(String.class)).getResponseAsString()); + assertEquals("['1']", c.doGet(URL + "/Test%2Fint1/" + getName(String[].class)).getResponseAsString()); + assertEquals("'1,2,3'", c.doGet(URL + "/Test%2Fint2/" + getName(String.class)).getResponseAsString()); + assertEquals("['1','2','3']", c.doGet(URL + "/Test%2Fint2/" + getName(String[].class)).getResponseAsString()); + assertEquals("[1,2,3]", c.doGet(URL + "/Test%2Fint2/" + getName(int[].class)).getResponseAsString()); + assertEquals("[1,2,3]", c.doGet(URL + "/Test%2Fint2/" + getName(Integer[].class)).getResponseAsString()); + assertEquals("[1]", c.doGet(URL + "/Test%2Fint3/" + getName(int[].class)).getResponseAsString()); + assertEquals("[1]", c.doGet(URL + "/Test%2Fint4/" + getName(int[].class)).getResponseAsString()); + assertEquals("[-1]", c.doGet(URL + "/Test%2Fint5/" + getName(int[].class)).getResponseAsString()); + assertEquals("true", c.doGet(URL + "/Test%2Fboolean1/" + getName(Boolean.class)).getResponseAsString()); + assertEquals("[true,true]", c.doGet(URL + "/Test%2Fboolean2/" + getName(Boolean[].class)).getResponseAsString()); + assertTrue(c.doGet(URL + "/Test%2Fpath/" + getName(String.class)).getResponseAsString().length() > 10); + assertEquals("'org.apache.juneau.microservice.RestMicroservice'", c.doGet(URL + "/Test%2FmainClass/" + getName(String.class)).getResponseAsString()); + + c.closeQuietly(); + } + + private String getName(Class<?> c) { + return RestUtils.encode(c.getName()); + } +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/1b4f98a0/org.apache.juneau.server.test/src/test/java/org/apache/juneau/server/CT_TestContent.java ---------------------------------------------------------------------- diff --git a/org.apache.juneau.server.test/src/test/java/org/apache/juneau/server/CT_TestContent.java b/org.apache.juneau.server.test/src/test/java/org/apache/juneau/server/CT_TestContent.java new file mode 100755 index 0000000..5e3546a --- /dev/null +++ b/org.apache.juneau.server.test/src/test/java/org/apache/juneau/server/CT_TestContent.java @@ -0,0 +1,706 @@ +/*************************************************************************************************************************** + * 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.server; + +import static org.junit.Assert.*; + +import java.io.*; +import java.net.*; + +import org.apache.juneau.client.*; +import org.apache.juneau.json.*; +import org.apache.juneau.plaintext.*; +import org.apache.juneau.urlencoding.*; +import org.junit.*; + +public class CT_TestContent { + + private static String URL = "/testContent"; + + //==================================================================================================== + // Basic tests using &Content parameter + //==================================================================================================== + @Test + public void testUsingContentParam() throws Exception { + RestClient c = new TestRestClient().setAccept("text/json+simple"); + String r; + + // @RestMethod(name="POST", path="/boolean") + // public boolean testBool(@Content boolean b) { + // return b; + // } + r = c.doPost(URL + "/boolean?content=true", null).getResponseAsString(); + assertEquals("true", r); + r = c.doPost(URL + "/boolean?content=(true)", null).getResponseAsString(); + assertEquals("true", r); + r = c.doPost(URL + "/boolean?content=$b(true)", null).getResponseAsString(); + assertEquals("true", r); + r = c.doPost(URL + "/boolean?content=false", null).getResponseAsString(); + assertEquals("false", r); + r = c.doPost(URL + "/boolean?content=(false)", null).getResponseAsString(); + assertEquals("false", r); + r = c.doPost(URL + "/boolean?content=$b(false)", null).getResponseAsString(); + assertEquals("false", r); + try { + r = c.doPost(URL + "/boolean?content=%00&noTrace=true", null).getResponseAsString(); + fail("Exception expected!"); + } catch (RestCallException e) { + assertEquals(400, e.getResponseCode()); + } + try { + r = c.doPost(URL + "/boolean?content=bad&noTrace=true", null).getResponseAsString(); + fail("Exception expected!"); + } catch (RestCallException e) { + assertEquals(400, e.getResponseCode()); + } + + + // @RestMethod(name="POST", path="/Boolean") + // public Boolean testBoolean(@Content Boolean b) { + // return b; + // } + r = c.doPost(URL + "/Boolean?content=true", null).getResponseAsString(); + assertEquals("true", r); + r = c.doPost(URL + "/Boolean?content=(true)", null).getResponseAsString(); + assertEquals("true", r); + r = c.doPost(URL + "/Boolean?content=$b(true)", null).getResponseAsString(); + assertEquals("true", r); + r = c.doPost(URL + "/Boolean?content=false", null).getResponseAsString(); + assertEquals("false", r); + r = c.doPost(URL + "/Boolean?content=(false)", null).getResponseAsString(); + assertEquals("false", r); + r = c.doPost(URL + "/Boolean?content=$b(false)", null).getResponseAsString(); + assertEquals("false", r); + r = c.doPost(URL + "/Boolean?content=%00", null).getResponseAsString(); + assertEquals("null", r); + try { + r = c.doPost(URL + "/Boolean?content=bad&noTrace=true", null).getResponseAsString(); + fail("Exception expected!"); + } catch (RestCallException e) { + assertEquals(400, e.getResponseCode()); + } + + // @RestMethod(name="POST", path="/int") + // public int testInt(@Content int i) { + // return i; + // } + r = c.doPost(URL + "/int?content=-123", null).getResponseAsString(); + assertEquals("-123", r); + r = c.doPost(URL + "/int?content=(-123)", null).getResponseAsString(); + assertEquals("-123", r); + r = c.doPost(URL + "/int?content=$n(-123)", null).getResponseAsString(); + assertEquals("-123", r); + try { + r = c.doPost(URL + "/int?content=%00&noTrace=true", null).getResponseAsString(); + fail("Exception expected!"); + } catch (RestCallException e) { + assertEquals(400, e.getResponseCode()); + } + try { + r = c.doPost(URL + "/int?content=bad&noTrace=true", null).getResponseAsString(); + fail("Exception expected!"); + } catch (RestCallException e) { + assertEquals(400, e.getResponseCode()); + } + + // @RestMethod(name="POST", path="/Integer") + // public Integer testInteger(@Content Integer i) { + // return i; + // } + r = c.doPost(URL + "/Integer?content=-123", null).getResponseAsString(); + assertEquals("-123", r); + r = c.doPost(URL + "/Integer?content=(-123)", null).getResponseAsString(); + assertEquals("-123", r); + r = c.doPost(URL + "/Integer?content=$n(-123)", null).getResponseAsString(); + assertEquals("-123", r); + r = c.doPost(URL + "/Integer?content=%00", null).getResponseAsString(); + assertEquals("null", r); + try { + r = c.doPost(URL + "/Integer?content=bad&noTrace=true", null).getResponseAsString(); + fail("Exception expected!"); + } catch (RestCallException e) { + assertEquals(400, e.getResponseCode()); + } + + // @RestMethod(name="POST", path="/float") + // public float testFloat(@Content float f) { + // return f; + // } + r = c.doPost(URL + "/float?content=-1.23", null).getResponseAsString(); + assertEquals("-1.23", r); + r = c.doPost(URL + "/float?content=(-1.23)", null).getResponseAsString(); + assertEquals("-1.23", r); + r = c.doPost(URL + "/float?content=$n(-1.23)", null).getResponseAsString(); + assertEquals("-1.23", r); + try { + r = c.doPost(URL + "/float?content=%00&noTrace=true", null).getResponseAsString(); + fail("Exception expected!"); + } catch (RestCallException e) { + assertEquals(400, e.getResponseCode()); + } + try { + r = c.doPost(URL + "/float?content=bad&noTrace=true", null).getResponseAsString(); + fail("Exception expected!"); + } catch (RestCallException e) { + assertEquals(400, e.getResponseCode()); + } + + // @RestMethod(name="POST", path="/Float") + // public Float testFloat2(@Content Float f) { + // return f; + // } + r = c.doPost(URL + "/Float?content=-1.23", null).getResponseAsString(); + assertEquals("-1.23", r); + r = c.doPost(URL + "/Float?content=(-1.23)", null).getResponseAsString(); + assertEquals("-1.23", r); + r = c.doPost(URL + "/Float?content=$n(-1.23)", null).getResponseAsString(); + assertEquals("-1.23", r); + r = c.doPost(URL + "/Float?content=%00", null).getResponseAsString(); + assertEquals("null", r); + try { + r = c.doPost(URL + "/Float?content=bad&noTrace=true", null).getResponseAsString(); + fail("Exception expected!"); + } catch (RestCallException e) { + assertEquals(400, e.getResponseCode()); + } + + // @RestMethod(name="POST", path="/Map") + // public TreeMap<String,String> testMap(@Content TreeMap<String,String> m) { + // return m; + // } + r = c.doPost(URL + "/Map?content=(a=b,c=d)", null).getResponseAsString(); + assertEquals("{a:'b',c:'d'}", r); + r = c.doPost(URL + "/Map?content=%00", null).getResponseAsString(); + assertEquals("null", r); + try { + r = c.doPost(URL + "/Map?content=bad&noTrace=true", null).getResponseAsString(); + fail("Exception expected!"); + } catch (RestCallException e) { + assertEquals(400, e.getResponseCode()); + } + + // @RestMethod(name="POST", path="/B") + // public DTO2s.B testPojo1(@Content DTO2s.B b) { + // return b; + // } + DTOs.B b = DTOs.B.create(); + r = c.doPost(URL + "/B?content=" + UonSerializer.DEFAULT.serialize(b), null).getResponseAsString(); + assertEquals("{f1:['a','b'],f2:['c','d'],f3:[1,2],f4:[3,4],f5:[['e','f'],['g','h']],f6:[['i','j'],['k','l']],f7:[{a:'a',b:1,c:true},{a:'a',b:1,c:true}],f8:[{a:'a',b:1,c:true},{a:'a',b:1,c:true}],f9:[[{a:'a',b:1,c:true}],[{a:'a',b:1,c:true}]],f10:[[{a:'a',b:1,c:true}],[{a:'a',b:1,c:true}]],f11:['a','b'],f12:['c','d'],f13:[1,2],f14:[3,4],f15:[['e','f'],['g','h']],f16:[['i','j'],['k','l']],f17:[{a:'a',b:1,c:true},{a:'a',b:1,c:true}],f18:[{a:'a',b:1,c:true},{a:'a',b:1,c:true}],f19:[[{a:'a',b:1,c:true}],[{a:'a',b:1,c:true}]],f20:[[{a:'a',b:1,c:true}],[{a:'a',b:1,c:true}]]}", r); + r = c.doPost(URL + "/B?content=" + UonSerializer.DEFAULT_SIMPLE.serialize(b), null).getResponseAsString(); + assertEquals("{f1:['a','b'],f2:['c','d'],f3:[1,2],f4:[3,4],f5:[['e','f'],['g','h']],f6:[['i','j'],['k','l']],f7:[{a:'a',b:1,c:true},{a:'a',b:1,c:true}],f8:[{a:'a',b:1,c:true},{a:'a',b:1,c:true}],f9:[[{a:'a',b:1,c:true}],[{a:'a',b:1,c:true}]],f10:[[{a:'a',b:1,c:true}],[{a:'a',b:1,c:true}]],f11:['a','b'],f12:['c','d'],f13:[1,2],f14:[3,4],f15:[['e','f'],['g','h']],f16:[['i','j'],['k','l']],f17:[{a:'a',b:1,c:true},{a:'a',b:1,c:true}],f18:[{a:'a',b:1,c:true},{a:'a',b:1,c:true}],f19:[[{a:'a',b:1,c:true}],[{a:'a',b:1,c:true}]],f20:[[{a:'a',b:1,c:true}],[{a:'a',b:1,c:true}]]}", r); + + // @RestMethod(name="POST", path="/C") + // public DTO2s.C testPojo2(@Content DTO2s.C c) { + // return c; + // } + DTOs.C x = DTOs.C.create(); + r = c.doPost(URL + "/C?content=" + UonSerializer.DEFAULT.serialize(x), null).getResponseAsString(); + assertEquals("{f1:['a','b'],f2:['c','d'],f3:[1,2],f4:[3,4],f5:[['e','f'],['g','h']],f6:[['i','j'],['k','l']],f7:[{a:'a',b:1,c:true},{a:'a',b:1,c:true}],f8:[{a:'a',b:1,c:true},{a:'a',b:1,c:true}],f9:[[{a:'a',b:1,c:true}],[{a:'a',b:1,c:true}]],f10:[[{a:'a',b:1,c:true}],[{a:'a',b:1,c:true}]],f11:['a','b'],f12:['c','d'],f13:[1,2],f14:[3,4],f15:[['e','f'],['g','h']],f16:[['i','j'],['k','l']],f17:[{a:'a',b:1,c:true},{a:'a',b:1,c:true}],f18:[{a:'a',b:1,c:true},{a:'a',b:1,c:true}],f19:[[{a:'a',b:1,c:true}],[{a:'a',b:1,c:true}]],f20:[[{a:'a',b:1,c:true}],[{a:'a',b:1,c:true}]]}", r); + r = c.doPost(URL + "/C?content=" + UonSerializer.DEFAULT_SIMPLE.serialize(x), null).getResponseAsString(); + assertEquals("{f1:['a','b'],f2:['c','d'],f3:[1,2],f4:[3,4],f5:[['e','f'],['g','h']],f6:[['i','j'],['k','l']],f7:[{a:'a',b:1,c:true},{a:'a',b:1,c:true}],f8:[{a:'a',b:1,c:true},{a:'a',b:1,c:true}],f9:[[{a:'a',b:1,c:true}],[{a:'a',b:1,c:true}]],f10:[[{a:'a',b:1,c:true}],[{a:'a',b:1,c:true}]],f11:['a','b'],f12:['c','d'],f13:[1,2],f14:[3,4],f15:[['e','f'],['g','h']],f16:[['i','j'],['k','l']],f17:[{a:'a',b:1,c:true},{a:'a',b:1,c:true}],f18:[{a:'a',b:1,c:true},{a:'a',b:1,c:true}],f19:[[{a:'a',b:1,c:true}],[{a:'a',b:1,c:true}]],f20:[[{a:'a',b:1,c:true}],[{a:'a',b:1,c:true}]]}", r); + + c.closeQuietly(); + } + + //==================================================================================================== + // Basic tests using &Content parameter with &Accept=text/json + //==================================================================================================== + @Test + public void testUsingContentParamJsonHeader() throws Exception { + RestClient c = new TestRestClient().setAccept("text/json+simple").setHeader("Content-Type", "text/json"); + String r; + + // @RestMethod(name="POST", path="/boolean") + // public boolean testBool(@Content boolean b) { + // return b; + // } + r = c.doPost(URL + "/boolean?content=true", null).getResponseAsString(); + assertEquals("true", r); + r = c.doPost(URL + "/boolean?content=false", null).getResponseAsString(); + assertEquals("false", r); + try { + r = c.doPost(URL + "/boolean?content=null&noTrace=true", null).getResponseAsString(); + fail("Exception expected!"); + } catch (RestCallException e) { + assertEquals(400, e.getResponseCode()); + } + try { + r = c.doPost(URL + "/boolean?content=bad&noTrace=true", null).getResponseAsString(); + fail("Exception expected!"); + } catch (RestCallException e) { + assertEquals(400, e.getResponseCode()); + } + + + // @RestMethod(name="POST", path="/Boolean") + // public Boolean testBoolean(@Content Boolean b) { + // return b; + // } + r = c.doPost(URL + "/Boolean?content=true", null).getResponseAsString(); + assertEquals("true", r); + r = c.doPost(URL + "/Boolean?content=false", null).getResponseAsString(); + assertEquals("false", r); + r = c.doPost(URL + "/Boolean?content=null", null).getResponseAsString(); + assertEquals("null", r); + try { + r = c.doPost(URL + "/Boolean?content=bad&noTrace=true", null).getResponseAsString(); + fail("Exception expected!"); + } catch (RestCallException e) { + assertEquals(400, e.getResponseCode()); + } + + // @RestMethod(name="POST", path="/int") + // public int testInt(@Content int i) { + // return i; + // } + r = c.doPost(URL + "/int?content=-123", null).getResponseAsString(); + assertEquals("-123", r); + try { + r = c.doPost(URL + "/int?content=null&noTrace=true", null).getResponseAsString(); + fail("Exception expected!"); + } catch (RestCallException e) { + assertEquals(400, e.getResponseCode()); + } + try { + r = c.doPost(URL + "/int?content=bad&noTrace=true", null).getResponseAsString(); + fail("Exception expected!"); + } catch (RestCallException e) { + assertEquals(400, e.getResponseCode()); + } + + // @RestMethod(name="POST", path="/Integer") + // public Integer testInteger(@Content Integer i) { + // return i; + // } + r = c.doPost(URL + "/Integer?content=-123", null).getResponseAsString(); + assertEquals("-123", r); + r = c.doPost(URL + "/Integer?content=null", null).getResponseAsString(); + assertEquals("null", r); + try { + r = c.doPost(URL + "/Integer?content=bad&noTrace=true", null).getResponseAsString(); + fail("Exception expected!"); + } catch (RestCallException e) { + assertEquals(400, e.getResponseCode()); + } + + // @RestMethod(name="POST", path="/float") + // public float testFloat(@Content float f) { + // return f; + // } + r = c.doPost(URL + "/float?content=-1.23", null).getResponseAsString(); + assertEquals("-1.23", r); + try { + r = c.doPost(URL + "/float?content=null&noTrace=true", null).getResponseAsString(); + fail("Exception expected!"); + } catch (RestCallException e) { + assertEquals(400, e.getResponseCode()); + } + try { + r = c.doPost(URL + "/float?content=bad&noTrace=true", null).getResponseAsString(); + fail("Exception expected!"); + } catch (RestCallException e) { + assertEquals(400, e.getResponseCode()); + } + + // @RestMethod(name="POST", path="/Float") + // public Float testFloat2(@Content Float f) { + // return f; + // } + r = c.doPost(URL + "/Float?content=-1.23", null).getResponseAsString(); + assertEquals("-1.23", r); + r = c.doPost(URL + "/Float?content=null", null).getResponseAsString(); + assertEquals("null", r); + try { + r = c.doPost(URL + "/Float?content=bad&noTrace=true", null).getResponseAsString(); + fail("Exception expected!"); + } catch (RestCallException e) { + assertEquals(400, e.getResponseCode()); + } + + // @RestMethod(name="POST", path="/Map") + // public TreeMap<String,String> testMap(@Content TreeMap<String,String> m) { + // return m; + // } + r = c.doPost(URL + "/Map?content=" + encode("{a:'b',c:'d'}"), null).getResponseAsString(); + assertEquals("{a:'b',c:'d'}", r); + r = c.doPost(URL + "/Map?content=null", null).getResponseAsString(); + assertEquals("null", r); + try { + r = c.doPost(URL + "/Map?content=bad&noTrace=true", null).getResponseAsString(); + fail("Exception expected!"); + } catch (RestCallException e) { + assertEquals(400, e.getResponseCode()); + } + + // @RestMethod(name="POST", path="/B") + // public DTO2s.B testPojo1(@Content DTO2s.B b) { + // return b; + // } + DTOs.B b = DTOs.B.create(); + r = c.doPost(URL + "/B?content=" + encode(JsonSerializer.DEFAULT_LAX.serialize(b)), null).getResponseAsString(); + assertEquals("{f1:['a','b'],f2:['c','d'],f3:[1,2],f4:[3,4],f5:[['e','f'],['g','h']],f6:[['i','j'],['k','l']],f7:[{a:'a',b:1,c:true},{a:'a',b:1,c:true}],f8:[{a:'a',b:1,c:true},{a:'a',b:1,c:true}],f9:[[{a:'a',b:1,c:true}],[{a:'a',b:1,c:true}]],f10:[[{a:'a',b:1,c:true}],[{a:'a',b:1,c:true}]],f11:['a','b'],f12:['c','d'],f13:[1,2],f14:[3,4],f15:[['e','f'],['g','h']],f16:[['i','j'],['k','l']],f17:[{a:'a',b:1,c:true},{a:'a',b:1,c:true}],f18:[{a:'a',b:1,c:true},{a:'a',b:1,c:true}],f19:[[{a:'a',b:1,c:true}],[{a:'a',b:1,c:true}]],f20:[[{a:'a',b:1,c:true}],[{a:'a',b:1,c:true}]]}", r); + + // @RestMethod(name="POST", path="/C") + // public DTO2s.C testPojo2(@Content DTO2s.C c) { + // return c; + // } + DTOs.C x = DTOs.C.create(); + r = c.doPost(URL + "/C?content=" + encode(JsonSerializer.DEFAULT_LAX.serialize(x)), null).getResponseAsString(); + assertEquals("{f1:['a','b'],f2:['c','d'],f3:[1,2],f4:[3,4],f5:[['e','f'],['g','h']],f6:[['i','j'],['k','l']],f7:[{a:'a',b:1,c:true},{a:'a',b:1,c:true}],f8:[{a:'a',b:1,c:true},{a:'a',b:1,c:true}],f9:[[{a:'a',b:1,c:true}],[{a:'a',b:1,c:true}]],f10:[[{a:'a',b:1,c:true}],[{a:'a',b:1,c:true}]],f11:['a','b'],f12:['c','d'],f13:[1,2],f14:[3,4],f15:[['e','f'],['g','h']],f16:[['i','j'],['k','l']],f17:[{a:'a',b:1,c:true},{a:'a',b:1,c:true}],f18:[{a:'a',b:1,c:true},{a:'a',b:1,c:true}],f19:[[{a:'a',b:1,c:true}],[{a:'a',b:1,c:true}]],f20:[[{a:'a',b:1,c:true}],[{a:'a',b:1,c:true}]]}", r); + + c.closeQuietly(); + } + + //==================================================================================================== + // Basic tests using &Content parameter with &Accept=text/json + //==================================================================================================== + @Test + public void testUsingContentParamJsonParam() throws Exception { + RestClient c = new TestRestClient().setAccept("text/json+simple"); + String r; + + // @RestMethod(name="POST", path="/boolean") + // public boolean testBool(@Content boolean b) { + // return b; + // } + r = c.doPost(URL + "/boolean?content=true&Content-Type=text/json", null).getResponseAsString(); + assertEquals("true", r); + r = c.doPost(URL + "/boolean?content=false&Content-Type=text/json", null).getResponseAsString(); + assertEquals("false", r); + try { + r = c.doPost(URL + "/boolean?content=null&Content-Type=text/json&noTrace=true", null).getResponseAsString(); + fail("Exception expected!"); + } catch (RestCallException e) { + assertEquals(400, e.getResponseCode()); + } + try { + r = c.doPost(URL + "/boolean?content=bad&Content-Type=text/json&noTrace=true", null).getResponseAsString(); + fail("Exception expected!"); + } catch (RestCallException e) { + assertEquals(400, e.getResponseCode()); + } + + + // @RestMethod(name="POST", path="/Boolean") + // public Boolean testBoolean(@Content Boolean b) { + // return b; + // } + r = c.doPost(URL + "/Boolean?content=true&Content-Type=text/json", null).getResponseAsString(); + assertEquals("true", r); + r = c.doPost(URL + "/Boolean?content=false&Content-Type=text/json", null).getResponseAsString(); + assertEquals("false", r); + r = c.doPost(URL + "/Boolean?content=null&Content-Type=text/json", null).getResponseAsString(); + assertEquals("null", r); + try { + r = c.doPost(URL + "/Boolean?content=bad&Content-Type=text/json&noTrace=true", null).getResponseAsString(); + fail("Exception expected!"); + } catch (RestCallException e) { + assertEquals(400, e.getResponseCode()); + } + + // @RestMethod(name="POST", path="/int") + // public int testInt(@Content int i) { + // return i; + // } + r = c.doPost(URL + "/int?content=-123&Content-Type=text/json", null).getResponseAsString(); + assertEquals("-123", r); + try { + r = c.doPost(URL + "/int?content=null&Content-Type=text/json&noTrace=true", null).getResponseAsString(); + fail("Exception expected!"); + } catch (RestCallException e) { + assertEquals(400, e.getResponseCode()); + } + try { + r = c.doPost(URL + "/int?content=bad&Content-Type=text/json&noTrace=true", null).getResponseAsString(); + fail("Exception expected!"); + } catch (RestCallException e) { + assertEquals(400, e.getResponseCode()); + } + + // @RestMethod(name="POST", path="/Integer") + // public Integer testInteger(@Content Integer i) { + // return i; + // } + r = c.doPost(URL + "/Integer?content=-123&Content-Type=text/json", null).getResponseAsString(); + assertEquals("-123", r); + r = c.doPost(URL + "/Integer?content=null&Content-Type=text/json", null).getResponseAsString(); + assertEquals("null", r); + try { + r = c.doPost(URL + "/Integer?content=bad&Content-Type=text/json&noTrace=true", null).getResponseAsString(); + fail("Exception expected!"); + } catch (RestCallException e) { + assertEquals(400, e.getResponseCode()); + } + + // @RestMethod(name="POST", path="/float") + // public float testFloat(@Content float f) { + // return f; + // } + r = c.doPost(URL + "/float?content=-1.23&Content-Type=text/json", null).getResponseAsString(); + assertEquals("-1.23", r); + try { + r = c.doPost(URL + "/float?content=null&Content-Type=text/json&noTrace=true", null).getResponseAsString(); + fail("Exception expected!"); + } catch (RestCallException e) { + assertEquals(400, e.getResponseCode()); + } + try { + r = c.doPost(URL + "/float?content=bad&Content-Type=text/json&noTrace=true", null).getResponseAsString(); + fail("Exception expected!"); + } catch (RestCallException e) { + assertEquals(400, e.getResponseCode()); + } + + // @RestMethod(name="POST", path="/Float") + // public Float testFloat2(@Content Float f) { + // return f; + // } + r = c.doPost(URL + "/Float?content=-1.23&Content-Type=text/json", null).getResponseAsString(); + assertEquals("-1.23", r); + r = c.doPost(URL + "/Float?content=null&Content-Type=text/json", null).getResponseAsString(); + assertEquals("null", r); + try { + r = c.doPost(URL + "/Float?content=bad&Content-Type=text/json&noTrace=true", null).getResponseAsString(); + fail("Exception expected!"); + } catch (RestCallException e) { + assertEquals(400, e.getResponseCode()); + } + + // @RestMethod(name="POST", path="/Map") + // public TreeMap<String,String> testMap(@Content TreeMap<String,String> m) { + // return m; + // } + r = c.doPost(URL + "/Map?content=" + encode("{a:'b',c:'d'}") + "&Content-Type=text/json", null).getResponseAsString(); + assertEquals("{a:'b',c:'d'}", r); + r = c.doPost(URL + "/Map?content=null&Content-Type=text/json", null).getResponseAsString(); + assertEquals("null", r); + try { + r = c.doPost(URL + "/Map?content=bad&Content-Type=text/json&noTrace=true", null).getResponseAsString(); + fail("Exception expected!"); + } catch (RestCallException e) { + assertEquals(400, e.getResponseCode()); + } + + // @RestMethod(name="POST", path="/B") + // public DTO2s.B testPojo1(@Content DTO2s.B b) { + // return b; + // } + DTOs.B b = DTOs.B.create(); + r = c.doPost(URL + "/B?content=" + encode(JsonSerializer.DEFAULT_LAX.serialize(b)) + "&Content-Type=text/json", null).getResponseAsString(); + assertEquals("{f1:['a','b'],f2:['c','d'],f3:[1,2],f4:[3,4],f5:[['e','f'],['g','h']],f6:[['i','j'],['k','l']],f7:[{a:'a',b:1,c:true},{a:'a',b:1,c:true}],f8:[{a:'a',b:1,c:true},{a:'a',b:1,c:true}],f9:[[{a:'a',b:1,c:true}],[{a:'a',b:1,c:true}]],f10:[[{a:'a',b:1,c:true}],[{a:'a',b:1,c:true}]],f11:['a','b'],f12:['c','d'],f13:[1,2],f14:[3,4],f15:[['e','f'],['g','h']],f16:[['i','j'],['k','l']],f17:[{a:'a',b:1,c:true},{a:'a',b:1,c:true}],f18:[{a:'a',b:1,c:true},{a:'a',b:1,c:true}],f19:[[{a:'a',b:1,c:true}],[{a:'a',b:1,c:true}]],f20:[[{a:'a',b:1,c:true}],[{a:'a',b:1,c:true}]]}", r); + + // @RestMethod(name="POST", path="/C") + // public DTO2s.C testPojo2(@Content DTO2s.C c) { + // return c; + // } + DTOs.C x = DTOs.C.create(); + r = c.doPost(URL + "/C?content=" + encode(JsonSerializer.DEFAULT_LAX.serialize(x)) + "&Content-Type=text/json", null).getResponseAsString(); + assertEquals("{f1:['a','b'],f2:['c','d'],f3:[1,2],f4:[3,4],f5:[['e','f'],['g','h']],f6:[['i','j'],['k','l']],f7:[{a:'a',b:1,c:true},{a:'a',b:1,c:true}],f8:[{a:'a',b:1,c:true},{a:'a',b:1,c:true}],f9:[[{a:'a',b:1,c:true}],[{a:'a',b:1,c:true}]],f10:[[{a:'a',b:1,c:true}],[{a:'a',b:1,c:true}]],f11:['a','b'],f12:['c','d'],f13:[1,2],f14:[3,4],f15:[['e','f'],['g','h']],f16:[['i','j'],['k','l']],f17:[{a:'a',b:1,c:true},{a:'a',b:1,c:true}],f18:[{a:'a',b:1,c:true},{a:'a',b:1,c:true}],f19:[[{a:'a',b:1,c:true}],[{a:'a',b:1,c:true}]],f20:[[{a:'a',b:1,c:true}],[{a:'a',b:1,c:true}]]}", r); + + c.closeQuietly(); + } + + //==================================================================================================== + // Basic tests using HTTP body content + //==================================================================================================== + @Test + public void testUsingContent() throws Exception { + RestClient c = new TestRestClient().setAccept("text/json+simple").setHeader("Content-Type", "text/uon").setSerializer(PlainTextSerializer.class); + String r; + + // @RestMethod(name="POST", path="/boolean") + // public boolean testBool(@Content boolean b) { + // return b; + // } + r = c.doPost(URL + "/boolean", "true").getResponseAsString(); + assertEquals("true", r); + r = c.doPost(URL + "/boolean", "(true)").getResponseAsString(); + assertEquals("true", r); + r = c.doPost(URL + "/boolean", "$b(true)").getResponseAsString(); + assertEquals("true", r); + r = c.doPost(URL + "/boolean", "false").getResponseAsString(); + assertEquals("false", r); + r = c.doPost(URL + "/boolean", "(false)").getResponseAsString(); + assertEquals("false", r); + r = c.doPost(URL + "/boolean", "$b(false)").getResponseAsString(); + assertEquals("false", r); + try { + r = c.doPost(URL + "/boolean?noTrace=true", "%00").getResponseAsString(); + fail("Exception expected!"); + } catch (RestCallException e) { + assertEquals(400, e.getResponseCode()); + } + try { + r = c.doPost(URL + "/boolean?noTrace=true", "bad").getResponseAsString(); + fail("Exception expected!"); + } catch (RestCallException e) { + assertEquals(400, e.getResponseCode()); + } + + + // @RestMethod(name="POST", path="/Boolean") + // public Boolean testBoolean(@Content Boolean b) { + // return b; + // } + r = c.doPost(URL + "/Boolean", "true").getResponseAsString(); + assertEquals("true", r); + r = c.doPost(URL + "/Boolean", "(true)").getResponseAsString(); + assertEquals("true", r); + r = c.doPost(URL + "/Boolean", "$b(true)").getResponseAsString(); + assertEquals("true", r); + r = c.doPost(URL + "/Boolean", "false").getResponseAsString(); + assertEquals("false", r); + r = c.doPost(URL + "/Boolean", "(false)").getResponseAsString(); + assertEquals("false", r); + r = c.doPost(URL + "/Boolean", "$b(false)").getResponseAsString(); + assertEquals("false", r); + r = c.doPost(URL + "/Boolean", "\u0000").getResponseAsString(); + assertEquals("null", r); + try { + r = c.doPost(URL + "/Boolean?noTrace=true", "bad").getResponseAsString(); + fail("Exception expected!"); + } catch (RestCallException e) { + assertEquals(400, e.getResponseCode()); + } + + // @RestMethod(name="POST", path="/int") + // public int testInt(@Content int i) { + // return i; + // } + r = c.doPost(URL + "/int", "-123").getResponseAsString(); + assertEquals("-123", r); + r = c.doPost(URL + "/int", "(-123)").getResponseAsString(); + assertEquals("-123", r); + r = c.doPost(URL + "/int", "$n(-123)").getResponseAsString(); + assertEquals("-123", r); + try { + r = c.doPost(URL + "/int?noTrace=true", "%00").getResponseAsString(); + fail("Exception expected!"); + } catch (RestCallException e) { + assertEquals(400, e.getResponseCode()); + } + try { + r = c.doPost(URL + "/int?noTrace=true", "bad").getResponseAsString(); + fail("Exception expected!"); + } catch (RestCallException e) { + assertEquals(400, e.getResponseCode()); + } + + // @RestMethod(name="POST", path="/Integer") + // public Integer testInteger(@Content Integer i) { + // return i; + // } + r = c.doPost(URL + "/Integer", "-123").getResponseAsString(); + assertEquals("-123", r); + r = c.doPost(URL + "/Integer", "(-123)").getResponseAsString(); + assertEquals("-123", r); + r = c.doPost(URL + "/Integer", "$n(-123)").getResponseAsString(); + assertEquals("-123", r); + r = c.doPost(URL + "/Integer", "\u0000").getResponseAsString(); + assertEquals("null", r); + try { + r = c.doPost(URL + "/Integer?noTrace=true", "bad").getResponseAsString(); + fail("Exception expected!"); + } catch (RestCallException e) { + assertEquals(400, e.getResponseCode()); + } + + // @RestMethod(name="POST", path="/float") + // public float testFloat(@Content float f) { + // return f; + // } + r = c.doPost(URL + "/float", "-1.23").getResponseAsString(); + assertEquals("-1.23", r); + r = c.doPost(URL + "/float", "(-1.23)").getResponseAsString(); + assertEquals("-1.23", r); + r = c.doPost(URL + "/float", "$n(-1.23)").getResponseAsString(); + assertEquals("-1.23", r); + try { + r = c.doPost(URL + "/float?noTrace=true", "\u0000").getResponseAsString(); + fail("Exception expected!"); + } catch (RestCallException e) { + assertEquals(400, e.getResponseCode()); + } + try { + r = c.doPost(URL + "/float?noTrace=true", "bad").getResponseAsString(); + fail("Exception expected!"); + } catch (RestCallException e) { + assertEquals(400, e.getResponseCode()); + } + + // @RestMethod(name="POST", path="/Float") + // public Float testFloat2(@Content Float f) { + // return f; + // } + r = c.doPost(URL + "/Float", "-1.23").getResponseAsString(); + assertEquals("-1.23", r); + r = c.doPost(URL + "/Float", "(-1.23)").getResponseAsString(); + assertEquals("-1.23", r); + r = c.doPost(URL + "/Float", "$n(-1.23)").getResponseAsString(); + assertEquals("-1.23", r); + r = c.doPost(URL + "/Float", "\u0000").getResponseAsString(); + assertEquals("null", r); + try { + r = c.doPost(URL + "/Float?noTrace=true", "bad").getResponseAsString(); + fail("Exception expected!"); + } catch (RestCallException e) { + assertEquals(400, e.getResponseCode()); + } + + // @RestMethod(name="POST", path="/Map") + // public TreeMap<String,String> testMap(@Content TreeMap<String,String> m) { + // return m; + // } + r = c.doPost(URL + "/Map", "(a=b,c=d)").getResponseAsString(); + assertEquals("{a:'b',c:'d'}", r); + r = c.doPost(URL + "/Map", "\u0000").getResponseAsString(); + assertEquals("null", r); + try { + r = c.doPost(URL + "/Map?noTrace=true", "bad").getResponseAsString(); + fail("Exception expected!"); + } catch (RestCallException e) { + assertEquals(400, e.getResponseCode()); + } + + // @RestMethod(name="POST", path="/B") + // public DTO2s.B testPojo1(@Content DTO2s.B b) { + // return b; + // } + DTOs.B b = DTOs.B.create(); + r = c.doPost(URL + "/B", "" + UonSerializer.DEFAULT.serialize(b)).getResponseAsString(); + assertEquals("{f1:['a','b'],f2:['c','d'],f3:[1,2],f4:[3,4],f5:[['e','f'],['g','h']],f6:[['i','j'],['k','l']],f7:[{a:'a',b:1,c:true},{a:'a',b:1,c:true}],f8:[{a:'a',b:1,c:true},{a:'a',b:1,c:true}],f9:[[{a:'a',b:1,c:true}],[{a:'a',b:1,c:true}]],f10:[[{a:'a',b:1,c:true}],[{a:'a',b:1,c:true}]],f11:['a','b'],f12:['c','d'],f13:[1,2],f14:[3,4],f15:[['e','f'],['g','h']],f16:[['i','j'],['k','l']],f17:[{a:'a',b:1,c:true},{a:'a',b:1,c:true}],f18:[{a:'a',b:1,c:true},{a:'a',b:1,c:true}],f19:[[{a:'a',b:1,c:true}],[{a:'a',b:1,c:true}]],f20:[[{a:'a',b:1,c:true}],[{a:'a',b:1,c:true}]]}", r); + r = c.doPost(URL + "/B", "" + UonSerializer.DEFAULT_SIMPLE.serialize(b)).getResponseAsString(); + assertEquals("{f1:['a','b'],f2:['c','d'],f3:[1,2],f4:[3,4],f5:[['e','f'],['g','h']],f6:[['i','j'],['k','l']],f7:[{a:'a',b:1,c:true},{a:'a',b:1,c:true}],f8:[{a:'a',b:1,c:true},{a:'a',b:1,c:true}],f9:[[{a:'a',b:1,c:true}],[{a:'a',b:1,c:true}]],f10:[[{a:'a',b:1,c:true}],[{a:'a',b:1,c:true}]],f11:['a','b'],f12:['c','d'],f13:[1,2],f14:[3,4],f15:[['e','f'],['g','h']],f16:[['i','j'],['k','l']],f17:[{a:'a',b:1,c:true},{a:'a',b:1,c:true}],f18:[{a:'a',b:1,c:true},{a:'a',b:1,c:true}],f19:[[{a:'a',b:1,c:true}],[{a:'a',b:1,c:true}]],f20:[[{a:'a',b:1,c:true}],[{a:'a',b:1,c:true}]]}", r); + + // @RestMethod(name="POST", path="/C") + // public DTO2s.C testPojo2(@Content DTO2s.C c) { + // return c; + // } + DTOs.C x = DTOs.C.create(); + r = c.doPost(URL + "/C", "" + UonSerializer.DEFAULT.serialize(x)).getResponseAsString(); + assertEquals("{f1:['a','b'],f2:['c','d'],f3:[1,2],f4:[3,4],f5:[['e','f'],['g','h']],f6:[['i','j'],['k','l']],f7:[{a:'a',b:1,c:true},{a:'a',b:1,c:true}],f8:[{a:'a',b:1,c:true},{a:'a',b:1,c:true}],f9:[[{a:'a',b:1,c:true}],[{a:'a',b:1,c:true}]],f10:[[{a:'a',b:1,c:true}],[{a:'a',b:1,c:true}]],f11:['a','b'],f12:['c','d'],f13:[1,2],f14:[3,4],f15:[['e','f'],['g','h']],f16:[['i','j'],['k','l']],f17:[{a:'a',b:1,c:true},{a:'a',b:1,c:true}],f18:[{a:'a',b:1,c:true},{a:'a',b:1,c:true}],f19:[[{a:'a',b:1,c:true}],[{a:'a',b:1,c:true}]],f20:[[{a:'a',b:1,c:true}],[{a:'a',b:1,c:true}]]}", r); + r = c.doPost(URL + "/C", "" + UonSerializer.DEFAULT_SIMPLE.serialize(x)).getResponseAsString(); + assertEquals("{f1:['a','b'],f2:['c','d'],f3:[1,2],f4:[3,4],f5:[['e','f'],['g','h']],f6:[['i','j'],['k','l']],f7:[{a:'a',b:1,c:true},{a:'a',b:1,c:true}],f8:[{a:'a',b:1,c:true},{a:'a',b:1,c:true}],f9:[[{a:'a',b:1,c:true}],[{a:'a',b:1,c:true}]],f10:[[{a:'a',b:1,c:true}],[{a:'a',b:1,c:true}]],f11:['a','b'],f12:['c','d'],f13:[1,2],f14:[3,4],f15:[['e','f'],['g','h']],f16:[['i','j'],['k','l']],f17:[{a:'a',b:1,c:true},{a:'a',b:1,c:true}],f18:[{a:'a',b:1,c:true},{a:'a',b:1,c:true}],f19:[[{a:'a',b:1,c:true}],[{a:'a',b:1,c:true}]],f20:[[{a:'a',b:1,c:true}],[{a:'a',b:1,c:true}]]}", r); + + c.closeQuietly(); + } + + + private String encode(String s) { + try { + return URLEncoder.encode(s, "UTF-8"); + } catch (UnsupportedEncodingException e) { + throw new RuntimeException(e); + } + } +}