HTTPCLIENT-688: HttpOptions#getAllowedMethods can now handle multiple Allow headers

Contributed by Andrea Selva <selva.andre at gmail.com>
Reviewed by Oleg Kalnichevski


git-svn-id: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpclient/trunk@594946 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2007-11-14 16:59:15 +00:00
parent 8616ef1ab2
commit 3ddce358ae
5 changed files with 152 additions and 13 deletions

View File

@ -1,3 +1,11 @@
Changes since 4.0 Alpha 2
-------------------
* [HTTPCLIENT-688] HttpOptions#getAllowedMethods can now handle multiple
Allow headers
Contributed by Andrea Selva <selva.andre at gmail.com>
Release 4.0 Alpha 2 Release 4.0 Alpha 2
------------------- -------------------

View File

@ -33,14 +33,13 @@ package org.apache.http.client.methods;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import org.apache.http.Header; import org.apache.http.Header;
import org.apache.http.HeaderElement; import org.apache.http.HeaderElement;
import org.apache.http.HeaderIterator;
import org.apache.http.HttpResponse; import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
/** /**
* HTTP OPTIONS method. * HTTP OPTIONS method.
@ -83,21 +82,20 @@ public class HttpOptions extends HttpRequestBase {
return METHOD_NAME; return METHOD_NAME;
} }
public Set getAllowedMethods(final HttpResponse response) public Set getAllowedMethods(final HttpResponse response) {
throws ParseException {
if (response == null) { if (response == null) {
throw new IllegalArgumentException("HTTP response may not be null"); throw new IllegalArgumentException("HTTP response may not be null");
} }
Header header = response.getFirstHeader("Allow");
if (header == null) { HeaderIterator it = response.headerIterator("Allow");
return Collections.EMPTY_SET; Set methods = new HashSet();
} while (it.hasNext()) {
Header header = it.nextHeader();
HeaderElement[] elements = header.getElements(); HeaderElement[] elements = header.getElements();
Set methods = new HashSet(elements.length);
for (int i = 0; i < elements.length; i++) { for (int i = 0; i < elements.length; i++) {
methods.add(elements[i].getName()); methods.add(elements[i].getName());
} }
}
return methods; return methods;
} }

View File

@ -34,6 +34,7 @@ import junit.framework.Test;
import junit.framework.TestCase; import junit.framework.TestCase;
import junit.framework.TestSuite; import junit.framework.TestSuite;
import org.apache.http.client.methods.TestAllMethods;
import org.apache.http.client.protocol.TestAllProtocol; import org.apache.http.client.protocol.TestAllProtocol;
import org.apache.http.conn.TestAllConn; import org.apache.http.conn.TestAllConn;
import org.apache.http.cookie.TestAllCookie; import org.apache.http.cookie.TestAllCookie;
@ -55,6 +56,7 @@ public class TestAll extends TestCase {
suite.addTest(TestAllConn.suite()); suite.addTest(TestAllConn.suite());
suite.addTest(TestAllConnImpl.suite()); suite.addTest(TestAllConnImpl.suite());
suite.addTest(TestAllProtocol.suite()); suite.addTest(TestAllProtocol.suite());
suite.addTest(TestAllMethods.suite());
return suite; return suite;
} }

View File

@ -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
* <http://www.apache.org/>.
*
*/
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);
}
}

View File

@ -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
* <http://www.apache.org/>.
*
*/
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"));
}
}