Skip to content

Commit

Permalink
Added tests for ETag for physical files
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrienCastex committed May 14, 2017
1 parent 07ab652 commit 6ef6350
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
82 changes: 82 additions & 0 deletions test/tests/etagPhysicalFile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
var webdav = require('../../lib/index.js'),
Client = require('webdav-fs'),
request = require('request'),
xml2js = require('xml2js'),
path = require('path'),
fs = require('fs')

module.exports = (test, options, index) => test('etag of physical file', isValid =>
{
var server = new webdav.WebDAVServer();
isValid = isValid.multiple(1, server);

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

server.rootResource.addChild(new webdav.PhysicalFile(filePath), e => {
if(e)
{
isValid(false, e)
return;
}

server.start(options.port + index);

var wfs = Client(
"http://127.0.0.1:" + (options.port + index)
);

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);
});
})
}

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

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

if(etag1 !== etag2)
{
isValid(false, 'ETag changed without file change');
return;
}

wfs.writeFile('/testFile.txt', 'New content', (e) => {
if(e)
{
isValid(false, e);
return;
}

propfind((doc) => {
const etag3 = doc['D:multistatus']['D:response'][0]['D:propstat'][0]['D:prop'][0]['D:getetag'][0];
if(etag1 === etag3)
isValid(false, 'ETag didn\'t change with file change');
else
isValid(true);
})
})
})
})
});
})
Empty file.
1 change: 1 addition & 0 deletions test/tests/etagPhysicalFile/testFile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
New content

0 comments on commit 6ef6350

Please sign in to comment.