Skip to content

Commit

Permalink
Some minor refactoring of the header code. (bunq#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
OGKevin committed Jul 11, 2018
1 parent 9d43c55 commit 2b392d1
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 74 deletions.
6 changes: 3 additions & 3 deletions src/main/java/com/bunq/sdk/http/ApiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,9 @@ public BunqResponseRaw post(
requestBodyBytes
);

if (customHeaders.containsKey(BunqHeader.CONTENT_TYPE.getHeader())) {
if (customHeaders.containsKey(BunqHeader.CONTENT_TYPE.getHeaderName())) {
bunqRequestBody = BunqRequestBody.create(
MediaType.parse(customHeaders.get(BunqHeader.CONTENT_TYPE.getHeader())),
MediaType.parse(customHeaders.get(BunqHeader.CONTENT_TYPE.getHeaderName())),
requestBodyBytes
);
}
Expand Down Expand Up @@ -279,7 +279,7 @@ private BunqResponseRaw createBunqResponseRaw(Response response)
private static String getResponseId(Response response) {
Map<String, String> headerMap = getHeadersMap(response);

return BunqHeader.CLIENT_RESPONSE_ID.getOrDefault(headerMap);
return BunqHeader.CLIENT_RESPONSE_ID.getHeaderValueOrDefault(headerMap);
}

/**
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/com/bunq/sdk/http/BunqBasicHeader.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.bunq.sdk.http;

import com.sun.org.apache.xpath.internal.functions.FuncBoolean;
import okhttp3.Response;

import java.util.ArrayList;
Expand All @@ -19,7 +18,7 @@ public class BunqBasicHeader implements Comparable<BunqBasicHeader> {
private final String value;

public static BunqBasicHeader get(BunqHeader header, Response response) {
return new BunqBasicHeader(header, response.header(header.getHeader()));
return new BunqBasicHeader(header, response.header(header.getHeaderName()));
}

public BunqBasicHeader(BunqHeader name, String value) {
Expand All @@ -36,7 +35,7 @@ public String getValue() {
}

private String forSigning() {
return getName().getHeader() + DELIMITER_HEADER_NAME_AND_VALUE + getValue();
return getName().getHeaderName() + DELIMITER_HEADER_NAME_AND_VALUE + getValue();
}

public static String collectForSigning(
Expand Down
22 changes: 12 additions & 10 deletions src/main/java/com/bunq/sdk/http/BunqHeader.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public enum BunqHeader {
this.defaultValue = defaultValue;
}

public static BunqHeader parse(String value) {
public static BunqHeader parseHeaderOrNull(String value) {
for (BunqHeader header:values()) {
if (header.equals(value)) {
return header;
Expand All @@ -44,15 +44,15 @@ public static BunqHeader parse(String value) {
return null;
}

public String getHeader() {
public String getHeaderName() {
return header;
}

public String getDefaultValue() {
return defaultValue;
}

private String getOrDefault(String value) {
private String getHeaderValueOrDefault(String value) {
if (value != null) {
return value;
}
Expand All @@ -61,26 +61,26 @@ private String getOrDefault(String value) {
}

public void addTo(Map<String, String> headers, String value) {
headers.put(getHeader(), getOrDefault(value));
headers.put(getHeaderName(), getHeaderValueOrDefault(value));
}

public void addTo(BunqRequestBuilder requestBuilder) {
addTo(requestBuilder, null);
}

public void addTo(BunqRequestBuilder requestBuilder, String value) {
requestBuilder.addHeader(getHeader(), getOrDefault(value));
requestBuilder.addHeader(getHeaderName(), getHeaderValueOrDefault(value));
}

public boolean equals(String header) {
return getHeader().equalsIgnoreCase(header);
return getHeaderName().equalsIgnoreCase(header);
}

public boolean isBunq() {
return getHeader().startsWith(PREFIX);
return getHeaderName().startsWith(PREFIX);
}

private String findKey(Collection<String> keys) {
private String findKeyOrNull(Collection<String> keys) {
for (String key:keys) {
if (this.equals(key)) {
return key;
Expand All @@ -90,11 +90,13 @@ private String findKey(Collection<String> keys) {
return null;
}

public String getOrDefault(Map<String, String> headers) {
String key = findKey(headers.keySet());
public String getHeaderValueOrDefault(Map<String, String> headers) {
String key = findKeyOrNull(headers.keySet());

if (key != null && headers.get(key) != null) {
return headers.get(key);
}

return getDefaultValue();
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/bunq/sdk/http/BunqRequestBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public BunqRequestBuilder url(URL url) {
}

private void addToAllHeader(String name, String value) {
BunqHeader header = BunqHeader.parse(name);
BunqHeader header = BunqHeader.parseHeaderOrNull(name);

if (header != null) {
this.allHeader.add(new BunqBasicHeader(header, value));
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/bunq/sdk/security/SecurityUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ private static byte[] getResponseBytes(
List<BunqBasicHeader> allResponseHeader = new ArrayList<>();

for (int i = INDEX_FIRST; i < allHeader.names().size(); i++) {
BunqHeader header = BunqHeader.parse(allHeader.name(i));
BunqHeader header = BunqHeader.parseHeaderOrNull(allHeader.name(i));

if (header != null && !BunqHeader.SERVER_SIGNATURE.equals(header)) {
allResponseHeader.add(new BunqBasicHeader(header, allHeader.get(allHeader.name(i))));
Expand Down
10 changes: 5 additions & 5 deletions src/test/java/com/bunq/sdk/BunqSdkTestBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,11 @@ private static SandboxUser generateNewSandboxUser() {
URL_PATH_SANDBOX_USER
)
.post(RequestBody.create(null, new byte[INDEX_FIRST]))
.addHeader(BunqHeader.CLIENT_REQUEST_ID.getHeader(), UUID.randomUUID().toString())
.addHeader(BunqHeader.CACHE_CONTROL.getHeader(), BunqHeader.CACHE_CONTROL.getDefaultValue())
.addHeader(BunqHeader.GEOLOCATION.getHeader(), BunqHeader.GEOLOCATION.getDefaultValue())
.addHeader(BunqHeader.LANGUAGE.getHeader(), BunqHeader.LANGUAGE.getDefaultValue())
.addHeader(BunqHeader.REGION.getHeader(), BunqHeader.REGION.getDefaultValue())
.addHeader(BunqHeader.CLIENT_REQUEST_ID.getHeaderName(), UUID.randomUUID().toString())
.addHeader(BunqHeader.CACHE_CONTROL.getHeaderName(), BunqHeader.CACHE_CONTROL.getDefaultValue())
.addHeader(BunqHeader.GEOLOCATION.getHeaderName(), BunqHeader.GEOLOCATION.getDefaultValue())
.addHeader(BunqHeader.LANGUAGE.getHeaderName(), BunqHeader.LANGUAGE.getDefaultValue())
.addHeader(BunqHeader.REGION.getHeaderName(), BunqHeader.REGION.getDefaultValue())
.build();

try {
Expand Down
136 changes: 85 additions & 51 deletions src/test/java/com/bunq/sdk/http/BunqHeaderTest.java
Original file line number Diff line number Diff line change
@@ -1,69 +1,103 @@
package com.bunq.sdk.http;

import org.junit.Test;

import java.util.Collections;
import java.util.Map;
import java.util.HashMap;

import org.junit.Test;
import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class BunqHeaderTest {
@Test
public void parse() {
// parse works case-insensitive
assertEquals(BunqHeader.CLIENT_RESPONSE_ID, BunqHeader.parse("X-Bunq-Client-Response-Id"));
assertEquals(BunqHeader.CLIENT_RESPONSE_ID, BunqHeader.parse("x-bunq-client-response-id"));
}

@Test
public void isBunq() {
assertFalse(BunqHeader.CACHE_CONTROL.isBunq());
assertFalse(BunqHeader.CONTENT_TYPE.isBunq());
assertFalse(BunqHeader.USER_AGENT.isBunq());
/**
* Expected error messages constatns.
*/
private static final String ERROR_STRING_COULD_NOT_DETERMINE_RESPONSE_ID = "Could not determine response id.";

/**
* Header name constants.
*/
private static final String HEADER_X_BUNQ_CLIENT_RESPONSE_ID = "X-Bunq-Client-Response-Id";
private static final String HEADER_LOWER_X_BUNQ_CLIENT_RESPONSE_ID = "x-bunq-client-response-id";
private static final String HEADER_X_BUNQ_SOME_OTHER_HEADER = "x-bunq-some-other-header";

/**
* Header value constatns.
*/
private static final String HEADER_VALUE_USER_AGENT = "test-agent";
private static final String HEADER_VALUE_LANGUAGE = "en_US";
private static final String HEADER_VALUE_GEOLOCATION = "0 0 0 0 000";
private static final String RESPONSE_ID = "test-id";

@Test
public void testParseHeaderCaseInsensitive() {
assertEquals(
BunqHeader.CLIENT_RESPONSE_ID,
BunqHeader.parseHeaderOrNull(HEADER_X_BUNQ_CLIENT_RESPONSE_ID)
);
assertEquals(
BunqHeader.CLIENT_RESPONSE_ID,
BunqHeader.parseHeaderOrNull(HEADER_LOWER_X_BUNQ_CLIENT_RESPONSE_ID)
);
}

@Test
public void testIsBunq() {
assertFalse(BunqHeader.CACHE_CONTROL.isBunq());
assertFalse(BunqHeader.CONTENT_TYPE.isBunq());
assertFalse(BunqHeader.USER_AGENT.isBunq());

assertTrue(BunqHeader.ATTACHMENT_DESCRIPTION.isBunq());
assertTrue(BunqHeader.CLIENT_AUTHENTICATION.isBunq());
assertTrue(BunqHeader.CLIENT_ENCRYPTION_HMAC.isBunq());
assertTrue(BunqHeader.CLIENT_ENCRYPTION_IV.isBunq());
assertTrue(BunqHeader.CLIENT_ENCRYPTION_IV.isBunq());
assertTrue(BunqHeader.CLIENT_ENCRYPTION_KEY.isBunq());
assertTrue(BunqHeader.CLIENT_REQUEST_ID.isBunq());
assertTrue(BunqHeader.CLIENT_SIGNATURE.isBunq());
assertTrue(BunqHeader.CLIENT_RESPONSE_ID.isBunq());
assertTrue(BunqHeader.GEOLOCATION.isBunq());
assertTrue(BunqHeader.REGION.isBunq());
assertTrue(BunqHeader.SERVER_SIGNATURE.isBunq());
assertTrue(BunqHeader.CLIENT_ENCRYPTION_KEY.isBunq());
assertTrue(BunqHeader.CLIENT_REQUEST_ID.isBunq());
}
assertTrue(BunqHeader.ATTACHMENT_DESCRIPTION.isBunq());
assertTrue(BunqHeader.CLIENT_AUTHENTICATION.isBunq());
assertTrue(BunqHeader.CLIENT_ENCRYPTION_HMAC.isBunq());
assertTrue(BunqHeader.CLIENT_ENCRYPTION_IV.isBunq());
assertTrue(BunqHeader.CLIENT_ENCRYPTION_IV.isBunq());
assertTrue(BunqHeader.CLIENT_ENCRYPTION_KEY.isBunq());
assertTrue(BunqHeader.CLIENT_REQUEST_ID.isBunq());
assertTrue(BunqHeader.CLIENT_SIGNATURE.isBunq());
assertTrue(BunqHeader.CLIENT_RESPONSE_ID.isBunq());
assertTrue(BunqHeader.GEOLOCATION.isBunq());
assertTrue(BunqHeader.REGION.isBunq());
assertTrue(BunqHeader.SERVER_SIGNATURE.isBunq());
assertTrue(BunqHeader.CLIENT_ENCRYPTION_KEY.isBunq());
assertTrue(BunqHeader.CLIENT_REQUEST_ID.isBunq());
}

@Test
public void getOrDefault() {
BunqHeader h = BunqHeader.CLIENT_RESPONSE_ID;
@Test
public void getOrDefault() {
BunqHeader header = BunqHeader.CLIENT_RESPONSE_ID;

assertEquals("test-id",
h.getOrDefault(Collections.singletonMap("x-bunq-client-response-id", "test-id")));
assertEquals("Could not determine response id.",
h.getOrDefault(Collections.singletonMap("x-bunq-some-other-header", "test-id")));
assertEquals("Could not determine response id.",
h.getOrDefault(Collections.<String, String>emptyMap()));
}
assertEquals(
RESPONSE_ID,
header.getHeaderValueOrDefault(
Collections.singletonMap(HEADER_LOWER_X_BUNQ_CLIENT_RESPONSE_ID, RESPONSE_ID)
)
);
assertEquals(
ERROR_STRING_COULD_NOT_DETERMINE_RESPONSE_ID,
header.getHeaderValueOrDefault(
Collections.singletonMap(HEADER_X_BUNQ_SOME_OTHER_HEADER, RESPONSE_ID)
)
);
assertEquals(
ERROR_STRING_COULD_NOT_DETERMINE_RESPONSE_ID,
header.getHeaderValueOrDefault(Collections.<String, String>emptyMap())
);
}

@Test
public void addToMap() {
Map<String, String> headers = new HashMap<>();
@Test
public void addToMap() {
Map<String, String> headers = new HashMap<>();

//sut
BunqHeader.LANGUAGE.addTo(headers, null);
BunqHeader.GEOLOCATION.addTo(headers, null);
BunqHeader.USER_AGENT.addTo(headers, "test-agent");
BunqHeader.LANGUAGE.addTo(headers, null);
BunqHeader.GEOLOCATION.addTo(headers, null);
BunqHeader.USER_AGENT.addTo(headers, HEADER_VALUE_USER_AGENT);

// verify
assertEquals("en_US", headers.get("X-Bunq-Language"));
assertEquals("0 0 0 0 000", headers.get("X-Bunq-Geolocation"));
assertEquals("test-agent", headers.get("User-Agent"));
}
assertEquals(HEADER_VALUE_LANGUAGE, headers.get(BunqHeader.LANGUAGE.getHeaderName()));
assertEquals(HEADER_VALUE_GEOLOCATION, headers.get(BunqHeader.GEOLOCATION.getHeaderName()));
assertEquals(HEADER_VALUE_USER_AGENT, headers.get(BunqHeader.USER_AGENT.getHeaderName()));
}
}

0 comments on commit 2b392d1

Please sign in to comment.