http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/d45e1351/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 deleted file mode 100755 index b6f5593..0000000 --- a/juneau-server-test/src/test/java/org/apache/juneau/server/CharsetEncodingsTest.java +++ /dev/null @@ -1,96 +0,0 @@ -// *************************************************************************************************************************** -// * 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/d45e1351/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 deleted file mode 100644 index 30428ad..0000000 --- a/juneau-server-test/src/test/java/org/apache/juneau/server/ClientVersionTest.java +++ /dev/null @@ -1,90 +0,0 @@ -// *************************************************************************************************************************** -// * 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/d45e1351/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 deleted file mode 100755 index 7c6e532..0000000 --- a/juneau-server-test/src/test/java/org/apache/juneau/server/ConfigTest.java +++ /dev/null @@ -1,58 +0,0 @@ -// *************************************************************************************************************************** -// * 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/d45e1351/juneau-server-test/src/test/java/org/apache/juneau/server/Constants.java ---------------------------------------------------------------------- diff --git a/juneau-server-test/src/test/java/org/apache/juneau/server/Constants.java b/juneau-server-test/src/test/java/org/apache/juneau/server/Constants.java deleted file mode 100755 index 7d51c7d..0000000 --- a/juneau-server-test/src/test/java/org/apache/juneau/server/Constants.java +++ /dev/null @@ -1,53 +0,0 @@ -// *************************************************************************************************************************** -// * 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.net.*; - - -public class Constants { - - private static String juneauSampleUrl = System.getProperty("JUNO_SAMPLE_URL", "http://localhost:10000"); - private static URI juneauSampleUri = (juneauSampleUrl == null ? null : URI.create(juneauSampleUrl)); - - /** - * Returns the value of the "JUNO_SAMPLE_URL" system property, or throws a {@link RuntimeException} - * if it's not set. - */ - public static String getJuneauSamplesUrl() { - if (juneauSampleUrl == null) - throw new RuntimeException("'JUNO_SAMPLE_URL' system property not set to URL of juneau.sample.war location."); - return juneauSampleUrl; - } - - public static URI getJuneauSamplesUri() { - if (juneauSampleUri == null) - throw new RuntimeException("'JUNO_SAMPLE_URL' system property not set to URL of juneau.sample.war location."); - return juneauSampleUri; - } - - private static String juneauServerTestUrl = System.getProperty("JUNO_SERVER_TEST_URL", "http://localhost:10001"); - private static URI juneauServerTestUri = (juneauServerTestUrl == null ? null : URI.create(juneauServerTestUrl)); - - public static String getServerTestUrl() { - if (juneauServerTestUrl == null) - throw new RuntimeException("'JUNO_SERVER_TEST_URL' system property not set to URL of juneau.sample.war location."); - return juneauServerTestUrl; - } - - public static URI getServerTestUri() { - if (juneauServerTestUri == null) - throw new RuntimeException("'JUNO_SERVER_TEST_URL' system property not set to URL of juneau.sample.war location."); - return juneauServerTestUri; - } -} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/d45e1351/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 deleted file mode 100755 index 03f6a89..0000000 --- a/juneau-server-test/src/test/java/org/apache/juneau/server/ContentTest.java +++ /dev/null @@ -1,706 +0,0 @@ -// *************************************************************************************************************************** -// * 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("{f01:['a','b'],f02:['c','d'],f03:[1,2],f04:[3,4],f05:[['e','f'],['g','h']],f06:[['i','j'],['k','l']],f07:[{a:'a',b:1,c:true},{a:'a',b:1,c:true}],f08:[{a:'a',b:1,c:true},{a:'a',b:1,c:true}],f09:[[{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("{f01:['a','b'],f02:['c','d'],f03:[1,2],f04:[3,4],f05:[['e','f'],['g','h']],f06:[['i','j'],['k','l']],f07:[{a:'a',b:1,c:true},{a:'a',b:1,c:true}],f08:[{a:'a',b:1,c:true},{a:'a',b:1,c:true}],f09:[[{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("{f01:['a','b'],f02:['c','d'],f03:[1,2],f04:[3,4],f05:[['e','f'],['g','h']],f06:[['i','j'],['k','l']],f07:[{a:'a',b:1,c:true},{a:'a',b:1,c:true}],f08:[{a:'a',b:1,c:true},{a:'a',b:1,c:true}],f09:[[{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("{f01:['a','b'],f02:['c','d'],f03:[1,2],f04:[3,4],f05:[['e','f'],['g','h']],f06:[['i','j'],['k','l']],f07:[{a:'a',b:1,c:true},{a:'a',b:1,c:true}],f08:[{a:'a',b:1,c:true},{a:'a',b:1,c:true}],f09:[[{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("{f01:['a','b'],f02:['c','d'],f03:[1,2],f04:[3,4],f05:[['e','f'],['g','h']],f06:[['i','j'],['k','l']],f07:[{a:'a',b:1,c:true},{a:'a',b:1,c:true}],f08:[{a:'a',b:1,c:true},{a:'a',b:1,c:true}],f09:[[{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("{f01:['a','b'],f02:['c','d'],f03:[1,2],f04:[3,4],f05:[['e','f'],['g','h']],f06:[['i','j'],['k','l']],f07:[{a:'a',b:1,c:true},{a:'a',b:1,c:true}],f08:[{a:'a',b:1,c:true},{a:'a',b:1,c:true}],f09:[[{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("{f01:['a','b'],f02:['c','d'],f03:[1,2],f04:[3,4],f05:[['e','f'],['g','h']],f06:[['i','j'],['k','l']],f07:[{a:'a',b:1,c:true},{a:'a',b:1,c:true}],f08:[{a:'a',b:1,c:true},{a:'a',b:1,c:true}],f09:[[{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("{f01:['a','b'],f02:['c','d'],f03:[1,2],f04:[3,4],f05:[['e','f'],['g','h']],f06:[['i','j'],['k','l']],f07:[{a:'a',b:1,c:true},{a:'a',b:1,c:true}],f08:[{a:'a',b:1,c:true},{a:'a',b:1,c:true}],f09:[[{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("{f01:['a','b'],f02:['c','d'],f03:[1,2],f04:[3,4],f05:[['e','f'],['g','h']],f06:[['i','j'],['k','l']],f07:[{a:'a',b:1,c:true},{a:'a',b:1,c:true}],f08:[{a:'a',b:1,c:true},{a:'a',b:1,c:true}],f09:[[{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("{f01:['a','b'],f02:['c','d'],f03:[1,2],f04:[3,4],f05:[['e','f'],['g','h']],f06:[['i','j'],['k','l']],f07:[{a:'a',b:1,c:true},{a:'a',b:1,c:true}],f08:[{a:'a',b:1,c:true},{a:'a',b:1,c:true}],f09:[[{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("{f01:['a','b'],f02:['c','d'],f03:[1,2],f04:[3,4],f05:[['e','f'],['g','h']],f06:[['i','j'],['k','l']],f07:[{a:'a',b:1,c:true},{a:'a',b:1,c:true}],f08:[{a:'a',b:1,c:true},{a:'a',b:1,c:true}],f09:[[{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("{f01:['a','b'],f02:['c','d'],f03:[1,2],f04:[3,4],f05:[['e','f'],['g','h']],f06:[['i','j'],['k','l']],f07:[{a:'a',b:1,c:true},{a:'a',b:1,c:true}],f08:[{a:'a',b:1,c:true},{a:'a',b:1,c:true}],f09:[[{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/d45e1351/juneau-server-test/src/test/java/org/apache/juneau/server/DTOs.java ---------------------------------------------------------------------- diff --git a/juneau-server-test/src/test/java/org/apache/juneau/server/DTOs.java b/juneau-server-test/src/test/java/org/apache/juneau/server/DTOs.java deleted file mode 100755 index 541245a..0000000 --- a/juneau-server-test/src/test/java/org/apache/juneau/server/DTOs.java +++ /dev/null @@ -1,139 +0,0 @@ -// *************************************************************************************************************************** -// * 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.util.*; - -import org.apache.juneau.annotation.*; -import org.apache.juneau.urlencoding.annotation.*; - -public class DTOs { - - @Bean(sort=true) - public static class A { - public String a; - public int b; - public boolean c; - - public static A create() { - A t = new A(); - t.a = "a"; - t.b = 1; - t.c = true; - return t; - } - - } - - @SuppressWarnings("serial") - @Bean(sort=true) - public static class B { - public String[] f01; - public List<String> f02; - public int[] f03; - public List<Integer> f04; - public String[][] f05; - public List<String[]> f06; - public A[] f07; - public List<A> f08; - public A[][] f09; - public List<List<A>> f10; - - private String[] f11; - private List<String> f12; - private int[] f13; - private List<Integer> f14; - private String[][] f15; - private List<String[]> f16; - private A[] f17; - private List<A> f18; - private A[][] f19; - private List<List<A>> f20; - - public String[] getF11() { return f11; } - public List<String> getF12() { return f12; } - public int[] getF13() { return f13; } - public List<Integer> getF14() { return f14; } - public String[][] getF15() { return f15; } - public List<String[]> getF16() { return f16; } - public A[] getF17() { return f17; } - public List<A> getF18() { return f18; } - public A[][] getF19() { return f19; } - public List<List<A>> getF20() { return f20; } - - public void setF11(String[] f11) { this.f11 = f11; } - public void setF12(List<String> f12) { this.f12 = f12; } - public void setF13(int[] f13) { this.f13 = f13; } - public void setF14(List<Integer> f14) { this.f14 = f14; } - public void setF15(String[][] f15) { this.f15 = f15; } - public void setF16(List<String[]> f16) { this.f16 = f16; } - public void setF17(A[] f17) { this.f17 = f17; } - public void setF18(List<A> f18) { this.f18 = f18; } - public void setF19(A[][] f19) { this.f19 = f19; } - public void setF20(List<List<A>> f20) { this.f20 = f20; } - - static B create() { - B t = new B(); - t.f01 = new String[]{"a","b"}; - t.f02 = new ArrayList<String>(){{add("c");add("d");}}; - t.f03 = new int[]{1,2}; - t.f04 = new ArrayList<Integer>(){{add(3);add(4);}}; - t.f05 = new String[][]{{"e","f"},{"g","h"}}; - t.f06 = new ArrayList<String[]>(){{add(new String[]{"i","j"});add(new String[]{"k","l"});}}; - t.f07 = new A[]{A.create(),A.create()}; - t.f08 = new ArrayList<A>(){{add(A.create());add(A.create());}}; - t.f09 = new A[][]{{A.create()},{A.create()}}; - t.f10 = new ArrayList<List<A>>(){{add(Arrays.asList(A.create()));add(Arrays.asList(A.create()));}}; - t.setF11(new String[]{"a","b"}); - t.setF12(new ArrayList<String>(){{add("c");add("d");}}); - t.setF13(new int[]{1,2}); - t.setF14(new ArrayList<Integer>(){{add(3);add(4);}}); - t.setF15(new String[][]{{"e","f"},{"g","h"}}); - t.setF16(new ArrayList<String[]>(){{add(new String[]{"i","j"});add(new String[]{"k","l"});}}); - t.setF17(new A[]{A.create(),A.create()}); - t.setF18(new ArrayList<A>(){{add(A.create());add(A.create());}}); - t.setF19(new A[][]{{A.create()},{A.create()}}); - t.setF20(new ArrayList<List<A>>(){{add(Arrays.asList(A.create()));add(Arrays.asList(A.create()));}}); - return t; - } - } - - @UrlEncoding(expandedParams=true) - public static class C extends B { - @SuppressWarnings("serial") - static C create() { - C t = new C(); - t.f01 = new String[]{"a","b"}; - t.f02 = new ArrayList<String>(){{add("c");add("d");}}; - t.f03 = new int[]{1,2}; - t.f04 = new ArrayList<Integer>(){{add(3);add(4);}}; - t.f05 = new String[][]{{"e","f"},{"g","h"}}; - t.f06 = new ArrayList<String[]>(){{add(new String[]{"i","j"});add(new String[]{"k","l"});}}; - t.f07 = new A[]{A.create(),A.create()}; - t.f08 = new ArrayList<A>(){{add(A.create());add(A.create());}}; - t.f09 = new A[][]{{A.create()},{A.create()}}; - t.f10 = new ArrayList<List<A>>(){{add(Arrays.asList(A.create()));add(Arrays.asList(A.create()));}}; - t.setF11(new String[]{"a","b"}); - t.setF12(new ArrayList<String>(){{add("c");add("d");}}); - t.setF13(new int[]{1,2}); - t.setF14(new ArrayList<Integer>(){{add(3);add(4);}}); - t.setF15(new String[][]{{"e","f"},{"g","h"}}); - t.setF16(new ArrayList<String[]>(){{add(new String[]{"i","j"});add(new String[]{"k","l"});}}); - t.setF17(new A[]{A.create(),A.create()}); - t.setF18(new ArrayList<A>(){{add(A.create());add(A.create());}}); - t.setF19(new A[][]{{A.create()},{A.create()}}); - t.setF20(new ArrayList<List<A>>(){{add(Arrays.asList(A.create()));add(Arrays.asList(A.create()));}}); - return t; - } - } -} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/d45e1351/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 deleted file mode 100755 index 731b786..0000000 --- a/juneau-server-test/src/test/java/org/apache/juneau/server/DefaultContentTypesTest.java +++ /dev/null @@ -1,497 +0,0 @@ -// *************************************************************************************************************************** -// * 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(); - } -}
