Skip to content

Commit 84bd1c9

Browse files
committed
Added tests for the JSON in the request body
1 parent f1243e7 commit 84bd1c9

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

test/tests/jsonRequest.js

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
"use strict";
2+
var webdav = require('../../lib/index.js'),
3+
request = require('request')
4+
5+
module.exports = (test, options, index) => test('request with a JSON body instead of XML', (isValid, server) =>
6+
{
7+
isValid = isValid.multiple(1, server);
8+
const _ = (e, cb) => {
9+
if(e)
10+
isValid(false, e);
11+
else
12+
cb();
13+
}
14+
15+
const fileName = 'file.txt';
16+
const fileNameDest = 'file.dest.txt';
17+
server.addResourceTree([
18+
new webdav.VirtualFile(fileName),
19+
new webdav.VirtualFile(fileNameDest),
20+
], e => _(e, () => {
21+
request({
22+
url: 'http://localhost:' + (options.port + index) + '/',
23+
method: 'PROPFIND',
24+
headers: {
25+
depth: 1,
26+
Accept: 'application/json'
27+
},
28+
body: JSON.stringify({
29+
'd:propfind': {
30+
_attributes: { 'xmlns:d': 'DAV:' },
31+
'd:prop': {
32+
'd:displayname': {},
33+
'd:resourcetype': {},
34+
'd:supportedlock': {}
35+
}
36+
}
37+
})
38+
}, (e, res, body) => _(e, () => {
39+
body = JSON.parse(body);
40+
41+
const responses = body['D:multistatus'][0]['D:response'];
42+
43+
if(responses.length !== 3)
44+
{
45+
isValid(false, 'The responses body must contains 3 \'DAV:response\' XML elements');
46+
return;
47+
}
48+
49+
for(let i = 0; i < 3; ++i)
50+
{
51+
const propstat = responses[i]['D:propstat'];
52+
if(propstat.length !== 1)
53+
{
54+
isValid(false, 'There must be only one propstart element (because no 404 expected) per response');
55+
return;
56+
}
57+
58+
const props = propstat[0]['D:prop'][0];
59+
const values = [ 'displayname', 'resourcetype', 'supportedlock' ];
60+
let found = 0;
61+
for(const name in props)
62+
{
63+
for(let j = 0; j < values.length; ++j)
64+
{
65+
if(name.indexOf(values[j]) !== -1)
66+
{
67+
++found;
68+
values.splice(j, 1);
69+
}
70+
}
71+
}
72+
73+
if(values.length !== 0)
74+
{
75+
isValid(false, 'Too few properties returned by the request');
76+
return;
77+
}
78+
79+
80+
if(found !== Object.keys(props).length)
81+
{
82+
isValid(false, 'Too many properties returned by the request');
83+
return;
84+
}
85+
}
86+
87+
isValid(true);
88+
}))
89+
}));
90+
})

0 commit comments

Comments
 (0)