Skip to content

Commit

Permalink
Added tests for the JSON in the request body
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrienCastex committed Jun 15, 2017
1 parent f1243e7 commit 84bd1c9
Showing 1 changed file with 90 additions and 0 deletions.
90 changes: 90 additions & 0 deletions test/tests/jsonRequest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
"use strict";
var webdav = require('../../lib/index.js'),
request = require('request')

module.exports = (test, options, index) => test('request with a JSON body instead of XML', (isValid, server) =>
{
isValid = isValid.multiple(1, server);
const _ = (e, cb) => {
if(e)
isValid(false, e);
else
cb();
}

const fileName = 'file.txt';
const fileNameDest = 'file.dest.txt';
server.addResourceTree([
new webdav.VirtualFile(fileName),
new webdav.VirtualFile(fileNameDest),
], e => _(e, () => {
request({
url: 'http://localhost:' + (options.port + index) + '/',
method: 'PROPFIND',
headers: {
depth: 1,
Accept: 'application/json'
},
body: JSON.stringify({
'd:propfind': {
_attributes: { 'xmlns:d': 'DAV:' },
'd:prop': {
'd:displayname': {},
'd:resourcetype': {},
'd:supportedlock': {}
}
}
})
}, (e, res, body) => _(e, () => {
body = JSON.parse(body);

const responses = body['D:multistatus'][0]['D:response'];

if(responses.length !== 3)
{
isValid(false, 'The responses body must contains 3 \'DAV:response\' XML elements');
return;
}

for(let i = 0; i < 3; ++i)
{
const propstat = responses[i]['D:propstat'];
if(propstat.length !== 1)
{
isValid(false, 'There must be only one propstart element (because no 404 expected) per response');
return;
}

const props = propstat[0]['D:prop'][0];
const values = [ 'displayname', 'resourcetype', 'supportedlock' ];
let found = 0;
for(const name in props)
{
for(let j = 0; j < values.length; ++j)
{
if(name.indexOf(values[j]) !== -1)
{
++found;
values.splice(j, 1);
}
}
}

if(values.length !== 0)
{
isValid(false, 'Too few properties returned by the request');
return;
}


if(found !== Object.keys(props).length)
{
isValid(false, 'Too many properties returned by the request');
return;
}
}

isValid(true);
}))
}));
})

0 comments on commit 84bd1c9

Please sign in to comment.