This is an automated email from the ASF dual-hosted git repository.
jamesbognar pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/juneau.git
The following commit(s) were added to refs/heads/master by this push:
new 0441033 Tests.
0441033 is described below
commit 044103339b4c3d6df8f6dfc277ae7a2c82150782
Author: JamesBognar <[email protected]>
AuthorDate: Sun May 13 12:11:05 2018 -0400
Tests.
---
.../org/apache/juneau/rest/test/GzipResource.java | 113 -------
.../java/org/apache/juneau/rest/test/Root.java | 3 +-
.../test/{ => client}/CallbackStringsResource.java | 2 +-
.../test/{ => client}/ClientFuturesResource.java | 2 +-
.../rest/test/{ => client}/FormDataResource.java | 2 +-
.../java/org/apache/juneau/rest/test/GzipTest.java | 340 ---------------------
.../org/apache/juneau/rest/test/_TestSuite.java | 1 -
.../juneau/rest/mock/MockServletRequest.java | 20 ++
.../juneau/rest/mock/MockServletResponse.java | 63 +++-
.../juneau/rest/headers/AcceptEncodingTest.java | 264 ++++++++++++++++
.../juneau/rest/headers/ContentEncodingTest.java | 94 ++++++
.../apache/juneau/rest/testutils/TestUtils.java | 31 ++
12 files changed, 464 insertions(+), 471 deletions(-)
diff --git
a/juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/GzipResource.java
b/juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/GzipResource.java
deleted file mode 100644
index 42f0e6c..0000000
---
a/juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/GzipResource.java
+++ /dev/null
@@ -1,113 +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.rest.test;
-
-import static org.apache.juneau.http.HttpMethodName.*;
-
-import java.io.*;
-
-import org.apache.juneau.encoders.*;
-import org.apache.juneau.plaintext.*;
-import org.apache.juneau.rest.*;
-import org.apache.juneau.rest.annotation.*;
-
-/**
- * JUnit automated testcase resource.
- */
-public class GzipResource {
-
-
//================================================================================
- // ConfigEncoder for "myencoding" encoding
-
//================================================================================
- public static class MyEncoder extends GzipEncoder {
- @Override /* ConfigEncoder */
- public String[] getCodings() {
- return new String[]{"mycoding"};
- }
- }
-
-
//====================================================================================================
- // Test with no compression enabled.
-
//====================================================================================================
- @RestResource(
- path="/testGzipOff",
- serializers=PlainTextSerializer.class,
- parsers=PlainTextParser.class
- )
- public static class TestGzipOff extends RestServlet {
- private static final long serialVersionUID = 1L;
- @RestMethod(name=GET, path="/")
- public String test1get() {
- return "foo";
- }
- @RestMethod(name=PUT, path="/")
- public String test1put(@Body String in) {
- return in;
- }
- }
-
-
//====================================================================================================
- // Test with compression enabled.
-
//====================================================================================================
- @RestResource(
- path="/testGzipOn",
- serializers=PlainTextSerializer.class,
- parsers=PlainTextParser.class,
- encoders=MyEncoder.class
- )
- public static class TestGzipOn extends RestServlet {
- private static final long serialVersionUID = 1L;
- @RestMethod(name=GET, path="/")
- public String test1() {
- return "foo";
- }
- @RestMethod(name=PUT, path="/")
- public String test1put(@Body String in) {
- return in;
- }
- // This method bypasses the content type and encoding from
- // the serializers and encoders when calling getOutputStream()
directly.
- @RestMethod(name=GET, path="/direct")
- public void direct(RestResponse res) throws Exception {
- res.setContentType("text/direct");
- OutputStream os = res.getOutputStream();
- os.write("test".getBytes());
- os.flush();
- }
-
- // This method bypasses the content type and encoding from
- // the serializers and encoders when calling getWriter()
directly.
- @RestMethod(name=GET, path="/direct2")
- public void direct2(RestResponse res) throws Exception {
- Writer w = res.getWriter();
- w.append("test");
- w.flush();
- }
-
- // This method uses getNegotiatedWriter() which should use GZip
encoding.
- @RestMethod(name=GET, path="/direct3")
- public void direct3(RestResponse res) throws Exception {
- Writer w = res.getNegotiatedWriter();
- w.append("test");
- w.flush();
- }
-
- // This method overrides the set of encoders at the method
level and so shouldn't use GZip encoding.
- @RestMethod(name=GET, path="/direct4",
encoders={IdentityEncoder.class})
- public void direct4(RestResponse res) throws Exception {
- Writer w = res.getNegotiatedWriter();
- w.append("test");
- w.flush();
- }
- }
-}
diff --git
a/juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/Root.java
b/juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/Root.java
index 5e0548d..2298c0c 100644
---
a/juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/Root.java
+++
b/juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/Root.java
@@ -18,6 +18,7 @@ import org.apache.juneau.microservice.resources.*;
import org.apache.juneau.rest.*;
import org.apache.juneau.rest.annotation.*;
import org.apache.juneau.rest.helper.*;
+import org.apache.juneau.rest.test.client.*;
@RestResource(
path="/",
@@ -30,8 +31,6 @@ import org.apache.juneau.rest.helper.*;
ConfigResource.class,
TransformsResource.class,
FormDataResource.class,
- GzipResource.TestGzipOff.class,
- GzipResource.TestGzipOn.class,
HeadersResource.class,
HtmlDocResource.class,
HtmlDocLinksResource.class,
diff --git
a/juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/CallbackStringsResource.java
b/juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/client/CallbackStringsResource.java
similarity index 96%
rename from
juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/CallbackStringsResource.java
rename to
juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/client/CallbackStringsResource.java
index 6b2ea5a..0843053 100644
---
a/juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/CallbackStringsResource.java
+++
b/juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/client/CallbackStringsResource.java
@@ -10,7 +10,7 @@
// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
express or implied. See the License for the *
// * specific language governing permissions and limitations under the
License. *
//
***************************************************************************************************************************
-package org.apache.juneau.rest.test;
+package org.apache.juneau.rest.test.client;
import static org.apache.juneau.http.HttpMethodName.*;
diff --git
a/juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/ClientFuturesResource.java
b/juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/client/ClientFuturesResource.java
similarity index 96%
rename from
juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/ClientFuturesResource.java
rename to
juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/client/ClientFuturesResource.java
index 3637839..3ab0f7a 100644
---
a/juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/ClientFuturesResource.java
+++
b/juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/client/ClientFuturesResource.java
@@ -10,7 +10,7 @@
// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
express or implied. See the License for the *
// * specific language governing permissions and limitations under the
License. *
//
***************************************************************************************************************************
-package org.apache.juneau.rest.test;
+package org.apache.juneau.rest.test.client;
import static org.apache.juneau.http.HttpMethodName.*;
diff --git
a/juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/FormDataResource.java
b/juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/client/FormDataResource.java
similarity index 96%
rename from
juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/FormDataResource.java
rename to
juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/client/FormDataResource.java
index 1183d5b..89b6452 100644
---
a/juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/FormDataResource.java
+++
b/juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/client/FormDataResource.java
@@ -10,7 +10,7 @@
// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
express or implied. See the License for the *
// * specific language governing permissions and limitations under the
License. *
//
***************************************************************************************************************************
-package org.apache.juneau.rest.test;
+package org.apache.juneau.rest.test.client;
import static org.apache.juneau.internal.IOUtils.*;
import static org.apache.juneau.http.HttpMethodName.*;
diff --git
a/juneau-microservice/juneau-microservice-test/src/test/java/org/apache/juneau/rest/test/GzipTest.java
b/juneau-microservice/juneau-microservice-test/src/test/java/org/apache/juneau/rest/test/GzipTest.java
deleted file mode 100644
index e401e6a..0000000
---
a/juneau-microservice/juneau-microservice-test/src/test/java/org/apache/juneau/rest/test/GzipTest.java
+++ /dev/null
@@ -1,340 +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.rest.test;
-
-import static javax.servlet.http.HttpServletResponse.*;
-import static org.apache.juneau.internal.IOUtils.*;
-import static org.apache.juneau.microservice.testutils.TestUtils.*;
-import static org.junit.Assert.*;
-
-import java.io.*;
-import java.util.zip.*;
-
-import org.apache.http.impl.client.*;
-import org.apache.juneau.rest.client.*;
-import org.junit.*;
-
-/**
- * Test Accept-Encoding and Content-Encoding handling.
- *
- * Note: WAS does automatic gzip decompression on http request messages, so
we have to invent
- * our own 'mycoding' compression.
- */
-public class GzipTest extends RestTestcase {
-
- private static boolean debug = false;
-
- private static String testGzipOff = "/testGzipOff";
- private static String testGzipOn = "/testGzipOn";
-
- // Converts string into a GZipped input stream.
- private static InputStream compress(String contents) throws Exception {
- ByteArrayOutputStream baos = new
ByteArrayOutputStream(contents.length()>>1);
- GZIPOutputStream gos = new GZIPOutputStream(baos);
- gos.write(contents.getBytes());
- gos.finish();
- gos.close();
- return new ByteArrayInputStream(baos.toByteArray());
- }
-
- private static String decompress(InputStream is) throws Exception {
- return read(new GZIPInputStream(is));
- }
-
-
//====================================================================================================
- // Test with no compression enabled.
-
//====================================================================================================
- @Test
- public void testGzipOff() throws Exception {
- RestClient c =
TestMicroservice.client().accept("text/plain").contentType("text/plain").build();
- RestCall r;
- String url = testGzipOff;
-
- // *** GET ***
-
- r = c.doGet(url);
- assertEquals("foo", r.getResponseAsString());
-
- r = c.doGet(url).acceptEncoding("");
- assertEquals("foo", r.getResponseAsString());
-
- r = c.doGet(url).acceptEncoding("*");
- assertEquals("foo", r.getResponseAsString());
-
- r = c.doGet(url).acceptEncoding("identity");
- assertEquals("foo", r.getResponseAsString());
-
- // Should match identity.
- r = c.doGet(url).acceptEncoding("mycoding");
- assertEquals("foo", r.getResponseAsString());
-
- // Shouldn't match.
- try {
- r =
c.doGet(url+"?noTrace=true").acceptEncoding("mycoding,identity;q=0").connect();
- fail("Exception expected");
- } catch (RestCallException e) {
- checkErrorResponse(debug, e, SC_NOT_ACCEPTABLE,
- "Unsupported encoding in request header
'Accept-Encoding': 'mycoding,identity;q=0'",
- "Supported codings: ['identity']"
- );
- }
-
- // Shouldn't match.
- try {
-
c.doGet(url+"?noTrace=true").acceptEncoding("mycoding,*;q=0").connect();
- fail("Exception expected");
- } catch (RestCallException e) {
- checkErrorResponse(debug, e, SC_NOT_ACCEPTABLE,
- "Unsupported encoding in request header
'Accept-Encoding': 'mycoding,*;q=0'",
- "Supported codings: ['identity']"
- );
- }
-
- // Should match identity
- r =
c.doGet(url).acceptEncoding("identity;q=0.8,mycoding;q=0.6");
- assertEquals("foo", r.getResponseAsString());
-
- // Should match identity
- r =
c.doGet(url).acceptEncoding("mycoding;q=0.8,identity;q=0.6");
- assertEquals("foo", r.getResponseAsString());
-
- // Should match identity
- r = c.doGet(url).acceptEncoding("mycoding;q=0.8,*;q=0.6");
- assertEquals("foo", r.getResponseAsString());
-
- // Should match identity
- r = c.doGet(url).acceptEncoding("*;q=0.8,myencoding;q=0.6");
- assertEquals("foo", r.getResponseAsString());
-
- // Shouldn't match
- try {
-
c.doGet(url+"?noTrace=true").acceptEncoding("identity;q=0").connect();
- fail("Exception expected");
- } catch (RestCallException e) {
- checkErrorResponse(debug, e, SC_NOT_ACCEPTABLE,
- "Unsupported encoding in request header
'Accept-Encoding': 'identity;q=0'",
- "Supported codings: ['identity']"
- );
- }
-
- // Shouldn't match
- try {
-
c.doGet(url+"?noTrace=true").acceptEncoding("identity;q=0.0").connect();
- fail("Exception expected");
- } catch (RestCallException e) {
- checkErrorResponse(debug, e, SC_NOT_ACCEPTABLE,
- "Unsupported encoding in request header
'Accept-Encoding': 'identity;q=0.0'",
- "Supported codings: ['identity']"
- );
- }
-
- // Shouldn't match
- try {
-
c.doGet(url+"?noTrace=true").acceptEncoding("*;q=0").connect();
- fail("Exception expected");
- } catch (RestCallException e) {
- checkErrorResponse(debug, e, SC_NOT_ACCEPTABLE,
- "Unsupported encoding in request header
'Accept-Encoding': '*;q=0'",
- "Supported codings: ['identity']"
- );
- }
-
- // Shouldn't match
- try {
-
c.doGet(url+"?noTrace=true").acceptEncoding("*;q=0.0").connect();
- fail("Exception expected");
- } catch (RestCallException e) {
- checkErrorResponse(debug, e, SC_NOT_ACCEPTABLE,
- "Unsupported encoding in request header
'Accept-Encoding': '*;q=0.0'",
- "Supported codings: ['identity']"
- );
- }
-
-
- // *** PUT ***
-
- r = c.doPut(url, new StringReader("foo"));
- assertEquals("foo", r.getResponseAsString());
-
- r = c.doPut(url, new
StringReader("foo")).header("Content-Encoding", "");
- assertEquals("foo", r.getResponseAsString());
-
- r = c.doPut(url, new
StringReader("foo")).header("Content-Encoding", "identity");
- assertEquals("foo", r.getResponseAsString());
-
- try {
- c.doPut(url+"?noTrace=true",
compress("foo")).header("Content-Encoding", "mycoding").connect();
- fail("Exception expected");
- } catch (RestCallException e) {
- checkErrorResponse(debug, e, SC_UNSUPPORTED_MEDIA_TYPE,
- "Unsupported encoding in request header
'Content-Encoding': 'mycoding'",
- "Supported codings: ['identity']"
- );
- }
-
- c.closeQuietly();
- }
-
-
//====================================================================================================
- // Test with compression enabled.
-
//====================================================================================================
- @Test
- public void testGzipOn() throws Exception {
-
- // Create a client that disables content compression support so
that we can get the gzipped content directly.
- CloseableHttpClient httpClient =
HttpClients.custom().setSSLSocketFactory(TestMicroservice.getSSLSocketFactory()).disableContentCompression().build();
-
- RestClient c = TestMicroservice.client().httpClient(httpClient,
false).accept("text/plain").contentType("text/plain").build();
- RestCall r;
- String url = testGzipOn;
-
- // *** GET ***
-
- r = c.doGet(url);
- assertEquals("foo", r.getResponseAsString());
-
- r = c.doGet(url).acceptEncoding("");
- assertEquals("foo", r.getResponseAsString());
-
- r = c.doGet(url).acceptEncoding("*");
- assertEquals("foo", decompress(r.getInputStream()));
-
- r = c.doGet(url).acceptEncoding("identity");
- assertEquals("foo", r.getResponseAsString());
-
- // Should match identity.
- r = c.doGet(url).acceptEncoding("mycoding");
- assertEquals("foo", decompress(r.getInputStream()));
-
- r =
c.doGet(url).acceptEncoding("mycoding,identity;q=0").connect();
- assertEquals("foo", decompress(r.getInputStream()));
-
- r = c.doGet(url).acceptEncoding("mycoding,*;q=0").connect();
- assertEquals("foo", decompress(r.getInputStream()));
-
- // Should match identity
- r =
c.doGet(url).acceptEncoding("identity;q=0.8,mycoding;q=0.6");
- assertEquals("foo", r.getResponseAsString());
-
- // Should match mycoding
- r =
c.doGet(url).acceptEncoding("mycoding;q=0.8,identity;q=0.6");
- assertEquals("foo", decompress(r.getInputStream()));
-
- // Should match mycoding
- r = c.doGet(url).acceptEncoding("mycoding;q=0.8,*;q=0.6");
- assertEquals("foo", decompress(r.getInputStream()));
-
- // Shouldn't match
- try {
-
c.doGet(url+"?noTrace=true").acceptEncoding("identity;q=0").connect();
- fail("Exception expected");
- } catch (RestCallException e) {
- checkErrorResponse(debug, e, SC_NOT_ACCEPTABLE,
- "Unsupported encoding in request header
'Accept-Encoding': 'identity;q=0'",
- "Supported codings: ['mycoding','identity']"
- );
- }
-
- // Shouldn't match
- try {
-
c.doGet(url+"?noTrace=true").acceptEncoding("identity;q=0.0").connect();
- fail("Exception expected");
- } catch (RestCallException e) {
- checkErrorResponse(debug, e, SC_NOT_ACCEPTABLE,
- "Unsupported encoding in request header
'Accept-Encoding': 'identity;q=0.0'",
- "Supported codings: ['mycoding','identity']"
- );
- }
-
- // Shouldn't match
- try {
-
c.doGet(url+"?noTrace=true").acceptEncoding("*;q=0").connect();
- fail("Exception expected");
- } catch (RestCallException e) {
- checkErrorResponse(debug, e, SC_NOT_ACCEPTABLE,
- "Unsupported encoding in request header
'Accept-Encoding': '*;q=0'",
- "Supported codings: ['mycoding','identity']"
- );
- }
-
- // Shouldn't match
- try {
-
c.doGet(url+"?noTrace=true").acceptEncoding("*;q=0.0").connect();
- fail("Exception expected");
- } catch (RestCallException e) {
- checkErrorResponse(debug, e, SC_NOT_ACCEPTABLE,
- "Unsupported encoding in request header
'Accept-Encoding': '*;q=0.0'",
- "Supported codings: ['mycoding','identity']"
- );
- }
-
-
- // *** PUT ***
-
- r = c.doPut(url, new StringReader("foo"));
- assertEquals("foo", r.getResponseAsString());
-
- r = c.doPut(url, new
StringReader("foo")).header("Content-Encoding", "");
- assertEquals("foo", r.getResponseAsString());
-
- r = c.doPut(url, new
StringReader("foo")).header("Content-Encoding", "identity");
- assertEquals("foo", r.getResponseAsString());
-
- r = c.doPut(url, compress("foo")).header("Content-Encoding",
"mycoding");
- assertEquals("foo", r.getResponseAsString());
-
- c.closeQuietly(); // We want to close our client because we
created the HttpClient in this method.
- }
-
-
//====================================================================================================
- // Test with compression enabled but with servlet using output stream
directly.
-
//====================================================================================================
- @Test
- public void testGzipOnDirect() throws Exception {
- // Create a client that disables content compression support so
that we can get the gzipped content directly.
- CloseableHttpClient httpClient =
HttpClientBuilder.create().setSSLSocketFactory(TestMicroservice.getSSLSocketFactory()).build();
- RestClient c = TestMicroservice.client().httpClient(httpClient,
false).accept("text/plain").contentType("text/plain").build();
- RestCall r = null;
- String s = null;
-
- // res.getOutputStream() called....should bypass encoding.
- r = c.doGet(testGzipOn + "/direct").acceptEncoding("mycoding");
- s = r.getResponseAsString();
- assertEquals("test", s);
-
assertTrue(r.getResponse().getHeaders("Content-Type")[0].getValue().contains("text/direct"));
// Should get header set manually.
- assertEquals(0,
r.getResponse().getHeaders("Content-Encoding").length); //
Should not be set.
-
- // res.getWriter() called....should bypass encoding.
- r = c.doGet(testGzipOn + "/direct2").acceptEncoding("mycoding");
- s = r.getResponseAsString();
- assertEquals("test", s);
- assertEquals(0,
r.getResponse().getHeaders("Content-Encoding").length); //
Should not be set.
-
- // res.getNegotiateWriter() called....should NOT bypass
encoding.
- r = c.doGet(testGzipOn + "/direct3").acceptEncoding("mycoding");
- try {
- assertEquals("mycoding",
r.getResponse().getHeaders("content-encoding")[0].getValue());
- } catch (RestCallException e) {
- // OK - HttpClient doesn't know what mycoding is.
- // Newer versions of HttpClient ignore this condition.
- }
-
- // res.getNegotiateWriter() called but
@RestMethod(encoders={})...should bypass encoding.
- r = c.doGet(testGzipOn + "/direct4").acceptEncoding("mycoding");
- s = r.getResponseAsString();
- assertEquals("test", s);
- assertEquals(0,
r.getResponse().getHeaders("Content-Encoding").length); //
Should not be set.
-
- c.closeQuietly(); // We want to close our client because we
created the HttpClient in this method.
- }
-}
diff --git
a/juneau-microservice/juneau-microservice-test/src/test/java/org/apache/juneau/rest/test/_TestSuite.java
b/juneau-microservice/juneau-microservice-test/src/test/java/org/apache/juneau/rest/test/_TestSuite.java
index c395deb..a6cc00f 100644
---
a/juneau-microservice/juneau-microservice-test/src/test/java/org/apache/juneau/rest/test/_TestSuite.java
+++
b/juneau-microservice/juneau-microservice-test/src/test/java/org/apache/juneau/rest/test/_TestSuite.java
@@ -29,7 +29,6 @@ import org.junit.runners.Suite.*;
ClientFuturesTest.class,
ConfigTest.class,
FormDataTest.class,
- GzipTest.class,
HeadersTest.class,
HtmlDocTest.class,
HtmlDocLinksTest.class,
diff --git
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/mock/MockServletRequest.java
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/mock/MockServletRequest.java
index 5f0a1fb..65a8281 100644
---
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/mock/MockServletRequest.java
+++
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/mock/MockServletRequest.java
@@ -1037,6 +1037,16 @@ public class MockServletRequest implements
HttpServletRequest {
}
/**
+ * Specifies the <code>Accept-Encoding</code> header value on the
request.
+ *
+ * @param value The new value.
+ * @return This object (for method chaining).
+ */
+ public MockServletRequest acceptEncoding(String value) {
+ return header("Accept-Encoding", value);
+ }
+
+ /**
* Specifies the <code>X-Client-Version</code> header value on the
request.
*
* @param value The new value.
@@ -1047,6 +1057,16 @@ public class MockServletRequest implements
HttpServletRequest {
}
/**
+ * Specifies the <code>Content-Encoding</code> header value on the
request.
+ *
+ * @param value The new value.
+ * @return This object (for method chaining).
+ */
+ public MockServletRequest contentEncoding(String value) {
+ return header("Content-Encoding", value);
+ }
+
+ /**
* Adds a form data entry to this request.
*
* @param key
diff --git
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/mock/MockServletResponse.java
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/mock/MockServletResponse.java
index d85c7d1..dd8028a 100644
---
a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/mock/MockServletResponse.java
+++
b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/mock/MockServletResponse.java
@@ -284,6 +284,19 @@ public class MockServletResponse implements
HttpServletResponse {
}
/**
+ * Throws an {@link AssertionError} if the response body does not
contain the expected text.
+ *
+ * @param text The expected text of the body.
+ * @return This object (for method chaining).
+ * @throws AssertionError Thrown if the body does not contain the
expected text.
+ */
+ public MockServletResponse assertBody(String text) throws
AssertionError {
+ if (! StringUtils.isEquals(text, getBodyAsString()))
+ throw new AssertionError(MessageFormat.format("Response
did not have the expected text. expected=[{0}], actual=[{1}]", text,
getBodyAsString()));
+ return this;
+ }
+
+ /**
* Throws an {@link AssertionError} if the response body does not
contain all of the expected substrings.
*
* @param substrings The expected substrings.
@@ -299,28 +312,54 @@ public class MockServletResponse implements
HttpServletResponse {
}
/**
- * Throws an {@link AssertionError} if the response body does not
contain the expected text.
+ * Throws an {@link AssertionError} if the response does not contain
the expected character encoding.
*
- * @param text The expected text of the body.
+ * @param value The expected character encoding.
* @return This object (for method chaining).
- * @throws AssertionError Thrown if the body does not contain the
expected text.
+ * @throws AssertionError Thrown if the response does not contain the
expected character encoding.
*/
- public MockServletResponse assertBody(String text) throws
AssertionError {
- if (! StringUtils.isEquals(text, getBodyAsString()))
- throw new AssertionError(MessageFormat.format("Response
did not have the expected text. expected=[{0}], actual=[{1}]", text,
getBodyAsString()));
+ public MockServletResponse assertCharset(String value) {
+ if (! StringUtils.isEquals(value, getCharacterEncoding()))
+ throw new AssertionError(MessageFormat.format("Response
did not have the expected character encoding. expected=[{0}], actual=[{1}]",
value, getBodyAsString()));
return this;
}
/**
- * Throws an {@link AssertionError} if the response does not contain
the expected character encoding.
+ * Throws an {@link AssertionError} if the response does not contain
the expected header value.
*
- * @param value The expected character encoding.
+ * @param name The header name.
+ * @param value The expected header value.
* @return This object (for method chaining).
- * @throws AssertionError Thrown if the response does not contain the
expected character encoding.
+ * @throws AssertionError Thrown if the response does not contain the
expected header value.
*/
- public MockServletResponse assertCharset(String value) {
- if (! StringUtils.isEquals(value, getCharacterEncoding()))
- throw new AssertionError(MessageFormat.format("Response
did not have the expected character encoding. expected=[{0}], actual=[{1}]",
value, getBodyAsString()));
+ public MockServletResponse assertHeader(String name, String value) {
+ if (! StringUtils.isEquals(value, getHeader(name)))
+ throw new AssertionError(MessageFormat.format("Response
did not have the expected value for header {0}. expected=[{1}], actual=[{2}]",
name, value, getHeader(name)));
return this;
}
+
+ /**
+ * Throws an {@link AssertionError} if the response header does not
contain all of the expected substrings.
+ *
+ * @param name The header name.
+ * @param substrings The expected substrings.
+ * @return This object (for method chaining).
+ * @throws AssertionError Thrown if the header does not contain one or
more of the expected substrings.
+ */
+ public MockServletResponse assertHeaderContains(String name,
String...substrings) {
+ String text = getHeader(name);
+ for (String substring : substrings)
+ if (! contains(text, substring))
+ throw new
AssertionError(MessageFormat.format("Response did not have the expected
substring in header {0}. expected=[{1}], header=[{2}]", name, substring, text));
+ return this;
+ }
+
+ /**
+ * Returns the body of the request.
+ *
+ * @return The body of the request.
+ */
+ public byte[] getBody() {
+ return baos.toByteArray();
+ }
}
diff --git
a/juneau-rest/juneau-rest-server/src/test/java/org/apache/juneau/rest/headers/AcceptEncodingTest.java
b/juneau-rest/juneau-rest-server/src/test/java/org/apache/juneau/rest/headers/AcceptEncodingTest.java
new file mode 100644
index 0000000..c4e53af
--- /dev/null
+++
b/juneau-rest/juneau-rest-server/src/test/java/org/apache/juneau/rest/headers/AcceptEncodingTest.java
@@ -0,0 +1,264 @@
+//
***************************************************************************************************************************
+// * Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file *
+// * distributed with this work for additional information regarding copyright
ownership. The ASF licenses this file *
+// * to you under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance *
+// * with the License. You may obtain a copy of the License at
*
+// *
*
+// * http://www.apache.org/licenses/LICENSE-2.0
*
+// *
*
+// * Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an *
+// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
express or implied. See the License for the *
+// * specific language governing permissions and limitations under the
License. *
+//
***************************************************************************************************************************
+package org.apache.juneau.rest.headers;
+
+import static org.apache.juneau.http.HttpMethodName.*;
+import static org.apache.juneau.rest.testutils.TestUtils.*;
+import static org.junit.Assert.*;
+
+import java.io.*;
+
+import org.apache.juneau.encoders.*;
+import org.apache.juneau.rest.*;
+import org.apache.juneau.rest.annotation.*;
+import org.apache.juneau.rest.mock.*;
+import org.junit.*;
+import org.junit.runners.*;
+
+/**
+ * Test behavior involving Accept-Encoding header.
+ */
+@SuppressWarnings({"javadoc"})
+@FixMethodOrder(MethodSorters.NAME_ASCENDING)
+public class AcceptEncodingTest {
+
+
//=================================================================================================================
+ // Setup classes
+
//=================================================================================================================
+
+ public static class MyEncoder extends GzipEncoder {
+ @Override /* ConfigEncoder */
+ public String[] getCodings() {
+ return new String[]{"mycoding"};
+ }
+ }
+
+
//=================================================================================================================
+ // Test with no compression enabled.
+
//=================================================================================================================
+
+ @RestResource
+ public static class A {
+ @RestMethod(name=GET)
+ public String get() {
+ return "foo";
+ }
+ }
+ static MockRest a = MockRest.create(A.class);
+
+ @Test
+ public void a01_noCompression() throws Exception {
+ a.request("GET", "/").execute().assertBody("foo");
+ a.request("GET",
"/").acceptEncoding("").execute().assertBody("foo");
+ a.request("GET",
"/").acceptEncoding("*").execute().assertBody("foo");
+ a.request("GET",
"/").acceptEncoding("identity").execute().assertBody("foo");
+ }
+
+ @Test
+ public void a02_noCompression_qValues() throws Exception {
+ // The following should all match identity.
+ a.request("GET",
"/").acceptEncoding("mycoding").execute().assertBody("foo");
+ a.request("GET",
"/").acceptEncoding("identity;q=0.8,mycoding;q=0.6").execute().assertBody("foo");
+ a.request("GET",
"/").acceptEncoding("mycoding;q=0.8,identity;q=0.6").execute().assertBody("foo");
+ a.request("GET",
"/").acceptEncoding("mycoding;q=0.8,*;q=0.6").execute().assertBody("foo");
+ a.request("GET",
"/").acceptEncoding("*;q=0.8,myencoding;q=0.6").execute().assertBody("foo");
+ }
+
+ @Test
+ public void a03_noCompression_nomatch() throws Exception {
+ a.request("GET",
"?noTrace=true").acceptEncoding("mycoding,identity;q=0").execute()
+ .assertStatus(406)
+ .assertBodyContains(
+ "Unsupported encoding in request header
'Accept-Encoding': 'mycoding,identity;q=0'",
+ "Supported codings: ['identity']"
+ );
+ a.request("GET",
"?noTrace=true").acceptEncoding("mycoding,*;q=0").execute()
+ .assertStatus(406)
+ .assertBodyContains(
+ "Unsupported encoding in request header
'Accept-Encoding': 'mycoding,*;q=0'",
+ "Supported codings: ['identity']"
+ );
+ a.request("GET",
"?noTrace=true").acceptEncoding("identity;q=0").execute()
+ .assertStatus(406)
+ .assertBodyContains(
+ "Unsupported encoding in request header
'Accept-Encoding': 'identity;q=0'",
+ "Supported codings: ['identity']"
+ );
+ a.request("GET",
"?noTrace=true").acceptEncoding("identity;q=0.0").execute()
+ .assertStatus(406)
+ .assertBodyContains(
+ "Unsupported encoding in request header
'Accept-Encoding': 'identity;q=0.0'",
+ "Supported codings: ['identity']"
+ );
+ a.request("GET",
"?noTrace=true").acceptEncoding("*;q=0").execute()
+ .assertStatus(406)
+ .assertBodyContains(
+ "Unsupported encoding in request header
'Accept-Encoding': '*;q=0'",
+ "Supported codings: ['identity']"
+ );
+ a.request("GET",
"?noTrace=true").acceptEncoding("*;q=0.0").execute()
+ .assertStatus(406)
+ .assertBodyContains(
+ "Unsupported encoding in request header
'Accept-Encoding': '*;q=0.0'",
+ "Supported codings: ['identity']"
+ );
+ }
+
+
//=================================================================================================================
+ // Test with compression enabled.
+
//=================================================================================================================
+
+ @RestResource(encoders=MyEncoder.class)
+ public static class B {
+ @RestMethod(name=GET)
+ public String test1() {
+ return "foo";
+ }
+ }
+ static MockRest b = MockRest.create(B.class);
+
+ @Test
+ public void b01_withCompression_identity() throws Exception {
+ b.request("GET", "/").execute().assertBody("foo");
+ b.request("GET",
"/").acceptEncoding("").execute().assertBody("foo");
+ b.request("GET",
"/").acceptEncoding("identity").execute().assertBody("foo");
+ }
+ @Test
+ public void b02_withCompression_identity_qValues() throws Exception {
+ b.request("GET",
"/").acceptEncoding("identity;q=0.8,mycoding;q=0.6").execute().assertBody("foo");
+ }
+ @Test
+ public void b03_withCompression_gzip() throws Exception {
+ assertEquals("foo", decompress(b.request("GET",
"/").acceptEncoding("*").execute().getBody()));
+ assertEquals("foo", decompress(b.request("GET",
"/").acceptEncoding("mycoding").execute().getBody()));
+ }
+ @Test
+ public void b04_withCompression_gzip_qValues() throws Exception {
+ assertEquals("foo", decompress(b.request("GET",
"/").acceptEncoding("mycoding,identity;q=0").execute().getBody()));
+ assertEquals("foo", decompress(b.request("GET",
"/").acceptEncoding("mycoding,*;q=0").execute().getBody()));
+ assertEquals("foo", decompress(b.request("GET",
"/").acceptEncoding("mycoding;q=0.8,identity;q=0.6").execute().getBody()));
+ assertEquals("foo", decompress(b.request("GET",
"/").acceptEncoding("mycoding;q=0.8,*;q=0.6").execute().getBody()));
+ }
+ @Test
+ public void b05_withCompression_nomatch() throws Exception {
+ b.request("GET",
"?noTrace=true").acceptEncoding("identity;q=0").execute()
+ .assertStatus(406)
+ .assertBodyContains(
+ "Unsupported encoding in request header
'Accept-Encoding': 'identity;q=0'",
+ "Supported codings: ['mycoding','identity']"
+ );
+ b.request("GET",
"?noTrace=true").acceptEncoding("identity;q=0.0").execute()
+ .assertStatus(406)
+ .assertBodyContains(
+ "Unsupported encoding in request header
'Accept-Encoding': 'identity;q=0.0'",
+ "Supported codings: ['mycoding','identity']"
+ );
+ b.request("GET",
"?noTrace=true").acceptEncoding("*;q=0").execute()
+ .assertStatus(406)
+ .assertBodyContains(
+ "Unsupported encoding in request header
'Accept-Encoding': '*;q=0'",
+ "Supported codings: ['mycoding','identity']"
+ );
+ b.request("GET",
"?noTrace=true").acceptEncoding("*;q=0.0").execute()
+ .assertStatus(406)
+ .assertBodyContains(
+ "Unsupported encoding in request header
'Accept-Encoding': '*;q=0.0'",
+ "Supported codings: ['mycoding','identity']"
+ );
+ }
+
+
+
//=================================================================================================================
+ // Test with compression enabled but with servlet using output stream
directly.
+
//=================================================================================================================
+
+ @RestResource(encoders=MyEncoder.class)
+ @SuppressWarnings("resource")
+ public static class C {
+ @RestMethod(name=GET, path="/direct1")
+ public void c01(RestResponse res) throws Exception {
+ // This method bypasses the content type and encoding
from
+ // the serializers and encoders when calling
getOutputStream() directly.
+ res.setContentType("text/direct");
+ OutputStream os = res.getOutputStream();
+ os.write("foo".getBytes());
+ os.flush();
+ }
+ @RestMethod(name=GET, path="/direct2")
+ public void c02(RestResponse res) throws Exception {
+ // This method bypasses the content type and encoding
from
+ // the serializers and encoders when calling
getWriter() directly.
+ Writer w = res.getWriter();
+ w.append("foo");
+ w.flush();
+ }
+ @RestMethod(name=GET, path="/direct3")
+ public void c03(RestResponse res) throws Exception {
+ // This method uses getNegotiatedWriter() which should
use GZip encoding.
+ Writer w = res.getNegotiatedWriter();
+ w.append("foo");
+ w.flush();
+ w.close();
+ }
+ @RestMethod(name=GET, path="/direct4",
encoders={IdentityEncoder.class})
+ public void c04(RestResponse res) throws Exception {
+ // This method overrides the set of encoders at the
method level and so shouldn't use GZip encoding.
+ Writer w = res.getNegotiatedWriter();
+ w.append("foo");
+ w.flush();
+ }
+ }
+ static MockRest c = MockRest.create(C.class);
+
+ @Test
+ public void c01_direct1() throws Exception {
+ c.request("GET",
"/direct1").acceptEncoding("mycoding").execute()
+ .assertHeader("Content-Encoding", null) // Should not
be set
+ .assertHeader("Content-Type", "text/direct")
+ .assertBody("foo");
+ c.request("GET", "/direct1").acceptEncoding("*").execute()
+ .assertHeader("Content-Encoding", null) // Should not
be set
+ .assertHeader("Content-Type", "text/direct")
+ .assertBody("foo");
+ }
+ @Test
+ public void c02_direct2() throws Exception {
+ c.request("GET",
"/direct2").acceptEncoding("mycoding").execute()
+ .assertHeader("Content-Encoding", null) // Should not
be set
+ .assertBody("foo");
+ c.request("GET", "/direct2").acceptEncoding("*").execute()
+ .assertHeader("Content-Encoding", null) // Should not
be set
+ .assertBody("foo");
+ }
+ @Test
+ public void c03_direct3() throws Exception {
+ byte[] body;
+ body = c.request("GET",
"/direct3").acceptEncoding("mycoding").execute()
+ .assertHeader("Content-Encoding", null) // Should not
be set
+ .getBody();
+ assertEquals("foo", decompress(body));
+ body = c.request("GET",
"/direct3").acceptEncoding("*").execute()
+ .assertHeader("Content-Encoding", null) // Should not
be set
+ .getBody();
+ assertEquals("foo", decompress(body));
+ }
+ @Test
+ public void c04_direct4() throws Exception {
+ c.request("GET",
"/direct4").acceptEncoding("mycoding").execute()
+ .assertHeader("Content-Encoding", null) // Should not
be set
+ .assertBody("foo");
+ c.request("GET", "/direct4").acceptEncoding("*").execute()
+ .assertHeader("Content-Encoding", null) // Should not
be set
+ .assertBody("foo");
+ }
+}
diff --git
a/juneau-rest/juneau-rest-server/src/test/java/org/apache/juneau/rest/headers/ContentEncodingTest.java
b/juneau-rest/juneau-rest-server/src/test/java/org/apache/juneau/rest/headers/ContentEncodingTest.java
new file mode 100644
index 0000000..e82c24d
--- /dev/null
+++
b/juneau-rest/juneau-rest-server/src/test/java/org/apache/juneau/rest/headers/ContentEncodingTest.java
@@ -0,0 +1,94 @@
+//
***************************************************************************************************************************
+// * Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file *
+// * distributed with this work for additional information regarding copyright
ownership. The ASF licenses this file *
+// * to you under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance *
+// * with the License. You may obtain a copy of the License at
*
+// *
*
+// * http://www.apache.org/licenses/LICENSE-2.0
*
+// *
*
+// * Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an *
+// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
express or implied. See the License for the *
+// * specific language governing permissions and limitations under the
License. *
+//
***************************************************************************************************************************
+package org.apache.juneau.rest.headers;
+
+import static org.apache.juneau.http.HttpMethodName.*;
+import static org.apache.juneau.rest.testutils.TestUtils.*;
+
+import org.apache.juneau.encoders.*;
+import org.apache.juneau.rest.annotation.*;
+import org.apache.juneau.rest.mock.*;
+import org.junit.*;
+import org.junit.runners.*;
+
+/**
+ * Test behavior involving Accept-Encoding header.
+ */
+@SuppressWarnings({"javadoc"})
+@FixMethodOrder(MethodSorters.NAME_ASCENDING)
+public class ContentEncodingTest {
+
+
//=================================================================================================================
+ // Setup classes
+
//=================================================================================================================
+
+ public static class MyEncoder extends GzipEncoder {
+ @Override /* ConfigEncoder */
+ public String[] getCodings() {
+ return new String[]{"mycoding"};
+ }
+ }
+
+
//=================================================================================================================
+ // Test with no compression enabled.
+
//=================================================================================================================
+
+ @RestResource
+ public static class A {
+ @RestMethod(name=PUT)
+ public String put(@Body String in) {
+ return in;
+ }
+ }
+ static MockRest a = MockRest.create(A.class);
+
+ @Test
+ public void a01_noCompression() throws Exception {
+ a.request("PUT", "/").body("foo").execute().assertBody("foo");
+ a.request("PUT",
"/").body("foo").contentEncoding("").execute().assertBody("foo");
+ a.request("PUT",
"/").body("foo").contentEncoding("identity").execute().assertBody("foo");
+ }
+ @Test
+ public void a02_noCompression_invalid() throws Exception {
+ a.request("PUT",
"?noTrace=true").body(compress("foo")).contentEncoding("mycoding").execute()
+ .assertStatus(415)
+ .assertBodyContains(
+ "Unsupported encoding in request header
'Content-Encoding': 'mycoding'",
+ "Supported codings: ['identity']"
+ );
+ }
+
+
//=================================================================================================================
+ // Test with compression enabled.
+
//=================================================================================================================
+
+ @RestResource(encoders=MyEncoder.class)
+ public static class B {
+ @RestMethod(name=PUT, path="/")
+ public String test1put(@Body String in) {
+ return in;
+ }
+ }
+ static MockRest b = MockRest.create(B.class);
+
+ @Test
+ public void b01_withCompression_identity() throws Exception {
+ b.request("PUT", "/").body("foo").execute().assertBody("foo");
+ b.request("PUT",
"/").body("foo").contentEncoding("").execute().assertBody("foo");
+ b.request("PUT",
"/").body("foo").contentEncoding("identity").execute().assertBody("foo");
+ }
+ @Test
+ public void b02_withCompression_gzip() throws Exception {
+ b.request("PUT",
"/").body(compress("foo")).contentEncoding("mycoding").execute().assertBody("foo");
+ }
+}
diff --git
a/juneau-rest/juneau-rest-server/src/test/java/org/apache/juneau/rest/testutils/TestUtils.java
b/juneau-rest/juneau-rest-server/src/test/java/org/apache/juneau/rest/testutils/TestUtils.java
index 8eb87a3..be1c4c0 100755
---
a/juneau-rest/juneau-rest-server/src/test/java/org/apache/juneau/rest/testutils/TestUtils.java
+++
b/juneau-rest/juneau-rest-server/src/test/java/org/apache/juneau/rest/testutils/TestUtils.java
@@ -12,11 +12,42 @@
//
***************************************************************************************************************************
package org.apache.juneau.rest.testutils;
+import static org.apache.juneau.internal.IOUtils.*;
import static org.apache.juneau.internal.StringUtils.*;
+import java.io.*;
+import java.util.zip.*;
+
@SuppressWarnings({"javadoc"})
public class TestUtils extends org.apache.juneau.testutils.TestUtils {
+ /**
+ * Converts string into a GZipped input stream.
+ *
+ * @param contents The contents to compress.
+ * @return The input stream converted to GZip.
+ * @throws Exception
+ */
+ public static final byte[] compress(String contents) throws Exception {
+ ByteArrayOutputStream baos = new
ByteArrayOutputStream(contents.length()>>1);
+ try (GZIPOutputStream gos = new GZIPOutputStream(baos)) {
+ gos.write(contents.getBytes());
+ gos.finish();
+ }
+ return baos.toByteArray();
+ }
+
+ /**
+ * Converts a GZipped input stream into a string.
+ *
+ * @param is The contents to decompress.
+ * @return The string.
+ * @throws Exception
+ */
+ public static final String decompress(byte[] is) throws Exception {
+ return read(new GZIPInputStream(new ByteArrayInputStream(is)));
+ }
+
public static final void dumpResponse(String r, String msg,
Object...args) {
System.err.println("*** Failure
****************************************************************************************");
// NOT DEBUG
System.err.println(format(msg, args));
--
To stop receiving notification emails like this one, please contact
[email protected].