Skip to content

Commit

Permalink
Added tests for JSON and XML responses for PROPPATCH and PROPFIND
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrienCastex committed May 16, 2017
1 parent 24836fc commit 3ab05de
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 46 deletions.
122 changes: 76 additions & 46 deletions test/tests/proppatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -35,24 +41,38 @@ 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: '<?xml version="1.0" encoding="utf-8" ?><D:propertyupdate xmlns:D="DAV:" xmlns:Z="http://ns.example.com/standards/z39.50/"><D:set><D:prop><Z:Authors><Z:Author>Jim Whitehead</Z:Author><Z:Author>Roy Fielding</Z:Author></Z:Authors></D:prop></D:set></D:propertyupdate>'
body: '<?xml version="1.0" encoding="utf-8" ?><D:propertyupdate xmlns:D="DAV:" xmlns:Z="http://ns.example.com/standards/z39.50/"><D:set><D:prop><Z:Authors><Z:Author>Jim Whitehead</Z:Author><Z:Author>Roy Fielding</Z:Author></Z:Authors></D:prop></D:set></D:propertyupdate>',
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 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))
Expand All @@ -63,56 +83,66 @@ 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)
{
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];
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: '<?xml version="1.0" encoding="utf-8" ?><D:propertyupdate xmlns:D="DAV:" xmlns:Z="http://ns.example.com/standards/z39.50/"><D:remove><D:prop><Z:Authors/></D:prop></D:remove></D:propertyupdate>'
}, (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: '<?xml version="1.0" encoding="utf-8" ?><D:propertyupdate xmlns:D="DAV:" xmlns:Z="http://ns.example.com/standards/z39.50/"><D:remove><D:prop><Z:Authors/></D:prop></D:remove></D:propertyupdate>',
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.');
}
});
})
};
}
Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions test/tests/proppatch/xmltestPFile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Content!

0 comments on commit 3ab05de

Please sign in to comment.