|
| 1 | +/** |
| 2 | + * |
| 3 | + * Copyright (c) 2015 Cisco Systems, Inc. See LICENSE file. |
| 4 | + */ |
| 5 | +"use strict"; |
| 6 | + |
| 7 | +var chai = require("chai"); |
| 8 | +var assert = chai.assert; |
| 9 | + |
| 10 | +var cloneDeep = require("lodash.cloneDeep"); |
| 11 | +var parseCompact = require("../../lib/parse/compact"); |
| 12 | +var jose = { |
| 13 | + JWK: require("../../lib/jwk") |
| 14 | +}; |
| 15 | + |
| 16 | +var fixtures = { |
| 17 | + "jws": cloneDeep(require("jose-cookbook/jws/4_1.rsa_v15_signature.json")), |
| 18 | + "jwe": cloneDeep(require("jose-cookbook/jwe/5_1.key_encryption_using_rsa_v15_and_aes-hmac-sha2.json")) |
| 19 | +}; |
| 20 | + |
| 21 | +describe("parse/compact", function() { |
| 22 | + it("parses compact JWS", function() { |
| 23 | + var fix = fixtures.jws; |
| 24 | + var input = fixtures.jws.output.compact; |
| 25 | + var output = parseCompact(input); |
| 26 | + assert.strictEqual(output.format, "compact"); |
| 27 | + assert.strictEqual(output.type, "JWS"); |
| 28 | + assert.deepEqual(output.header, fix.signing.protected); |
| 29 | + assert.strictEqual(output.input, input); |
| 30 | + |
| 31 | + assert.strictEqual(typeof output.perform, "function"); |
| 32 | + var promise = jose.JWK.asKey(fix.input.key); |
| 33 | + promise = promise.then(function(key) { |
| 34 | + return output.perform(key); |
| 35 | + }); |
| 36 | + promise = promise.then(function(result) { |
| 37 | + assert.strictEqual(result.payload.toString("utf8"), |
| 38 | + fix.input.payload); |
| 39 | + }); |
| 40 | + return promise; |
| 41 | + }); |
| 42 | + it("parses compact JWE", function() { |
| 43 | + var fix = fixtures.jwe; |
| 44 | + var input = fix.output.compact; |
| 45 | + var output = parseCompact(input); |
| 46 | + assert.strictEqual(output.format, "compact"); |
| 47 | + assert.strictEqual(output.type, "JWE"); |
| 48 | + assert.deepEqual(output.header, fix.encrypting_content.protected); |
| 49 | + assert.strictEqual(output.input, input); |
| 50 | + |
| 51 | + assert.strictEqual(typeof output.perform, "function"); |
| 52 | + var promise = jose.JWK.asKey(fix.input.key); |
| 53 | + promise = promise.then(function(key) { |
| 54 | + return output.perform(key); |
| 55 | + }); |
| 56 | + promise = promise.then(function(result) { |
| 57 | + assert.strictEqual(result.plaintext.toString("utf8"), |
| 58 | + fix.input.plaintext); |
| 59 | + }); |
| 60 | + return promise; |
| 61 | + }); |
| 62 | +}); |
0 commit comments