forked from renovatebot/renovate
/
index.spec.ts
189 lines (175 loc) · 5.27 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
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
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, removeAuthorizationHeaders } 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);
});
it('removeAuthorizationHeaders Amazon', async () => {
expect(
await removeAuthorizationHeaders({
auth: 'auth',
headers: {
authorization: 'auth',
},
hostname: 'amazon.com',
href: 'https://amazon.com',
search: 'something X-Amz-Algorithm something',
})
).toEqual({
headers: {},
hostname: 'amazon.com',
href: 'https://amazon.com',
search: 'something X-Amz-Algorithm something',
});
});
it('removeAuthorizationHeaders Amazon ports', async () => {
expect(
await removeAuthorizationHeaders({
auth: 'auth',
headers: {
authorization: 'auth',
},
hostname: 'amazon.com',
href: 'https://amazon.com',
port: 3000,
search: 'something X-Amz-Algorithm something',
})
).toEqual({
headers: {},
hostname: 'amazon.com',
href: 'https://amazon.com',
search: 'something X-Amz-Algorithm something',
});
});
it('removeAuthorizationHeaders Azure blob', async () => {
expect(
await removeAuthorizationHeaders({
auth: 'auth',
headers: {
authorization: 'auth',
},
hostname: 'store123.blob.core.windows.net',
href:
'https://<store>.blob.core.windows.net/<some id>//docker/registry/v2/blobs',
})
).toEqual({
headers: {},
hostname: 'store123.blob.core.windows.net',
href:
'https://<store>.blob.core.windows.net/<some id>//docker/registry/v2/blobs',
});
});
it('removeAuthorizationHeaders keep auth', async () => {
expect(
await removeAuthorizationHeaders({
auth: 'auth',
headers: {
authorization: 'auth',
},
hostname: 'renovate.com',
href: 'https://renovate.com',
search: 'something',
})
).toEqual({
auth: 'auth',
headers: {
authorization: 'auth',
},
hostname: 'renovate.com',
href: 'https://renovate.com',
search: 'something',
});
});
});