-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
65 lines (47 loc) · 2.42 KB
/
test.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
// These are not proper unit tests, they are just to see some examples and play around.
const fs = require('fs');
const { CrawlingAPI, ScraperAPI, LeadsAPI, ScreenshotsAPI } = require('./index.js');
const { test } = require('./src/config.js');
if (test.normalToken === '' || test.javascriptToken === '') {
console.error('Please add a test normal token and javascript token in src/config.js to run tests');
process.exit(1);
}
function processTestResponse(response) {
if (response.statusCode === 200) {
if (undefined !== response.json) {
console.log(response.json);
} else {
console.log(response.body);
}
console.log('Test passed');
} else {
console.error('Test failed, expected statusCode 200 got ' + response.statusCode);
process.exit(1);
}
}
function processScreenshotTestResponse(response) {
if (response.statusCode === 200) {
fs.writeFileSync('test.jpg', response.body, { encoding: 'binary' });
console.log('Test passed');
} else {
console.error('Test failed, expected statusCode 200 got ' + response.statusCode);
process.exit(1);
}
}
function processTestError(error) {
console.error('Test failed: ' + error);
process.exit(1);
}
const normalAPI = new CrawlingAPI({ token: test.normalToken });
normalAPI.get('https://httpbin.org/anything?hello=world').then(processTestResponse).catch(processTestError);
normalAPI.post('https://httpbin.org/post', { hello: 'post' }).then(processTestResponse).catch(processTestError);
normalAPI.post('https://httpbin.org/post', { hello: 'json' }, { postType: 'json' }).then(processTestResponse).catch(processTestError);
normalAPI.put('https://httpbin.org/put', { hello: 'put' }).then(processTestResponse).catch(processTestError);
const javascriptAPI = new CrawlingAPI({ token: test.javascriptToken });
javascriptAPI.get('https://httpbin.org/anything?hello=world').then(processTestResponse).catch(processTestError);
const scraperAPI = new ScraperAPI({ token: test.normalToken });
scraperAPI.get('https://www.amazon.com/Halo-SleepSack-Swaddle-Triangle-Neutral/dp/B01LAG1TOS').then(processTestResponse).catch(processTestError);
const leadsAPI = new LeadsAPI({ token: test.normalToken });
leadsAPI.getFromDomain('httpbin.org').then(processTestResponse).catch(processTestError);
const screenshotsAPI = new ScreenshotsAPI({ token: test.normalToken });
screenshotsAPI.get('https://www.amazon.com').then(processScreenshotTestResponse).catch(processTestError);