/
index.spec.ts
109 lines (99 loc) · 3.21 KB
/
index.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
import nock from 'nock';
import { getName } from '../../../test/util';
import { EXTERNAL_HOST_ERROR } from '../../constants/error-messages';
import * as hostRules from '../host-rules';
import { Http } from '.';
const baseUrl = 'http://renovate.com';
describe(getName(__filename), () => {
let http: Http;
beforeEach(() => {
http = new Http('dummy');
nock.cleanAll();
hostRules.clear();
});
it('get', async () => {
nock(baseUrl).get('/test').reply(200);
expect(await http.get('http://renovate.com/test')).toMatchSnapshot();
expect(nock.isDone()).toBe(true);
});
it('returns 429 error', async () => {
nock(baseUrl).get('/test').reply(429);
await expect(http.get('http://renovate.com/test')).rejects.toThrow(
'Response code 429 (Too Many Requests)'
);
expect(nock.isDone()).toBe(true);
});
it('converts 404 error to ExternalHostError', async () => {
nock(baseUrl).get('/test').reply(404);
hostRules.add({ abortOnError: true });
await expect(http.get('http://renovate.com/test')).rejects.toThrow(
EXTERNAL_HOST_ERROR
);
expect(nock.isDone()).toBe(true);
});
it('ignores 404 error and does not throw ExternalHostError', async () => {
nock(baseUrl).get('/test').reply(404);
hostRules.add({ abortOnError: true, abortIgnoreStatusCodes: [404] });
await expect(http.get('http://renovate.com/test')).rejects.toThrow(
'Response code 404 (Not Found)'
);
expect(nock.isDone()).toBe(true);
});
it('getJson', async () => {
nock(baseUrl).get('/').reply(200, '{ "test": true }');
expect(await http.getJson('http://renovate.com')).toMatchSnapshot();
});
it('postJson', async () => {
nock(baseUrl).post('/').reply(200, {});
expect(
await http.postJson('http://renovate.com', { body: {}, baseUrl })
).toMatchSnapshot();
expect(nock.isDone()).toBe(true);
});
it('putJson', async () => {
nock(baseUrl).put('/').reply(200, {});
expect(
await http.putJson('http://renovate.com', { body: {}, baseUrl })
).toMatchSnapshot();
expect(nock.isDone()).toBe(true);
});
it('patchJson', async () => {
nock(baseUrl).patch('/').reply(200, {});
expect(
await http.patchJson('http://renovate.com', { body: {}, baseUrl })
).toMatchSnapshot();
expect(nock.isDone()).toBe(true);
});
it('deleteJson', async () => {
nock(baseUrl).delete('/').reply(200, {});
expect(
await http.deleteJson('http://renovate.com', { body: {}, baseUrl })
).toMatchSnapshot();
expect(nock.isDone()).toBe(true);
});
it('headJson', async () => {
nock(baseUrl).head('/').reply(200, {});
expect(
await http.headJson('http://renovate.com', { body: {}, baseUrl })
).toMatchSnapshot();
expect(nock.isDone()).toBe(true);
});
it('stream', async () => {
nock(baseUrl).get('/some').reply(200, {});
const stream = http.stream('/some', {
baseUrl,
});
expect(stream).toBeDefined();
let data = '';
stream.on('data', (c) => {
data += c;
});
const done = new Promise((resolve, reject) => {
stream.on('end', resolve);
stream.on('error', reject);
});
await done;
expect(data).toBe('{}');
expect(nock.isDone()).toBe(true);
});
});