Skip to content

Commit 1c349b9

Browse files
committed
refactor: updated code to cover missing cases and resolve comments
1 parent 819c965 commit 1c349b9

2 files changed

Lines changed: 32 additions & 15 deletions

File tree

src/test/java/apimatic/core/security/DigestCodecFactoryTest.java

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,21 @@
22

33
import io.apimatic.core.security.DigestCodec;
44
import io.apimatic.core.security.DigestCodecFactory;
5-
import org.junit.Rule;
65
import org.junit.Test;
7-
import org.junit.rules.ExpectedException;
86
import org.junit.runner.RunWith;
97
import org.mockito.junit.MockitoJUnitRunner;
108

119
import static org.junit.Assert.assertArrayEquals;
1210
import static org.junit.Assert.assertEquals;
11+
import static org.junit.Assert.assertThrows;
1312

1413
@RunWith(MockitoJUnitRunner.class)
1514
public class DigestCodecFactoryTest {
1615

17-
/** Rule for expecting exceptions in tests. */
18-
@Rule
19-
public ExpectedException thrown = ExpectedException.none();
20-
2116
private static final byte HEX_BYTE_1 = 0x0A;
2217
private static final byte HEX_BYTE_2 = 0x1B;
2318
private static final byte HEX_BYTE_3 = (byte) 0xFF;
2419

25-
private static final byte[] BASE64_INPUT = {1, 2, 3, 4, 5};
2620
private static final byte BASE64_BYTE_1 = 1;
2721
private static final byte BASE64_BYTE_2 = 2;
2822
private static final byte BASE64_BYTE_3 = 3;
@@ -56,15 +50,17 @@ public void testHexEncodeEmpty() {
5650
@Test
5751
public void testHexDecodeInvalidLength() {
5852
DigestCodec codec = DigestCodecFactory.hex();
59-
thrown.expect(IllegalArgumentException.class);
60-
codec.decode("abc");
53+
assertThrows(IllegalArgumentException.class, () -> {
54+
codec.decode("abc");
55+
});
6156
}
6257

6358
@Test
6459
public void testHexDecodeInvalidCharacter() {
6560
DigestCodec codec = DigestCodecFactory.hex();
66-
thrown.expect(IllegalArgumentException.class);
67-
codec.decode("zzzzz");
61+
assertThrows(IllegalArgumentException.class, () -> {
62+
codec.decode("zzzzz");
63+
});
6864
}
6965

7066
@Test
@@ -88,8 +84,9 @@ public void testBase64EncodeEmpty() {
8884
@Test
8985
public void testBase64DecodeInvalid() {
9086
DigestCodec codec = DigestCodecFactory.base64();
91-
thrown.expect(IllegalArgumentException.class);
92-
codec.decode("!@#$");
87+
assertThrows(IllegalArgumentException.class, () -> {
88+
codec.decode("!@#$");
89+
});
9390
}
9491

9592
@Test
@@ -114,7 +111,8 @@ public void testBase64UrlEncodeEmpty() {
114111
@Test
115112
public void testBase64UrlDecodeInvalid() {
116113
DigestCodec codec = DigestCodecFactory.base64Url();
117-
thrown.expect(IllegalArgumentException.class);
118-
codec.decode("!@#$");
114+
assertThrows(IllegalArgumentException.class, () -> {
115+
codec.decode("!@#$");
116+
});
119117
}
120118
}

src/test/java/apimatic/core/security/HmacSignatureVerifierTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,25 @@ public void verifyAsyncWrongSignatureValueForTemplate() throws Exception {
406406
);
407407
}
408408

409+
@Test
410+
public void verifyAsyncWrongSignatureValueWithQuotesForTemplate() throws Exception {
411+
Map<String, String> headers = new HashMap<>();
412+
headers.put(SIGNATURE_HEADER, "v0=\"a\"complex");
413+
Request request = mockRequest(headers, BODY);
414+
415+
HmacSignatureVerifier verifier = new HmacSignatureVerifier(
416+
SECRET_KEY, SIGNATURE_HEADER, DEFAULT_DIGEST_CODEC,
417+
SIGNATURE_TEMPLATE_RESOLVER, DEFAULT_ALGORITHM, "v0={digest}complex"
418+
);
419+
420+
VerificationResult result = verifier.verifyAsync(request).get();
421+
Assert.assertFalse(result.isSuccess());
422+
Assert.assertEquals(
423+
"Malformed signature header '" + SIGNATURE_HEADER + "'.",
424+
result.getErrors().get(0)
425+
);
426+
}
427+
409428
// Request signature template resolver tests
410429

411430
@Test

0 commit comments

Comments
 (0)