-
Notifications
You must be signed in to change notification settings - Fork 4
/
file.js
142 lines (129 loc) · 4.25 KB
/
file.js
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
'use strict';
require('should');
var fs = require('fs');
var join = require('path').join;
var url = require('url');
var sinon = require('sinon');
var File = require('..').File;
var fixture = join(__dirname, 'fixture');
describe('File', function() {
var options;
beforeEach(function() {
options = {
directory: fixture,
proxy: 'http://static.alipayobjects.com',
cache: false
};
});
it('before proxy', function(done) {
options.beforeProxy = function(urlPath, cb, next) {
if (urlPath === 'beforeproxy.js') {
this.emit('found');
cb(null, 'beforeproxy');
} else {
next();
}
};
var spy = sinon.spy();
new File('beforeproxy.js', options)
.on('found', spy)
.end(function(err, data) {
if (err) return done(err);
data.should.be.eql('beforeproxy');
spy.calledOnce.should.be.true;
done();
});
});
it('should response from local', function(done) {
var spy = sinon.spy();
new File('a.js', options)
.on('found', spy)
.end(function(err, data) {
if (err) return done(err);
data.toString().should.be.eql('define("a", function(){});');
spy.calledOnce.should.be.true;
spy.calledWith(join(options.directory, 'a.js')).should.be.true;
done();
});
});
it('should response 404 from local', function(done) {
options.proxy = '';
var spy = sinon.spy();
new File('c.js', options)
.on('not found', spy)
.end(function(err) {
err.should.be.an.instanceof(Error);
spy.calledOnce.should.be.true;
spy.calledWith(join(options.directory, 'c.js')).should.be.true;
done();
});
});
it('should response from remote', function(done) {
var seajs = fs.readFileSync(join(__dirname, './fixture/sea.js')).toString();
var spy1 = sinon.spy();
var spy2 = sinon.spy();
new File('seajs/seajs/2.1.1/sea.js', options)
.on('not found', spy1)
.on('found', spy2)
.end(function(err, data) {
if (err) return done(err);
data.toString().should.be.eql(seajs);
spy1.calledOnce.should.be.true;
spy2.calledOnce.should.be.true;
spy1.calledWith(join(options.directory, 'seajs/seajs/2.1.1/sea.js')).should.be.true;
spy2.calledWith(url.resolve(options.proxy, 'seajs/seajs/2.1.1/sea.js')).should.be.true;
done();
});
});
it('should response 404 from remote', function(done) {
var spy = sinon.spy();
new File('seajs/seajs/2.1.1/not-exist.js', options)
.on('not found', spy)
.end(function(err) {
err.should.be.an.instanceof(Error);
spy.calledTwice.should.be.true;
spy.calledWith(join(options.directory, 'seajs/seajs/2.1.1/not-exist.js')).should.be.true;
spy.calledWith(url.resolve(options.proxy, 'seajs/seajs/2.1.1/not-exist.js')).should.be.true;
done();
});
});
it('should response from remote and cached', function(done) {
options.cache = true;
var file = 'seajs/1.3.1/sea.js';
var cached = join(__dirname, 'fixture', file);
fs.existsSync(cached) && fs.unlinkSync(cached);
var spy1 = sinon.spy();
var spy2 = sinon.spy();
var spy3 = sinon.spy();
new File(file, options)
.on('not found', spy1)
.on('found', spy2)
.on('cached', spy3)
.end(function(err) {
if (err) return done(err);
spy1.calledOnce.should.be.true;
spy2.calledOnce.should.be.true;
spy3.calledOnce.should.be.true;
spy1.calledWith(join(options.directory, file)).should.be.true;
spy2.calledWith(url.resolve(options.proxy, file)).should.be.true;
spy3.calledWith(join(options.directory, file)).should.be.true;
fs.existsSync(cached).should.be.true;
fs.unlinkSync(cached);
done();
});
});
it('should get ttf', function(done) {
var len = fs.readFileSync(join(fixture, 'a.ttf')).length;
console.log(len);
var spy = sinon.spy();
new File('a.ttf', options)
.on('found', spy)
.end(function(err, data) {
if (err) return done(err);
spy.calledOnce.should.be.true;
new Buffer(data).length.should.eql(len);
spy.calledWith(join(options.directory, 'a.ttf')).should.be.true;
done();
});
});
});