Skip to content

Commit

Permalink
Fixed sonar report issues. Added test cases for null and empty GET re…
Browse files Browse the repository at this point in the history
…quests.
  • Loading branch information
fileme committed Aug 13, 2019
1 parent ca3c67b commit 923d729
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -34,57 +34,125 @@ 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;
}
};
}

@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);
Assert.assertNotNull(authorizationHeaderValue);
}

@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);
Assert.assertNotNull(authorizationHeaderValue);
}

@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);
Expand Down

0 comments on commit 923d729

Please sign in to comment.