From 3ddce358ae13cc69c1b612bce2f666129b3b941d Mon Sep 17 00:00:00 2001 From: Oleg Kalnichevski Date: Wed, 14 Nov 2007 16:59:15 +0000 Subject: [PATCH] HTTPCLIENT-688: HttpOptions#getAllowedMethods can now handle multiple Allow headers Contributed by Andrea Selva Reviewed by Oleg Kalnichevski git-svn-id: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpclient/trunk@594946 13f79535-47bb-0310-9956-ffa450edef68 --- RELEASE_NOTES.txt | 8 ++ .../http/client/methods/HttpOptions.java | 24 +++--- .../java/org/apache/http/client/TestAll.java | 2 + .../http/client/methods/TestAllMethods.java | 54 +++++++++++++ .../http/client/methods/TestHttpOptions.java | 77 +++++++++++++++++++ 5 files changed, 152 insertions(+), 13 deletions(-) create mode 100644 module-client/src/test/java/org/apache/http/client/methods/TestAllMethods.java create mode 100644 module-client/src/test/java/org/apache/http/client/methods/TestHttpOptions.java diff --git a/RELEASE_NOTES.txt b/RELEASE_NOTES.txt index 038b011d7..94235e326 100644 --- a/RELEASE_NOTES.txt +++ b/RELEASE_NOTES.txt @@ -1,3 +1,11 @@ +Changes since 4.0 Alpha 2 +------------------- + +* [HTTPCLIENT-688] HttpOptions#getAllowedMethods can now handle multiple + Allow headers + Contributed by Andrea Selva + + Release 4.0 Alpha 2 ------------------- diff --git a/module-client/src/main/java/org/apache/http/client/methods/HttpOptions.java b/module-client/src/main/java/org/apache/http/client/methods/HttpOptions.java index 2751e8e3b..0eb5c132c 100644 --- a/module-client/src/main/java/org/apache/http/client/methods/HttpOptions.java +++ b/module-client/src/main/java/org/apache/http/client/methods/HttpOptions.java @@ -33,14 +33,13 @@ package org.apache.http.client.methods; import java.net.URI; import java.net.URISyntaxException; -import java.util.Collections; import java.util.HashSet; import java.util.Set; import org.apache.http.Header; import org.apache.http.HeaderElement; +import org.apache.http.HeaderIterator; import org.apache.http.HttpResponse; -import org.apache.http.ParseException; /** * HTTP OPTIONS method. @@ -83,20 +82,19 @@ public class HttpOptions extends HttpRequestBase { return METHOD_NAME; } - public Set getAllowedMethods(final HttpResponse response) - throws ParseException { - + public Set getAllowedMethods(final HttpResponse response) { if (response == null) { throw new IllegalArgumentException("HTTP response may not be null"); } - Header header = response.getFirstHeader("Allow"); - if (header == null) { - return Collections.EMPTY_SET; - } - HeaderElement[] elements = header.getElements(); - Set methods = new HashSet(elements.length); - for (int i = 0; i < elements.length; i++) { - methods.add(elements[i].getName()); + + HeaderIterator it = response.headerIterator("Allow"); + Set methods = new HashSet(); + while (it.hasNext()) { + Header header = it.nextHeader(); + HeaderElement[] elements = header.getElements(); + for (int i = 0; i < elements.length; i++) { + methods.add(elements[i].getName()); + } } return methods; } diff --git a/module-client/src/test/java/org/apache/http/client/TestAll.java b/module-client/src/test/java/org/apache/http/client/TestAll.java index d4d0f5d7b..0fa6786dc 100644 --- a/module-client/src/test/java/org/apache/http/client/TestAll.java +++ b/module-client/src/test/java/org/apache/http/client/TestAll.java @@ -34,6 +34,7 @@ import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; +import org.apache.http.client.methods.TestAllMethods; import org.apache.http.client.protocol.TestAllProtocol; import org.apache.http.conn.TestAllConn; import org.apache.http.cookie.TestAllCookie; @@ -55,6 +56,7 @@ public class TestAll extends TestCase { suite.addTest(TestAllConn.suite()); suite.addTest(TestAllConnImpl.suite()); suite.addTest(TestAllProtocol.suite()); + suite.addTest(TestAllMethods.suite()); return suite; } diff --git a/module-client/src/test/java/org/apache/http/client/methods/TestAllMethods.java b/module-client/src/test/java/org/apache/http/client/methods/TestAllMethods.java new file mode 100644 index 000000000..eea2197a9 --- /dev/null +++ b/module-client/src/test/java/org/apache/http/client/methods/TestAllMethods.java @@ -0,0 +1,54 @@ +/* + * $HeadURL:$ + * $Revision:$ + * $Date:$ + * ==================================================================== + * 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. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ + +package org.apache.http.client.methods; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +public class TestAllMethods extends TestCase { + + public TestAllMethods(String testName) { + super(testName); + } + + public static Test suite() { + TestSuite suite = new TestSuite(); + suite.addTest(TestHttpOptions.suite()); + return suite; + } + + public static void main(String args[]) { + String[] testCaseName = { TestAllMethods.class.getName() }; + junit.textui.TestRunner.main(testCaseName); + } + +} diff --git a/module-client/src/test/java/org/apache/http/client/methods/TestHttpOptions.java b/module-client/src/test/java/org/apache/http/client/methods/TestHttpOptions.java new file mode 100644 index 000000000..4a3fcd1ad --- /dev/null +++ b/module-client/src/test/java/org/apache/http/client/methods/TestHttpOptions.java @@ -0,0 +1,77 @@ +/* + * $HeadURL:$ + * $Revision:$ + * $Date:$ + * ==================================================================== + * 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. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Software Foundation. For more + * information on the Apache Software Foundation, please see + * . + * + */ + +package org.apache.http.client.methods; + +import java.io.IOException; +import java.util.Set; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +import org.apache.http.ProtocolVersion; +import org.apache.http.message.BasicHttpResponse; +import org.apache.http.message.BasicStatusLine; + +public class TestHttpOptions extends TestCase { + + // ------------------------------------------------------------ Constructor + public TestHttpOptions(final String testName) throws IOException { + super(testName); + } + + // ------------------------------------------------------------------- Main + public static void main(String args[]) { + String[] testCaseName = { TestHttpOptions.class.getName() }; + junit.textui.TestRunner.main(testCaseName); + } + + // ------------------------------------------------------- TestCase Methods + + public static Test suite() { + return new TestSuite(TestHttpOptions.class); + } + + public void testMultipleAllows() { + ProtocolVersion proto = new ProtocolVersion("HTTP", 1, 1); + BasicStatusLine line = new BasicStatusLine(proto, 200, "test reason"); + BasicHttpResponse resp = new BasicHttpResponse(line); + resp.addHeader("Allow", "POST"); + resp.addHeader("Allow", "GET"); + + HttpOptions opt = new HttpOptions(); + Set methodsName = opt.getAllowedMethods(resp); + + assertTrue(methodsName.contains("POST")); + assertTrue(methodsName.contains("GET")); + } + +}