Skip to content

Commit b2e703b

Browse files
committed
tests for ldp.readContainer
1 parent acff1ac commit b2e703b

3 files changed

Lines changed: 45 additions & 14 deletions

File tree

lib/ldp.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,14 @@ LDP.prototype.readFile = function (filename, callback) {
8787
};
8888

8989
LDP.prototype.readContainerMeta = function (directory, callback) {
90+
if (directory[directory.length-1] !== '/') {
91+
directory += '/';
92+
}
9093
fs.readFile(directory + this.suffixMeta, {
9194
'encoding': 'utf8'
9295
}, function(err, data) {
9396
if (err) {
94-
data = "";
97+
return callback({ status: 404, message:'Metafile not found'});
9598
}
9699
return callback(null, data);
97100
});
@@ -291,15 +294,11 @@ LDP.prototype.get = function(filename, uri, includeBody, callback) {
291294

292295
// Found a container
293296
if (stats.isDirectory()) {
294-
return ldp.readContainerMeta(filename, function(err, data) {
297+
return ldp.readContainerMeta(filename, function(err, metaFile) {
295298
if (err) {
296-
debug.handlers('GET/HEAD -- Read error:' + err);
297-
return callback({
298-
status: err.status,
299-
message: err.message
300-
});
299+
metaFile = '';
301300
}
302-
ldp.listContainer(filename, uri, data, function (err, data) {
301+
ldp.listContainer(filename, uri, metaFile, function (err, data) {
303302
if (err) {
304303
debug.handlers('GET/HEAD -- Read error:' + err);
305304
return callback({

test/ldp.js

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,46 @@ describe('LDP', function () {
1717
});
1818
});
1919

20+
it ('return file if file exists', function (done) {
21+
// file can be empty as well
22+
write('hello world', 'fileExists.txt');
23+
ldp.readFile(__dirname + '/resources/fileExists.txt', function(err, file) {
24+
rm('fileExists.txt');
25+
assert.notOk(err);
26+
assert.equal(file, 'hello world');
27+
done();
28+
});
29+
});
2030
});
2131

22-
describe('LDP', function () {
23-
24-
});
32+
describe('readContainer', function () {
33+
it ('should return 404 if .meta is not found', function (done) {
34+
ldp.readContainerMeta('resources/', function(err) {
35+
assert.equal(err.status, 404);
36+
done();
37+
});
38+
});
2539

26-
describe('LDP', function () {
40+
it ('should return content if metaFile exists', function (done) {
41+
// file can be empty as well
42+
write('', '.meta');
43+
ldp.readContainerMeta(__dirname + '/resources/', function(err, metaFile) {
44+
rm('.meta');
45+
assert.notOk(err);
46+
assert.equal(metaFile, '');
47+
done();
48+
});
49+
});
2750

51+
it ('should work also if trailing `/` is not passed', function (done) {
52+
// file can be empty as well
53+
write('', '.meta');
54+
ldp.readContainerMeta(__dirname + '/resources', function(err, metaFile) {
55+
rm('.meta');
56+
assert.notOk(err);
57+
assert.equal(metaFile, '');
58+
done();
59+
});
60+
});
2861
});
29-
3062
});

test/params.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ var read = require('./test-utils').read;
88

99
var ldnode = require('../index');
1010

11-
describe('params', function () {
11+
describe('LDNODE params', function () {
1212

1313
describe('suffixMeta', function () {
1414
describe('not passed', function() {

0 commit comments

Comments
 (0)