Skip to content

Commit 92f0346

Browse files
gormedbhstahl
authored andcommitted
added tests for relativeLocation
1 parent cd9d6cb commit 92f0346

File tree

2 files changed

+183
-0
lines changed

2 files changed

+183
-0
lines changed

test/Test-DataStore.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ describe('DataStore', () => {
2020
done();
2121
});
2222

23+
it('relativeLocation option must be boolean', (done) => {
24+
assert.equal(typeof datastore.relativeLocation, 'boolean');
25+
done();
26+
});
27+
2328
it('should provide extensions', (done) => {
2429
datastore.should.have.property('extensions');
2530
assert.equal(datastore.extensions, null);

test/Test-EndToEnd.js

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,184 @@ describe('EndToEnd', () => {
216216
});
217217
});
218218

219+
describe('FileStore with relativeLocation', () => {
220+
let file_id;
221+
let deferred_file_id;
222+
before(() => {
223+
server = new Server();
224+
server.datastore = new FileStore({
225+
path: STORE_PATH,
226+
// configure the store to return relative path in Location Header
227+
relativeLocation: true
228+
});
229+
agent = request.agent(server.listen());
230+
});
231+
232+
after((done) => {
233+
// Remove the files directory
234+
exec(`rm -r ${FILES_DIRECTORY}`, (err) => {
235+
if (err) {
236+
return done(err);
237+
}
238+
239+
// clear the config
240+
server.datastore.configstore.clear();
241+
return done();
242+
});
243+
});
244+
245+
describe('HEAD', () => {
246+
it('should 404 file ids that dont exist', (done) => {
247+
agent.head(`${STORE_PATH}/${file_id}`)
248+
.set('Tus-Resumable', TUS_RESUMABLE)
249+
.expect(404)
250+
.expect('Tus-Resumable', TUS_RESUMABLE)
251+
.end(done);
252+
});
253+
});
254+
255+
describe('POST', () => {
256+
it('should create a file that will be deleted', (done) => {
257+
agent.post(STORE_PATH)
258+
.set('Tus-Resumable', TUS_RESUMABLE)
259+
.set('Upload-Defer-Length', 1)
260+
.set('Tus-Resumable', TUS_RESUMABLE)
261+
.expect(201)
262+
.end((err, res) => {
263+
assert.equal('location' in res.headers, true);
264+
assert.equal(res.headers['tus-resumable'], TUS_RESUMABLE);
265+
// Save the id for subsequent tests
266+
file_to_delete = res.headers.location.split('/').pop();
267+
done();
268+
});
269+
});
270+
271+
it('should create a file and respond with its _relative_ location', (done) => {
272+
agent.post(STORE_PATH)
273+
.set('Tus-Resumable', TUS_RESUMABLE)
274+
.set('Upload-Length', TEST_FILE_SIZE)
275+
.set('Upload-Metadata', TEST_METADATA)
276+
.set('Tus-Resumable', TUS_RESUMABLE)
277+
.expect(201)
278+
.end((err, res) => {
279+
assert.equal('location' in res.headers, true);
280+
assert.equal(res.headers['tus-resumable'], TUS_RESUMABLE);
281+
// the location header is relative to the store path
282+
assert.equal(res.headers.location.indexOf(STORE_PATH) > -1, false);
283+
// Save the id for subsequent tests
284+
file_id = res.headers.location.split('/').pop();
285+
done();
286+
});
287+
});
288+
289+
it('should create a file with a deferred length', (done) => {
290+
agent.post(STORE_PATH)
291+
.set('Tus-Resumable', TUS_RESUMABLE)
292+
.set('Upload-Defer-Length', 1)
293+
.set('Upload-Metadata', TEST_METADATA)
294+
.set('Tus-Resumable', TUS_RESUMABLE)
295+
.expect(201)
296+
.end((err, res) => {
297+
assert.equal('location' in res.headers, true);
298+
assert.equal(res.headers['tus-resumable'], TUS_RESUMABLE);
299+
// Save the id for subsequent tests
300+
deferred_file_id = res.headers.location.split('/').pop();
301+
done();
302+
});
303+
});
304+
});
305+
306+
describe('HEAD', () => {
307+
before((done) => {
308+
// Remove the file to delete for 410 Gone test
309+
exec(`rm ${FILES_DIRECTORY}/${file_to_delete}`, () => {
310+
return done();
311+
});
312+
});
313+
314+
it('should return 410 Gone for the file that has been deleted', (done) => {
315+
agent.head(`${STORE_PATH}/${file_to_delete}`)
316+
.set('Tus-Resumable', TUS_RESUMABLE)
317+
.expect(410)
318+
.expect('Tus-Resumable', TUS_RESUMABLE)
319+
.end(done);
320+
});
321+
322+
it('should return a starting offset, metadata for the new file', (done) => {
323+
agent.head(`${STORE_PATH}/${file_id}`)
324+
.set('Tus-Resumable', TUS_RESUMABLE)
325+
.expect(200)
326+
.expect('Upload-Metadata', TEST_METADATA)
327+
.expect('Upload-Offset', 0)
328+
.expect('Upload-Length', TEST_FILE_SIZE)
329+
.expect('Tus-Resumable', TUS_RESUMABLE)
330+
.end(done);
331+
});
332+
333+
it('should return the defer length of the new deferred file', (done) => {
334+
agent.head(`${STORE_PATH}/${deferred_file_id}`)
335+
.set('Tus-Resumable', TUS_RESUMABLE)
336+
.expect(200)
337+
.expect('Upload-Offset', 0)
338+
.expect('Upload-Defer-Length', 1)
339+
.expect('Tus-Resumable', TUS_RESUMABLE)
340+
.end(done);
341+
});
342+
});
343+
344+
describe('PATCH', () => {
345+
it('should 404 paths without a file id', (done) => {
346+
agent.patch(`${STORE_PATH}/`)
347+
.set('Tus-Resumable', TUS_RESUMABLE)
348+
.set('Upload-Offset', 0)
349+
.set('Content-Type', 'application/offset+octet-stream')
350+
.expect(404)
351+
.expect('Tus-Resumable', TUS_RESUMABLE)
352+
.end(done);
353+
});
354+
355+
it('should 404 paths that do not exist', (done) => {
356+
agent.patch(`${STORE_PATH}/dont_exist`)
357+
.set('Tus-Resumable', TUS_RESUMABLE)
358+
.set('Upload-Offset', 0)
359+
.set('Content-Type', 'application/offset+octet-stream')
360+
.expect(404)
361+
.expect('Tus-Resumable', TUS_RESUMABLE)
362+
.end(done);
363+
});
364+
365+
it('should upload the file', (done) => {
366+
const read_stream = fs.createReadStream(TEST_FILE_PATH);
367+
const write_stream = agent.patch(`${STORE_PATH}/${file_id}`)
368+
.set('Tus-Resumable', TUS_RESUMABLE)
369+
.set('Upload-Offset', 0)
370+
.set('Content-Type', 'application/offset+octet-stream');
371+
372+
write_stream.on('response', (res) => {
373+
assert.equal(res.statusCode, 204);
374+
assert.equal(res.header['tus-resumable'], TUS_RESUMABLE);
375+
assert.equal(res.header['upload-offset'], `${TEST_FILE_SIZE}`);
376+
done();
377+
});
378+
379+
read_stream.pipe(write_stream);
380+
});
381+
});
382+
383+
describe('HEAD', () => {
384+
it('should return the ending offset of the uploaded file', (done) => {
385+
agent.head(`${STORE_PATH}/${file_id}`)
386+
.set('Tus-Resumable', TUS_RESUMABLE)
387+
.expect(200)
388+
.expect('Upload-Metadata', TEST_METADATA)
389+
.expect('Upload-Offset', TEST_FILE_SIZE)
390+
.expect('Upload-Length', TEST_FILE_SIZE)
391+
.expect('Tus-Resumable', TUS_RESUMABLE)
392+
.end(done);
393+
});
394+
});
395+
});
396+
219397
describe('GCSDataStore', () => {
220398
if (process.env.TRAVIS_SECURE_ENV_VARS !== 'true') {
221399
return;

0 commit comments

Comments
 (0)