#58: added toString implementations for JwtMap, DefaultJwt and DefaultJws with tests

This commit is contained in:
Les Hazlewood 2015-10-12 14:17:13 -07:00
parent 0e8ee78fc4
commit 4d230a0725
5 changed files with 46 additions and 0 deletions

View File

@ -44,4 +44,9 @@ public class DefaultJws<B> implements Jws<B> {
public String getSignature() {
return this.signature;
}
@Override
public String toString() {
return "header=" + header + ",body=" + body + ",signature=" + signature;
}
}

View File

@ -37,4 +37,9 @@ public class DefaultJwt<B> implements Jwt<Header,B> {
public B getBody() {
return body;
}
@Override
public String toString() {
return "header=" + header + ",body=" + body;
}
}

View File

@ -150,4 +150,9 @@ public class JwtMap implements Map<String,Object> {
public Set<Entry<String, Object>> entrySet() {
return map.entrySet();
}
@Override
public String toString() {
return map.toString();
}
}

View File

@ -17,6 +17,8 @@ package io.jsonwebtoken.impl
import io.jsonwebtoken.JwsHeader
import io.jsonwebtoken.Jwts
import io.jsonwebtoken.SignatureAlgorithm
import io.jsonwebtoken.impl.crypto.MacProvider
import org.junit.Test
import static org.junit.Assert.*
@ -32,4 +34,15 @@ class DefaultJwsTest {
assertEquals jws.getBody(), 'foo'
assertEquals jws.getSignature(), 'sig'
}
@Test
void testToString() {
//create random signing key for testing:
byte[] key = MacProvider.generateKey().encoded
String compact = Jwts.builder().claim('foo', 'bar').signWith(SignatureAlgorithm.HS256, key).compact();
int i = compact.lastIndexOf('.')
String signature = compact.substring(i + 1)
def jws = Jwts.parser().setSigningKey(key).parseClaimsJws(compact)
assertEquals 'header={alg=HS256},body={foo=bar},signature=' + signature, jws.toString()
}
}

View File

@ -0,0 +1,18 @@
package io.jsonwebtoken.impl
import io.jsonwebtoken.Jwt
import io.jsonwebtoken.Jwts
import org.junit.Test
import static org.junit.Assert.assertEquals
class DefaultJwtTest {
@Test
void testToString() {
String compact = Jwts.builder().setHeaderParam('foo', 'bar').setAudience('jsmith').compact();
Jwt jwt = Jwts.parser().parseClaimsJwt(compact);
assertEquals 'header={foo=bar, alg=none},body={aud=jsmith}', jwt.toString()
}
}