-
Notifications
You must be signed in to change notification settings - Fork 188
/
ScraperPromise.js
381 lines (354 loc) · 8.75 KB
/
ScraperPromise.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/* global describe, it, beforeEach, afterEach, $ */
var assert = require('assert'),
sjs = require('../src/Scraper'),
StaticScraper = sjs.StaticScraper,
DynamicScraper = sjs.DynamicScraper,
ScraperPromise = sjs.ScraperPromise,
HN_CLONE = 'http://localhost:3000/hacker-news-clone',
domain = require('domain');
function exec(ScraperType) {
function isDynamic() {
return ScraperType === DynamicScraper;
}
describe('onStatusCode', function() {
it('with code', function(done) {
var s = new ScraperPromise(new ScraperType())
.get(HN_CLONE);
var temp = s
.onStatusCode(202, function() {
assert.fail('This status code should not trigger.');
})
.onStatusCode(200, function() {
done();
});
assert.ok(temp === s);
});
it('without code', function(done) {
var s = new ScraperPromise(new ScraperType())
.get(HN_CLONE);
var temp = s
.onStatusCode(function(code) {
assert.equal(code, 200);
done();
});
assert.ok(temp === s);
});
});
it('timeout', function(done) {
var s = new ScraperPromise(new ScraperType())
.get(HN_CLONE)
.onStatusCode(function(code) {
assert.equal(code, 200);
});
var temp = s.timeout(100, function() {
done();
});
assert.ok(temp === s);
});
it('then', function(done) {
var s = new ScraperPromise(new ScraperType())
.get(HN_CLONE);
var temp = s.then(function() {
done();
});
assert.ok(temp === s);
});
it('then', function(done) {
var s = new ScraperPromise(new ScraperType())
.get(HN_CLONE);
var temp = s.async(function(_, done) {
done();
});
s.done(function() {
done();
});
assert.ok(temp === s);
});
describe('catch', function() {
it('on sync', function(done) {
var s = new ScraperPromise(new ScraperType())
.get(HN_CLONE)
.then(function() {
throw new Error('random message');
});
var temp = s.catch(function(err) {
assert.equal(err.message, 'random message');
done();
});
assert.ok(s === temp);
});
it('on async', function(done) {
var s = new ScraperPromise(new ScraperType())
.get(HN_CLONE)
.async(function(_, done) {
done(new Error('random message'));
});
var temp = s.catch(function(err) {
assert.equal(err.message, 'random message');
done();
});
assert.ok(s === temp);
});
});
// FIXME - this is not working for the dynamic scraper with factory
if (!isDynamic()) {
it('error without catch', function(done) {
var d = domain.create();
d.on('error', function(err) {
assert.equal(err.message, 'random message');
done();
});
d.run(function() {
new ScraperPromise(new ScraperType())
.get(HN_CLONE)
.then(function() {
throw new Error('random message');
});
});
});
}
describe('scrape', function() {
var expectedVal;
if (isDynamic()) {
expectedVal = 9;
} else {
expectedVal = 10;
}
it('without extra arguments', function(done) {
var s = new ScraperPromise(new ScraperType())
.get(HN_CLONE);
var fn = function($) {
return $('.title a').map(function() {
return $(this).text();
}).get();
};
var temp = s.scrape(fn, function(news) {
assert.equal(news.length, expectedVal);
done();
});
assert.ok(temp === s);
});
it('without extra arguments', function(done) {
var s = new ScraperPromise(new ScraperType())
.get(HN_CLONE);
var fn = function($, selector) {
return $(selector).map(function() {
return $(this).text();
}).get();
};
var temp = s.scrape(fn, function(news) {
assert.equal(news.length, expectedVal);
done();
}, '.title a');
assert.ok(temp === s);
});
it('with only the scraping function', function(done) {
var s = new ScraperPromise(new ScraperType())
.get(HN_CLONE);
var fn = function($) {
return $('.title a').map(function() {
return $(this).text();
}).get();
};
var temp = s.scrape(fn);
temp.then(function(news, utils) {
assert.equal(news.length, expectedVal);
done();
});
assert.ok(temp === s);
});
it('with error', function(done) {
var s = new ScraperPromise(new ScraperType())
.get(HN_CLONE);
var temp;
temp = s
.catch(function(err) {
assert.equal(err.message, 'Error inside scraping fn.');
})
.scrape(function() {
throw new Error('Error inside scraping fn.');
}, function() {
assert.fail('Invalid call.');
})
.done(function() {
done();
});
assert.ok(temp === s);
});
});
it('delay', function(done) {
var s = new ScraperPromise(new ScraperType())
.get(HN_CLONE)
.onStatusCode(function(code) {
assert.equal(code, 200);
});
var temp = s.delay(100);
assert.ok(temp === s);
var expectedContent = isDynamic()?'Dynamic Content':0;
s.scrape(function($) {
return $('.dynamic').text();
}, function(result) {
assert.equal(result, expectedContent);
done();
});
});
it('request', function(done) {
var s = new ScraperPromise(new ScraperType());
var temp = s.request({
url: HN_CLONE,
method: 'POST'
});
assert.ok(temp === s);
var fn = function($) {
return $('#POST_MESSAGE').text();
};
s.scrape(fn, function(result) {
assert.equal(result, 'random text');
done();
});
});
it('done', function(done) {
var s = new ScraperPromise(new ScraperType());
s.get(HN_CLONE);
var temp = s.done(function() {
done();
});
assert.ok(temp === s);
});
it('_setChainParameter', function() {
var s = new ScraperPromise(new ScraperType());
s._setChainParameter(5);
assert.equal(s.chainParameter, 5);
});
describe('_fire', function() {
it('without error', function(done) {
var s = new ScraperPromise(new ScraperType());
s.done(function() {
done();
});
s._fire();
});
it('with error', function(done) {
var c = 0;
var s = new ScraperPromise(new ScraperType())
.done(function() {
assert.equal(c, 1);
done();
})
.catch(function(err) {
c++;
assert.equal(err.message, 'msg');
});
s._fire(new Error('msg'));
});
});
it('_setPromises', function() {
var s = new ScraperPromise(new ScraperType());
var promises = [
function() {}
];
s._setPromises(promises);
assert.ok(s.promises === promises);
});
it('clone', function() {
var s = new ScraperPromise(new ScraperType())
.catch(function() {})
.done(function() {})
.then(function() {})
.onStatusCode(200, function() {})
.onStatusCode(function() {})
.timeout(10)
.delay(10);
var clone = s.clone();
assert.ok(clone instanceof ScraperPromise);
assert.ok(clone.promises === s.promises);
assert.ok(clone.scraper !== s.scraper);
assert.ok(clone.doneCallback === s.doneCallback);
assert.ok(clone.errorCallback === s.errorCallback);
assert.ok(clone.chainParameter === s.chainParameter);
});
it('passing values between promises', function(done) {
new ScraperPromise(new ScraperType())
.done(function(result, utils) {
assert.deepEqual(result, 5);
done();
})
.then(function(last, utils) {
assert.deepEqual(last, undefined);
return 1;
})
.onStatusCode(200, function(utils) {
assert.deepEqual(utils.lastReturn, 1);
return utils.lastReturn + 1;
})
.onStatusCode(function(code, utils) {
assert.deepEqual(utils.lastReturn, 2);
return utils.lastReturn + 1;
})
.delay(10, function(utils) {
assert.deepEqual(utils.lastReturn, 3);
return utils.lastReturn + 1;
})
.scrape(function() {}, function(result, utils) {
assert.deepEqual(utils.lastReturn, 4);
return utils.lastReturn + 1;
})
.get(HN_CLONE);
});
describe('usage of utils', function() {
it('stop()', function(done) {
var c = 0;
new ScraperPromise(new ScraperType())
.get(HN_CLONE)
.then(function() {
c++;
})
.then(function(last, utils) {
c++;
utils.stop();
})
.then(function() {
c++;
})
.done(function() {
assert.equal(c, 2);
done();
});
});
it('scraper', function(done) {
var s = new ScraperPromise(new ScraperType());
s.get(HN_CLONE)
.done(function(_, utils) {
assert.ok(utils.scraper === s);
done();
});
});
it('params', function(done) {
var s = new ScraperPromise(new ScraperType());
s.get(HN_CLONE)
.done(function(_, utils) {
assert.ok(!utils.params);
done();
});
});
});
}
describe('Scraper Promise', function() {
describe('with StaticScraper', function() {
exec(StaticScraper);
});
describe('with DynamicScraper', function() {
describe('with Factory', function() {
beforeEach(function() {
DynamicScraper.startFactory();
});
afterEach(function() {
DynamicScraper.closeFactory();
});
exec(DynamicScraper);
});
describe('without Factory', function() {
exec(DynamicScraper);
});
});
});