Set<String> crit = new HashSet<>();
crit.add("iat");
crit.add("exp");
crit.add("nbf");
final Base64URL mod = new Base64URL("abc123");
final Base64URL exp = new Base64URL("def456");
final KeyUse use = KeyUse.ENCRYPTION;
final String kid = "1234";
RSAKey jwk = new RSAKey(mod, exp, use, null, JWEAlgorithm.RSA1_5, kid, null, null, null);
List<Base64> certChain = new LinkedList<>();
certChain.add(new Base64("asd"));
certChain.add(new Base64("fgh"));
certChain.add(new Base64("jkl"));
JWSHeader h = new JWSHeader.Builder(JWSAlgorithm.RS256).
type(new JOSEObjectType("JWT")).
contentType("application/json").
criticalParams(crit).
jwkURL(new URL("https://example.com/jku.json")).
jwk(jwk).
x509CertURL(new URL("https://example/cert.b64")).
x509CertThumbprint(new Base64URL("789iop")).
x509CertSHA256Thumbprint(new Base64URL("789asd")).
x509CertChain(certChain).
keyID("1234").
customParam("xCustom", "+++").
build();
Base64URL base64URL = h.toBase64URL();
// Parse back
h = JWSHeader.parse(base64URL);
assertEquals(JWSAlgorithm.RS256, h.getAlgorithm());
assertEquals(new JOSEObjectType("JWT"), h.getType());
assertTrue(h.getCriticalParams().contains("iat"));
assertTrue(h.getCriticalParams().contains("exp"));
assertTrue(h.getCriticalParams().contains("nbf"));
assertEquals(3, h.getCriticalParams().size());
assertEquals("application/json", h.getContentType());
assertEquals(new URL("https://example.com/jku.json"), h.getJWKURL());
assertEquals("1234", h.getKeyID());
jwk = (RSAKey)h.getJWK();
assertNotNull(jwk);
assertEquals(new Base64URL("abc123"), jwk.getModulus());
assertEquals(new Base64URL("def456"), jwk.getPublicExponent());
assertEquals(KeyUse.ENCRYPTION, jwk.getKeyUse());
assertEquals(JWEAlgorithm.RSA1_5, jwk.getAlgorithm());
assertEquals("1234", jwk.getKeyID());
assertEquals(new URL("https://example/cert.b64"), h.getX509CertURL());
assertEquals(new Base64URL("789iop"), h.getX509CertThumbprint());
assertEquals(new Base64URL("789asd"), h.getX509CertSHA256Thumbprint());
certChain = h.getX509CertChain();
assertEquals(3, certChain.size());
assertEquals(new Base64("asd"), certChain.get(0));
assertEquals(new Base64("fgh"), certChain.get(1));
assertEquals(new Base64("jkl"), certChain.get(2));
assertEquals("+++", (String)h.getCustomParam("xCustom"));
assertEquals(1, h.getCustomParams().size());
assertEquals(base64URL, h.getParsedBase64URL());
assertTrue(h.getIncludedParams().contains("alg"));
assertTrue(h.getIncludedParams().contains("typ"));
assertTrue(h.getIncludedParams().contains("cty"));
assertTrue(h.getIncludedParams().contains("crit"));
assertTrue(h.getIncludedParams().contains("jku"));
assertTrue(h.getIncludedParams().contains("jwk"));
assertTrue(h.getIncludedParams().contains("kid"));
assertTrue(h.getIncludedParams().contains("x5u"));
assertTrue(h.getIncludedParams().contains("x5t"));
assertTrue(h.getIncludedParams().contains("x5c"));
assertTrue(h.getIncludedParams().contains("xCustom"));
assertEquals(12, h.getIncludedParams().size());
// Test copy constructor
h = new JWSHeader(h);
assertEquals(JWSAlgorithm.RS256, h.getAlgorithm());
assertEquals(new JOSEObjectType("JWT"), h.getType());
assertTrue(h.getCriticalParams().contains("iat"));
assertTrue(h.getCriticalParams().contains("exp"));
assertTrue(h.getCriticalParams().contains("nbf"));
assertEquals(3, h.getCriticalParams().size());
assertEquals("application/json", h.getContentType());
assertEquals(new URL("https://example.com/jku.json"), h.getJWKURL());
assertEquals("1234", h.getKeyID());
jwk = (RSAKey)h.getJWK();
assertNotNull(jwk);
assertEquals(new Base64URL("abc123"), jwk.getModulus());
assertEquals(new Base64URL("def456"), jwk.getPublicExponent());
assertEquals(KeyUse.ENCRYPTION, jwk.getKeyUse());
assertEquals(JWEAlgorithm.RSA1_5, jwk.getAlgorithm());
assertEquals("1234", jwk.getKeyID());
assertEquals(new URL("https://example/cert.b64"), h.getX509CertURL());
assertEquals(new Base64URL("789iop"), h.getX509CertThumbprint());
assertEquals(new Base64URL("789asd"), h.getX509CertSHA256Thumbprint());
certChain = h.getX509CertChain();
assertEquals(3, certChain.size());
assertEquals(new Base64("asd"), certChain.get(0));
assertEquals(new Base64("fgh"), certChain.get(1));