http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/bea31abd/juneau-server-test/src/test/java/org/apache/juneau/server/CallbackStringsTest.java ---------------------------------------------------------------------- diff --git a/juneau-server-test/src/test/java/org/apache/juneau/server/CallbackStringsTest.java b/juneau-server-test/src/test/java/org/apache/juneau/server/CallbackStringsTest.java new file mode 100755 index 0000000..8f240c5 --- /dev/null +++ b/juneau-server-test/src/test/java/org/apache/juneau/server/CallbackStringsTest.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 CallbackStringsTest { + + //==================================================================================================== + // 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/bea31abd/juneau-server-test/src/test/java/org/apache/juneau/server/CharsetEncodingsTest.java ---------------------------------------------------------------------- diff --git a/juneau-server-test/src/test/java/org/apache/juneau/server/CharsetEncodingsTest.java b/juneau-server-test/src/test/java/org/apache/juneau/server/CharsetEncodingsTest.java new file mode 100755 index 0000000..0744637 --- /dev/null +++ b/juneau-server-test/src/test/java/org/apache/juneau/server/CharsetEncodingsTest.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 CharsetEncodingsTest { + + 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/bea31abd/juneau-server-test/src/test/java/org/apache/juneau/server/ClientVersionTest.java ---------------------------------------------------------------------- diff --git a/juneau-server-test/src/test/java/org/apache/juneau/server/ClientVersionTest.java b/juneau-server-test/src/test/java/org/apache/juneau/server/ClientVersionTest.java new file mode 100644 index 0000000..9867c7e --- /dev/null +++ b/juneau-server-test/src/test/java/org/apache/juneau/server/ClientVersionTest.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 ClientVersionTest { + + 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(s, "[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(s, "[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(s, "[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(s, "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/bea31abd/juneau-server-test/src/test/java/org/apache/juneau/server/ConfigTest.java ---------------------------------------------------------------------- diff --git a/juneau-server-test/src/test/java/org/apache/juneau/server/ConfigTest.java b/juneau-server-test/src/test/java/org/apache/juneau/server/ConfigTest.java new file mode 100755 index 0000000..aefa47d --- /dev/null +++ b/juneau-server-test/src/test/java/org/apache/juneau/server/ConfigTest.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 ConfigTest { + + 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/bea31abd/juneau-server-test/src/test/java/org/apache/juneau/server/ContentTest.java ---------------------------------------------------------------------- diff --git a/juneau-server-test/src/test/java/org/apache/juneau/server/ContentTest.java b/juneau-server-test/src/test/java/org/apache/juneau/server/ContentTest.java new file mode 100755 index 0000000..0db6c73 --- /dev/null +++ b/juneau-server-test/src/test/java/org/apache/juneau/server/ContentTest.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 ContentTest { + + 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); + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/bea31abd/juneau-server-test/src/test/java/org/apache/juneau/server/DefaultContentTypesTest.java ---------------------------------------------------------------------- diff --git a/juneau-server-test/src/test/java/org/apache/juneau/server/DefaultContentTypesTest.java b/juneau-server-test/src/test/java/org/apache/juneau/server/DefaultContentTypesTest.java new file mode 100755 index 0000000..695a41b --- /dev/null +++ b/juneau-server-test/src/test/java/org/apache/juneau/server/DefaultContentTypesTest.java @@ -0,0 +1,497 @@ +/*************************************************************************************************************************** + * 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 org.apache.juneau.client.*; +import org.apache.juneau.json.*; +import org.junit.*; + + +public class DefaultContentTypesTest { + + private static String URL = "/testDefaultContentTypes"; + private static boolean debug = false; + + //==================================================================================================== + // Test that default Accept and Content-Type headers on servlet annotation are picked up. + //==================================================================================================== + @Test + public void testDefaultHeadersOnServletAnnotation() throws Exception { + RestClient client = new TestRestClient(JsonSerializer.DEFAULT, JsonParser.DEFAULT); + String r; + + String url = URL + "/testDefaultHeadersOnServletAnnotation"; + + client.setAccept("").setContentType(""); + r = client.doPut(url, "").getResponseAsString(); + assertEquals("s2/p2", r); + + client.setAccept("text/s1").setContentType(""); + r = client.doPut(url, "").getResponseAsString(); + assertEquals("s1/p2", r); + + client.setAccept("").setContentType("text/p1"); + r = client.doPut(url, "").getResponseAsString(); + assertEquals("s2/p1", r); + + client.setAccept("text/s1").setContentType("text/p1"); + r = client.doPut(url, "").getResponseAsString(); + assertEquals("s1/p1", r); + + client.setAccept("text/s2").setContentType(""); + r = client.doPut(url, "").getResponseAsString(); + assertEquals("s2/p2", r); + + client.setAccept("").setContentType("text/p2"); + r = client.doPut(url, "").getResponseAsString(); + assertEquals("s2/p2", r); + + client.setAccept("text/s2").setContentType("text/p2"); + r = client.doPut(url, "").getResponseAsString(); + assertEquals("s2/p2", r); + + try { + client.setAccept("text/s3").setContentType(""); + r = client.doPut(url+"?noTrace=true", "").getResponseAsString(); + fail("Exception expected"); + } catch (RestCallException e) { + checkErrorResponse(debug, e, SC_NOT_ACCEPTABLE, + "Unsupported media-type in request header 'Accept': 'text/s3'", + "Supported media-types: [text/s1, text/s2]" + ); + } + + try { + client.setAccept("").setContentType("text/p3"); + r = client.doPut(url+"?noTrace=true", "").getResponseAsString(); + fail("Exception expected"); + } catch (RestCallException e) { + checkErrorResponse(debug, e, SC_UNSUPPORTED_MEDIA_TYPE, + "Unsupported media-type in request header 'Content-Type': 'text/p3'", + "Supported media-types: [text/p1, text/p2]" + ); + } + + try { + client.setAccept("text/s3").setContentType("text/p3"); + r = client.doPut(url+"?noTrace=true", "").getResponseAsString(); + fail("Exception expected"); + } catch (RestCallException e) { + checkErrorResponse(debug, e, SC_UNSUPPORTED_MEDIA_TYPE, + "Unsupported media-type in request header 'Content-Type': 'text/p3'", + "Supported media-types: [text/p1, text/p2]" + ); + } + + client.closeQuietly(); + } + + //==================================================================================================== + // Test that default Accept and Content-Type headers on servlet annotation are picked up + // when @RestMethod.parsers/serializers annotations are used. + //==================================================================================================== + @Test + public void testRestMethodParsersSerializers() throws Exception { + RestClient client = new TestRestClient(JsonSerializer.DEFAULT, JsonParser.DEFAULT); + String r; + + String url = URL + "/testRestMethodParsersSerializers"; + + try { + client.setAccept("").setContentType(""); + r = client.doPut(url+"?noTrace=true", "").getResponseAsString(); + fail("Exception expected"); + } catch (RestCallException e) { + checkErrorResponse(debug, e, SC_UNSUPPORTED_MEDIA_TYPE, + "Unsupported media-type in request header 'Content-Type': 'text/p2'", + "Supported media-types: [text/p3]" + ); + } + + try { + client.setAccept("text/s1").setContentType(""); + r = client.doPut(url+"?noTrace=true", "").getResponseAsString(); + fail("Exception expected"); + } catch (RestCallException e) { + checkErrorResponse(debug, e, SC_UNSUPPORTED_MEDIA_TYPE, + "Unsupported media-type in request header 'Content-Type': 'text/p2'", + "Supported media-types: [text/p3]" + ); + } + + try { + client.setAccept("").setContentType("text/p1"); + r = client.doPut(url+"?noTrace=true", "").getResponseAsString(); + fail("Exception expected"); + } catch (RestCallException e) { + checkErrorResponse(debug, e, SC_UNSUPPORTED_MEDIA_TYPE, + "Unsupported media-type in request header 'Content-Type': 'text/p1'", + "Supported media-types: [text/p3]" + ); + } + + try { + client.setAccept("text/s1").setContentType("text/p1"); + r = client.doPut(url+"?noTrace=true", "").getResponseAsString(); + fail("Exception expected"); + } catch (RestCallException e) { + checkErrorResponse(debug, e, SC_UNSUPPORTED_MEDIA_TYPE, + "Unsupported media-type in request header 'Content-Type': 'text/p1'", + "Supported media-types: [text/p3]" + ); + } + + try { + client.setAccept("text/s2").setContentType(""); + r = client.doPut(url+"?noTrace=true", "").getResponseAsString(); + fail("Exception expected"); + } catch (RestCallException e) { + checkErrorResponse(debug, e, SC_UNSUPPORTED_MEDIA_TYPE, + "Unsupported media-type in request header 'Content-Type': 'text/p2'", + "Supported media-types: [text/p3]" + ); + } + + try { + client.setAccept("").setContentType("text/p2"); + r = client.doPut(url+"?noTrace=true", "").getResponseAsString(); + fail("Exception expected"); + } catch (RestCallException e) { + checkErrorResponse(debug, e, SC_UNSUPPORTED_MEDIA_TYPE, + "Unsupported media-type in request header 'Content-Type': 'text/p2'", + "Supported media-types: [text/p3]" + ); + } + + try { + client.setAccept("text/s2").setContentType("text/p2"); + r = client.doPut(url+"?noTrace=true", "").getResponseAsString(); + fail("Exception expected"); + } catch (RestCallException e) { + checkErrorResponse(debug, e, SC_UNSUPPORTED_MEDIA_TYPE, + "Unsupported media-type in request header 'Content-Type': 'text/p2'", + "Supported media-types: [text/p3]" + ); + } + + try { + client.setAccept("text/s3").setContentType(""); + r = client.doPut(url+"?noTrace=true", "").getResponseAsString(); + fail("Exception expected"); + } catch (RestCallException e) { + checkErrorResponse(debug, e, SC_UNSUPPORTED_MEDIA_TYPE, + "Unsupported media-type in request header 'Content-Type': 'text/p2'", + "Supported media-types: [text/p3]" + ); + } + + try { + client.setAccept("").setContentType("text/p3"); + r = client.doPut(url+"?noTrace=true", "").getResponseAsString(); + fail("Exception expected"); + } catch (RestCallException e) { + checkErrorResponse(debug, e, SC_NOT_ACCEPTABLE, + "Unsupported media-type in request header 'Accept': 'text/s2'", + "Supported media-types: [text/s3]" + ); + } + + client.setAccept("text/s3").setContentType("text/p3"); + r = client.doPut(url, "").getResponseAsString(); + assertEquals("s3/p3", r); + + client.closeQuietly(); + } + + //==================================================================================================== + // Test that default Accept and Content-Type headers on servlet annotation are picked up + // when @RestMethod.addParsers/addSerializers annotations are used. + //==================================================================================================== + @Test + public void testRestMethodAddParsersSerializers() throws Exception { + RestClient client = new TestRestClient(JsonSerializer.DEFAULT, JsonParser.DEFAULT); + String r; + + String url = URL + "/testRestMethodAddParsersSerializers"; + + client.setAccept("").setContentType(""); + r = client.doPut(url, "").getResponseAsString(); + assertEquals("s2/p2", r); + + client.setAccept("text/s1").setContentType(""); + r = client.doPut(url, "").getResponseAsString(); + assertEquals("s1/p2", r); + + client.setAccept("").setContentType("text/p1"); + r = client.doPut(url, "").getResponseAsString(); + assertEquals("s2/p1", r); + + client.setAccept("text/s1").setContentType("text/p1"); + r = client.doPut(url, "").getResponseAsString(); + assertEquals("s1/p1", r); + + client.setAccept("text/s2").setContentType(""); + r = client.doPut(url, "").getResponseAsString(); + assertEquals("s2/p2", r); + + client.setAccept("").setContentType("text/p2"); + r = client.doPut(url, "").getResponseAsString(); + assertEquals("s2/p2", r); + + client.setAccept("text/s2").setContentType("text/p2"); + r = client.doPut(url, "").getResponseAsString(); + assertEquals("s2/p2", r); + + client.setAccept("text/s3").setContentType(""); + r = client.doPut(url, "").getResponseAsString(); + assertEquals("s3/p2", r); + + client.setAccept("").setContentType("text/p3"); + r = client.doPut(url, "").getResponseAsString(); + assertEquals("s2/p3", r); + + client.setAccept("text/s3").setContentType("text/p3"); + r = client.doPut(url, "").getResponseAsString(); + assertEquals("s3/p3", r); + + try { + client.setAccept("").setContentType("text/p4"); + r = client.doPut(url+"?noTrace=true", "").getResponseAsString(); + fail("Exception expected"); + } catch (RestCallException e) { + // Note that parsers defined on method are listed before parsers defined on class. + checkErrorResponse(debug, e, SC_UNSUPPORTED_MEDIA_TYPE, + "Unsupported media-type in request header 'Content-Type': 'text/p4'", + "Supported media-types: [text/p3, text/p1, text/p2]" + ); + } + + try { + client.setAccept("text/s4").setContentType(""); + r = client.doPut(url+"?noTrace=true", "").getResponseAsString(); + fail("Exception expected"); + } catch (RestCallException e) { + // Note that serializers defined on method are listed before serializers defined on class. + checkErrorResponse(debug, e, SC_NOT_ACCEPTABLE, + "Unsupported media-type in request header 'Accept': 'text/s4'", + "Supported media-types: [text/s3, text/s1, text/s2]" + ); + } + + client.closeQuietly(); + } + + //==================================================================================================== + // Various Accept incantations. + //==================================================================================================== + @Test + public void testAccept() throws Exception { + RestClient client = new TestRestClient(JsonSerializer.DEFAULT, JsonParser.DEFAULT).setContentType("text/p1"); + String r; + + String url = URL + "/testAccept"; + + // "*/*" should match the first serializer, not the default serializer. + client.setAccept("*/*"); + r = client.doPut(url, "").getResponseAsString(); + assertEquals("s1/p1", r); + + // "text/*" should match the first serializer, not the default serializer. + client.setAccept("text/*"); + r = client.doPut(url, "").getResponseAsString(); + assertEquals("s1/p1", r); + + try { + client.setAccept("bad/*"); + r = client.doPut(url+"?noTrace=true", "").getResponseAsString(); + fail("Exception expected"); + } catch (RestCallException e) { + checkErrorResponse(debug, e, SC_NOT_ACCEPTABLE, + "Unsupported media-type in request header 'Accept': 'bad/*'", + "Supported media-types: [text/s1, text/s2]" + ); + } + + client.setAccept("bad/*,text/*"); + r = client.doPut(url, "").getResponseAsString(); + assertEquals("s1/p1", r); + + client.setAccept("text/*,bad/*"); + r = client.doPut(url, "").getResponseAsString(); + assertEquals("s1/p1", r); + + client.setAccept("text/s1;q=0.5,text/s2"); + r = client.doPut(url, "").getResponseAsString(); + assertEquals("s2/p1", r); + + client.setAccept("text/s1,text/s2;q=0.5"); + r = client.doPut(url, "").getResponseAsString(); + assertEquals("s1/p1", r); + + client.closeQuietly(); + } + + //==================================================================================================== + // Test that default Accept and Content-Type headers on method annotation are picked up + // when @RestMethod.parsers/serializers annotations are used. + //==================================================================================================== + @Test + public void testRestMethodParserSerializerAnnotations() throws Exception { + RestClient client = new TestRestClient(JsonSerializer.DEFAULT, JsonParser.DEFAULT); + String r; + + String url = URL + "/testRestMethodParserSerializerAnnotations"; + + client.setAccept("").setContentType(""); + r = client.doPut(url, "").getResponseAsString(); + assertEquals("s3/p3", r); + + try { + client.setAccept("text/s1").setContentType(""); + r = client.doPut(url+"?noTrace=true", "").getResponseAsString(); + fail("Exception expected"); + } catch (RestCallException e) { + checkErrorResponse(debug, e, SC_NOT_ACCEPTABLE, + "Unsupported media-type in request header 'Accept': 'text/s1'", + "Supported media-types: [text/s3]" + ); + } + + try { + client.setAccept("").setContentType("text/p1"); + r = client.doPut(url+"?noTrace=true", "").getResponseAsString(); + fail("Exception expected"); + } catch (RestCallException e) { + checkErrorResponse(debug, e, SC_UNSUPPORTED_MEDIA_TYPE, + "Unsupported media-type in request header 'Content-Type': 'text/p1'", + "Supported media-types: [text/p3]" + ); + } + + try { + client.setAccept("text/s1").setContentType("text/p1"); + r = client.doPut(url+"?noTrace=true", "").getResponseAsString(); + fail("Exception expected"); + } catch (RestCallException e) { + checkErrorResponse(debug, e, SC_UNSUPPORTED_MEDIA_TYPE, + "Unsupported media-type in request header 'Content-Type': 'text/p1'", + "Supported media-types: [text/p3]" + ); + } + + try { + client.setAccept("text/s2").setContentType(""); + r = client.doPut(url+"?noTrace=true", "").getResponseAsString(); + fail("Exception expected"); + } catch (RestCallException e) { + checkErrorResponse(debug, e, SC_NOT_ACCEPTABLE, + "Unsupported media-type in request header 'Accept': 'text/s2'", + "Supported media-types: [text/s3]" + ); + } + + try { + client.setAccept("").setContentType("text/p2"); + r = client.doPut(url+"?noTrace=true", "").getResponseAsString(); + fail("Exception expected"); + } catch (RestCallException e) { + checkErrorResponse(debug, e, SC_UNSUPPORTED_MEDIA_TYPE, + "Unsupported media-type in request header 'Content-Type': 'text/p2'", + "Supported media-types: [text/p3]" + ); + } + + try { + client.setAccept("text/s2").setContentType("text/p2"); + r = client.doPut(url+"?noTrace=true", "").getResponseAsString(); + fail("Exception expected"); + } catch (RestCallException e) { + checkErrorResponse(debug, e, SC_UNSUPPORTED_MEDIA_TYPE, + "Unsupported media-type in request header 'Content-Type': 'text/p2'", + "Supported media-types: [text/p3]" + ); + } + + client.setAccept("text/s3").setContentType(""); + r = client.doPut(url, "").getResponseAsString(); + assertEquals("s3/p3", r); + + client.setAccept("").setContentType("text/p3"); + r = client.doPut(url, "").getResponseAsString(); + assertEquals("s3/p3", r); + + client.setAccept("text/s3").setContentType("text/p3"); + r = client.doPut(url, "").getResponseAsString(); + assertEquals("s3/p3", r); + + client.closeQuietly(); + } + + //==================================================================================================== + // Test that default Accept and Content-Type headers on method annotation are picked up + // when @RestMethod.addParsers/addSerializers annotations are used. + //==================================================================================================== + @Test + public void testRestMethodAddParsersSerializersAnnotations() throws Exception { + RestClient client = new TestRestClient(JsonSerializer.DEFAULT, JsonParser.DEFAULT); + String r; + + String url = URL + "/testRestMethodAddParsersSerializersAnnotations"; + + client.setAccept("").setContentType(""); + r = client.doPut(url, "").getResponseAsString(); + assertEquals("s3/p3", r); + + client.setAccept("text/s1").setContentType(""); + r = client.doPut(url, "").getResponseAsString(); + assertEquals("s1/p3", r); + + client.setAccept("").setContentType("text/p1"); + r = client.doPut(url, "").getResponseAsString(); + assertEquals("s3/p1", r); + + client.setAccept("text/s1").setContentType("text/p1"); + r = client.doPut(url, "").getResponseAsString(); + assertEquals("s1/p1", r); + + client.setAccept("text/s2").setContentType(""); + r = client.doPut(url, "").getResponseAsString(); + assertEquals("s2/p3", r); + + client.setAccept("").setContentType("text/p2"); + r = client.doPut(url, "").getResponseAsString(); + assertEquals("s3/p2", r); + + client.setAccept("text/s2").setContentType("text/p2"); + r = client.doPut(url, "").getResponseAsString(); + assertEquals("s2/p2", r); + + client.setAccept("text/s3").setContentType(""); + r = client.doPut(url, "").getResponseAsString(); + assertEquals("s3/p3", r); + + client.setAccept("").setContentType("text/p3"); + r = client.doPut(url, "").getResponseAsString(); + assertEquals("s3/p3", r); + + client.setAccept("text/s3").setContentType("text/p3"); + r = client.doPut(url, "").getResponseAsString(); + assertEquals("s3/p3", r); + + client.closeQuietly(); + } +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/bea31abd/juneau-server-test/src/test/java/org/apache/juneau/server/ErrorConditionsTest.java ---------------------------------------------------------------------- diff --git a/juneau-server-test/src/test/java/org/apache/juneau/server/ErrorConditionsTest.java b/juneau-server-test/src/test/java/org/apache/juneau/server/ErrorConditionsTest.java new file mode 100755 index 0000000..2123fa5 --- /dev/null +++ b/juneau-server-test/src/test/java/org/apache/juneau/server/ErrorConditionsTest.java @@ -0,0 +1,220 @@ +/*************************************************************************************************************************** + * 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 org.apache.juneau.*; +import org.apache.juneau.client.*; +import org.apache.juneau.json.*; +import org.junit.*; + + +public class ErrorConditionsTest { + + private static String URL = "/testErrorConditions"; + private static boolean debug = false; + private static RestClient client; + + @BeforeClass + public static void beforeClass() { + client = new TestRestClient(JsonSerializer.DEFAULT, JsonParser.DEFAULT); + } + + @AfterClass + public static void afterClass() { + client.closeQuietly(); + } + //==================================================================================================== + // Test non-existent properties + //==================================================================================================== + @Test + public void testNonExistentBeanProperties() throws Exception { + String url = URL + "/testNonExistentBeanProperties"; + + try { + client.doPut(url + "?noTrace=true", new ObjectMap("{f2:'foo'}")).getResponseAsString(); + fail("Exception expected"); + } catch (RestCallException e) { + checkErrorResponse(debug, e, SC_BAD_REQUEST, + "Could not convert request body content to class type 'org.apache.juneau.server.ErrorConditionsResource$Test1' using parser 'org.apache.juneau.json.JsonParser'", + "Unknown property 'f2' encountered while trying to parse into class 'org.apache.juneau.server.ErrorConditionsResource$Test1'"); + } + + try { + client.doPut(url + "?noTrace=true", new ObjectMap("{f1:'foo', f2:'foo'}")).getResponseAsString(); + fail("Exception expected"); + } catch (RestCallException e) { + checkErrorResponse(debug, e, SC_BAD_REQUEST, + "Could not convert request body content to class type 'org.apache.juneau.server.ErrorConditionsResource$Test1' using parser 'org.apache.juneau.json.JsonParser'", + "Unknown property 'f2' encountered while trying to parse into class 'org.apache.juneau.server.ErrorConditionsResource$Test1'"); + } + } + + //==================================================================================================== + // Test trying to set properties to wrong data type + //==================================================================================================== + @Test + public void testWrongDataType() throws Exception { + String url = URL + "/testWrongDataType"; + try { + client.doPut(url + "?noTrace=true", new ObjectMap("{f1:'foo'}")).getResponseAsString(); + fail("Exception expected"); + } catch (RestCallException e) { + checkErrorResponse(debug, e, SC_BAD_REQUEST, + "Could not convert request body content to class type 'org.apache.juneau.server.ErrorConditionsResource$Test2' using parser 'org.apache.juneau.json.JsonParser'.", + "Could not convert string 'foo' to class 'int'"); + } + } + + //==================================================================================================== + // Test trying to parse into class with non-public no-arg constructor. + //==================================================================================================== + @Test + public void testParseIntoNonConstructableBean() throws Exception { + String url = URL + "/testParseIntoNonConstructableBean"; + try { + client.doPut(url + "?noTrace=true", new ObjectMap("{f1:1}")).getResponseAsString(); + fail("Exception expected"); + } catch (RestCallException e) { + checkErrorResponse(debug, e, SC_BAD_REQUEST, + "Class 'org.apache.juneau.server.ErrorConditionsResource$Test3a' could not be instantiated."); + } + } + + //==================================================================================================== + // Test trying to parse into non-static inner class + //==================================================================================================== + @Test + public void testParseIntoNonStaticInnerClass() throws Exception { + String url = URL + "/testParseIntoNonStaticInnerClass"; + try { + client.doPut(url + "?noTrace=true", new ObjectMap("{f1:1}")).getResponseAsString(); + fail("Exception expected"); + } catch (RestCallException e) { + checkErrorResponse(debug, e, SC_BAD_REQUEST, + "Class 'org.apache.juneau.server.ErrorConditionsResource$Test3b' could not be instantiated. Reason: 'No properties detected on bean class'"); + } + } + + //==================================================================================================== + // Test trying to parse into non-public inner class + //==================================================================================================== + @Test + public void testParseIntoNonPublicInnerClass() throws Exception { + String url = URL + "/testParseIntoNonPublicInnerClass"; + try { + client.doPut(url + "?noTrace=true", new ObjectMap("{f1:1}")).getResponseAsString(); + fail("Exception expected"); + } catch (RestCallException e) { + checkErrorResponse(debug, e, SC_BAD_REQUEST, + "Class 'org.apache.juneau.server.ErrorConditionsResource$Test3b1' could not be instantiated", + "Class is not public"); + } + } + + //==================================================================================================== + // Test exception thrown during bean construction. + //==================================================================================================== + @Test + public void testThrownConstructorException() throws Exception { + String url = URL + "/testThrownConstructorException"; + try { + client.doPut(url + "?noTrace=true", "'foo'").getResponseAsString(); + fail("Exception expected"); + } catch (RestCallException e) { + checkErrorResponse(debug, e, SC_BAD_REQUEST, + "Could not convert request body content to class type 'org.apache.juneau.server.ErrorConditionsResource$Test3c' using parser 'org.apache.juneau.json.JsonParser'.", + "Caused by (RuntimeException): Test error"); + } + } + + //==================================================================================================== + // Test trying to set parameters to invalid types. + //==================================================================================================== + @Test + public void testSetParameterToInvalidTypes() throws Exception { + String url = URL + "/testSetParameterToInvalidTypes"; + try { + client.doPut(url + "/1?noTrace=true&p1=foo", "").getResponseAsString(); + fail("Exception expected"); + } catch (RestCallException e) { + checkErrorResponse(debug, e, SC_BAD_REQUEST, + "Could not convert PARAM 'p1' to type 'int' on method 'org.apache.juneau.server.ErrorConditionsResource.testSetParameterToInvalidTypes'"); + } + + try { + client.doPut(url + "/foo?noTrace=true&p1=1", "").getResponseAsString(); + fail("Exception expected"); + } catch (RestCallException e) { + checkErrorResponse(debug, e, SC_BAD_REQUEST, + "Could not convert ATTR 'a1' to type 'int' on method 'org.apache.juneau.server.ErrorConditionsResource.testSetParameterToInvalidTypes'"); + } + + try { + client.doPut(url + "/1?noTrace=true&p1=1", "").setHeader("h1", "foo").getResponseAsString(); + fail("Exception expected"); + } catch (RestCallException e) { + checkErrorResponse(debug, e, SC_BAD_REQUEST, + "Could not convert HEADER 'h1' to type 'int' on method 'org.apache.juneau.server.ErrorConditionsResource.testSetParameterToInvalidTypes'"); + } + } + + //==================================================================================================== + // Test SC_NOT_FOUND & SC_METHOD_NOT_ALLOWED + //==================================================================================================== + @Test + public void test404and405() throws Exception { + String url = URL + "/test404and405"; + try { + client.doGet(URL + "/testNonExistent?noTrace=true").getResponseAsString(); + fail("Exception expected"); + } catch (RestCallException e) { + checkErrorResponse(debug, e, SC_NOT_FOUND, + "Method 'GET' not found on resource with matching pattern on path '/testNonExistent'"); + } + + try { + client.doPut(url + "?noTrace=true", "").getResponseAsString(); + fail("Exception expected"); + } catch (RestCallException e) { + checkErrorResponse(debug, e, SC_NOT_FOUND, + "Method 'PUT' not found on resource with matching pattern on path '/test404and405'"); + } + + try { + client.doPost(url + "?noTrace=true", "").getResponseAsString(); + fail("Exception expected"); + } catch (RestCallException e) { + checkErrorResponse(debug, e, SC_METHOD_NOT_ALLOWED, + "Method 'POST' not found on resource."); + } + } + + //==================================================================================================== + // Test SC_PRECONDITION_FAILED + //==================================================================================================== + @Test + public void test412() throws Exception { + String url = URL + "/test412"; + try { + client.doGet(url + "?noTrace=true").getResponseAsString(); + fail("Exception expected"); + } catch (RestCallException e) { + checkErrorResponse(debug, e, SC_PRECONDITION_FAILED, + "Method 'GET' not found on resource on path '/test412' with matching matcher."); + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/bea31abd/juneau-server-test/src/test/java/org/apache/juneau/server/GroupsTest.java ---------------------------------------------------------------------- diff --git a/juneau-server-test/src/test/java/org/apache/juneau/server/GroupsTest.java b/juneau-server-test/src/test/java/org/apache/juneau/server/GroupsTest.java new file mode 100755 index 0000000..fd865f9 --- /dev/null +++ b/juneau-server-test/src/test/java/org/apache/juneau/server/GroupsTest.java @@ -0,0 +1,122 @@ +/*************************************************************************************************************************** + * 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.json.*; +import org.junit.*; + + +public class GroupsTest { + + private static String URL = "/testGroups"; + private static boolean debug = false; + + //==================================================================================================== + // Serializer defined on class. + //==================================================================================================== + @Test + public void testSerializerDefinedOnClass() throws Exception { + RestClient client = new TestRestClient(JsonSerializer.DEFAULT, JsonParser.DEFAULT); + String url = URL + "/testSerializerDefinedOnClass"; + String r; + + try { + client.setContentType("text/p1"); + r = client.doGet(url+"?noTrace=true").getResponseAsString(); + fail("Exception expected"); + } catch (RestCallException e) { + checkErrorResponse(debug, e, SC_NOT_ACCEPTABLE, + "Unsupported media-type in request header 'Accept': 'application/json'", + "Supported media-types: [text/s1, text/s2]" + ); + } + + client.setAccept("text/s1").setContentType(""); + r = client.doGet(url).getResponseAsString(); + assertEquals("text/s,GET", r); + + client.setAccept("text/s2").setContentType(""); + r = client.doGet(url).getResponseAsString(); + assertEquals("text/s,GET", r); + + try { + client.setAccept("text/s3").setContentType(""); + r = client.doGet(url+"?noTrace=true").getResponseAsString(); + assertEquals("text/s,GET", r); + } catch (RestCallException e) { + checkErrorResponse(debug, e, SC_NOT_ACCEPTABLE, + "Unsupported media-type in request header 'Accept': 'text/s3'", + "Supported media-types: [text/s1, text/s2]" + ); + } + + try { + client.setAccept("text/json").setContentType("text/p1"); + r = client.doPut(url+"?noTrace=true", new StringReader("foo")).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/s1, text/s2]" + ); + } + + try { + client.setAccept("text/s1").setContentType("text/json"); + r = client.doPut(url+"?noTrace=true", new StringReader("foo")).getResponseAsString(); + fail("Exception expected"); + } catch (RestCallException e) { + checkErrorResponse(debug, e, SC_UNSUPPORTED_MEDIA_TYPE, + "Unsupported media-type in request header 'Content-Type': 'text/json'", + "Supported media-types: [text/p1, text/p2]" + ); + } + + client.setContentType("text/p1").setAccept("text/s1"); + r = client.doPut(url, new StringReader("foo")).getResponseAsString(); + assertEquals("text/s,foo", r); + + client.setContentType("text/p2").setAccept("text/s2"); + r = client.doPut(url, new StringReader("foo")).getResponseAsString(); + assertEquals("text/s,foo", r); + + try { + client.setContentType("text/p1").setAccept("text/s3"); + r = client.doPut(url+"?noTrace=true", new StringReader("foo")).getResponseAsString(); + } catch (RestCallException e) { + checkErrorResponse(debug, e, SC_NOT_ACCEPTABLE, + "Unsupported media-type in request header 'Accept': 'text/s3'", + "Supported media-types: [text/s1, text/s2]" + ); + } + + try { + client.setContentType("text/p3").setAccept("text/s1"); + r = client.doPut(url+"?noTrace=true", new StringReader("foo")).getResponseAsString(); + } catch (RestCallException e) { + checkErrorResponse(debug, e, SC_UNSUPPORTED_MEDIA_TYPE, + "Unsupported media-type in request header 'Content-Type': 'text/p3'", + "Supported media-types: [text/p1, text/p2]" + ); + } + + client.closeQuietly(); + } +}
