Skip to content

Commit 4a14ef1

Browse files
author
nicola
committed
adding tests for ldp.put and add a trailing on container operations
1 parent b2e703b commit 4a14ef1

3 files changed

Lines changed: 51 additions & 16 deletions

File tree

‎lib/handlers/put.js‎

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,12 @@ function handler(req, res) {
1717

1818
var filePath = utils.uriToFilename(req.path, ldp.root);
1919

20-
// PUT requests not supported on containers. Use POST instead
21-
if (filePath[filePath.length - 1] === '/') {
22-
return res
23-
.status(409)
24-
.send("PUT to containers not supported. Use POST method instead");
25-
}
26-
27-
ldp.writeFile(filePath, req.text, function(err) {
20+
ldp.put(filePath, req.text, function(err) {
2821
if (err) {
29-
debug("PUT -- Write error: " + err);
22+
debug("PUT -- Write error: " + err.message);
3023
return res
31-
.status(500)
32-
.send("Can't write file: "+ err);
24+
.status(err.status)
25+
.send("Can't write file: "+ err.message);
3326
}
3427

3528
debug("PUT -- Write Ok. Bytes written: " + req.text.length);

‎lib/ldp.js‎

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -266,13 +266,29 @@ LDP.prototype.listContainer = function (filename, uri, containerData, callback)
266266
});
267267
};
268268

269-
LDP.prototype.writeFile = function (filePath, contents, cb) {
269+
LDP.prototype.put = function (filePath, contents, callback) {
270+
271+
// PUT requests not supported on containers. Use POST instead
272+
if (filePath[filePath.length - 1] === '/') {
273+
return callback({
274+
status: 409,
275+
message: "PUT to containers not supported. Use POST method instead"
276+
});
277+
}
278+
270279
mkdirp(path.dirname(filePath), function (err) {
271280
if (err) {
272281
debug.handlers("PUT -- Error creating directory: " + err);
273-
return cb(err);
282+
return callback({ status: 500, message: err });
274283
}
275-
return fs.writeFile(filePath, contents, cb);
284+
return fs.writeFile(filePath, contents, function() {
285+
if (err) {
286+
debug.handlers("PUT -- Error writing file: " + err);
287+
return callback({ status: 500, message: err});
288+
}
289+
// Success!
290+
return callback();
291+
});
276292
});
277293
};
278294

@@ -344,6 +360,10 @@ LDP.prototype.delete = function(filename, callback) {
344360
};
345361

346362
LDP.prototype.deleteContainerMetadata = function(directory, callback) {
363+
if (directory[directory.length-1] !== '/') {
364+
directory += '/';
365+
}
366+
347367
return fs.unlink(directory + this.suffixMeta, function(err, data) {
348368
if (err) {
349369
debug.container("DELETE -- unlink() error: " + err);
@@ -469,7 +489,10 @@ LDP.prototype.createNewContainer = function (uri, containerPath, containerGraph,
469489
};
470490

471491
LDP.prototype.writeContainerMetadata = function (directory, container, callback) {
472-
fs.writeFile(directory + this.suffixMeta, container, callback);
492+
if (directory[directory.length-1] !== '/') {
493+
directory += '/';
494+
}
495+
return fs.writeFile(directory + this.suffixMeta, container, callback);
473496
};
474497

475498
LDP.prototype.isMetadataFile = function (filename) {

‎test/ldp.js‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ describe('LDP', function () {
2929
});
3030
});
3131

32-
describe('readContainer', function () {
32+
describe('readContainerMeta', function () {
3333
it ('should return 404 if .meta is not found', function (done) {
3434
ldp.readContainerMeta('resources/', function(err) {
3535
assert.equal(err.status, 404);
@@ -59,4 +59,23 @@ describe('LDP', function () {
5959
});
6060
});
6161
});
62+
63+
describe('put', function() {
64+
it('should write a file in an existing dir', function(done) {
65+
ldp.put(__dirname + '/resources/testPut.txt', 'hello world', function (err) {
66+
assert.notOk(err);
67+
var found = read('testPut.txt');
68+
rm('testPut.txt');
69+
assert.equal(found, 'hello world');
70+
done();
71+
});
72+
});
73+
74+
it('should fail if a trailing `/` is passed', function(done) {
75+
ldp.put(__dirname + '/resources/', 'hello world', function (err) {
76+
assert.equal(err.status, 409);
77+
done();
78+
});
79+
});
80+
});
6281
});

0 commit comments

Comments
 (0)