Skip to content

Commit

Permalink
Adding tests to exercise more of the code
Browse files Browse the repository at this point in the history
Signed-off-by: John Bard <jbard@vmware.com>
  • Loading branch information
John Bard committed Mar 2, 2018
1 parent 4bb88b5 commit 986017b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/main/java/Example.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,39 @@

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.impl.Base64Codec;

import javax.crypto.spec.SecretKeySpec;
import java.security.Key;

public class Example {

public static String exerciseBase64Codec(String input) {
Base64Codec codec = new Base64Codec();
return new String(codec.decode(codec.encode(input.getBytes())));
}

public static String exerciseBuilder(String key, String subject) {
Base64Codec codec = new Base64Codec();
return Jwts.builder()
.setSubject(subject)
.signWith(SignatureAlgorithm.HS256, codec.encode(key.getBytes()))
.compact();
}

public static Claims exerciseParser(String keyStr, String compactStr) {
Base64Codec codec = new Base64Codec();
Key key = new SecretKeySpec(
keyStr.getBytes(),
SignatureAlgorithm.HS256.getJcaName()
);
return Jwts.parser()
.setSigningKey(key)
.parseClaimsJws(compactStr)
.getBody();
}

public static void main(String[] args) {
String input = "abc123";
System.out.println("\n");
Expand Down
16 changes: 16 additions & 0 deletions src/test/java/ExampleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,20 @@ public void testExerciseBase64Codec() throws Exception {
assertEquals(input, Example.exerciseBase64Codec(input));
}

@Test
public void testDefaultJwtBuilder() throws Exception {
String key = "abc123";
String subject = "Joe";
String expected = "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJKb2UifQ.dpfYOKiEwcUXaok-UztzgvBh1HcKr3pqAFFTOJ3sbsU";
assertEquals(expected, Example.exerciseBuilder(key, subject));
}

@Test
public void testDefaultJwtParser() throws Exception {
String key = "abc123";
String compact = "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJKb2UifQ.dpfYOKiEwcUXaok-UztzgvBh1HcKr3pqAFFTOJ3sbsU";
String expected = "Joe";
assertEquals(expected, Example.exerciseParser(key, compact).getSubject());
}

}

0 comments on commit 986017b

Please sign in to comment.