-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.spec.js
214 lines (188 loc) · 5.3 KB
/
index.spec.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
'use strict';
const fs = require('fs');
const nock = require('nock');
const sqrap = require('./index');
const selectors = {
authorName: [
{
selector: 'span.author-name',
text: true
}
],
authorUrl: [
{
selector: 'span.author-name > a',
attribute: 'href'
}
],
title: [
{
selector: 'h1',
text: 'true'
}
],
text: [
{
selector: '.content > p',
text: true
},
{
selector: '.extra-content > p',
text: true
}
],
image: [
{
selector: 'meta[property="og:image"]',
attribute: 'content'
}
],
html: [
{
selector: '.main-container > div.content',
html: true
},
{
selector: '.main-container > div.extra-content',
html: true
}
]
};
describe('Scrapes the website info based on a selectors object', () => {
test('Test that the scraper extracted the proper content from the html page', async () => {
nock('https://example.com')
.defaultReplyHeaders({
'Content-Type': 'text/html'
})
.get('/')
.reply(200, () => fs.createReadStream('./mock.html'));
const content = await sqrap('https://example.com/', { selectors });
expect(content.authorName).toEqual('John');
expect(content.authorUrl).toEqual('https://example.com/author/john');
expect(content.title).toEqual('Title');
expect(content.text).toEqual('This is a paragraph with a link.This is extra.');
expect(content.image).toEqual('https://cdn.example.com/someimage');
expect(content.html).toEqual(
'\n <p>This is a paragraph with a <a href="https://example.com/somelink">link</a>.</p>\n <p>This is extra.</p>'
);
nock.cleanAll();
});
test('Test that the scraper throws an error when the content type is not "text/html"', async () => {
nock('https://example.com')
.defaultReplyHeaders({
'Content-Type': 'application/json'
})
.get('/')
.reply(200, { status: 'ok' });
try {
await sqrap('https://example.com/', { selectors });
} catch (error) {
expect(error.message).toEqual('Unsupported content type application/json');
}
nock.cleanAll();
});
test('Test that the scraper throws an error when the response status code is not 200', async () => {
nock('https://example.com')
.get('/')
.reply(500);
try {
await sqrap('https://example.com/', { selectors });
} catch (error) {
expect(error.message).toEqual('Http status code 500');
}
nock.cleanAll();
});
test('Test that the scraper throws an error when the response status code is not 200', async () => {
nock('https://example.com')
.get('/')
.reply(500);
try {
await sqrap('https://example.com/', { selectors });
} catch (error) {
expect(error.message).toEqual('Http status code 500');
}
nock.cleanAll();
});
test('Test that the scraper throws an error when a group has no groupSelectors defined', async () => {
nock('https://example.com')
.defaultReplyHeaders({
'Content-Type': 'text/html'
})
.get('/')
.reply(200, () => fs.createReadStream('./mock.html'));
try {
const testSelectors = Object.assign(selectors, {
group: [
{
selector: '.group',
group: true
}
]
});
await sqrap('https://example.com/', { selectors: testSelectors });
} catch (error) {
expect(error.message).toEqual('Group selection missing group selectors.');
}
nock.cleanAll();
});
test('Test that the scraper throws an error when a group has a nested group', async () => {
nock('https://example.com')
.defaultReplyHeaders({
'Content-Type': 'text/html'
})
.get('/')
.reply(200, () => fs.createReadStream('./mock.html'));
try {
const testSelectors = Object.assign({}, selectors, {
group: [
{
selector: '.group',
group: true,
groupSelectors: {
willFail: [
{
selector: '.member',
group: true
}
]
}
}
]
});
await sqrap('https://example.com/', { selectors: testSelectors });
} catch (error) {
expect(error.message).toEqual('Nested group selectors are not allowed.');
}
nock.cleanAll();
});
test('Test that the scraper extracts an array of objects for a specified group', async () => {
nock('https://example.com')
.defaultReplyHeaders({
'Content-Type': 'text/html'
})
.get('/')
.reply(200, () => fs.createReadStream('./mock.html'));
const testSelectors = Object.assign({}, selectors, {
group: [
{
selector: '.group',
group: true,
groupSelectors: {
member: [
{
selector: '.member',
text: true
}
]
}
}
]
});
const content = await sqrap('https://example.com/', { selectors: testSelectors });
expect(content.group.length).toEqual(3);
expect(content.group[0].member).toEqual('One');
expect(content.group[1].member).toEqual('Two');
expect(content.group[2].member).toEqual('Three');
nock.cleanAll();
});
});