Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
- Added case-insensitive check to getResponseHeadersStartingWith method
- Added validation for null prefix
  • Loading branch information
alex committed Jul 12, 2021
1 parent 43c1ee8 commit 3f9606e
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/main/java/org/javaswift/joss/headers/Header.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@ public void setHeader(HttpRequestBase request) {

public static List<org.apache.http.Header> getResponseHeadersStartingWith(HttpResponse response, String prefix) {
List<org.apache.http.Header> headers = new ArrayList<org.apache.http.Header>();
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);
}
}
Expand Down
56 changes: 56 additions & 0 deletions src/test/java/org/javaswift/joss/headers/HeaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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<org.apache.http.Header> 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<org.apache.http.Header> 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<org.apache.http.Header> matchedHeaders = Header.getResponseHeadersStartingWith(
response,
searchString
);
assertEquals(0, matchedHeaders.size());
}
}

0 comments on commit 3f9606e

Please sign in to comment.