forked from Claviz/bellboy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http-source.spec.ts
137 lines (127 loc) · 3.37 KB
/
http-source.spec.ts
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
import { Job, HttpProcessor } from '../src';
import { CustomDestination, CustomTimeoutDestination } from './helpers';
const express = require('express');
const app = express();
app.get('/json', function (req: any, res: any) {
res.send([{
text: 'hello!',
}]);
});
app.get('/delimited', function (req: any, res: any) {
res.send('{"text": "hello"};{"text": "world"}');
});
app.get('/big-json', function (req: any, res: any) {
let obj = {
arr: [] as string[],
};
for (let i = 0; i < 13333; i++) {
obj.arr.push('test');
}
res.send(obj);
});
app.get('/paginated-json', function (req: any, res: any) {
res.send({
pagination: {
nextUrl: 'http://localhost:3000/paginated-json-1',
},
texts: [{
text: 'hello1',
}]
});
});
app.get('/paginated-json-1', function (req: any, res: any) {
res.send({
texts: [{
text: 'hello2',
}]
});
});
const server = app.listen(3000);
beforeAll(async () => {
});
beforeEach(async () => {
});
afterAll(async () => {
server.close();
})
it('gets big JSON data from HTTP', async () => {
const destination = new CustomTimeoutDestination({
batchSize: 100,
timeout: 10,
});
const processor = new HttpProcessor({
dataFormat: 'json',
jsonPath: 'arr.*',
connection: {
method: `GET`,
url: `http://localhost:3000/big-json`,
},
});
const job = new Job(processor, [destination]);
await job.run();
expect(destination.getData().length).toEqual(13333);
});
it('gets JSON data from HTTP', async () => {
const destination = new CustomDestination({
batchSize: 1,
});
const processor = new HttpProcessor({
dataFormat: 'json',
jsonPath: '.',
connection: {
method: `GET`,
url: `http://localhost:3000/json`,
},
});
const job = new Job(processor, [destination]);
await job.run();
expect(destination.getData()).toEqual([{
text: 'hello!',
}]);
});
it('gets delimited data from HTTP', async () => {
const destination = new CustomDestination({
batchSize: 1,
});
const processor = new HttpProcessor({
dataFormat: 'delimited',
delimiter: ';',
connection: {
method: `GET`,
url: `http://localhost:3000/delimited`,
},
});
const job = new Job(processor, [destination]);
await job.run();
expect(destination.getData()).toEqual(['{"text": "hello"}', '{"text": "world"}']);
});
it('gets paginated JSON data from HTTP', async () => {
const connection = {
method: `GET`,
url: `http://localhost:3000/paginated-json`,
};
const destination = new CustomDestination({
batchSize: 1,
});
const processor = new HttpProcessor({
dataFormat: 'json',
jsonPath: 'texts.*',
connection,
nextRequest: async function (header) {
if (header && header.pagination) {
return {
...connection,
url: header.pagination.nextUrl,
};
}
return null;
},
});
const job = new Job(processor, [destination]);
await job.run();
expect(destination.getData()).toEqual([{
text: 'hello1',
}, {
text: 'hello2',
}]);
});