diff --git a/src/main/java/org/javaswift/joss/headers/Header.java b/src/main/java/org/javaswift/joss/headers/Header.java index d6355ce9..f750c66b 100644 --- a/src/main/java/org/javaswift/joss/headers/Header.java +++ b/src/main/java/org/javaswift/joss/headers/Header.java @@ -35,8 +35,11 @@ public void setHeader(HttpRequestBase request) { public static List getResponseHeadersStartingWith(HttpResponse response, String prefix) { List headers = new ArrayList(); + if (prefix == null){ + return headers; + } for (org.apache.http.Header header : response.getAllHeaders()) { - if (header.getName().startsWith(prefix)) { + if (header.getName().toLowerCase().startsWith(prefix.toLowerCase())) { headers.add(header); } } diff --git a/src/test/java/org/javaswift/joss/headers/HeaderTest.java b/src/test/java/org/javaswift/joss/headers/HeaderTest.java index 8266c747..33fcdc6d 100644 --- a/src/test/java/org/javaswift/joss/headers/HeaderTest.java +++ b/src/test/java/org/javaswift/joss/headers/HeaderTest.java @@ -8,8 +8,11 @@ import mockit.NonStrictExpectations; import org.apache.http.HttpResponse; +import org.apache.http.message.BasicHeader; import org.junit.Test; +import java.util.List; + public class HeaderTest { private String headerName = "some-header"; @@ -44,4 +47,57 @@ public void headerWithValue(@Mocked final org.apache.http.Header header) { assertEquals("alpha", Header.convertResponseHeader(response, headerName)); } + @Test + public void getHeadersWithHeaderNameMatchingPrefixCase() { + + String headerValue = "34567as7dta8asd7"; + String headerName = "x-account-meta-temp-url-key"; + String searchString = "x-account-meta-"; + final org.apache.http.Header myHeader = new BasicHeader(headerName, headerValue); + + new Expectations() {{ + response.getAllHeaders(); + result = new org.apache.http.Header[]{myHeader}; + }}; + List matchedHeaders = Header.getResponseHeadersStartingWith( + response, + searchString + ); + assertEquals(1, matchedHeaders.size()); + assertEquals(headerName, matchedHeaders.get(0).getName()); + assertEquals(headerValue, matchedHeaders.get(0).getValue()); + + } + + @Test + public void getHeadersWithHeaderNameNotMatchingPrefixCase() { + + String headerValue = "34567as7dta8asd7"; + String headerName = "x-account-meta-temp-url-key"; + String searchString = "X-Account-Meta-"; + final org.apache.http.Header myHeader = new BasicHeader(headerName, headerValue); + + new Expectations() {{ + response.getAllHeaders(); + result = new org.apache.http.Header[]{myHeader}; + }}; + List matchedHeaders = Header.getResponseHeadersStartingWith( + response, + searchString + ); + assertEquals(1, matchedHeaders.size()); + assertEquals(headerName, matchedHeaders.get(0).getName()); + assertEquals(headerValue, matchedHeaders.get(0).getValue()); + } + + @Test + public void getHeadersWithNullPrefix() { + + String searchString = null; + List matchedHeaders = Header.getResponseHeadersStartingWith( + response, + searchString + ); + assertEquals(0, matchedHeaders.size()); + } }