From 923d72944894d211d1b3d0cf4a87246c61ddf652 Mon Sep 17 00:00:00 2001 From: Ahmed Hosny Date: Tue, 13 Aug 2019 09:12:37 +0000 Subject: [PATCH] Fixed sonar report issues. Added test cases for null and empty GET requests. --- .../signers/SpringHttpRequestSigner.java | 3 +- .../signers/SpringHttpRequestSignerTest.java | 96 ++++++++++++++++--- 2 files changed, 83 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/mastercard/developer/signers/SpringHttpRequestSigner.java b/src/main/java/com/mastercard/developer/signers/SpringHttpRequestSigner.java index cb39164..af7e71e 100644 --- a/src/main/java/com/mastercard/developer/signers/SpringHttpRequestSigner.java +++ b/src/main/java/com/mastercard/developer/signers/SpringHttpRequestSigner.java @@ -5,7 +5,6 @@ import java.nio.charset.Charset; import java.security.PrivateKey; -import org.springframework.http.HttpMessage; import org.springframework.http.HttpRequest; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; @@ -22,7 +21,7 @@ public SpringHttpRequestSigner(String consumerKey, PrivateKey signingKey) { public void sign(HttpRequest request, byte[] bytes) { HttpHeaders headers = request.getHeaders(); Charset charset = getCharset(headers); - String payload = new String(bytes, charset); + String payload = (null==bytes ? null : new String(bytes, charset)); String authHeader = OAuth.getAuthorizationHeader(request.getURI(), request.getMethod().toString(), payload, charset, consumerKey, signingKey); headers.add(OAuth.AUTHORIZATION_HEADER_NAME, authHeader); } diff --git a/src/test/java/com/mastercard/developer/signers/SpringHttpRequestSignerTest.java b/src/test/java/com/mastercard/developer/signers/SpringHttpRequestSignerTest.java index fbf1a7b..47b5751 100644 --- a/src/test/java/com/mastercard/developer/signers/SpringHttpRequestSignerTest.java +++ b/src/test/java/com/mastercard/developer/signers/SpringHttpRequestSignerTest.java @@ -2,7 +2,6 @@ import com.mastercard.developer.test.TestUtils; -import org.springframework.http.HttpMessage; import org.springframework.http.HttpRequest; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; @@ -18,9 +17,10 @@ public class SpringHttpRequestSignerTest { - private static final HttpMethod method = HttpMethod.POST; - private static final String body = "{\"foo\":\"bar\"}"; - private static final String consumerKey = "Some key"; + private static final HttpMethod POST_METHOD = HttpMethod.POST; + private static final HttpMethod GET_METHOD = HttpMethod.GET; + private static final String DEFAULT_BODY = "{\"foo\":\"bar\"}"; + private static final String DEFAULT_CONSUMER_KEY = "Some key"; private PrivateKey signingKey; private URI uri; @@ -34,15 +34,19 @@ public void initialize() throws Exception { uri = new URI("https://api.mastercard.com/service"); headers = new HttpHeaders(); request = new HttpRequest() { + @Override public HttpMethod getMethod(){ - return method; + return POST_METHOD; } + @Override public String getMethodValue(){ return getMethod().toString(); } + @Override public URI getURI(){ return uri; } + @Override public HttpHeaders getHeaders(){ return headers; } @@ -50,11 +54,11 @@ public HttpHeaders getHeaders(){ } @Test - public void testSign_ShouldAddOAuth1HeaderToPostRequest() throws Exception { + public void testSignShouldAddOAuth1HeaderToPostRequest() { // WHEN - SpringHttpRequestSigner instanceUnderTest = new SpringHttpRequestSigner(consumerKey, signingKey); - instanceUnderTest.sign(request, body.getBytes()); + SpringHttpRequestSigner instanceUnderTest = new SpringHttpRequestSigner(DEFAULT_CONSUMER_KEY, signingKey); + instanceUnderTest.sign(request, DEFAULT_BODY.getBytes()); // THEN String authorizationHeaderValue = headers.getFirst(HttpHeaders.AUTHORIZATION); @@ -62,14 +66,14 @@ public void testSign_ShouldAddOAuth1HeaderToPostRequest() throws Exception { } @Test - public void testSign_ShouldAddOAuth1HeaderToPostRequestWithCharset() throws Exception { + public void testSignShouldAddOAuth1HeaderToPostRequestWithCharset() { // GIVEN headers.setContentType(MediaType.APPLICATION_JSON_UTF8); // WHEN - SpringHttpRequestSigner instanceUnderTest = new SpringHttpRequestSigner(consumerKey, signingKey); - instanceUnderTest.sign(request, body.getBytes()); + SpringHttpRequestSigner instanceUnderTest = new SpringHttpRequestSigner(DEFAULT_CONSUMER_KEY, signingKey); + instanceUnderTest.sign(request, DEFAULT_BODY.getBytes()); // THEN String authorizationHeaderValue = headers.getFirst(HttpHeaders.AUTHORIZATION); @@ -77,14 +81,78 @@ public void testSign_ShouldAddOAuth1HeaderToPostRequestWithCharset() throws Exce } @Test - public void testSign_ShouldAddOAuth1HeaderToPostRequestWithInvalidCharset() throws Exception { + public void testSignShouldAddOAuth1HeaderToPostRequestWithInvalidCharset() { // GIVEN headers.setContentType(MediaType.APPLICATION_PDF); // WHEN - SpringHttpRequestSigner instanceUnderTest = new SpringHttpRequestSigner(consumerKey, signingKey); - instanceUnderTest.sign(request, body.getBytes()); + SpringHttpRequestSigner instanceUnderTest = new SpringHttpRequestSigner(DEFAULT_CONSUMER_KEY, signingKey); + instanceUnderTest.sign(request, DEFAULT_BODY.getBytes()); + + // THEN + String authorizationHeaderValue = headers.getFirst(HttpHeaders.AUTHORIZATION); + Assert.assertNotNull(authorizationHeaderValue); + } + + @Test + public void testSignShouldAddOAuth1HeaderToGetRequestNullBody() { + + // GIVEN + request = new HttpRequest() { + @Override + public HttpMethod getMethod(){ + return GET_METHOD; + } + @Override + public String getMethodValue(){ + return getMethod().toString(); + } + @Override + public URI getURI(){ + return uri; + } + @Override + public HttpHeaders getHeaders(){ + return headers; + } + }; + + // WHEN + SpringHttpRequestSigner instanceUnderTest = new SpringHttpRequestSigner(DEFAULT_CONSUMER_KEY, signingKey); + instanceUnderTest.sign(request, null); + + // THEN + String authorizationHeaderValue = headers.getFirst(HttpHeaders.AUTHORIZATION); + Assert.assertNotNull(authorizationHeaderValue); + } + + @Test + public void testSignShouldAddOAuth1HeaderToGetRequestEmptyBody() { + + // GIVEN + request = new HttpRequest() { + @Override + public HttpMethod getMethod(){ + return GET_METHOD; + } + @Override + public String getMethodValue(){ + return getMethod().toString(); + } + @Override + public URI getURI(){ + return uri; + } + @Override + public HttpHeaders getHeaders(){ + return headers; + } + }; + + // WHEN + SpringHttpRequestSigner instanceUnderTest = new SpringHttpRequestSigner(DEFAULT_CONSUMER_KEY, signingKey); + instanceUnderTest.sign(request, "".getBytes()); // THEN String authorizationHeaderValue = headers.getFirst(HttpHeaders.AUTHORIZATION);