JCLOUDS-155: Making header handling in OpenStack case-insensitive

Submitted by Rodney Beede
This commit is contained in:
Andrew Phillips 2013-07-29 20:24:15 -04:00
parent bf723a8649
commit 375cb2075d
2 changed files with 21 additions and 8 deletions

View File

@ -56,9 +56,11 @@ public class ParseAuthenticationResponseFromHeaders implements Function<HttpResp
*/
public AuthenticationResponse apply(HttpResponse from) {
releasePayload(from);
// HTTP headers are case insensitive (RFC 2616) so we must allow for that when looking an header names for the URL keyword
Builder<String, URI> builder = ImmutableMap.builder();
for (Entry<String, String> entry : from.getHeaders().entries()) {
if (entry.getKey().endsWith(URL_SUFFIX))
if (entry.getKey().toLowerCase().endsWith(URL_SUFFIX.toLowerCase()))
builder.put(entry.getKey(), getURI(entry.getValue()));
}
AuthenticationResponse response = new AuthenticationResponse(checkNotNull(from.getFirstHeaderOrNull(AUTH_TOKEN),

View File

@ -21,7 +21,6 @@ import static org.testng.Assert.assertEquals;
import java.net.URI;
import org.jclouds.Constants;
import org.jclouds.blobstore.reference.BlobStoreConstants;
import org.jclouds.http.HttpResponse;
import org.jclouds.openstack.domain.AuthenticationResponse;
import org.testng.annotations.Test;
@ -33,8 +32,8 @@ import com.google.inject.Injector;
import com.google.inject.name.Names;
/**
* Tests behavior of {@code ParseContainerListFromJsonResponse}
*
* Tests behavior of {@code ParseAuthenticationResponseFromHeaders}
*
* @author Adrian Cole
*/
@Test(groups = "unit", testName = "ParseAuthenticationResponseFromHeadersTest")
@ -44,7 +43,6 @@ public class ParseAuthenticationResponseFromHeadersTest {
@Override
protected void configure() {
bindConstant().annotatedWith(Names.named(BlobStoreConstants.PROPERTY_USER_METADATA_PREFIX)).to("sdf");
bindConstant().annotatedWith(Names.named(Constants.PROPERTY_API_VERSION)).to("1");
}
@ -53,14 +51,27 @@ public class ParseAuthenticationResponseFromHeadersTest {
public void testReplaceLocalhost() {
ParseAuthenticationResponseFromHeaders parser = i.getInstance(ParseAuthenticationResponseFromHeaders.class);
parser = parser.setHostToReplace("fooman");
HttpResponse response = HttpResponse.builder().statusCode(204).message("No Content")
.addHeader("X-Auth-Token", "token")
.addHeader("X-Storage-Token", "token")
.addHeader("X-Storage-Url", "http://127.0.0.1:8080/v1/token").build();
AuthenticationResponse md = parser.apply(response);
assertEquals(md, new AuthenticationResponse("token", ImmutableMap.<String, URI> of("X-Storage-Url", URI
.create("http://fooman:8080/v1/token"))));
assertEquals(md, new AuthenticationResponse("token", ImmutableMap.<String, URI> of("X-Storage-Url",
URI.create("http://fooman:8080/v1/token"))));
}
public void testHandleHeadersCaseInsensitively() {
ParseAuthenticationResponseFromHeaders parser = i.getInstance(ParseAuthenticationResponseFromHeaders.class);
parser = parser.setHostToReplace("fooman");
HttpResponse response = HttpResponse.builder().statusCode(204).message("No Content")
.addHeader("x-auth-token", "token")
.addHeader("x-storage-token", "token")
.addHeader("x-storage-url", "http://127.0.0.1:8080/v1/token").build();
AuthenticationResponse md = parser.apply(response);
assertEquals(md, new AuthenticationResponse("token", ImmutableMap.<String, URI> of("x-storage-url",
URI.create("http://fooman:8080/v1/token"))));
}
}