|
| 1 | +import { describe, test, expect } from '@jest/globals' |
| 2 | +import { Base64JsonCodec } from '../../src/v2/transports/http/base64JsonCodec.mjs' |
| 3 | + |
| 4 | + |
| 5 | +describe( 'Base64JsonCodec', () => { |
| 6 | + describe( 'encodeBase64Json', () => { |
| 7 | + test( 'encodes object to base64 string', () => { |
| 8 | + const { base64String, encodeError } = Base64JsonCodec |
| 9 | + .encodeBase64Json( { plainObjectToEncode: { hello: 'world' } } ) |
| 10 | + |
| 11 | + expect( encodeError ).toBeNull() |
| 12 | + expect( typeof base64String ).toBe( 'string' ) |
| 13 | + expect( base64String.length ).toBeGreaterThan( 0 ) |
| 14 | + } ) |
| 15 | + |
| 16 | + |
| 17 | + test( 'returns error for undefined input', () => { |
| 18 | + const { base64String, encodeError } = Base64JsonCodec |
| 19 | + .encodeBase64Json( { plainObjectToEncode: undefined } ) |
| 20 | + |
| 21 | + expect( base64String ).toBeNull() |
| 22 | + expect( encodeError.errorCode ).toBe( 'invalid_payload' ) |
| 23 | + } ) |
| 24 | + |
| 25 | + |
| 26 | + test( 'returns error for null input', () => { |
| 27 | + const { base64String, encodeError } = Base64JsonCodec |
| 28 | + .encodeBase64Json( { plainObjectToEncode: null } ) |
| 29 | + |
| 30 | + expect( base64String ).toBeNull() |
| 31 | + expect( encodeError.errorCode ).toBe( 'invalid_payload' ) |
| 32 | + } ) |
| 33 | + |
| 34 | + |
| 35 | + test( 'encodes complex nested object', () => { |
| 36 | + const complex = { a: [ 1, 2 ], b: { c: true } } |
| 37 | + const { base64String, encodeError } = Base64JsonCodec |
| 38 | + .encodeBase64Json( { plainObjectToEncode: complex } ) |
| 39 | + |
| 40 | + expect( encodeError ).toBeNull() |
| 41 | + expect( base64String ).toBeDefined() |
| 42 | + } ) |
| 43 | + } ) |
| 44 | + |
| 45 | + |
| 46 | + describe( 'decodeBase64Json', () => { |
| 47 | + test( 'decodes base64 string to object', () => { |
| 48 | + const original = { hello: 'world', number: 42 } |
| 49 | + const { base64String } = Base64JsonCodec |
| 50 | + .encodeBase64Json( { plainObjectToEncode: original } ) |
| 51 | + |
| 52 | + const { decodedPlainObject, decodeError } = Base64JsonCodec |
| 53 | + .decodeBase64Json( { base64StringToDecode: base64String } ) |
| 54 | + |
| 55 | + expect( decodeError ).toBeNull() |
| 56 | + expect( decodedPlainObject ).toEqual( original ) |
| 57 | + } ) |
| 58 | + |
| 59 | + |
| 60 | + test( 'returns error for undefined input', () => { |
| 61 | + const { decodedPlainObject, decodeError } = Base64JsonCodec |
| 62 | + .decodeBase64Json( { base64StringToDecode: undefined } ) |
| 63 | + |
| 64 | + expect( decodedPlainObject ).toBeNull() |
| 65 | + expect( decodeError.errorCode ).toBe( 'invalid_payload' ) |
| 66 | + } ) |
| 67 | + |
| 68 | + |
| 69 | + test( 'returns error for non-string input', () => { |
| 70 | + const { decodedPlainObject, decodeError } = Base64JsonCodec |
| 71 | + .decodeBase64Json( { base64StringToDecode: 12345 } ) |
| 72 | + |
| 73 | + expect( decodedPlainObject ).toBeNull() |
| 74 | + expect( decodeError.errorMessage ).toBe( 'Input must be a string' ) |
| 75 | + } ) |
| 76 | + |
| 77 | + |
| 78 | + test( 'returns error for invalid base64 json', () => { |
| 79 | + const { decodedPlainObject, decodeError } = Base64JsonCodec |
| 80 | + .decodeBase64Json( { base64StringToDecode: 'not-valid-base64-json!!!' } ) |
| 81 | + |
| 82 | + expect( decodedPlainObject ).toBeNull() |
| 83 | + expect( decodeError.errorMessage ).toContain( 'Base64/JSON decode failed' ) |
| 84 | + } ) |
| 85 | + } ) |
| 86 | +} ) |
0 commit comments