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"));
JWEHeader h = new JWEHeader.Builder(JWEAlgorithm.RSA1_5, EncryptionMethod.A256GCM).
type(new JOSEObjectType("JWT")).
compressionAlgorithm(CompressionAlgorithm.DEF).
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").
agreementPartyUInfo(new Base64URL("abc")).
agreementPartyVInfo(new Base64URL("xyz")).
pbes2Salt(new Base64URL("omg")).
pbes2Count(1000).
iv(new Base64URL("101010")).
authTag(new Base64URL("202020")).
customParam("xCustom", "+++").
build();
Base64URL base64URL = h.toBase64URL();
// Parse back
h = JWEHeader.parse(base64URL);
assertEquals(JWEAlgorithm.RSA1_5, h.getAlgorithm());
assertEquals(new JOSEObjectType("JWT"), h.getType());
assertEquals(EncryptionMethod.A256GCM, h.getEncryptionMethod());
assertEquals(CompressionAlgorithm.DEF, h.getCompressionAlgorithm());
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(new Base64URL("abc"), h.getAgreementPartyUInfo());
assertEquals(new Base64URL("xyz"), h.getAgreementPartyVInfo());
assertEquals(new Base64URL("omg"), h.getPBES2Salt());
assertEquals(1000, h.getPBES2Count());
assertEquals(new Base64URL("101010"), h.getIV());
assertEquals(new Base64URL("202020"), h.getAuthTag());
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("enc"));
assertTrue(h.getIncludedParams().contains("zip"));
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("apu"));
assertTrue(h.getIncludedParams().contains("apv"));
assertTrue(h.getIncludedParams().contains("p2s"));
assertTrue(h.getIncludedParams().contains("p2c"));
assertTrue(h.getIncludedParams().contains("iv"));
assertTrue(h.getIncludedParams().contains("tag"));
assertTrue(h.getIncludedParams().contains("xCustom"));
assertEquals(18, h.getIncludedParams().size());
// Test copy constructor
h = new JWEHeader(h);
assertEquals(JWEAlgorithm.RSA1_5, h.getAlgorithm());
assertEquals(new JOSEObjectType("JWT"), h.getType());
assertEquals(EncryptionMethod.A256GCM, h.getEncryptionMethod());
assertEquals(CompressionAlgorithm.DEF, h.getCompressionAlgorithm());
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());