Skip to content

Commit

Permalink
Added tests for moving the properties (MOVE method)
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrienCastex committed May 24, 2017
1 parent 2b9cde8 commit 7236e50
Showing 1 changed file with 97 additions and 0 deletions.
97 changes: 97 additions & 0 deletions test/tests/moveProp.js
Original file line number Diff line number Diff line change
@@ -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: '<?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>'
}, (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');
})
})
})
}))
}));
})

0 comments on commit 7236e50

Please sign in to comment.