You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wink.apache.org by bl...@apache.org on 2009/08/07 05:10:46 UTC
svn commit: r801869 [5/21] - in /incubator/wink/trunk: wink-integration-test/
wink-integration-test/wink-server-integration-test-support/
wink-integration-test/wink-server-integration-test-support/src/main/java/org/apache/wink/test/integration/
wink-in...
Added: incubator/wink/trunk/wink-itests/wink-itest/wink-itest-context/src/test/java/org/apache/wink/itest/providers/ProvidersMethodsTest.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-itests/wink-itest/wink-itest-context/src/test/java/org/apache/wink/itest/providers/ProvidersMethodsTest.java?rev=801869&view=auto
==============================================================================
--- incubator/wink/trunk/wink-itests/wink-itest/wink-itest-context/src/test/java/org/apache/wink/itest/providers/ProvidersMethodsTest.java (added)
+++ incubator/wink/trunk/wink-itests/wink-itest/wink-itest-context/src/test/java/org/apache/wink/itest/providers/ProvidersMethodsTest.java Fri Aug 7 03:10:22 2009
@@ -0,0 +1,1134 @@
+/*
+ * 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.wink.itest.providers;
+
+import java.io.IOException;
+
+import javax.ws.rs.Produces;
+import javax.ws.rs.ext.ContextResolver;
+import javax.ws.rs.ext.ExceptionMapper;
+import javax.ws.rs.ext.Providers;
+
+import junit.framework.TestCase;
+
+import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.HttpException;
+import org.apache.commons.httpclient.methods.GetMethod;
+import org.apache.wink.test.integration.ServerEnvironmentInfo;
+
+public class ProvidersMethodsTest extends TestCase {
+
+ public String getBaseURI() {
+ return ServerEnvironmentInfo.getBaseURI() + "/providers";
+ }
+
+ /**
+ * Tests that
+ * {@link Providers#getContextResolver(Class, javax.ws.rs.core.MediaType)}
+ * will return null when a {@link ContextResolver} is not provided that
+ * matches the requested Context type.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testContextResolverNoMatch() throws HttpException, IOException {
+ HttpClient client = new HttpClient();
+
+ GetMethod getMethod =
+ new GetMethod(
+ getBaseURI() + "/context/providers/contextresolver?className=java.lang.Throwable&mediaType=*%2F*");
+ try {
+ client.executeMethod(getMethod);
+ assertEquals(200, getMethod.getStatusCode());
+ assertEquals("nullcontextresolver", getMethod.getResponseBodyAsString());
+ } finally {
+ getMethod.releaseConnection();
+ }
+ }
+
+ /**
+ * Tests that a
+ * {@link Providers#getContextResolver(Class, javax.ws.rs.core.MediaType)}
+ * will return a single context resolver when a single matching
+ * {@link ContextResolver} is provided by the application.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testContextResolverMatchSingle() throws HttpException, IOException {
+ HttpClient client = new HttpClient();
+
+ GetMethod getMethod =
+ new GetMethod(
+ getBaseURI() + "/context/providers/contextresolver?className=java.lang.Exception&mediaType=*%2F*");
+ try {
+ client.executeMethod(getMethod);
+ assertEquals(200, getMethod.getStatusCode());
+ assertEquals("org.apache.wink.itest.providers.MyExceptionContextResolver",
+ getMethod.getResponseBodyAsString());
+ } finally {
+ getMethod.releaseConnection();
+ }
+ }
+
+ /**
+ * Tests that a {@link ContextResolver} with a {@link Produces} annotation
+ * of text/xml will not be returned when given a non-compatible media type
+ * (my/type).
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testContextResolverNoMatchBySpecificMediaType() throws HttpException, IOException {
+ HttpClient client = new HttpClient();
+
+ GetMethod getMethod =
+ new GetMethod(
+ getBaseURI() + "/context/providers/contextresolver?className=java.lang.Exception&mediaType=my%2Ftype");
+ try {
+ client.executeMethod(getMethod);
+ assertEquals(200, getMethod.getStatusCode());
+ assertEquals("nullcontextresolver", getMethod.getResponseBodyAsString());
+ } finally {
+ getMethod.releaseConnection();
+ }
+ }
+
+ /**
+ * Tests that a {@link ContextResolver} with a {@link Produces} annotation
+ * of text/xml will be returned when given the specific text/xml type.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testContextResolverMatchBySpecificMediaType() throws HttpException, IOException {
+ HttpClient client = new HttpClient();
+
+ GetMethod getMethod =
+ new GetMethod(
+ getBaseURI() + "/context/providers/contextresolver?className=java.lang.Exception&mediaType=text%2Fxml");
+ try {
+ client.executeMethod(getMethod);
+ assertEquals(200, getMethod.getStatusCode());
+ assertEquals("org.apache.wink.itest.providers.MyExceptionContextResolver",
+ getMethod.getResponseBodyAsString());
+ } finally {
+ getMethod.releaseConnection();
+ }
+ }
+
+ /**
+ * Tests that a {@link ContextResolver} with a {@link Produces} annotation
+ * of text/xml will be returned when given the wildcard/xml type.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testContextResolverMatchBySpecificMediaTypeTypeWildcard() throws HttpException,
+ IOException {
+ HttpClient client = new HttpClient();
+
+ GetMethod getMethod =
+ new GetMethod(
+ getBaseURI() + "/context/providers/contextresolver?className=java.lang.Exception&mediaType=*%2Fxml");
+ try {
+ client.executeMethod(getMethod);
+ assertEquals(200, getMethod.getStatusCode());
+ assertEquals("org.apache.wink.itest.providers.MyExceptionContextResolver",
+ getMethod.getResponseBodyAsString());
+ } finally {
+ getMethod.releaseConnection();
+ }
+ }
+
+ /**
+ * Tests that a {@link ContextResolver} with a {@link Produces} annotation
+ * of text/xml will be returned when given the text/* type.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testContextResolverMatchBySpecificMediaTypeSubtypeWildcard() throws HttpException,
+ IOException {
+ HttpClient client = new HttpClient();
+
+ GetMethod getMethod =
+ new GetMethod(
+ getBaseURI() + "/context/providers/contextresolver?className=java.lang.Exception&mediaType=text%2F*");
+ try {
+ client.executeMethod(getMethod);
+ assertEquals(200, getMethod.getStatusCode());
+ assertEquals("org.apache.wink.itest.providers.MyExceptionContextResolver",
+ getMethod.getResponseBodyAsString());
+ } finally {
+ getMethod.releaseConnection();
+ }
+ }
+
+// /**
+// * Tests that when finding a {@link ContextResolver} both the application
+// * provided and runtime provided context resolvers are searched. Invokes
+// * with a specific JAXB class and verifies that the final context is a
+// * JAXBContext. In this case, the runtime provided context resolver is used.
+// *
+// * @throws HttpException
+// * @throws IOException
+// */
+// public void testMultipleContextResolverRuntimeAndApplicationMatchByMediaTypeWildcardInvokeWithClassInvokeRuntimeProvided()
+// throws HttpException, IOException {
+// HttpClient client = new HttpClient();
+//
+// GetMethod getMethod =
+// new GetMethod(
+// getBaseURI() + "/context/providers/contextresolver?className=javax.xml.bind.JAXBContext&mediaType=*%2F*&invokeWithClassName=org.apache.wink.itest.providers.otherxml.OtherRootElement");
+// try {
+// client.executeMethod(getMethod);
+// assertEquals(200, getMethod.getStatusCode());
+// assertTrue(getMethod.getResponseBodyAsString(), getMethod.getResponseBodyAsString()
+// .contains("JAXBContext"));
+// } finally {
+// getMethod.releaseConnection();
+// }
+// }
+
+ /**
+ * Tests that when finding a {@link ContextResolver} both the application
+ * provided and runtime provided context resolvers are searched. Invokes
+ * with a specific JAXB class and verifies that the final context is a
+ * JAXBContext. In this case, the application provided context resolver is
+ * used.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testMultipleContextResolverRuntimeAndApplicationMatchByMediaTypeWildcardInvokeWithClassInvokeApplicationProvided()
+ throws HttpException, IOException {
+ HttpClient client = new HttpClient();
+
+ GetMethod getMethod =
+ new GetMethod(
+ getBaseURI() + "/context/providers/contextresolver?className=javax.xml.bind.JAXBContext&mediaType=*%2F*&invokeWithClassName=org.apache.wink.itest.providers.xml.RootElement");
+ try {
+ client.executeMethod(getMethod);
+ assertEquals(200, getMethod.getStatusCode());
+ assertTrue(getMethod.getResponseBodyAsString(), getMethod.getResponseBodyAsString()
+ .contains("JAXBContext"));
+ } finally {
+ getMethod.releaseConnection();
+ }
+ }
+
+// /**
+// * Tests that when there are multiple {@link ContextResolver}s that could
+// * respond to a given type, that a proxy is returned that will call all of
+// * them.
+// *
+// * @throws HttpException
+// * @throws IOException
+// */
+// public void testMultipleContextResolverRuntimeAndApplicationMatchByMediaTypeSpecificTextXML()
+// throws HttpException, IOException {
+// HttpClient client = new HttpClient();
+//
+// GetMethod getMethod =
+// new GetMethod(
+// getBaseURI() + "/context/providers/contextresolver?className=javax.xml.bind.JAXBContext&mediaType=text%2Fxml");
+// try {
+// client.executeMethod(getMethod);
+// assertEquals(200, getMethod.getStatusCode());
+// assertTrue(getMethod.getResponseBodyAsString().startsWith("$Proxy"));
+// } finally {
+// getMethod.releaseConnection();
+// }
+// }
+
+// /**
+// * Tests that when the application provided {@link ContextResolver} which
+// * has a {@link Produces} annotation with text/xml is not the
+// * ContextResolver returned when searching for application/json media type.
+// *
+// * @throws HttpException
+// * @throws IOException
+// */
+// public void testMultipleContextResolverRuntimeAndApplicationMatchByMediaTypeSpecificApplicationJSON()
+// throws HttpException, IOException {
+// HttpClient client = new HttpClient();
+//
+// GetMethod getMethod =
+// new GetMethod(
+// getBaseURI() + "/context/providers/contextresolver?className=javax.xml.bind.JAXBContext&mediaType=application%2Fjson");
+// try {
+// client.executeMethod(getMethod);
+// assertEquals(200, getMethod.getStatusCode());
+// assertTrue(getMethod.getResponseBodyAsString().contains("JAXBContext"));
+// } finally {
+// getMethod.releaseConnection();
+// }
+// }
+
+// /**
+// * Tests that when the application provided {@link ContextResolver} which
+// * has a {@link Produces} annotation with text/xml is not called when an
+// * application/json is searched. This method should be able to invoke the
+// * runtime provided JAXBContext ContextResolver but return null.
+// *
+// * @throws HttpException
+// * @throws IOException
+// */
+// public void testMultipleContextResolverRuntimeAndApplicationMatchByMediaTypeSpecificApplicationJSONInvokeNegative()
+// throws HttpException, IOException {
+// HttpClient client = new HttpClient();
+//
+// GetMethod getMethod =
+// new GetMethod(
+// getBaseURI() + "/context/providers/contextresolver?className=javax.xml.bind.JAXBContext&mediaType=application%2Fjson&invokeWithClassName=org.apache.wink.itest.providers.xml.RootElement");
+// try {
+// client.executeMethod(getMethod);
+// assertEquals(200, getMethod.getStatusCode());
+// assertEquals("null", getMethod.getResponseBodyAsString());
+// } finally {
+// getMethod.releaseConnection();
+// }
+// }
+
+ /**
+ * Tests that when the application provided {@link ContextResolver} which
+ * has a {@link Produces} annotation with text/xml is called when an
+ * text/xml is searched. This method should be able to invoke the
+ * application provided JAXBContext ContextResolver and return a
+ * JAXBContext.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testMultipleContextResolverRuntimeAndApplicationMatchByMediaTypeSpecificTextXMLInvoke()
+ throws HttpException, IOException {
+ HttpClient client = new HttpClient();
+
+ GetMethod getMethod =
+ new GetMethod(
+ getBaseURI() + "/context/providers/contextresolver?className=javax.xml.bind.JAXBContext&mediaType=text%2Fxml&invokeWithClassName=org.apache.wink.itest.providers.xml.RootElement");
+ try {
+ client.executeMethod(getMethod);
+ assertEquals(200, getMethod.getStatusCode());
+ assertTrue(getMethod.getResponseBodyAsString(), getMethod.getResponseBodyAsString()
+ .contains("JAXBContext"));
+ } finally {
+ getMethod.releaseConnection();
+ }
+ }
+
+ /**
+ * Tests that a {@link ContextResolver} with a wildcard/xml {@link Produces}
+ * annotation will match a (specific)/xml type.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testContextResolverMatchMultipleSortByProducesWildcardType() throws HttpException,
+ IOException {
+ HttpClient client = new HttpClient();
+
+ GetMethod getMethod =
+ new GetMethod(
+ getBaseURI() + "/context/providers/contextresolver?className=java.lang.String&mediaType=abcd%2Fxml&invokeWithClassName=java.lang.Short&returnToStringValue=true");
+ try {
+ client.executeMethod(getMethod);
+ assertEquals(200, getMethod.getStatusCode());
+ assertEquals("allwildcardshort", getMethod.getResponseBodyAsString());
+ } finally {
+ getMethod.releaseConnection();
+ }
+ }
+
+ /**
+ * Tests that a {@link ContextResolver} with a text/wildcard
+ * {@link Produces} annotation will match a text/(specific) type.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testContextResolverMatchMultipleSortByProducesWildcardSubtype()
+ throws HttpException, IOException {
+ HttpClient client = new HttpClient();
+
+ GetMethod getMethod =
+ new GetMethod(
+ getBaseURI() + "/context/providers/contextresolver?className=java.lang.String&mediaType=text%2Fabcd&invokeWithClassName=java.lang.Short&returnToStringValue=true");
+ try {
+ client.executeMethod(getMethod);
+ assertEquals(200, getMethod.getStatusCode());
+ assertEquals("textwildcardonly", getMethod.getResponseBodyAsString());
+ } finally {
+ getMethod.releaseConnection();
+ }
+ }
+
+ /**
+ * Tests that a {@link ContextResolver} with a wildcard/wildcard
+ * {@link Produces} annotation will match a (specific)/(specific) type.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testContextResolverMatchMultipleSortByProducesAllWildcard() throws HttpException,
+ IOException {
+ HttpClient client = new HttpClient();
+
+ GetMethod getMethod =
+ new GetMethod(
+ getBaseURI() + "/context/providers/contextresolver?className=java.lang.String&mediaType=abcd%2Fdef&invokeWithClassName=java.lang.Short&returnToStringValue=true");
+ try {
+ client.executeMethod(getMethod);
+ assertEquals(200, getMethod.getStatusCode());
+ assertEquals("allwildcardshort", getMethod.getResponseBodyAsString());
+ } finally {
+ getMethod.releaseConnection();
+ }
+ }
+
+ /**
+ * Tests that a match with multiple {@link ContextResolver}s with the same
+ * media type and generic type will use a proxy that finds the single
+ * resolver that returns a non-null.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testContextResolverMatchMultipleSortByProducesFindOne() throws HttpException,
+ IOException {
+ HttpClient client = new HttpClient();
+
+ GetMethod getMethod =
+ new GetMethod(
+ getBaseURI() + "/context/providers/contextresolver?className=java.lang.String&mediaType=text%2Fxml&invokeWithClassName=java.lang.Integer&returnToStringValue=true");
+ try {
+ client.executeMethod(getMethod);
+ assertEquals(200, getMethod.getStatusCode());
+ assertEquals("integerxmlonly", getMethod.getResponseBodyAsString());
+ } finally {
+ getMethod.releaseConnection();
+ }
+
+ client = new HttpClient();
+
+ getMethod =
+ new GetMethod(
+ getBaseURI() + "/context/providers/contextresolver?className=java.lang.String&mediaType=text%2Fxml&invokeWithClassName=java.lang.Long&returnToStringValue=true");
+ try {
+ client.executeMethod(getMethod);
+ assertEquals(200, getMethod.getStatusCode());
+ assertEquals("longxml2only", getMethod.getResponseBodyAsString());
+ } finally {
+ getMethod.releaseConnection();
+ }
+ }
+
+ /**
+ * Tests that a match with multiple {@link ContextResolver}s will return the
+ * most specific responses before the wildcard responses.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testContextResolverMatchAnyMoreSpecificThanWildcards() throws HttpException,
+ IOException {
+ HttpClient client = new HttpClient();
+
+ GetMethod getMethod =
+ new GetMethod(
+ getBaseURI() + "/context/providers/contextresolver?className=java.lang.String&mediaType=text%2Fxml&invokeWithClassName=java.lang.Short&returnToStringValue=true");
+ try {
+ client.executeMethod(getMethod);
+ assertEquals(200, getMethod.getStatusCode());
+ String responseBody = getMethod.getResponseBodyAsString();
+ assertTrue(responseBody, "shortxmlandjson".equals(responseBody) || "shortxml2only"
+ .equals(responseBody)
+ || "shortxmlonly".equals(responseBody));
+ } finally {
+ getMethod.releaseConnection();
+ }
+ }
+
+ /**
+ * Tests that a {@link ContextResolver} with a wildcard/wildcard
+ * {@link Produces} annotation will match any media type.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testContextResolverNoProducesMatchNotExpectedMediaType() throws HttpException,
+ IOException {
+ HttpClient client = new HttpClient();
+
+ GetMethod getMethod =
+ new GetMethod(
+ getBaseURI() + "/context/providers/contextresolver?className=java.lang.String&mediaType=my%2Ftype");
+ try {
+ client.executeMethod(getMethod);
+ assertEquals(200, getMethod.getStatusCode());
+ assertEquals("org.apache.wink.itest.providers.MyStringContextForAllWildcard",
+ getMethod.getResponseBodyAsString());
+ } finally {
+ getMethod.releaseConnection();
+ }
+ }
+
+ /**
+ * Tests that a Provider can return an {@link ExceptionMapper}.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testGetExceptionMapperAppSuppliedProvider() throws HttpException, IOException {
+ HttpClient client = new HttpClient();
+
+ GetMethod getMethod =
+ new GetMethod(
+ getBaseURI() + "/context/providers/exception?className=org.apache.wink.itest.providers.MyException");
+ try {
+ client.executeMethod(getMethod);
+ assertEquals(200, getMethod.getStatusCode());
+ assertEquals("org.apache.wink.itest.providers.ExceptionMapperForMyException",
+ getMethod.getResponseBodyAsString());
+ } finally {
+ getMethod.releaseConnection();
+ }
+ }
+
+ /**
+ * Tests that a {@link Providers} will return the correct exception mapper
+ * given an exception that is a sub-class of the {@link ExceptionMapper}
+ * class.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testGetExceptionMapperAppSuppliedInheritanceTree() throws HttpException,
+ IOException {
+ HttpClient client = new HttpClient();
+
+ GetMethod getMethod =
+ new GetMethod(
+ getBaseURI() + "/context/providers/exception?className=org.apache.wink.itest.providers.MyException2");
+ try {
+ client.executeMethod(getMethod);
+ assertEquals(200, getMethod.getStatusCode());
+ assertEquals("org.apache.wink.itest.providers.ExceptionMapperForMyException",
+ getMethod.getResponseBodyAsString());
+ } finally {
+ getMethod.releaseConnection();
+ }
+ }
+
+ /**
+ * Tests that a {@link Providers} will return a null value when given a
+ * class that does not have an {@link ExceptionMapper}.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testGetExceptionMapperNoMatching() throws HttpException, IOException {
+ HttpClient client = new HttpClient();
+
+ GetMethod getMethod =
+ new GetMethod(
+ getBaseURI() + "/context/providers/exception?className=java.lang.Throwable");
+ try {
+ client.executeMethod(getMethod);
+ assertEquals(200, getMethod.getStatusCode());
+ assertEquals("null", getMethod.getResponseBodyAsString());
+ } finally {
+ getMethod.releaseConnection();
+ }
+ }
+
+ /**
+ * Tests that
+ * {@link Providers#getMessageBodyWriter(Class, java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType)}
+ * uses the media type to filter out potential message body writers.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testGetMessageBodyWriterSortByProducesMediaType() throws HttpException, IOException {
+ HttpClient client = new HttpClient();
+
+ GetMethod getMethod =
+ new GetMethod(
+ getBaseURI() + "/context/providers/messagebodywriter?className=java.lang.Integer&mediaType=text%2Fxml");
+ try {
+ client.executeMethod(getMethod);
+ assertEquals(200, getMethod.getStatusCode());
+ String response = getMethod.getResponseBodyAsString();
+ assertEquals("org.apache.wink.itest.providers.MyMessageBodyWriterXMLAndJSONForNumber",
+ response);
+ } finally {
+ getMethod.releaseConnection();
+ }
+
+ getMethod =
+ new GetMethod(
+ getBaseURI() + "/context/providers/messagebodywriter?className=java.lang.Integer&mediaType=application%2Fjson");
+ try {
+ client.executeMethod(getMethod);
+ assertEquals(200, getMethod.getStatusCode());
+ String response = getMethod.getResponseBodyAsString();
+ assertTrue(response,
+ response
+ .contains("org.apache.wink.itest.providers.MyMessageBodyWriterXMLAndJSONForNumber") || response
+ .contains("org.apache.wink.itest.providers.MyMessageBodyWriterJSONForInteger"));
+ } finally {
+ getMethod.releaseConnection();
+ }
+ }
+
+ /**
+ * Tests that
+ * {@link Providers#getMessageBodyWriter(Class, java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType)}
+ * uses the media type to filter out potential message body writers and will
+ * respect more specific types over wildcards.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testGetMessageBodyWriterSortByProducesMediaTypeWithWildcards()
+ throws HttpException, IOException {
+ HttpClient client = new HttpClient();
+
+ GetMethod getMethod =
+ new GetMethod(
+ getBaseURI() + "/context/providers/messagebodywriter?className=java.lang.Short&mediaType=application%2Fjson");
+ try {
+ client.executeMethod(getMethod);
+ assertEquals(200, getMethod.getStatusCode());
+ String response = getMethod.getResponseBodyAsString();
+ assertEquals("org.apache.wink.itest.providers.MyMessageBodyWriterJSONForShort",
+ response);
+ } finally {
+ getMethod.releaseConnection();
+ }
+
+ getMethod =
+ new GetMethod(
+ getBaseURI() + "/context/providers/messagebodywriter?className=java.lang.Short&mediaType=application%2F*");
+ try {
+ client.executeMethod(getMethod);
+ assertEquals(200, getMethod.getStatusCode());
+ String response = getMethod.getResponseBodyAsString();
+ assertTrue(response,
+ response
+ .contains("org.apache.wink.itest.providers.MyMessageBodyWriterJSONForShort"));
+ } finally {
+ getMethod.releaseConnection();
+ }
+
+ getMethod =
+ new GetMethod(
+ getBaseURI() + "/context/providers/messagebodywriter?className=java.lang.Short&mediaType=application%2Fmytype");
+ try {
+ client.executeMethod(getMethod);
+ assertEquals(200, getMethod.getStatusCode());
+ String response = getMethod.getResponseBodyAsString();
+ assertEquals("org.apache.wink.itest.providers.MyMessageBodyWriterApplicationWildcardForShort",
+ response);
+ } finally {
+ getMethod.releaseConnection();
+ }
+
+ getMethod =
+ new GetMethod(
+ getBaseURI() + "/context/providers/messagebodywriter?className=java.lang.Short&mediaType=mytype%2Fmysubtype");
+ try {
+ client.executeMethod(getMethod);
+ assertEquals(200, getMethod.getStatusCode());
+ String response = getMethod.getResponseBodyAsString();
+
+ assertEquals("org.apache.wink.itest.providers.MyMessageBodyWriterWildcardForShort",
+ response);
+ } finally {
+ getMethod.releaseConnection();
+ }
+ }
+
+ /**
+ * Tests that
+ * {@link Providers#getMessageBodyWriter(Class, java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType)}
+ * will find MessageBodyWriters that have inherited the MessageBodyWriter
+ * interface.
+ *
+ * @throws IOException
+ * @throws HttpException
+ */
+ public void testGetMessageBodyWriterWhichInheritsWriterInterface() throws HttpException,
+ IOException {
+ HttpClient client = new HttpClient();
+
+ GetMethod getMethod =
+ new GetMethod(
+ getBaseURI() + "/context/providers/messagebodywriter?className=java.util.List&mediaType=abcd%2Fefgh");
+ try {
+ client.executeMethod(getMethod);
+ assertEquals(200, getMethod.getStatusCode());
+ String response = getMethod.getResponseBodyAsString();
+ assertEquals("org.apache.wink.itest.providers.MyMessageBodyWriterInherited",
+ response);
+ } finally {
+ getMethod.releaseConnection();
+ }
+ }
+
+ /**
+ * Tests that
+ * {@link Providers#getMessageBodyWriter(Class, java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType)}
+ * will filter out writers that do not match the isWritable method.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testGetMessageBodyWriterSortByIsWritable() throws HttpException, IOException {
+ HttpClient client = new HttpClient();
+
+ GetMethod getMethod =
+ new GetMethod(
+ getBaseURI() + "/context/providers/messagebodywriter?className=java.lang.Integer&mediaType=application%2Fjson");
+ try {
+ client.executeMethod(getMethod);
+ assertEquals(200, getMethod.getStatusCode());
+ String response = getMethod.getResponseBodyAsString();
+ assertTrue(response,
+ "org.apache.wink.itest.providers.MyMessageBodyWriterJSONForInteger"
+ .equals(response) || "org.apache.wink.itest.providers.MyMessageBodyWriterXMLAndJSONForNumber"
+ .equals(response));
+ } finally {
+ getMethod.releaseConnection();
+ }
+
+ getMethod =
+ new GetMethod(
+ getBaseURI() + "/context/providers/messagebodywriter?className=java.lang.Long&mediaType=application%2Fjson");
+ try {
+ client.executeMethod(getMethod);
+ assertEquals(200, getMethod.getStatusCode());
+ String response = getMethod.getResponseBodyAsString();
+ assertTrue(response,
+ "org.apache.wink.itest.providers.MyMessageBodyWriterJSONForLong"
+ .equals(response) || "org.apache.wink.itest.providers.MyMessageBodyWriterXMLAndJSONForNumber"
+ .equals(response));
+ } finally {
+ getMethod.releaseConnection();
+ }
+
+ getMethod =
+ new GetMethod(
+ getBaseURI() + "/context/providers/messagebodywriter?className=java.lang.Short&mediaType=application%2Fjson");
+ try {
+ client.executeMethod(getMethod);
+ assertEquals(200, getMethod.getStatusCode());
+ String response = getMethod.getResponseBodyAsString();
+ assertEquals(response,
+ "org.apache.wink.itest.providers.MyMessageBodyWriterJSONForShort");
+ } finally {
+ getMethod.releaseConnection();
+ }
+
+ getMethod =
+ new GetMethod(
+ getBaseURI() + "/context/providers/messagebodywriter?className=java.lang.Long&mediaType=text%2Fxml");
+ try {
+ client.executeMethod(getMethod);
+ assertEquals(200, getMethod.getStatusCode());
+ String response = getMethod.getResponseBodyAsString();
+ assertEquals("org.apache.wink.itest.providers.MyMessageBodyWriterXMLAndJSONForNumber",
+ response);
+ } finally {
+ getMethod.releaseConnection();
+ }
+
+ getMethod =
+ new GetMethod(
+ getBaseURI() + "/context/providers/messagebodywriter?className=java.lang.Integer&mediaType=text%2Fxml");
+ try {
+ client.executeMethod(getMethod);
+ assertEquals(200, getMethod.getStatusCode());
+ String response = getMethod.getResponseBodyAsString();
+ assertEquals("org.apache.wink.itest.providers.MyMessageBodyWriterXMLAndJSONForNumber",
+ response);
+ } finally {
+ getMethod.releaseConnection();
+ }
+ }
+
+ /**
+ * Tests that
+ * {@link Providers#getMessageBodyWriter(Class, java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType)}
+ * will use the application provided MessageBodyWriters before runtime
+ * provided.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testGetMessageBodyWriterUserPrecedenceOverRuntime() throws HttpException,
+ IOException {
+ HttpClient client = new HttpClient();
+
+ GetMethod getMethod =
+ new GetMethod(
+ getBaseURI() + "/context/providers/messagebodywriter?className=java.lang.String&mediaType=text%2Fplain");
+ try {
+ client.executeMethod(getMethod);
+ assertEquals(200, getMethod.getStatusCode());
+ String response = getMethod.getResponseBodyAsString();
+ assertEquals("org.apache.wink.itest.providers.MyStringWriterForStrings",
+ response);
+ } finally {
+ getMethod.releaseConnection();
+ }
+
+ }
+
+ /**
+ * Tests that a null is returned when calling
+ * {@link Providers#getMessageBodyWriter(Class, java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType)}
+ * if there are no suitable MessageBodyWriters.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testGetMessageBodyWriterNoMatching() throws HttpException, IOException {
+ HttpClient client = new HttpClient();
+
+ GetMethod getMethod =
+ new GetMethod(
+ getBaseURI() + "/context/providers/messagebodywriter?className=java.lang.Float&mediaType=application%2Fjson");
+ try {
+ client.executeMethod(getMethod);
+ assertEquals(200, getMethod.getStatusCode());
+ String response = getMethod.getResponseBodyAsString();
+ assertEquals("nullwriter", response);
+ } finally {
+ getMethod.releaseConnection();
+ }
+
+ getMethod =
+ new GetMethod(
+ getBaseURI() + "/context/providers/messagebodywriter?className=java.lang.Exception&mediaType=*%2F*");
+ try {
+ client.executeMethod(getMethod);
+ assertEquals(200, getMethod.getStatusCode());
+ String response = getMethod.getResponseBodyAsString();
+ assertEquals("org.apache.wink.common.internal.providers.entity.FormatedExceptionProvider",
+ response);
+ } finally {
+ getMethod.releaseConnection();
+ }
+ }
+
+ /**
+ * Tests that a null is returned when calling
+ * {@link Providers#getMessageBodyReader(Class, java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType)}
+ * if there are no suitable MessageBodyReader.
+ *
+ * @throws IOException
+ * @throws HttpException
+ */
+ public void testGetMessageBodyReaderNoMatching() throws HttpException, IOException {
+ HttpClient client = new HttpClient();
+
+ GetMethod getMethod =
+ new GetMethod(
+ getBaseURI() + "/context/providers/messagebodyreader?className=java.lang.Float&mediaType=mynonexistenttype%2Fmysubtype");
+ try {
+ client.executeMethod(getMethod);
+ assertEquals(200, getMethod.getStatusCode());
+ String response = getMethod.getResponseBodyAsString();
+ assertEquals("nullreader", response);
+ } finally {
+ getMethod.releaseConnection();
+ }
+
+ getMethod =
+ new GetMethod(
+ getBaseURI() + "/context/providers/messagebodyreader?className=java.lang.Exception&mediaType=*%2F*");
+ try {
+ client.executeMethod(getMethod);
+ assertEquals(200, getMethod.getStatusCode());
+ String response = getMethod.getResponseBodyAsString();
+ assertEquals("nullreader", response);
+ } finally {
+ getMethod.releaseConnection();
+ }
+ }
+
+ /**
+ * Tests that
+ * {@link Providers#getMessageBodyReader(Class, java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType)}
+ * will filter out writers that do not match the isWritable method.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testGetMessageBodyReaderSortByIsReadable() throws HttpException, IOException {
+ HttpClient client = new HttpClient();
+
+ GetMethod getMethod =
+ new GetMethod(
+ getBaseURI() + "/context/providers/messagebodyreader?className=java.lang.Integer&mediaType=application%2Fjson");
+ try {
+ client.executeMethod(getMethod);
+ assertEquals(200, getMethod.getStatusCode());
+ String response = getMethod.getResponseBodyAsString();
+ assertTrue(response,
+ "org.apache.wink.itest.providers.readers.MyMessageBodyReaderJSONForInteger"
+ .equals(response) || "org.apache.wink.itest.providers.readers.MyMessageBodyReaderXMLAndJSONForNumber"
+ .equals(response));
+ } finally {
+ getMethod.releaseConnection();
+ }
+
+ getMethod =
+ new GetMethod(
+ getBaseURI() + "/context/providers/messagebodyreader?className=java.lang.Long&mediaType=application%2Fjson");
+ try {
+ client.executeMethod(getMethod);
+ assertEquals(200, getMethod.getStatusCode());
+ String response = getMethod.getResponseBodyAsString();
+ assertTrue(response,
+ "org.apache.wink.itest.providers.readers.MyMessageBodyReaderJSONForLong"
+ .equals(response) || "org.apache.wink.itest.providers.readers.MyMessageBodyReaderXMLAndJSONForNumber"
+ .equals(response));
+ } finally {
+ getMethod.releaseConnection();
+ }
+
+ getMethod =
+ new GetMethod(
+ getBaseURI() + "/context/providers/messagebodyreader?className=java.lang.Short&mediaType=application%2Fjson");
+ try {
+ client.executeMethod(getMethod);
+ assertEquals(200, getMethod.getStatusCode());
+ String response = getMethod.getResponseBodyAsString();
+ assertEquals("org.apache.wink.itest.providers.readers.MyMessageBodyReaderJSONForShort",
+ response);
+ } finally {
+ getMethod.releaseConnection();
+ }
+
+ getMethod =
+ new GetMethod(
+ getBaseURI() + "/context/providers/messagebodyreader?className=java.lang.Long&mediaType=text%2Fxml");
+ try {
+ client.executeMethod(getMethod);
+ assertEquals(200, getMethod.getStatusCode());
+ String response = getMethod.getResponseBodyAsString();
+ assertEquals("org.apache.wink.itest.providers.readers.MyMessageBodyReaderXMLAndJSONForNumber",
+ response);
+ } finally {
+ getMethod.releaseConnection();
+ }
+
+ getMethod =
+ new GetMethod(
+ getBaseURI() + "/context/providers/messagebodyreader?className=java.lang.Integer&mediaType=text%2Fxml");
+ try {
+ client.executeMethod(getMethod);
+ assertEquals(200, getMethod.getStatusCode());
+ String response = getMethod.getResponseBodyAsString();
+ assertEquals("org.apache.wink.itest.providers.readers.MyMessageBodyReaderXMLAndJSONForNumber",
+ response);
+ } finally {
+ getMethod.releaseConnection();
+ }
+ }
+
+ /**
+ * Tests that
+ * {@link Providers#getMessageBodyReader(Class, java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType)}
+ * uses the media type to filter out potential message body readers.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testGetMessageBodyReaderSortByConsumesMediaType() throws HttpException, IOException {
+ HttpClient client = new HttpClient();
+
+ GetMethod getMethod =
+ new GetMethod(
+ getBaseURI() + "/context/providers/messagebodyreader?className=java.lang.Integer&mediaType=text%2Fxml");
+ try {
+ client.executeMethod(getMethod);
+ assertEquals(200, getMethod.getStatusCode());
+ String response = getMethod.getResponseBodyAsString();
+ assertEquals("org.apache.wink.itest.providers.readers.MyMessageBodyReaderXMLAndJSONForNumber",
+ response);
+ } finally {
+ getMethod.releaseConnection();
+ }
+
+ getMethod =
+ new GetMethod(
+ getBaseURI() + "/context/providers/messagebodyreader?className=java.lang.Integer&mediaType=application%2Fjson");
+ try {
+ client.executeMethod(getMethod);
+ assertEquals(200, getMethod.getStatusCode());
+ String response = getMethod.getResponseBodyAsString();
+ assertTrue(response,
+ response
+ .contains("org.apache.wink.itest.providers.readers.MyMessageBodyReaderXMLAndJSONForNumber") || response
+ .contains("org.apache.wink.itest.providers.readers.MyMessageBodyReaderJSONForInteger"));
+ } finally {
+ getMethod.releaseConnection();
+ }
+ }
+
+ /**
+ * Tests that
+ * {@link Providers#getMessageBodyReader(Class, java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType)}
+ * uses the media type to filter out potential message body readers and will
+ * respect more specific types over wildcards.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testGetMessageBodyReaderSortByConsumesMediaTypeWithWildcards()
+ throws HttpException, IOException {
+ HttpClient client = new HttpClient();
+
+ GetMethod getMethod =
+ new GetMethod(
+ getBaseURI() + "/context/providers/messagebodyreader?className=java.lang.Short&mediaType=application%2Fjson");
+ try {
+ client.executeMethod(getMethod);
+ assertEquals(200, getMethod.getStatusCode());
+ String response = getMethod.getResponseBodyAsString();
+ assertEquals("org.apache.wink.itest.providers.readers.MyMessageBodyReaderJSONForShort",
+ response);
+ } finally {
+ getMethod.releaseConnection();
+ }
+
+ getMethod =
+ new GetMethod(
+ getBaseURI() + "/context/providers/messagebodyreader?className=java.lang.Short&mediaType=application%2F*");
+ try {
+ client.executeMethod(getMethod);
+ assertEquals(200, getMethod.getStatusCode());
+ String response = getMethod.getResponseBodyAsString();
+ assertEquals("org.apache.wink.itest.providers.readers.MyMessageBodyReaderJSONForShort",
+ response);
+ } finally {
+ getMethod.releaseConnection();
+ }
+
+ getMethod =
+ new GetMethod(
+ getBaseURI() + "/context/providers/messagebodyreader?className=java.lang.Short&mediaType=application%2Fmytype");
+ try {
+ client.executeMethod(getMethod);
+ assertEquals(200, getMethod.getStatusCode());
+ String response = getMethod.getResponseBodyAsString();
+ assertEquals("org.apache.wink.itest.providers.readers.MyMessageBodyReaderApplicationWildcardForShort",
+ response);
+ } finally {
+ getMethod.releaseConnection();
+ }
+
+ getMethod =
+ new GetMethod(
+ getBaseURI() + "/context/providers/messagebodyreader?className=java.lang.Short&mediaType=mytype%2Fmysubtype");
+ try {
+ client.executeMethod(getMethod);
+ assertEquals(200, getMethod.getStatusCode());
+ String response = getMethod.getResponseBodyAsString();
+
+ assertEquals("org.apache.wink.itest.providers.readers.MyMessageBodyReaderWildcardForShort",
+ response);
+ } finally {
+ getMethod.releaseConnection();
+ }
+ }
+
+ /**
+ * Tests that
+ * {@link Providers#getMessageBodyReader(Class, java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType)}
+ * will use the application provided MessageBodyReaders before runtime
+ * provided.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testGetMessageBodyReaderUserPrecedenceOverRuntime() throws HttpException,
+ IOException {
+ HttpClient client = new HttpClient();
+
+ GetMethod getMethod =
+ new GetMethod(
+ getBaseURI() + "/context/providers/messagebodyreader?className=java.lang.String&mediaType=text%2Fplain");
+ try {
+ client.executeMethod(getMethod);
+ assertEquals(200, getMethod.getStatusCode());
+ String response = getMethod.getResponseBodyAsString();
+ assertEquals("org.apache.wink.itest.providers.readers.MyMessageBodyReaderForStrings",
+ response);
+ } finally {
+ getMethod.releaseConnection();
+ }
+
+ }
+
+ /**
+ * Tests that
+ * {@link Providers#getMessageBodyReader(Class, java.lang.reflect.Type, java.lang.annotation.Annotation[], javax.ws.rs.core.MediaType)}
+ * will find MessageBodyReaders that have inherited the MessageBodyReader
+ * interface.
+ *
+ * @throws IOException
+ * @throws HttpException
+ */
+ public void testGetMessageBodyReaderWhichInheritsReaderInterface() throws HttpException,
+ IOException {
+ HttpClient client = new HttpClient();
+
+ GetMethod getMethod =
+ new GetMethod(
+ getBaseURI() + "/context/providers/messagebodyreader?className=java.util.Set&mediaType=tuv%2Fwxyz");
+ try {
+ client.executeMethod(getMethod);
+ assertEquals(200, getMethod.getStatusCode());
+ String response = getMethod.getResponseBodyAsString();
+ assertEquals("org.apache.wink.itest.providers.readers.MyMessageBodyReaderInherited",
+ response);
+ } finally {
+ getMethod.releaseConnection();
+ }
+
+ getMethod =
+ new GetMethod(
+ getBaseURI() + "/context/providers/messagebodyreader?className=java.util.List&mediaType=tuv%2Fwxyz");
+ try {
+ client.executeMethod(getMethod);
+ assertEquals(200, getMethod.getStatusCode());
+ String response = getMethod.getResponseBodyAsString();
+ assertEquals("nullreader", response);
+ } finally {
+ getMethod.releaseConnection();
+ }
+ }
+}
Added: incubator/wink/trunk/wink-itests/wink-itest/wink-itest-context/src/test/java/org/apache/wink/itest/request/RequestMethodsTest.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-itests/wink-itest/wink-itest-context/src/test/java/org/apache/wink/itest/request/RequestMethodsTest.java?rev=801869&view=auto
==============================================================================
--- incubator/wink/trunk/wink-itests/wink-itest/wink-itest-context/src/test/java/org/apache/wink/itest/request/RequestMethodsTest.java (added)
+++ incubator/wink/trunk/wink-itests/wink-itest/wink-itest-context/src/test/java/org/apache/wink/itest/request/RequestMethodsTest.java Fri Aug 7 03:10:22 2009
@@ -0,0 +1,816 @@
+/*
+ * 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.wink.itest.request;
+
+import java.io.IOException;
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.Locale;
+import java.util.TimeZone;
+
+import javax.ws.rs.core.Request;
+
+import junit.framework.TestCase;
+
+import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.HttpException;
+import org.apache.commons.httpclient.methods.GetMethod;
+import org.apache.commons.httpclient.methods.PostMethod;
+import org.apache.commons.httpclient.methods.PutMethod;
+import org.apache.commons.httpclient.methods.StringRequestEntity;
+import org.apache.wink.test.integration.ServerEnvironmentInfo;
+
+/**
+ * Tests the {@link Request} implementation.
+ */
+public class RequestMethodsTest extends TestCase {
+
+ final private static SimpleDateFormat rfc1123Format =
+ new SimpleDateFormat(
+ "EEE, dd MMM yyyy HH:mm:ss zzz",
+ Locale.ENGLISH);
+
+ final private static SimpleDateFormat rfc850Format =
+ new SimpleDateFormat(
+ "EEEE, dd-MMM-yy HH:mm:ss zzz",
+ Locale.ENGLISH);
+
+ final private static SimpleDateFormat asctimeDateFormat =
+ new SimpleDateFormat(
+ "EEE MMM dd HH:mm:ss yyyy",
+ Locale.ENGLISH);
+
+ final private static SimpleDateFormat asctimeDateFormatWithOneDigit =
+ new SimpleDateFormat(
+ "EEE MMM d HH:mm:ss yyyy",
+ Locale.ENGLISH);
+
+ {
+ /*
+ * the implementation allows you to set a different time zone on the
+ * requests for If-Modified-Since headers and it will do the
+ * "right thing" (this is more leniant). However, asctime does not have
+ * a time zone specified so it is assumed that all datetimes are in
+ * GMT/UTC so the tests have to assume that the GMT time zone is used.
+ */
+ asctimeDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
+ asctimeDateFormatWithOneDigit.setTimeZone(TimeZone.getTimeZone("GMT"));
+ }
+
+ public String getBaseURI() {
+ return ServerEnvironmentInfo.getBaseURI() + "/request";
+ }
+
+ /**
+ * Tests the {@link Request#evaluatePreconditions(Date)} that uses the
+ * <code>If-Modified-Since</code> header and the RFC 1123 date format.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testEvaluateDateIfModifiedSinceUsingRFC1123Format() throws HttpException,
+ IOException {
+ checkIfModifiedSinceUsingSuppliedDateFormat(rfc1123Format);
+ }
+
+ /**
+ * Tests the {@link Request#evaluatePreconditions(Date)} that uses the
+ * <code>If-Modified-Since</code> header and the RFC 850 date format.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testEvaluateDateIfModifiedSinceUsingRFC850Format() throws HttpException,
+ IOException {
+ checkIfModifiedSinceUsingSuppliedDateFormat(rfc850Format);
+ }
+
+ /**
+ * Tests the {@link Request#evaluatePreconditions(Date)} that uses the
+ * <code>If-Modified-Since</code> header and the Asctime date format.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testEvaluateDateIfModifiedSinceUsingAscTimeFormat() throws HttpException,
+ IOException {
+ SimpleDateFormat formatter =
+ (Calendar.getInstance().get(Calendar.DAY_OF_MONTH) < 10)
+ ? asctimeDateFormatWithOneDigit : asctimeDateFormat;
+ checkIfModifiedSinceUsingSuppliedDateFormat(formatter);
+ }
+
+ private void checkIfModifiedSinceUsingSuppliedDateFormat(SimpleDateFormat formatter)
+ throws IOException, HttpException {
+ HttpClient client = new HttpClient();
+
+ PutMethod putMethod = new PutMethod(getBaseURI() + "/context/request/date");
+ Date d2 = new Date(System.currentTimeMillis() - 120000);
+ Date d = new Date(System.currentTimeMillis() - 60000);
+ String date = DateFormat.getDateTimeInstance().format(d);
+ putMethod.setRequestEntity(new StringRequestEntity(date, "text/string", "UTF-8"));
+ try {
+ /*
+ * sets a last modified date
+ */
+ client.executeMethod(putMethod);
+ assertEquals(204, putMethod.getStatusCode());
+ } finally {
+ putMethod.releaseConnection();
+ }
+
+ GetMethod getMethod = new GetMethod(getBaseURI() + "/context/request/date");
+ getMethod.setRequestHeader("If-Modified-Since", formatter.format(d));
+ try {
+ /*
+ * verifies that if the exact date is sent in and used in
+ * If-Modified-Since header, then the server will be ok and that it
+ * will return 304
+ */
+ client.executeMethod(getMethod);
+ assertEquals(304, getMethod.getStatusCode());
+ } finally {
+ getMethod.releaseConnection();
+ }
+
+ getMethod = new GetMethod(getBaseURI() + "/context/request/date");
+ try {
+ /*
+ * verifies that if no If-Modified-Since header is sent, then the
+ * server will be ok and the Request instance won't build a
+ * response.
+ */
+ client.executeMethod(getMethod);
+ assertEquals(200, getMethod.getStatusCode());
+ assertEquals("the date: " + rfc1123Format.format(d), getMethod
+ .getResponseBodyAsString());
+
+ rfc1123Format.setTimeZone(TimeZone.getTimeZone("GMT"));
+ assertEquals(rfc1123Format.format(d), getMethod.getResponseHeader("Last-Modified")
+ .getValue());
+ rfc1123Format.setTimeZone(TimeZone.getDefault());
+ } finally {
+ getMethod.releaseConnection();
+ }
+
+ String lastModified = getMethod.getResponseHeader("Last-Modified").getValue();
+ getMethod = new GetMethod(getBaseURI() + "/context/request/date");
+ getMethod.setRequestHeader("If-Modified-Since", lastModified);
+ try {
+ /*
+ * verifies that using Last-Modified response header sent by server
+ * as If-Modified-Since request header, then the server will return
+ * a 304
+ */
+ client.executeMethod(getMethod);
+ assertEquals(304, getMethod.getStatusCode());
+ } finally {
+ getMethod.releaseConnection();
+ }
+
+ getMethod = new GetMethod(getBaseURI() + "/context/request/date");
+ getMethod.setRequestHeader("If-Modified-Since", formatter.format(d2));
+ try {
+ /*
+ * verifies that using a If-Modified-Since earlier than the
+ * Last-Modified response header sent by server then the server will
+ * return a 200 with entity
+ */
+ client.executeMethod(getMethod);
+ assertEquals(200, getMethod.getStatusCode());
+ assertEquals("the date: " + rfc1123Format.format(d), getMethod
+ .getResponseBodyAsString());
+
+ rfc1123Format.setTimeZone(TimeZone.getTimeZone("GMT"));
+ assertEquals(rfc1123Format.format(d), getMethod.getResponseHeader("Last-Modified")
+ .getValue());
+ rfc1123Format.setTimeZone(TimeZone.getDefault());
+ } finally {
+ getMethod.releaseConnection();
+ }
+
+ getMethod = new GetMethod(getBaseURI() + "/context/request/date");
+ getMethod.setRequestHeader("If-Modified-Since", formatter.format(new Date()));
+ try {
+ /*
+ * verifies that using a If-Modified-Since later than the
+ * Last-Modified response header sent by server, then the server
+ * will return a 304
+ */
+ client.executeMethod(getMethod);
+ assertEquals(304, getMethod.getStatusCode());
+ } finally {
+ getMethod.releaseConnection();
+ }
+ }
+
+ /**
+ * Tests the {@link Request#evaluatePreconditions(Date)} that uses the
+ * <code>If-Unmodified-Since</code> header using RFC 1123.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testEvaluateDateIfUnmodifiedSinceUsingRFC1123() throws HttpException, IOException {
+ checkIfUnmodifiedSinceUsingSuppliedDateFormat(rfc1123Format);
+ }
+
+ /**
+ * Tests the {@link Request#evaluatePreconditions(Date)} that uses the
+ * <code>If-Unmodified-Since</code> header using RFC 850.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testEvaluateDateIfUnmodifiedSinceUsingRFC850() throws HttpException, IOException {
+ checkIfUnmodifiedSinceUsingSuppliedDateFormat(rfc850Format);
+ }
+
+ /**
+ * Tests the {@link Request#evaluatePreconditions(Date)} that uses the
+ * <code>If-Unmodified-Since</code> header using Asctime.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testEvaluateDateIfUnmodifiedSinceUsingAscTime() throws HttpException, IOException {
+ SimpleDateFormat dateFormat =
+ (Calendar.getInstance().get(Calendar.DAY_OF_MONTH) < 10)
+ ? asctimeDateFormatWithOneDigit : asctimeDateFormat;
+ checkIfUnmodifiedSinceUsingSuppliedDateFormat(dateFormat);
+ }
+
+ private void checkIfUnmodifiedSinceUsingSuppliedDateFormat(SimpleDateFormat formatter)
+ throws IOException, HttpException {
+ HttpClient client = new HttpClient();
+
+ PutMethod putMethod = new PutMethod(getBaseURI() + "/context/request/date");
+ Date d2 = new Date(System.currentTimeMillis() - 120000);
+ Date d = new Date(System.currentTimeMillis() - 60000);
+ String date = DateFormat.getDateTimeInstance().format(d);
+ putMethod.setRequestEntity(new StringRequestEntity(date, "text/string", "UTF-8"));
+ try {
+ /*
+ * sets a last modified date
+ */
+ client.executeMethod(putMethod);
+ assertEquals(204, putMethod.getStatusCode());
+ } finally {
+ putMethod.releaseConnection();
+ }
+
+ GetMethod getMethod = new GetMethod(getBaseURI() + "/context/request/date");
+ getMethod.setRequestHeader("If-Unmodified-Since", formatter.format(d));
+ try {
+ /*
+ * verifies that if the exact date is sent in and used in
+ * If-Unmodified-Since header, then the server will be ok and that
+ * it will return 200
+ */
+ client.executeMethod(getMethod);
+ assertEquals(200, getMethod.getStatusCode());
+ assertEquals("the date: " + rfc1123Format.format(d), getMethod
+ .getResponseBodyAsString());
+
+ rfc1123Format.setTimeZone(TimeZone.getTimeZone("GMT"));
+ assertEquals(rfc1123Format.format(d), getMethod.getResponseHeader("Last-Modified")
+ .getValue());
+ rfc1123Format.setTimeZone(TimeZone.getDefault());
+ } finally {
+ getMethod.releaseConnection();
+ }
+
+ getMethod = new GetMethod(getBaseURI() + "/context/request/date");
+ try {
+ /*
+ * verifies that if no If-Unmodified-Since header is sent, then the
+ * server will be ok and the Request instance won't build a
+ * response.
+ */
+ client.executeMethod(getMethod);
+ assertEquals(200, getMethod.getStatusCode());
+ assertEquals("the date: " + rfc1123Format.format(d), getMethod
+ .getResponseBodyAsString());
+
+ rfc1123Format.setTimeZone(TimeZone.getTimeZone("GMT"));
+ assertEquals(rfc1123Format.format(d), getMethod.getResponseHeader("Last-Modified")
+ .getValue());
+ rfc1123Format.setTimeZone(TimeZone.getDefault());
+ } finally {
+ getMethod.releaseConnection();
+ }
+
+ String lastModified = getMethod.getResponseHeader("Last-Modified").getValue();
+
+ getMethod = new GetMethod(getBaseURI() + "/context/request/date");
+ getMethod.setRequestHeader("If-Unmodified-Since", lastModified);
+ try {
+ /*
+ * verifies that using Last-Modified response header sent by server
+ * as If-Unmodified-Since request header, then the server will
+ * return the entity
+ */
+ client.executeMethod(getMethod);
+ assertEquals(200, getMethod.getStatusCode());
+ assertEquals("the date: " + rfc1123Format.format(d), getMethod
+ .getResponseBodyAsString());
+
+ rfc1123Format.setTimeZone(TimeZone.getTimeZone("GMT"));
+ assertEquals(rfc1123Format.format(d), getMethod.getResponseHeader("Last-Modified")
+ .getValue());
+ rfc1123Format.setTimeZone(TimeZone.getDefault());
+ } finally {
+ getMethod.releaseConnection();
+ }
+
+ getMethod = new GetMethod(getBaseURI() + "/context/request/date");
+ getMethod.setRequestHeader("If-Unmodified-Since", formatter.format(d2));
+ try {
+ /*
+ * verifies that using a If-Unmodified-Since earlier than the
+ * Last-Modified response header sent by server then the server will
+ * return a 412
+ */
+ client.executeMethod(getMethod);
+ assertEquals(412, getMethod.getStatusCode());
+ } finally {
+ getMethod.releaseConnection();
+ }
+
+ getMethod = new GetMethod(getBaseURI() + "/context/request/date");
+ getMethod.setRequestHeader("If-Unmodified-Since", formatter.format(new Date()));
+ try {
+ /*
+ * verifies that using a If-Unmodified-Since later than the
+ * Last-Modified response header sent by server, then the server
+ * will return 200 and the entity
+ */
+ client.executeMethod(getMethod);
+ assertEquals(200, getMethod.getStatusCode());
+ assertEquals("the date: " + rfc1123Format.format(d), getMethod
+ .getResponseBodyAsString());
+
+ rfc1123Format.setTimeZone(TimeZone.getTimeZone("GMT"));
+ assertEquals(rfc1123Format.format(d), getMethod.getResponseHeader("Last-Modified")
+ .getValue());
+ rfc1123Format.setTimeZone(TimeZone.getDefault());
+ } finally {
+ getMethod.releaseConnection();
+ }
+ }
+
+ /**
+ * Tests the
+ * {@link Request#evaluatePreconditions(javax.ws.rs.core.EntityTag)} that
+ * uses the <code>If-Match</code> header and a strong ETag.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testEvaluateEtagIfMatchStrong() throws HttpException, IOException {
+ try {
+ checkETagIfMatch("\"myentitytagABCXYZ\"", false);
+ } catch (Exception e) {
+ e.printStackTrace();
+ fail(e.getMessage());
+ }
+ }
+
+ /**
+ * Tests the
+ * {@link Request#evaluatePreconditions(javax.ws.rs.core.EntityTag)} that
+ * uses the <code>If-Match</code> header and a weak ETag.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testEvaluateEtagIfMatchWeak() throws HttpException, IOException {
+ try {
+ checkETagIfMatch("\"myentitytagABCXYZ\"", true);
+ } catch (Exception e) {
+ e.printStackTrace();
+ fail(e.getMessage());
+ }
+ }
+
+ /**
+ * Tests the
+ * {@link Request#evaluatePreconditions(javax.ws.rs.core.EntityTag)} that
+ * uses the <code>If-Match</code> header.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void checkETagIfMatch(String etag, boolean isEntityTagWeak) throws HttpException,
+ IOException {
+ HttpClient client = new HttpClient();
+
+ final String justTheTag = etag;
+ final String setETag = isEntityTagWeak ? "W/" + justTheTag : justTheTag;
+ String isWeak = isEntityTagWeak ? "true" : "false";
+
+ PutMethod putMethod = new PutMethod(getBaseURI() + "/context/request/etag");
+ putMethod.setRequestEntity(new StringRequestEntity(setETag, "text/string", "UTF-8"));
+ try {
+ /*
+ * sets an entity tag
+ */
+ client.executeMethod(putMethod);
+ assertEquals(204, putMethod.getStatusCode());
+ } finally {
+ putMethod.releaseConnection();
+ }
+
+ GetMethod getMethod = new GetMethod(getBaseURI() + "/context/request/etag");
+ getMethod.setRequestHeader("If-Match", setETag);
+ try {
+ /*
+ * verifies that if the exact etag is sent in, then the request is
+ * allowed to proceed
+ */
+ client.executeMethod(getMethod);
+ assertEquals(200, getMethod.getStatusCode());
+ assertEquals("the etag: " + justTheTag + isWeak, getMethod.getResponseBodyAsString());
+ } finally {
+ getMethod.releaseConnection();
+ }
+
+ getMethod = new GetMethod(getBaseURI() + "/context/request/etag");
+ try {
+ /*
+ * verifies that a request without an If-Match header will still
+ * proceed
+ */
+ client.executeMethod(getMethod);
+ assertEquals(200, getMethod.getStatusCode());
+ assertEquals("the etag: " + justTheTag + isWeak, getMethod.getResponseBodyAsString());
+ } finally {
+ getMethod.releaseConnection();
+ }
+
+ getMethod = new GetMethod(getBaseURI() + "/context/request/etag");
+ getMethod.setRequestHeader("If-Match", setETag.substring(1, setETag.length() - 1));
+ try {
+ /*
+ * verifies that an unquoted entity tag is not a valid entity tag
+ */
+ client.executeMethod(getMethod);
+ assertEquals(400, getMethod.getStatusCode());
+ } finally {
+ getMethod.releaseConnection();
+ }
+
+ getMethod = new GetMethod(getBaseURI() + "/context/request/etag");
+ getMethod.setRequestHeader("If-Match", setETag.substring(0, setETag.length() - 1));
+ try {
+ /*
+ * verifies that a misquoted entity tag is not a valid entity tag
+ */
+ client.executeMethod(getMethod);
+ assertEquals(400, getMethod.getStatusCode());
+ } finally {
+ getMethod.releaseConnection();
+ }
+
+ getMethod = new GetMethod(getBaseURI() + "/context/request/etag");
+ getMethod.setRequestHeader("If-Match", setETag.substring(1, setETag.length()));
+ try {
+ /*
+ * verifies that a misquoted entity tag is not a valid entity tag
+ */
+ client.executeMethod(getMethod);
+ assertEquals(400, getMethod.getStatusCode());
+ } finally {
+ getMethod.releaseConnection();
+ }
+
+ getMethod = new GetMethod(getBaseURI() + "/context/request/etag");
+ getMethod.setRequestHeader("If-Match", "\"someothervalue\"");
+ try {
+ /*
+ * verifies that if an etag is sent that does not match the server
+ * etag, that a 412 is returned
+ */
+ client.executeMethod(getMethod);
+ assertEquals(412, getMethod.getStatusCode());
+ } finally {
+ getMethod.releaseConnection();
+ }
+
+ getMethod = new GetMethod(getBaseURI() + "/context/request/etag");
+ getMethod.setRequestHeader("If-Match", "\"austin\", \"powers\"");
+ try {
+ /*
+ * verifies that if multiple etags are sent that do not match the
+ * server etag, that a 412 is returned
+ */
+ client.executeMethod(getMethod);
+ assertEquals(412, getMethod.getStatusCode());
+ } finally {
+ getMethod.releaseConnection();
+ }
+
+ getMethod = new GetMethod(getBaseURI() + "/context/request/etag");
+ getMethod.addRequestHeader("If-Match", "\"austin\", \"powers\"");
+ try {
+ /*
+ * verifies that if multiple etags are sent that do not match the
+ * server etag, that a 412 is returned
+ */
+ client.executeMethod(getMethod);
+ assertEquals(412, getMethod.getStatusCode());
+ } finally {
+ getMethod.releaseConnection();
+ }
+
+ getMethod = new GetMethod(getBaseURI() + "/context/request/etag");
+ getMethod.addRequestHeader("If-Match", "\"austin\", " + setETag + " , \"powers\"");
+ try {
+ /*
+ * verifies that if multiple etags are sent that do match the server
+ * etag, that a 200 and entity body is returned
+ */
+ client.executeMethod(getMethod);
+ assertEquals(200, getMethod.getStatusCode());
+ assertEquals("the etag: " + justTheTag + isWeak, getMethod.getResponseBodyAsString());
+ } finally {
+ getMethod.releaseConnection();
+ }
+ }
+
+ /**
+ * Tests the
+ * {@link Request#evaluatePreconditions(javax.ws.rs.core.EntityTag)} that
+ * uses the <code>If-None-Match</code> header with strong entity tag.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testEvaluateEtagIfNoneMatchStrong() throws HttpException, IOException {
+ checkETagIfNoneMatch("\"myentitytagABCXYZ\"", false);
+ }
+
+ /**
+ * Tests the
+ * {@link Request#evaluatePreconditions(javax.ws.rs.core.EntityTag)} that
+ * uses the <code>If-None-Match</code> header with weak entity tag.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void testEvaluateEtagIfNoneMatchWeak() throws HttpException, IOException {
+ checkETagIfNoneMatch("\"myentitytagABCXYZ\"", true);
+ }
+
+ /**
+ * Tests the
+ * {@link Request#evaluatePreconditions(javax.ws.rs.core.EntityTag)} that
+ * uses the <code>If-None-Match</code> header.
+ *
+ * @throws HttpException
+ * @throws IOException
+ */
+ public void checkETagIfNoneMatch(String etag, boolean isEntityTagWeak) throws HttpException,
+ IOException {
+ HttpClient client = new HttpClient();
+ final String justTheTag = etag;
+ final String setETag = isEntityTagWeak ? "W/" + justTheTag : justTheTag;
+ String isWeak = isEntityTagWeak ? "true" : "false";
+
+ PutMethod putMethod = new PutMethod(getBaseURI() + "/context/request/etag");
+ putMethod.setRequestEntity(new StringRequestEntity(setETag, "text/string", "UTF-8"));
+ try {
+ /*
+ * sets an entity tag
+ */
+ client.executeMethod(putMethod);
+ assertEquals(204, putMethod.getStatusCode());
+ } finally {
+ putMethod.releaseConnection();
+ }
+
+ GetMethod getMethod = new GetMethod(getBaseURI() + "/context/request/etag");
+ getMethod.setRequestHeader("If-None-Match", setETag);
+ try {
+ /*
+ * verifies that if the exact etag is sent in, then the response is
+ * a 304
+ */
+ client.executeMethod(getMethod);
+ assertEquals(304, getMethod.getStatusCode());
+ assertEquals(setETag, getMethod.getResponseHeader("ETag").getValue());
+ } finally {
+ getMethod.releaseConnection();
+ }
+
+ getMethod = new GetMethod(getBaseURI() + "/context/request/etag");
+ getMethod.setRequestHeader("If-None-Match", "\"*\"");
+ try {
+ /*
+ * verifies that if a "*" etag is sent in, then the response returns
+ * a 304
+ */
+ client.executeMethod(getMethod);
+ assertEquals(304, getMethod.getStatusCode());
+ assertEquals(setETag, getMethod.getResponseHeader("ETag").getValue());
+ } finally {
+ getMethod.releaseConnection();
+ }
+
+ PostMethod postMethod = new PostMethod(getBaseURI() + "/context/request/etag");
+ postMethod.setRequestHeader("If-None-Match", setETag);
+ try {
+ /*
+ * verifies that if a matching etag is sent in, then the response
+ * returns a 412
+ */
+ client.executeMethod(postMethod);
+ assertEquals(412, postMethod.getStatusCode());
+ assertEquals(setETag, postMethod.getResponseHeader("ETag").getValue());
+ } finally {
+ postMethod.releaseConnection();
+ }
+
+ postMethod = new PostMethod(getBaseURI() + "/context/request/etag");
+ postMethod.setRequestHeader("If-None-Match", "\"*\"");
+ try {
+ /*
+ * verifies that if a "*" etag is sent in, then the response returns
+ * a 412
+ */
+ client.executeMethod(postMethod);
+ assertEquals(412, postMethod.getStatusCode());
+ assertEquals(setETag, postMethod.getResponseHeader("ETag").getValue());
+ } finally {
+ postMethod.releaseConnection();
+ }
+
+ getMethod = new GetMethod(getBaseURI() + "/context/request/etag");
+ try {
+ /*
+ * verifies that a request without an If-None-Match header will
+ * still proceed
+ */
+ client.executeMethod(getMethod);
+ assertEquals(200, getMethod.getStatusCode());
+ assertEquals("the etag: " + justTheTag + isWeak, getMethod.getResponseBodyAsString());
+ } finally {
+ getMethod.releaseConnection();
+ }
+
+ postMethod = new PostMethod(getBaseURI() + "/context/request/etag");
+ try {
+ /*
+ * verifies that a request without an If-None-Match header will
+ * still proceed
+ */
+ client.executeMethod(postMethod);
+ assertEquals(200, postMethod.getStatusCode());
+ assertEquals("the etag: " + justTheTag + isWeak, postMethod.getResponseBodyAsString());
+ } finally {
+ postMethod.releaseConnection();
+ }
+
+ getMethod = new GetMethod(getBaseURI() + "/context/request/etag");
+ getMethod.setRequestHeader("If-None-Match", setETag.substring(1, setETag.length() - 1));
+ try {
+ /*
+ * verifies that an unquoted entity tag is invalid
+ */
+ client.executeMethod(getMethod);
+ assertEquals(400, getMethod.getStatusCode());
+ } finally {
+ getMethod.releaseConnection();
+ }
+
+ getMethod = new GetMethod(getBaseURI() + "/context/request/etag");
+ getMethod.setRequestHeader("If-None-Match", setETag.substring(0, setETag.length() - 1));
+ try {
+ /*
+ * verifies that a misquoted entity tag is invalid
+ */
+ client.executeMethod(getMethod);
+ assertEquals(400, getMethod.getStatusCode());
+ } finally {
+ getMethod.releaseConnection();
+ }
+
+ getMethod = new GetMethod(getBaseURI() + "/context/request/etag");
+ getMethod.setRequestHeader("If-None-Match", setETag.substring(1, setETag.length()));
+ try {
+ /*
+ * verifies that a misquoted entity tag is invalid
+ */
+ client.executeMethod(getMethod);
+ assertEquals(400, getMethod.getStatusCode());
+ } finally {
+ getMethod.releaseConnection();
+ }
+
+ getMethod = new GetMethod(getBaseURI() + "/context/request/etag");
+ getMethod.setRequestHeader("If-None-Match", "\"someothervalue\"");
+ try {
+ /*
+ * verifies that if an etag is sent that does not match the server
+ * etag, that request is allowed to proceed
+ */
+ client.executeMethod(getMethod);
+ assertEquals(200, getMethod.getStatusCode());
+ assertEquals("the etag: " + justTheTag + isWeak, getMethod.getResponseBodyAsString());
+ } finally {
+ getMethod.releaseConnection();
+ }
+
+ getMethod = new GetMethod(getBaseURI() + "/context/request/etag");
+ getMethod.setRequestHeader("If-None-Match", "\"austin\", \"powers\"");
+ try {
+ /*
+ * verifies that if multiple etags are sent that do not match the
+ * server etag, that the request is allowed to proceed
+ */
+ client.executeMethod(getMethod);
+ assertEquals(200, getMethod.getStatusCode());
+ assertEquals("the etag: " + justTheTag + isWeak, getMethod.getResponseBodyAsString());
+ } finally {
+ getMethod.releaseConnection();
+ }
+
+ getMethod = new GetMethod(getBaseURI() + "/context/request/etag");
+ getMethod.addRequestHeader("If-None-Match", "\"austin\", \"powers\"");
+ try {
+ /*
+ * verifies that if multiple etags are sent that do not match the
+ * server etag, then a 200 and the request entity is returned
+ */
+ client.executeMethod(getMethod);
+ assertEquals(200, getMethod.getStatusCode());
+ assertEquals("the etag: " + justTheTag + isWeak, getMethod.getResponseBodyAsString());
+ } finally {
+ getMethod.releaseConnection();
+ }
+
+ postMethod = new PostMethod(getBaseURI() + "/context/request/etag");
+ postMethod.addRequestHeader("If-None-Match", "\"austin\", \"powers\"");
+ try {
+ /*
+ * verifies that a request without an If-None-Match header will
+ * still proceed
+ */
+ client.executeMethod(postMethod);
+ assertEquals(200, postMethod.getStatusCode());
+ assertEquals("the etag: " + justTheTag + isWeak, postMethod.getResponseBodyAsString());
+ } finally {
+ postMethod.releaseConnection();
+ }
+
+ getMethod = new GetMethod(getBaseURI() + "/context/request/etag");
+ getMethod.addRequestHeader("If-None-Match", "\"austin\", " + setETag + " , \"powers\"");
+ try {
+ /*
+ * verifies that if multiple etags are sent that do match the server
+ * etag, that a 304 is returned
+ */
+ client.executeMethod(getMethod);
+ assertEquals(304, getMethod.getStatusCode());
+ assertEquals(setETag, getMethod.getResponseHeader("ETag").getValue());
+ } finally {
+ getMethod.releaseConnection();
+ }
+
+ postMethod = new PostMethod(getBaseURI() + "/context/request/etag");
+ postMethod.addRequestHeader("If-None-Match", "\"austin\", " + setETag + " , \"powers\"");
+ try {
+ /*
+ * verifies that a request with an If-None-Match header will fail
+ */
+ client.executeMethod(postMethod);
+ assertEquals(412, postMethod.getStatusCode());
+ assertEquals(setETag, getMethod.getResponseHeader("ETag").getValue());
+ } finally {
+ postMethod.releaseConnection();
+ }
+ }
+
+ // TODO: add selectVariant tests by querying the various
+ // /context/request/variant/* paths
+
+}