From 7236e50a9697584c0a6702825b8a8510dc6103e0 Mon Sep 17 00:00:00 2001 From: Adrien Castex Date: Wed, 24 May 2017 09:57:04 +0200 Subject: [PATCH] Added tests for moving the properties (MOVE method) --- test/tests/moveProp.js | 97 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 test/tests/moveProp.js diff --git a/test/tests/moveProp.js b/test/tests/moveProp.js new file mode 100644 index 00000000..7ddbb769 --- /dev/null +++ b/test/tests/moveProp.js @@ -0,0 +1,97 @@ +"use strict"; +var webdav = require('../../lib/index.js'), + request = require('request'), + xmljs = require('xml-js'); + +module.exports = (test, options, index) => test('move a virtual resource with properties', isValid => +{ + var server = new webdav.WebDAVServer(); + server.start(options.port + index); + isValid = isValid.multiple(1, server); + const _ = (e, cb) => { + if(e) + isValid(false, e); + else + cb(); + } + + const url = 'http://localhost:' + (options.port + index); + + function move(source, dest, callback) + { + request({ + url: url + source, + method: 'MOVE', + headers: { + Destination: url + dest + } + }, (e, res, body) => _(e, () => { + callback(res.statusCode < 300); + })) + } + + function propfind(path, callback) + { + request({ + url: url + path, + method: 'PROPFIND', + }, (e, res, body) => _(e, () => { + try + { + callback(xmljs.xml2js(body, { compact: true, alwaysArray: true })); + } + catch(e) + { + isValid(false, e); + } + })) + } + + server.rootResource.addChild(new webdav.VirtualFile('file.txt'), e => _(e, () => { + request({ + url: url + '/file.txt', + method: 'PROPPATCH', + body: ' \ + \ + \ + \ + \ + Jim Whitehead \ + Roy Fielding \ + \ + \ + \ + ' + }, (e, res, body) => _(e, () => { + + function getXMLInfo(xml) + { + xml = xml['D:multistatus'][0]['D:response'][0]['D:propstat'][0]['D:prop'][0]; + + delete xml['D:getlastmodified']; + delete xml['D:creationdate']; + delete xml['D:displayname']; + delete xml['D:getetag']; + + return xml; + } + + propfind('/file.txt', xml => { + const xmlFile1 = getXMLInfo(xml); + + move('/file.txt', '/file2.txt', (moved) => { + if(!moved) + { + isValid(false); + return; + } + + propfind('/file2.txt', xml => { + const xmlFile2 = getXMLInfo(xml); + isValid(JSON.stringify(xmlFile1) === JSON.stringify(xmlFile2), 'Properties not properly copied'); + }) + }) + }) + })) + })); +}) \ No newline at end of file