summaryrefslogtreecommitdiffstats
path: root/test/lib/file_handler.test.js
blob: 1ba709444570a5ba71119ed6eb897b21df340e62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
var FileHandler = require('../../lib/file_handler');

describe('FileHandler', function() {
  it('should handle content files', function() {
    var buffer = new Buffer('File Content');
    var handler = new FileHandler({
      filename: 'test',
      content: buffer,
      contentType: 'image/jpeg'
    });

    expect(handler.type).to.equal('content');
    expect(handler.content).to.equal(buffer);
    expect(handler.contentType).to.equal('image/jpeg');
  });

  it('should handle files on the filesystem', function() {
    var path = __dirname + '/../assets/secret.txt';
    var handler = new FileHandler({
      path: path
    });

    expect(handler.type).to.equal('path');
    expect(handler.path).to.equal(path);
    expect(handler.filename).to.eql('secret.txt');
    expect(handler.contentType).to.equal('text/plain');
  });

  it('should replace the contentType even if it is empty', function() {
    var url = 'http://i.imgur.com/HnAga.png';
    var handler = new FileHandler({
      filename: 'test',
      url: url,
      contentType: ''
    });

    expect(handler.type).to.equal('url');
    expect(handler.url).to.equal(url);
    expect(handler.contentType).to.equal('image/png');
  });

  it('should handle url files', function() {
    var url = 'http://i.imgur.com/HnAga.png';
    var handler = new FileHandler({
      filename: 'test',
      url: url
    });

    expect(handler.type).to.equal('url');
    expect(handler.url).to.equal(url);
    expect(handler.contentType).to.equal('image/png');
  });

  it('should handle empty files', function() {
    var handler = new FileHandler({
      filename: 'test'
    });

    expect(handler.filename).to.equal('test');
    expect(handler.type).to.equal('none');
  });

  it('should handle inline content', function() {
    var buffer = new Buffer('File Content');
    var handler = new FileHandler({
      filename: 'test',
      cid: 'testcid',
      content: buffer,
      contentType: 'image/jpeg'
    });

    expect(handler.type).to.equal('content');
    expect(handler.content).to.equal(buffer);
    expect(handler.contentType).to.equal('image/jpeg');
    expect(handler.cid).to.equal('testcid');
  });
});