Skip to content

Commit

Permalink
Implemented tests with 'xml-js'
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrienCastex committed May 14, 2017
1 parent bd60886 commit 15b0b97
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 44 deletions.
29 changes: 18 additions & 11 deletions test/tests/etagPhysicalFile.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var webdav = require('../../lib/index.js'),
Client = require('webdav-fs'),
request = require('request'),
xml2js = require('xml2js'),
xmljs = require('xml-js'),
path = require('path'),
fs = require('fs')

Expand All @@ -12,7 +12,7 @@ module.exports = (test, options, index) => test('etag of physical file', isValid

const filePath = path.join(__dirname, 'etagPhysicalFile', 'testFile.txt')
if(fs.existsSync(filePath))
fs.unlink(filePath);
fs.unlinkSync(filePath);
fs.writeFileSync(filePath, 'Old content');

server.rootResource.addChild(new webdav.PhysicalFile(filePath), e => {
Expand All @@ -39,21 +39,28 @@ module.exports = (test, options, index) => test('etag of physical file', isValid
isValid(false, e);
return;
}
if(res.statusCode > 300)
{
isValid(false, 'Status code received : ' + res.statusCode);
return;
}

xml2js.parseString(body, (e, doc) => {
if(e)
isValid(false, e);
else
callback(doc);
});
try
{
callback(xmljs.xml2js(body, { compact: true, alwaysArray: true }));
}
catch(e)
{
isValid(e);
}
})
}

propfind((doc) => {
const etag1 = doc['D:multistatus']['D:response'][0]['D:propstat'][0]['D:prop'][0]['D:getetag'][0];
const etag1 = doc['D:multistatus'][0]['D:response'][0]['D:propstat'][0]['D:prop'][0]['D:getetag'][0]._text[0];

propfind((doc) => {
const etag2 = doc['D:multistatus']['D:response'][0]['D:propstat'][0]['D:prop'][0]['D:getetag'][0];
const etag2 = doc['D:multistatus'][0]['D:response'][0]['D:propstat'][0]['D:prop'][0]['D:getetag'][0]._text[0];

if(etag1 !== etag2)
{
Expand All @@ -69,7 +76,7 @@ module.exports = (test, options, index) => test('etag of physical file', isValid
}

propfind((doc) => {
const etag3 = doc['D:multistatus']['D:response'][0]['D:propstat'][0]['D:prop'][0]['D:getetag'][0];
const etag3 = doc['D:multistatus'][0]['D:response'][0]['D:propstat'][0]['D:prop'][0]['D:getetag'][0]._text[0];
if(etag1 === etag3)
isValid(false, 'ETag didn\'t change with file change');
else
Expand Down
27 changes: 17 additions & 10 deletions test/tests/etagVirtualFile.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var webdav = require('../../lib/index.js'),
Client = require('webdav-fs'),
request = require('request'),
xml2js = require('xml2js')
xmljs = require('xml-js')

module.exports = (test, options, index) => test('etag of virtual file', isValid =>
{
Expand Down Expand Up @@ -31,21 +31,28 @@ module.exports = (test, options, index) => test('etag of virtual file', isValid
isValid(false, e);
return;
}
if(res.statusCode > 300)
{
isValid(false, 'Status code received : ' + res.statusCode);
return;
}

xml2js.parseString(body, (e, doc) => {
if(e)
isValid(false, e);
else
callback(doc);
});
try
{
callback(xmljs.xml2js(body, { compact: true, alwaysArray: true }));
}
catch(e)
{
isValid(e);
}
})
}

propfind((doc) => {
const etag1 = doc['D:multistatus']['D:response'][0]['D:propstat'][0]['D:prop'][0]['D:getetag'][0];
const etag1 = doc['D:multistatus'][0]['D:response'][0]['D:propstat'][0]['D:prop'][0]['D:getetag'][0]._text[0];

propfind((doc) => {
const etag2 = doc['D:multistatus']['D:response'][0]['D:propstat'][0]['D:prop'][0]['D:getetag'][0];
const etag2 = doc['D:multistatus'][0]['D:response'][0]['D:propstat'][0]['D:prop'][0]['D:getetag'][0]._text[0];

if(etag1 !== etag2)
{
Expand All @@ -61,7 +68,7 @@ module.exports = (test, options, index) => test('etag of virtual file', isValid
}

propfind((doc) => {
const etag3 = doc['D:multistatus']['D:response'][0]['D:propstat'][0]['D:prop'][0]['D:getetag'][0];
const etag3 = doc['D:multistatus'][0]['D:response'][0]['D:propstat'][0]['D:prop'][0]['D:getetag'][0]._text[0];
if(etag1 === etag3)
isValid(false, 'ETag didn\'t change with file change');
else
Expand Down
24 changes: 1 addition & 23 deletions test/tests/statVirtualFile.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
var webdav = require('../../lib/index.js'),
Client = require('webdav-fs'),
request = require('request'),
xml2js = require('xml2js')
request = require('request')

module.exports = (test, options, index) => test('stat of virtual file', isValid =>
{
Expand All @@ -27,26 +26,5 @@ module.exports = (test, options, index) => test('stat of virtual file', isValid
wfs.stat('/notFoundFile.txt', (e, stat) => {
isValid(!!e);
})

function propfind(callback)
{
request({
url: "http://127.0.0.1:" + (options.port + index) + '/testFile.txt',
method: 'PROPFIND'
}, (e, res, body) => {
if(e)
{
isValid(false, e);
return;
}

xml2js.parseString(body, (e, doc) => {
if(e)
isValid(false, e);
else
callback(doc);
});
})
}
});
})

0 comments on commit 15b0b97

Please sign in to comment.