import assert from 'assert'; import * as xml from '../lib/xml-parser.js'; // the libxmljs implementation gets confused by whitespace nodes between // elements, which are added below for readability, so strip them. const stripWS = xml => xml.replace(/>[ \n]*<'); suite('xml-parser', function() { test('parseError with message and code', function() { const payload = 'ConditionNotMetThe ' + 'condition specified using HTTP conditional header(s) is not ' + 'met.\nRequestId:8775fa29-201e-00d0-1b1a-2eda27000000\n' + 'Time:2019-06-29T01:35:15.5670223Z' ; assert.deepEqual(xml.parseError({payload}), { code: 'ConditionNotMet', message: 'The condition specified using HTTP conditional header(s) is not met.\n' + 'RequestId:8775fa29-201e-00d0-1b1a-2eda27000000\nTime:2019-06-29T01:35:15.5670223Z', detail: undefined, }); }); test('parseError with message without code (5xx)', function() { const payload = 'The ' + 'condition specified using HTTP conditional header(s) is not ' + 'met.\nRequestId:8775fa29-201e-00d0-1b1a-2eda27000000\n' + 'Time:2019-06-29T01:35:15.5670223Z' ; assert.deepEqual(xml.parseError({payload, statusCode: 501}), { code: 'InternalErrorWithoutCode', message: 'The condition specified using HTTP conditional header(s) is not met.\n' + 'RequestId:8775fa29-201e-00d0-1b1a-2eda27000000\nTime:2019-06-29T01:35:15.5670223Z', detail: undefined, }); }); test('parseError with message without code (4xx)', function() { const payload = 'The ' + 'condition specified using HTTP conditional header(s) is not ' + 'met.\nRequestId:8775fa29-201e-00d0-1b1a-2eda27000000\n' + 'Time:2019-06-29T01:35:15.5670223Z' ; assert.deepEqual(xml.parseError({payload, statusCode: 401}), { code: 'ErrorWithoutCode', message: 'The condition specified using HTTP conditional header(s) is not met.\n' + 'RequestId:8775fa29-201e-00d0-1b1a-2eda27000000\nTime:2019-06-29T01:35:15.5670223Z', detail: undefined, }); }); test('parseError without message', function() { const payload = 'ConditionNotMet'; assert.deepEqual(xml.parseError({payload, statusCode: 401}), { code: 'ConditionNotMet', message: `No error message given, in payload '${payload}'`, detail: undefined, }); }); test('parseError with detail', function() { const payload = 'AuthenticationFailedServer ' + 'failed to authenticate the request.Signed ' + 'permission must be specified in signature or SAS ' + 'identifier'; assert.deepEqual(xml.parseError({payload, statusCode: 401}), { code: 'AuthenticationFailed', message: 'Server failed to authenticate the request.', detail: 'Signed permission must be specified in signature or SAS identifier', }); }); test('queueParseListQueues plural', function() { const payload = stripWS(` fast-azure-test-que fast-azure-test-queue testing fast-azure-test-queue-foo testing fast-azure-test-queue-v2 fast-azure- storage testing /jungle/auth-test-1dfb1916-46fc-46f4-aaaf-858f12d6fc65 10 `); assert.deepEqual(xml.queueParseListQueues({payload}), { nextMarker: '', marker: '/jungle/auth-test-1dfb1916-46fc-46f4-aaaf-858f12d6fc65', maxResults: 10, prefix: 'fast-azure-test-que', queues: [ { metadata: {purpose: 'testing'}, name: 'fast-azure-test-queue' }, { metadata: {purpose: 'testing'}, name: 'fast-azure-test-queue-foo', }, { metadata: { applicationName: 'fast-azure- storage', purpose: 'testing', }, name: 'fast-azure-test-queue-v2', }, ], }); }); test('queueParsePeekMessages single', function() { const payload = stripWS(` ff755a36-441a-468c-b35c-55b199cafed3 Sat, 29 Jun 2019 02:54:44 GMT Sat, 29 Jun 2019 02:56:44 GMT 0 my-message3 `); assert.deepEqual(xml.queueParsePeekMessages({payload}), [ { dequeueCount: 0, expirationTime: new Date('2019-06-29T02:56:44.000Z'), insertionTime: new Date('2019-06-29T02:54:44.000Z'), messageId: 'ff755a36-441a-468c-b35c-55b199cafed3', messageText: 'my-message3' } ]); }); test('queueParseGetMessages none', function() { const payload = stripWS(` `); assert.deepEqual(xml.queueParseGetMessages({payload}), []); }); test('queueParseGetMessages single', function() { const payload = stripWS(` 60986a2f-759e-4e90-9efa-8f313229eeeb Sat, 29 Jun 2019 02:58:29 GMT Sat, 29 Jun 2019 03:00:29 GMT AgAAAAMAAAAAAAAANr7kziYu1QE= Sat, 29 Jun 2019 03:00:29 GMT 1 my-message5 `); assert.deepEqual(xml.queueParseGetMessages({payload}), [ { dequeueCount: 1, expirationTime: new Date('2019-06-29T03:00:29.000Z'), insertionTime: new Date('2019-06-29T02:58:29.000Z'), messageId: '60986a2f-759e-4e90-9efa-8f313229eeeb', messageText: 'my-message5', popReceipt: 'AgAAAAMAAAAAAAAANr7kziYu1QE=', timeNextVisible: new Date('2019-06-29T03:00:29.000Z'), } ]); }); test('blobParseListContainers single', function() { const payload = stripWS(` fast-azure-blob-container fast-azure-blob-container-delete-with-condition Sat, 29 Jun 2019 01:44:16 GMT "0x8D6FC334BEDB92B" unlocked available a week 10 test /jungle/mkr /jungle/auth-test-1dfb1916-46fc-46f4-aaaf-858f12d6fc65 10 `); assert.deepEqual(xml.blobParseListContainers({payload}), { containers: [ { metadata: { scope: 'test' }, name: 'fast-azure-blob-container-delete-with-condition', properties: { eTag: '"0x8D6FC334BEDB92B"', lastModified: 'Sat, 29 Jun 2019 01:44:16 GMT', leaseState: 'available', leaseStatus: 'unlocked', leaseDuration: 'a week', publicAccessLevel: '10' } } ], marker: '/jungle/mkr', nextMarker: '/jungle/auth-test-1dfb1916-46fc-46f4-aaaf-858f12d6fc65', maxResults: 10, prefix: 'fast-azure-blob-container' }); }); test('blobParseListContainers plural', function() { const payload = stripWS(` fast-azure-blob-container fast-azure-blob-container-with-metadata Sun, 30 Jun 2019 20:16:29 GMT "0x8D6FD97D62F6E79" unlocked available fast-azure-blob-container-with-properties Sun, 30 Jun 2019 20:16:32 GMT "0x8D6FD97D7B2EAA9" unlocked available fast-azure-blob-container-without-metadata Sun, 30 Jun 2019 20:16:29 GMT "0x8D6FD97D6353C2D" unlocked available `); assert.deepEqual(xml.blobParseListContainers({payload}), { containers: [ { name: 'fast-azure-blob-container-with-metadata', properties: { eTag: '"0x8D6FD97D62F6E79"', lastModified: 'Sun, 30 Jun 2019 20:16:29 GMT', leaseStatus: 'unlocked', leaseState: 'available', }, metadata: undefined, }, { name: 'fast-azure-blob-container-with-properties', properties: { eTag: '"0x8D6FD97D7B2EAA9"', lastModified: 'Sun, 30 Jun 2019 20:16:32 GMT', leaseStatus: 'unlocked', leaseState: 'available', }, metadata: undefined, }, { name: 'fast-azure-blob-container-without-metadata', properties: { eTag: '"0x8D6FD97D6353C2D"', lastModified: 'Sun, 30 Jun 2019 20:16:29 GMT', leaseStatus: 'unlocked', leaseState: 'available', }, metadata: undefined, }, ], prefix: 'fast-azure-blob-container', nextMarker: '' }); }); test('blobParseContainerACL singular', function() { const payload = stripWS(` 1 2019-06-29T01:47:09.0000000Z 2019-06-30T01:47:09.0000000Z rwacl `); assert.deepEqual(xml.blobParseContainerACL({payload}), [ { id: '1', permission: { add: true, create: true, delete: false, list: true, read: true, write: true }, start: '2019-06-29T01:47:09.0000000Z', expiry: '2019-06-30T01:47:09.0000000Z' } ]); }); test('blobParseContainerACL plural', function() { const payload = stripWS(` 1 2019-06-29T01:47:09.0000000Z rwacl 2 2019-06-29T01:47:09.0000000Z `); assert.deepEqual(xml.blobParseContainerACL({payload}), [ { id: '1', permission: { add: true, create: true, delete: false, list: true, read: true, write: true }, start: '2019-06-29T01:47:09.0000000Z' }, { id: '2', permission: { add: false, create: false, delete: false, list: false, read: false, write: false }, start: '2019-06-29T01:47:09.0000000Z' } ]); }); test('blobParseListBlobs none', function() { const payload = stripWS(` `); assert.deepEqual(xml.blobParseListBlobs({payload}), { blobs: [], nextMarker: '', }); }); test('blobParseListBlobs plural', function() { const payload = stripWS(` tempBlockBlob tempBlockBlob_append_block_with_conditional_headers Sat, 29 Jun 2019 02:19:56 GMT 0x8D6FC38476C2BB0 4 application/octet-stream AppendBlob unlocked available true tempBlockBlob_if_none_matching_conditional_header Sat, 29 Jun 2019 02:19:54 GMT 0x8D6FC384649113A 11 application/octet-stream PiWWCnnbxptnTNTsZ6csYg== BlockBlob unlocked available true testing `); assert.deepEqual(xml.blobParseListBlobs({payload}), { blobs: [{ cacheControl: '', contentEncoding: '', contentLanguage: '', contentLength: '4', contentMD5: '', contentType: 'application/octet-stream', eTag: '0x8D6FC38476C2BB0', lastModified: 'Sat, 29 Jun 2019 02:19:56 GMT', leaseState: 'available', leaseStatus: 'unlocked', name: 'tempBlockBlob_append_block_with_conditional_headers', serverEncrypted: 'true', type: 'AppendBlob' }, { cacheControl: '', contentEncoding: '', contentLanguage: '', contentLength: '11', contentMD5: 'PiWWCnnbxptnTNTsZ6csYg==', contentType: 'application/octet-stream', eTag: '0x8D6FC384649113A', lastModified: 'Sat, 29 Jun 2019 02:19:54 GMT', leaseState: 'available', leaseStatus: 'unlocked', name: 'tempBlockBlob_if_none_matching_conditional_header', serverEncrypted: 'true', type: 'BlockBlob', metadata: {purpose: 'testing'}, }], prefix: 'tempBlockBlob' }); }); test('blobParseListBlock empty', function() { const payload = stripWS(` `); assert.deepEqual(xml.blobParseListBlock({payload}), { committedBlocks: [], uncommittedBlocks: [], }); }); test('blobParseListBlock singular', function() { const payload = stripWS(` ZmFzdGF6dXJlLTAwMQ== 574 ZmFzdGF6dXJlLTAwMg== 163 `); assert.deepEqual(xml.blobParseListBlock({payload}), { committedBlocks: [ {blockId: 'ZmFzdGF6dXJlLTAwMQ==', size: '574'} ], uncommittedBlocks: [ {blockId: 'ZmFzdGF6dXJlLTAwMg==', size: '163'} ], }); }); test('blobParseListBlock plural', function() { const payload = stripWS(` AmFzdGF6dXJlLTAwMQ== 574 ZmFzdGF6dXJlLTAwMQ== 575 AmFzdGF6dXJlLTAwMg== 163 ZmFzdGF6dXJlLTAwMg== 164 `); assert.deepEqual(xml.blobParseListBlock({payload}), { committedBlocks: [ {blockId: 'AmFzdGF6dXJlLTAwMQ==', size: '574'}, {blockId: 'ZmFzdGF6dXJlLTAwMQ==', size: '575'} ], uncommittedBlocks: [ {blockId: 'AmFzdGF6dXJlLTAwMg==', size: '163'}, {blockId: 'ZmFzdGF6dXJlLTAwMg==', size: '164'} ], }); }); test('blobParseServiceProperties singular', function() { const payload = stripWS(` 1.0 true true true true 80 1.0 false false 1.0 true false true 1 POST * content-length 300 `); assert.deepEqual(xml.blobParseServiceProperties({payload}), { corsRules: [ { allowedHeaders: '', allowedMethods: 'POST', allowedOrigins: '*', exposedHeaders: 'content-length', maxAgeInSeconds: '300', }, ], hourMetrics: { enabled: 'false', retentionPolicy: {enabled: 'false'}, version: '1.0', }, logging: { delete: 'true', read: 'true', retentionPolicy: {days: '80', enabled: 'true'}, version: '1.0', write: 'true', }, minuteMetrics: { enabled: 'true', includeAPIs: 'false', retentionPolicy: {days: '1', enabled: 'true'}, version: '1.0', } }); }); });