From 15b0b9772b389ea95648b9fcb2e46486c7e278aa Mon Sep 17 00:00:00 2001 From: Adrien Castex Date: Sun, 14 May 2017 22:19:28 +0200 Subject: [PATCH] Implemented tests with 'xml-js' --- test/tests/etagPhysicalFile.js | 29 ++++++++++++++++++----------- test/tests/etagVirtualFile.js | 27 +++++++++++++++++---------- test/tests/statVirtualFile.js | 24 +----------------------- 3 files changed, 36 insertions(+), 44 deletions(-) diff --git a/test/tests/etagPhysicalFile.js b/test/tests/etagPhysicalFile.js index f756c881..f7272be4 100644 --- a/test/tests/etagPhysicalFile.js +++ b/test/tests/etagPhysicalFile.js @@ -1,7 +1,7 @@ var webdav = require('../../lib/index.js'), Client = require('webdav-fs'), request = require('request'), - xml2js = require('xml2js'), + xmljs = require('xml-js'), path = require('path'), fs = require('fs') @@ -12,7 +12,7 @@ module.exports = (test, options, index) => test('etag of physical file', isValid const filePath = path.join(__dirname, 'etagPhysicalFile', 'testFile.txt') if(fs.existsSync(filePath)) - fs.unlink(filePath); + fs.unlinkSync(filePath); fs.writeFileSync(filePath, 'Old content'); server.rootResource.addChild(new webdav.PhysicalFile(filePath), e => { @@ -39,21 +39,28 @@ module.exports = (test, options, index) => test('etag of physical file', isValid isValid(false, e); return; } + if(res.statusCode > 300) + { + isValid(false, 'Status code received : ' + res.statusCode); + return; + } - xml2js.parseString(body, (e, doc) => { - if(e) - isValid(false, e); - else - callback(doc); - }); + try + { + callback(xmljs.xml2js(body, { compact: true, alwaysArray: true })); + } + catch(e) + { + isValid(e); + } }) } propfind((doc) => { - const etag1 = doc['D:multistatus']['D:response'][0]['D:propstat'][0]['D:prop'][0]['D:getetag'][0]; + const etag1 = doc['D:multistatus'][0]['D:response'][0]['D:propstat'][0]['D:prop'][0]['D:getetag'][0]._text[0]; propfind((doc) => { - const etag2 = doc['D:multistatus']['D:response'][0]['D:propstat'][0]['D:prop'][0]['D:getetag'][0]; + const etag2 = doc['D:multistatus'][0]['D:response'][0]['D:propstat'][0]['D:prop'][0]['D:getetag'][0]._text[0]; if(etag1 !== etag2) { @@ -69,7 +76,7 @@ module.exports = (test, options, index) => test('etag of physical file', isValid } propfind((doc) => { - const etag3 = doc['D:multistatus']['D:response'][0]['D:propstat'][0]['D:prop'][0]['D:getetag'][0]; + const etag3 = doc['D:multistatus'][0]['D:response'][0]['D:propstat'][0]['D:prop'][0]['D:getetag'][0]._text[0]; if(etag1 === etag3) isValid(false, 'ETag didn\'t change with file change'); else diff --git a/test/tests/etagVirtualFile.js b/test/tests/etagVirtualFile.js index 96ccb2d4..e585bb70 100644 --- a/test/tests/etagVirtualFile.js +++ b/test/tests/etagVirtualFile.js @@ -1,7 +1,7 @@ var webdav = require('../../lib/index.js'), Client = require('webdav-fs'), request = require('request'), - xml2js = require('xml2js') + xmljs = require('xml-js') module.exports = (test, options, index) => test('etag of virtual file', isValid => { @@ -31,21 +31,28 @@ module.exports = (test, options, index) => test('etag of virtual file', isValid isValid(false, e); return; } + if(res.statusCode > 300) + { + isValid(false, 'Status code received : ' + res.statusCode); + return; + } - xml2js.parseString(body, (e, doc) => { - if(e) - isValid(false, e); - else - callback(doc); - }); + try + { + callback(xmljs.xml2js(body, { compact: true, alwaysArray: true })); + } + catch(e) + { + isValid(e); + } }) } propfind((doc) => { - const etag1 = doc['D:multistatus']['D:response'][0]['D:propstat'][0]['D:prop'][0]['D:getetag'][0]; + const etag1 = doc['D:multistatus'][0]['D:response'][0]['D:propstat'][0]['D:prop'][0]['D:getetag'][0]._text[0]; propfind((doc) => { - const etag2 = doc['D:multistatus']['D:response'][0]['D:propstat'][0]['D:prop'][0]['D:getetag'][0]; + const etag2 = doc['D:multistatus'][0]['D:response'][0]['D:propstat'][0]['D:prop'][0]['D:getetag'][0]._text[0]; if(etag1 !== etag2) { @@ -61,7 +68,7 @@ module.exports = (test, options, index) => test('etag of virtual file', isValid } propfind((doc) => { - const etag3 = doc['D:multistatus']['D:response'][0]['D:propstat'][0]['D:prop'][0]['D:getetag'][0]; + const etag3 = doc['D:multistatus'][0]['D:response'][0]['D:propstat'][0]['D:prop'][0]['D:getetag'][0]._text[0]; if(etag1 === etag3) isValid(false, 'ETag didn\'t change with file change'); else diff --git a/test/tests/statVirtualFile.js b/test/tests/statVirtualFile.js index 4c1ee345..3b184ae4 100644 --- a/test/tests/statVirtualFile.js +++ b/test/tests/statVirtualFile.js @@ -1,7 +1,6 @@ var webdav = require('../../lib/index.js'), Client = require('webdav-fs'), - request = require('request'), - xml2js = require('xml2js') + request = require('request') module.exports = (test, options, index) => test('stat of virtual file', isValid => { @@ -27,26 +26,5 @@ module.exports = (test, options, index) => test('stat of virtual file', isValid wfs.stat('/notFoundFile.txt', (e, stat) => { isValid(!!e); }) - - function propfind(callback) - { - request({ - url: "http://127.0.0.1:" + (options.port + index) + '/testFile.txt', - method: 'PROPFIND' - }, (e, res, body) => { - if(e) - { - isValid(false, e); - return; - } - - xml2js.parseString(body, (e, doc) => { - if(e) - isValid(false, e); - else - callback(doc); - }); - }) - } }); }) \ No newline at end of file