Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/main/java/com/box/sdk/BoxAPIResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.GZIPInputStream;
Expand Down Expand Up @@ -79,9 +79,11 @@ public BoxAPIResponse(HttpURLConnection connection) {
throw new BoxAPIException("Couldn't connect to the Box API due to a network error.", e);
}

Map<String, String> responseHeaders = new HashMap<String, String>();
Map<String, String> responseHeaders = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
for (String headerKey : connection.getHeaderFields().keySet()) {
responseHeaders.put(headerKey, connection.getHeaderField(headerKey));
if (headerKey != null) {
responseHeaders.put(headerKey, connection.getHeaderField(headerKey));
}
}
this.headers = responseHeaders;

Expand Down
8 changes: 3 additions & 5 deletions src/main/java/com/box/sdk/BoxAPIResponseException.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package com.box.sdk;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import com.eclipsesource.json.JsonObject;
import com.eclipsesource.json.ParseException;


/**
* Thrown to indicate than an error occured while returning with a response from the Box API.
Expand All @@ -30,7 +28,7 @@ public BoxAPIResponseException(String message, BoxAPIResponse responseObj) {
JsonObject responseJSON = null;
this.responseObj = responseObj;

Map<String, List<String>> responseHeaders = new HashMap<String, List<String>>();
Map<String, List<String>> responseHeaders = new TreeMap<String, List<String>>(String.CASE_INSENSITIVE_ORDER);
for (String headerKey : responseObj.getHeaders().keySet()) {
List<String> headerValues = new ArrayList<String>();
headerValues.add(responseObj.getHeaderField(headerKey));
Expand All @@ -56,7 +54,7 @@ public BoxAPIResponseException(String message, BoxAPIResponse responseObj) {

this.setMessage(message + " [" + responseObj.getResponseCode() + requestId + "]" + apiMessage);

} catch (ParseException ex) {
} catch (Exception ex) {
this.setMessage(message + " [" + responseObj.getResponseCode() + "]");
}
}
Expand Down
16 changes: 16 additions & 0 deletions src/test/java/com/box/sdk/BoxAPIResponseExceptionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;
import java.util.TreeMap;

import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
Expand Down Expand Up @@ -162,4 +164,18 @@ public void testAPIResponseExceptionWithHTMLBodyReturnsCorrectErrorMessage() thr

Assert.fail("Never threw a BoxAPIResponseException");
}

@Test
@Category(UnitTest.class)
public void testResponseExceptionHeadersIsCaseInsensitive() {
Map<String, String> headers = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
headers.put("FOO", "bAr");
BoxAPIResponse responseObject = new BoxAPIResponse(202, headers);
BoxAPIResponseException responseException = new BoxAPIResponseException("Test Message", responseObject);

Assert.assertTrue(responseException.getHeaders().containsKey("foo"));
Assert.assertTrue(responseException.getHeaders().containsKey("fOo"));
Assert.assertTrue(responseException.getHeaders().containsKey("FOO"));
Assert.assertEquals("bAr", responseException.getHeaders().get("foo").get(0));
}
}
28 changes: 28 additions & 0 deletions src/test/java/com/box/sdk/BoxAPIResponseTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.box.sdk;

import org.junit.Assert;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import java.util.Map;
import java.util.TreeMap;

/**
*
*/
public class BoxAPIResponseTest {

@Test
@Category(UnitTest.class)
public void testAPIResponseHeaderIsCaseInsensitive() {
Map<String, String> headers = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
headers.put("FOO", "bAr");
BoxAPIResponse responseObject = new BoxAPIResponse(202, headers);

Assert.assertTrue(responseObject.getHeaders().containsKey("foo"));
Assert.assertTrue(responseObject.getHeaders().containsKey("fOo"));
Assert.assertTrue(responseObject.getHeaders().containsKey("FOO"));
Assert.assertEquals("bAr", responseObject.getHeaders().get("foo"));
}

}