diff --git a/test/tests/proppatch.js b/test/tests/proppatch.js index a4fdcfa2..cf0c5b31 100644 --- a/test/tests/proppatch.js +++ b/test/tests/proppatch.js @@ -8,24 +8,30 @@ module.exports = (test, options, index) => test('PROPPATCH method', isValid => { var server = new webdav.WebDAVServer(); server.start(options.port + index); - isValid = isValid.multiple(4, server); + isValid = isValid.multiple(4 * 2, server); - server.rootResource.addChild(new webdav.VirtualFile('testFile.txt'), test('testFile.txt')); - server.rootResource.addChild(new webdav.VirtualFolder('testFolder'), test('testFolder')); + testGroup('xml', false); + testGroup('json', true); - const pFileName = 'testPFile.txt'; - const pFilePath = path.join(__dirname, 'proppatch', pFileName); - if(!fs.existsSync(pFilePath)) - fs.writeFileSync(pFilePath, 'Content!'); - server.rootResource.addChild(new webdav.PhysicalFile(pFilePath), test(pFileName)); + function testGroup(prefix, isJSON) + { + server.rootResource.addChild(new webdav.VirtualFile(prefix + 'testFile.txt'), test(prefix + 'testFile.txt', isJSON)); + server.rootResource.addChild(new webdav.VirtualFolder(prefix + 'testFolder'), test(prefix + 'testFolder', isJSON)); + + const pFileName = prefix + 'testPFile.txt'; + const pFilePath = path.join(__dirname, 'proppatch', pFileName); + if(!fs.existsSync(pFilePath)) + fs.writeFileSync(pFilePath, 'Content!'); + server.rootResource.addChild(new webdav.PhysicalFile(pFilePath), test(pFileName, isJSON)); - const pFolderName = 'testPFile.txt'; - const pFolderPath = path.join(__dirname, 'proppatch', pFolderName); - if(!fs.existsSync(pFolderPath)) - fs.writeFileSync(pFolderPath, 'Content!'); - server.rootResource.addChild(new webdav.PhysicalFolder(pFolderPath), test(pFolderName)); + const pFolderName = prefix + 'testPFile.txt'; + const pFolderPath = path.join(__dirname, 'proppatch', pFolderName); + if(!fs.existsSync(pFolderPath)) + fs.writeFileSync(pFolderPath, 'Content!'); + server.rootResource.addChild(new webdav.PhysicalFolder(pFolderPath), test(pFolderName, isJSON)); + } - function test(name) + function test(name, isJSON) { return (e) => { if(e) @@ -35,12 +41,27 @@ module.exports = (test, options, index) => test('PROPPATCH method', isValid => } const url = 'http://localhost:' + (options.port + index) + '/' + name; + + function tryCatch(callback) + { + try + { + callback(); + } + catch(e) + { + isValid(false, 'Bad response body for ' + (isJSON ? 'JSON' : 'XML') + ' reponse.'); + } + } // Add authors request({ url: url, method: 'PROPPATCH', - body: 'Jim WhiteheadRoy Fielding' + body: 'Jim WhiteheadRoy Fielding', + headers: { + Accept: isJSON ? 'application/json' : undefined + } }, (e, res, body) => { if(e) { @@ -48,11 +69,10 @@ module.exports = (test, options, index) => test('PROPPATCH method', isValid => return; } - const xml = xmljs.xml2js(body, { compact: true, alwaysArray: true }); - const response = xml['D:multistatus'][0]['D:response'][0]; + tryCatch(() => { + const xml = isJSON ? JSON.parse(body) : 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)) @@ -63,7 +83,10 @@ module.exports = (test, options, index) => test('PROPPATCH method', isValid => request({ url: url, - method: 'PROPFIND' + method: 'PROPFIND', + headers: { + Accept: isJSON ? 'application/json' : undefined + } }, (e, res, body) => { if(e) { @@ -71,48 +94,55 @@ module.exports = (test, options, index) => test('PROPPATCH method', isValid => 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]; + tryCatch(() => { + const xml = isJSON ? JSON.parse(body) : 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) + if(prop['x:Authors'].length !== 1) { - isValid(false, e); + isValid(false); return; } - + + // Remove authors request({ url: url, - method: 'PROPFIND' + method: 'PROPPATCH', + body: '', + headers: { + Accept: isJSON ? 'application/json' : undefined + } }, (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); + request({ + url: url, + method: 'PROPFIND', + headers: { + Accept: isJSON ? 'application/json' : undefined + } + }, (e, res, body) => { + if(e) + { + isValid(false, e); + return; + } + + tryCatch(() => { + const xml = isJSON ? JSON.parse(body) : 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.'); - } + }); }) }; } diff --git a/test/tests/proppatch/testPFile.txt b/test/tests/proppatch/jsontestPFile.txt similarity index 100% rename from test/tests/proppatch/testPFile.txt rename to test/tests/proppatch/jsontestPFile.txt diff --git a/test/tests/proppatch/xmltestPFile.txt b/test/tests/proppatch/xmltestPFile.txt new file mode 100644 index 00000000..9e5b634b --- /dev/null +++ b/test/tests/proppatch/xmltestPFile.txt @@ -0,0 +1 @@ +Content! \ No newline at end of file