|
| 1 | +/* eslint-env node, mocha */ |
| 2 | +'use strict'; |
| 3 | + |
| 4 | +const assert = require('assert'); |
| 5 | +const http = require('http'); |
| 6 | +const fs = require('fs'); |
| 7 | +const FileStore = require('../lib/stores/FileStore'); |
| 8 | +const DeleteHandler = require('../lib/handlers/DeleteHandler'); |
| 9 | + |
| 10 | +describe('DeleteHandler', () => { |
| 11 | + let res = null; |
| 12 | + const path = '/files'; |
| 13 | + const pathClean = path.replace(/^\//, ''); |
| 14 | + const namingFunction = (req) => req.url.replace(/\//g, '-'); |
| 15 | + const store = new FileStore({ path, namingFunction}); |
| 16 | + const handler = new DeleteHandler(store); |
| 17 | + const filePath = "/1234"; |
| 18 | + const req = { headers: {}, url: path+filePath}; |
| 19 | + |
| 20 | + beforeEach((done) => { |
| 21 | + res = new http.ServerResponse({ method: 'DELETE' }); |
| 22 | + done(); |
| 23 | + }); |
| 24 | + |
| 25 | + describe('send()', () => { |
| 26 | + it('must be 404 if no file found', (done) => { |
| 27 | + handler.send(req, res) |
| 28 | + .then(() => { |
| 29 | + assert.equal(res.statusCode, 404); |
| 30 | + return done(); |
| 31 | + }) |
| 32 | + .catch(done); |
| 33 | + }); |
| 34 | + |
| 35 | + it('must be 404 if invalid path', (done) => { |
| 36 | + let new_req = Object.assign({}, req); |
| 37 | + new_req.url = '/test/should/not/work/1234'; |
| 38 | + handler.send(new_req, res) |
| 39 | + .then(() => { |
| 40 | + assert.equal(res.statusCode, 404); |
| 41 | + return done(); |
| 42 | + }) |
| 43 | + .catch(done); |
| 44 | + }); |
| 45 | + |
| 46 | + it('must acknowledge successful DELETE requests with the 204', (done) => { |
| 47 | + fs.closeSync(fs.openSync(pathClean+filePath, 'w')); |
| 48 | + handler.send(req, res) |
| 49 | + .then(() => { |
| 50 | + assert.equal(res.statusCode, 204); |
| 51 | + return done(); |
| 52 | + }) |
| 53 | + .catch(done); |
| 54 | + }); |
| 55 | + }); |
| 56 | +}); |
0 commit comments