You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@juneau.apache.org by ja...@apache.org on 2016/08/09 17:15:59 UTC
[38/44] incubator-juneau git commit: Rename CT_* testcases.
http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/df0f8689/org.apache.juneau.server.test/src/test/java/org/apache/juneau/server/TestAcceptCharsetTest.java
----------------------------------------------------------------------
diff --git a/org.apache.juneau.server.test/src/test/java/org/apache/juneau/server/TestAcceptCharsetTest.java b/org.apache.juneau.server.test/src/test/java/org/apache/juneau/server/TestAcceptCharsetTest.java
new file mode 100755
index 0000000..93a02b5
--- /dev/null
+++ b/org.apache.juneau.server.test/src/test/java/org/apache/juneau/server/TestAcceptCharsetTest.java
@@ -0,0 +1,123 @@
+/***************************************************************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations under the License.
+ ***************************************************************************************************************************/
+package org.apache.juneau.server;
+
+import static javax.servlet.http.HttpServletResponse.*;
+import static org.apache.juneau.server.TestUtils.*;
+import static org.junit.Assert.*;
+
+import java.io.*;
+
+import org.apache.juneau.client.*;
+import org.apache.juneau.internal.*;
+import org.junit.*;
+
+public class TestAcceptCharsetTest {
+
+ boolean debug = false;
+
+ //====================================================================================================
+ // Test that Q-values are being resolved correctly.
+ //====================================================================================================
+ @Test
+ public void testQValues() throws Exception {
+ RestClient client = new TestRestClient().setHeader("Accept", "text/plain");
+
+ check1(client, "utf-8", "utf-8");
+ check1(client, "iso-8859-1", "iso-8859-1");
+ check1(client, "bad,utf-8", "utf-8");
+ check1(client, "utf-8,bad", "utf-8");
+ check1(client, "bad;q=0.9,utf-8;q=0.1", "utf-8");
+ check1(client, "bad;q=0.1,utf-8;q=0.9", "utf-8");
+ check1(client, "utf-8,iso-8859-1", "utf-8");
+ check1(client, "iso-8859-1,utf-8", "utf-8");
+ check1(client, "utf-8;q=0.9,iso-8859-1;q=0.1", "utf-8");
+ check1(client, "utf-8;q=0.1,iso-8859-1;q=0.9", "iso-8859-1");
+ check1(client, "*", "utf-8");
+ check1(client, "bad,iso-8859-1;q=0.5,*;q=0.1", "iso-8859-1");
+ check1(client, "bad,iso-8859-1;q=0.1,*;q=0.5", "utf-8");
+
+ client.closeQuietly();
+ }
+
+ private void check1(RestClient client, String requestCharset, String responseCharset) throws Exception {
+ RestCall r;
+ InputStream is;
+ String url = "/testAcceptCharset/testQValues";
+ r = client.doGet(url).setHeader("Accept-Charset", requestCharset).connect();
+ assertTrue(r.getResponse().getFirstHeader("Content-Type").getValue().toLowerCase().contains(responseCharset));
+ is = r.getInputStream();
+ assertEquals("foo", IOUtils.read(new InputStreamReader(is, responseCharset)));
+ }
+
+ //====================================================================================================
+ // Validate various Accept-Charset variations.
+ //====================================================================================================
+ @Test
+ public void testCharsetOnResponse() throws Exception {
+ RestClient client = new TestRestClient().setAccept("text/plain").setContentType("text/plain");
+ String url = "/testAcceptCharset/testCharsetOnResponse";
+ String r;
+
+ r = client.doPut(url, new StringReader("")).getResponseAsString();
+ assertEquals("utf-8/utf-8", r.toLowerCase());
+
+ r = client.doPut(url, new StringReader("")).setHeader("Accept-Charset", "Shift_JIS").getResponseAsString();
+ assertEquals("utf-8/shift_jis", r.toLowerCase());
+
+ try {
+ r = client.doPut(url+"?noTrace=true", new StringReader("")).setHeader("Accept-Charset", "BAD").getResponseAsString();
+ fail("Exception expected");
+ } catch (RestCallException e) {
+ checkErrorResponse(debug, e, SC_NOT_ACCEPTABLE, "No supported charsets in header 'Accept-Charset': 'BAD'");
+ }
+
+ r = client.doPut(url, new StringReader("")).setHeader("Accept-Charset", "UTF-8").getResponseAsString();
+ assertEquals("utf-8/utf-8", r.toLowerCase());
+
+ r = client.doPut(url, new StringReader("")).setHeader("Accept-Charset", "bad,iso-8859-1").getResponseAsString();
+ assertEquals("utf-8/iso-8859-1", r.toLowerCase());
+
+ r = client.doPut(url, new StringReader("")).setHeader("Accept-Charset", "bad;q=0.9,iso-8859-1;q=0.1").getResponseAsString();
+ assertEquals("utf-8/iso-8859-1", r.toLowerCase());
+
+ r = client.doPut(url, new StringReader("")).setHeader("Accept-Charset", "bad;q=0.1,iso-8859-1;q=0.9").getResponseAsString();
+ assertEquals("utf-8/iso-8859-1", r.toLowerCase());
+
+ client.setHeader("Accept-Charset", "utf-8");
+
+ r = client.doPut(url, new StringReader("")).setHeader("Content-Type", "text/plain").getResponseAsString();
+ assertEquals("utf-8/utf-8", r.toLowerCase());
+
+ r = client.doPut(url, new StringReader("")).setHeader("Content-Type", "text/plain;charset=utf-8").getResponseAsString();
+ assertEquals("utf-8/utf-8", r.toLowerCase());
+
+ r = client.doPut(url, new StringReader("")).setHeader("Content-Type", "text/plain;charset=UTF-8").getResponseAsString();
+ assertEquals("utf-8/utf-8", r.toLowerCase());
+
+ r = client.doPut(url, new StringReader("")).setHeader("Content-Type", "text/plain;charset=iso-8859-1").getResponseAsString();
+ assertEquals("iso-8859-1/utf-8", r.toLowerCase());
+
+ r = client.doPut(url, new StringReader("")).setHeader("Content-Type", "text/plain;charset=Shift_JIS").getResponseAsString();
+ assertEquals("shift_jis/utf-8", r.toLowerCase());
+
+ try {
+ r = client.doPut(url + "?noTrace=true&Content-Type=text/plain;charset=BAD", new StringReader("")).getResponseAsString();
+ fail("Exception expected");
+ } catch (RestCallException e) {
+ checkErrorResponse(debug, e, SC_UNSUPPORTED_MEDIA_TYPE, "Unsupported charset in header 'Content-Type': 'text/plain;charset=BAD'");
+ }
+
+ client.closeQuietly();
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/df0f8689/org.apache.juneau.server.test/src/test/java/org/apache/juneau/server/TestBeanContextPropertiesTest.java
----------------------------------------------------------------------
diff --git a/org.apache.juneau.server.test/src/test/java/org/apache/juneau/server/TestBeanContextPropertiesTest.java b/org.apache.juneau.server.test/src/test/java/org/apache/juneau/server/TestBeanContextPropertiesTest.java
new file mode 100755
index 0000000..50ec9c6
--- /dev/null
+++ b/org.apache.juneau.server.test/src/test/java/org/apache/juneau/server/TestBeanContextPropertiesTest.java
@@ -0,0 +1,37 @@
+/***************************************************************************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations under the License.
+ ***************************************************************************************************************************/
+package org.apache.juneau.server;
+
+import static org.junit.Assert.*;
+
+import org.apache.juneau.client.*;
+import org.apache.juneau.json.*;
+import org.junit.*;
+
+public class TestBeanContextPropertiesTest {
+
+ boolean debug = false;
+
+ //====================================================================================================
+ // Validate that filters defined on class filter to underlying bean context.
+ //====================================================================================================
+ @Test
+ public void testClassTransforms() throws Exception {
+ RestClient client = new TestRestClient(JsonSerializer.class, JsonParser.class);
+ String r;
+ r = client.doGet("/testBeanContext/testClassTransforms/2001-07-04T15:30:45Z?d2=2001-07-05T15:30:45Z").setHeader("X-D3", "2001-07-06T15:30:45Z").getResponseAsString();
+ assertEquals("d1=2001-07-04T15:30:45Z,d2=2001-07-05T15:30:45Z,d3=2001-07-06T15:30:45Z", r);
+
+ client.closeQuietly();
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/df0f8689/org.apache.juneau.server.test/src/test/java/org/apache/juneau/server/TestCallbackStringsTest.java
----------------------------------------------------------------------
diff --git a/org.apache.juneau.server.test/src/test/java/org/apache/juneau/server/TestCallbackStringsTest.java b/org.apache.juneau.server.test/src/test/java/org/apache/juneau/server/TestCallbackStringsTest.java
new file mode 100755
index 0000000..2d8ccca
--- /dev/null
+++ b/org.apache.juneau.server.test/src/test/java/org/apache/juneau/server/TestCallbackStringsTest.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 TestCallbackStringsTest {
+
+ //====================================================================================================
+ // 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/df0f8689/org.apache.juneau.server.test/src/test/java/org/apache/juneau/server/TestCharsetEncodingsTest.java
----------------------------------------------------------------------
diff --git a/org.apache.juneau.server.test/src/test/java/org/apache/juneau/server/TestCharsetEncodingsTest.java b/org.apache.juneau.server.test/src/test/java/org/apache/juneau/server/TestCharsetEncodingsTest.java
new file mode 100755
index 0000000..996f283
--- /dev/null
+++ b/org.apache.juneau.server.test/src/test/java/org/apache/juneau/server/TestCharsetEncodingsTest.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 TestCharsetEncodingsTest {
+
+ 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/df0f8689/org.apache.juneau.server.test/src/test/java/org/apache/juneau/server/TestClientVersionTest.java
----------------------------------------------------------------------
diff --git a/org.apache.juneau.server.test/src/test/java/org/apache/juneau/server/TestClientVersionTest.java b/org.apache.juneau.server.test/src/test/java/org/apache/juneau/server/TestClientVersionTest.java
new file mode 100644
index 0000000..96a6304
--- /dev/null
+++ b/org.apache.juneau.server.test/src/test/java/org/apache/juneau/server/TestClientVersionTest.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 TestClientVersionTest {
+
+ private static String URL = "/testClientVersion";
+
+ //====================================================================================================
+ // Basic tests - default X-Client-Version header.
+ //====================================================================================================
+ @Test
+ public void testDefaultHeader() throws Exception {
+ RestClient c = new TestRestClient(PlainTextSerializer.class, PlainTextParser.class);
+ String url = URL + "/defaultHeader";
+
+ assertEquals("no-version", c.doGet(url).getResponseAsString());
+
+ for (String s : "0, 0.0, 0.1, .1, .9, .99".split("\\s*,\\s*")) {
+ c.setClientVersion(s);
+ assertEquals("[0.0,1.0)", c.doGet(url).getResponseAsString());
+ }
+
+ for (String s : "1, 1.0, 1.0.0, 1.0.1".split("\\s*,\\s*")) {
+ c.setClientVersion(s);
+ assertEquals("[1.0,1.0]", c.doGet(url).getResponseAsString());
+ }
+
+ for (String s : "1.1, 1.1.1, 1.2, 1.9.9".split("\\s*,\\s*")) {
+ c.setClientVersion(s);
+ assertEquals("[1.1,2)", c.doGet(url).getResponseAsString());
+ }
+
+ for (String s : "2, 2.0, 2.1, 9, 9.9".split("\\s*,\\s*")) {
+ c.setClientVersion(s);
+ assertEquals("2", c.doGet(url).getResponseAsString());
+ }
+
+ c.closeQuietly();
+ }
+
+ //====================================================================================================
+ // Basic tests - Custom-Client-Version header.
+ //====================================================================================================
+ @Test
+ public void testCustomHeader() throws Exception {
+ RestClient c = new TestRestClient(PlainTextSerializer.class, PlainTextParser.class);
+ String url = URL + "/customHeader";
+
+ assertEquals("no-version", c.doGet(url).getResponseAsString());
+
+ for (String s : "0, 0.0, 0.1, .1, .9, .99".split("\\s*,\\s*")) {
+ c.setHeader("Custom-Client-Version", s);
+ assertEquals("[0.0,1.0)", c.doGet(url).getResponseAsString());
+ }
+
+ for (String s : "1, 1.0, 1.0.0, 1.0.1".split("\\s*,\\s*")) {
+ c.setHeader("Custom-Client-Version", s);
+ assertEquals("[1.0,1.0]", c.doGet(url).getResponseAsString());
+ }
+
+ for (String s : "1.1, 1.1.1, 1.2, 1.9.9".split("\\s*,\\s*")) {
+ c.setHeader("Custom-Client-Version", s);
+ assertEquals("[1.1,2)", c.doGet(url).getResponseAsString());
+ }
+
+ for (String s : "2, 2.0, 2.1, 9, 9.9".split("\\s*,\\s*")) {
+ c.setHeader("Custom-Client-Version", s);
+ assertEquals("2", c.doGet(url).getResponseAsString());
+ }
+
+ c.closeQuietly();
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/df0f8689/org.apache.juneau.server.test/src/test/java/org/apache/juneau/server/TestConfigTest.java
----------------------------------------------------------------------
diff --git a/org.apache.juneau.server.test/src/test/java/org/apache/juneau/server/TestConfigTest.java b/org.apache.juneau.server.test/src/test/java/org/apache/juneau/server/TestConfigTest.java
new file mode 100755
index 0000000..a417093
--- /dev/null
+++ b/org.apache.juneau.server.test/src/test/java/org/apache/juneau/server/TestConfigTest.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 TestConfigTest {
+
+ 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/df0f8689/org.apache.juneau.server.test/src/test/java/org/apache/juneau/server/TestContentTest.java
----------------------------------------------------------------------
diff --git a/org.apache.juneau.server.test/src/test/java/org/apache/juneau/server/TestContentTest.java b/org.apache.juneau.server.test/src/test/java/org/apache/juneau/server/TestContentTest.java
new file mode 100755
index 0000000..84a6010
--- /dev/null
+++ b/org.apache.juneau.server.test/src/test/java/org/apache/juneau/server/TestContentTest.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 TestContentTest {
+
+ 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/df0f8689/org.apache.juneau.server.test/src/test/java/org/apache/juneau/server/TestDefaultContentTypesTest.java
----------------------------------------------------------------------
diff --git a/org.apache.juneau.server.test/src/test/java/org/apache/juneau/server/TestDefaultContentTypesTest.java b/org.apache.juneau.server.test/src/test/java/org/apache/juneau/server/TestDefaultContentTypesTest.java
new file mode 100755
index 0000000..613619f
--- /dev/null
+++ b/org.apache.juneau.server.test/src/test/java/org/apache/juneau/server/TestDefaultContentTypesTest.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 TestDefaultContentTypesTest {
+
+ 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();
+ }
+}