From bd6088689c66d83c245483802434634631b4f5a7 Mon Sep 17 00:00:00 2001 From: Adrien Castex Date: Sun, 14 May 2017 22:19:03 +0200 Subject: [PATCH] Added PROPPATCH tests --- test/tests/proppatchVirtual.js | 99 ++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 test/tests/proppatchVirtual.js diff --git a/test/tests/proppatchVirtual.js b/test/tests/proppatchVirtual.js new file mode 100644 index 00000000..a9cf110b --- /dev/null +++ b/test/tests/proppatchVirtual.js @@ -0,0 +1,99 @@ +var webdav = require('../../lib/index.js'), + request = require('request'), + xmljs = require('xml-js') + +module.exports = (test, options, index) => test('proppatch a virtual resource', isValid => +{ + var server = new webdav.WebDAVServer(); + server.start(options.port + index); + isValid = isValid.multiple(1, server); + + server.rootResource.addChild(new webdav.VirtualFile('testFile.txt'), e => { + if(e) + { + isValid(false, e) + return; + } + + const url = 'http://localhost:' + (options.port + index) + '/testFile.txt'; + + // Add authors + request({ + url: url, + method: 'PROPPATCH', + body: 'Jim WhiteheadRoy Fielding' + }, (e, res, body) => { + if(e) + { + isValid(false, e); + return; + } + + const xml = xmljs.xml2js(body, { compact: true, alwaysArray: true }); + const response = xml['D:multistatus'][0]['D:response'][0]; + + try + { + if(!(response['D:propstat'][0]['D:prop'][0]['x:Authors'].length === 1 && + response['D:propstat'][0]['D:status'][0]._text[0].indexOf('HTTP/1.1 20') === 0 && + response['D:href'][0]._text[0] === url)) + { + isValid(false, 'Error occured in the response.'); + return; + } + + request({ + url: url, + method: 'PROPFIND' + }, (e, res, body) => { + if(e) + { + isValid(false, e); + return; + } + + const xml = xmljs.xml2js(body, { compact: true, alwaysArray: true }); + const prop = xml['D:multistatus'][0]['D:response'][0]['D:propstat'][0]['D:prop'][0]; + + if(prop['x:Authors'].length !== 1) + { + isValid(false); + return; + } + + // Remove authors + request({ + url: url, + method: 'PROPPATCH', + body: '' + }, (e, res, body) => { + if(e) + { + isValid(false, e); + return; + } + + request({ + url: url, + method: 'PROPFIND' + }, (e, res, body) => { + if(e) + { + isValid(false, e); + return; + } + const xml = xmljs.xml2js(body, { compact: true, alwaysArray: true }); + const prop = xml['D:multistatus'][0]['D:response'][0]['D:propstat'][0]['D:prop'][0]; + + isValid(prop['x:Authors'] === undefined); + }); + }); + }); + } + catch(e) + { + isValid(false, 'Bad response body.'); + } + }) + }) +}) \ No newline at end of file