|
1 | 1 | import { TransportOptions } from '@sentry/types';
|
2 | 2 | import { SentryError } from '@sentry/utils';
|
| 3 | +import * as https from 'https'; |
3 | 4 | import * as HttpsProxyAgent from 'https-proxy-agent';
|
4 | 5 |
|
5 | 6 | import { HTTPSTransport } from '../../src/transports/https';
|
@@ -153,36 +154,110 @@ describe('HTTPSTransport', () => {
|
153 | 154 | expect(requestOptions.headers).toEqual(expect.objectContaining({ a: 'b' }));
|
154 | 155 | });
|
155 | 156 |
|
156 |
| - test('https proxy', async () => { |
157 |
| - mockReturnCode = 200; |
158 |
| - const transport = createTransport({ |
159 |
| - dsn, |
160 |
| - httpsProxy: 'https://example.com:8080', |
| 157 | + describe('proxy', () => { |
| 158 | + test('can be configured through client option', async () => { |
| 159 | + const transport = createTransport({ |
| 160 | + dsn, |
| 161 | + httpsProxy: 'https://example.com:8080', |
| 162 | + }); |
| 163 | + const client = (transport.client as unknown) as { proxy: Record<string, string | number>; secureProxy: boolean }; |
| 164 | + expect(client).toBeInstanceOf(HttpsProxyAgent); |
| 165 | + expect(client.secureProxy).toEqual(true); |
| 166 | + expect(client.proxy).toEqual(expect.objectContaining({ protocol: 'https:', port: 8080, host: 'example.com' })); |
161 | 167 | });
|
162 |
| - await transport.sendEvent({ |
163 |
| - message: 'test', |
| 168 | + |
| 169 | + test('can be configured through env variables option', async () => { |
| 170 | + process.env.https_proxy = 'https://example.com:8080'; |
| 171 | + const transport = createTransport({ |
| 172 | + dsn, |
| 173 | + httpsProxy: 'https://example.com:8080', |
| 174 | + }); |
| 175 | + const client = (transport.client as unknown) as { proxy: Record<string, string | number>; secureProxy: boolean }; |
| 176 | + expect(client).toBeInstanceOf(HttpsProxyAgent); |
| 177 | + expect(client.secureProxy).toEqual(true); |
| 178 | + expect(client.proxy).toEqual(expect.objectContaining({ protocol: 'https:', port: 8080, host: 'example.com' })); |
| 179 | + delete process.env.https_proxy; |
164 | 180 | });
|
165 | 181 |
|
166 |
| - const requestOptions = (transport.module!.request as jest.Mock).mock.calls[0][0]; |
167 |
| - assertBasicOptions(requestOptions); |
168 |
| - expect(requestOptions.agent).toBeInstanceOf(HttpsProxyAgent); |
169 |
| - expect(requestOptions.agent.secureProxy).toEqual(true); |
170 |
| - expect(requestOptions.agent.proxy).toEqual( |
171 |
| - expect.objectContaining({ protocol: 'https:', port: 8080, host: 'example.com' }), |
172 |
| - ); |
173 |
| - }); |
| 182 | + test('https proxies have priority in client option', async () => { |
| 183 | + const transport = createTransport({ |
| 184 | + dsn, |
| 185 | + httpProxy: 'http://unsecure-example.com:8080', |
| 186 | + httpsProxy: 'https://example.com:8080', |
| 187 | + }); |
| 188 | + const client = (transport.client as unknown) as { proxy: Record<string, string | number>; secureProxy: boolean }; |
| 189 | + expect(client).toBeInstanceOf(HttpsProxyAgent); |
| 190 | + expect(client.secureProxy).toEqual(true); |
| 191 | + expect(client.proxy).toEqual(expect.objectContaining({ protocol: 'https:', port: 8080, host: 'example.com' })); |
| 192 | + }); |
174 | 193 |
|
175 |
| - test('tls certificate', async () => { |
176 |
| - mockReturnCode = 200; |
177 |
| - const transport = createTransport({ |
178 |
| - caCerts: './some/path.pem', |
179 |
| - dsn, |
| 194 | + test('https proxies have priority in env variables', async () => { |
| 195 | + process.env.http_proxy = 'http://unsecure-example.com:8080'; |
| 196 | + process.env.https_proxy = 'https://example.com:8080'; |
| 197 | + const transport = createTransport({ |
| 198 | + dsn, |
| 199 | + }); |
| 200 | + const client = (transport.client as unknown) as { proxy: Record<string, string | number>; secureProxy: boolean }; |
| 201 | + expect(client).toBeInstanceOf(HttpsProxyAgent); |
| 202 | + expect(client.secureProxy).toEqual(true); |
| 203 | + expect(client.proxy).toEqual(expect.objectContaining({ protocol: 'https:', port: 8080, host: 'example.com' })); |
| 204 | + delete process.env.http_proxy; |
| 205 | + delete process.env.https_proxy; |
180 | 206 | });
|
181 |
| - await transport.sendEvent({ |
182 |
| - message: 'test', |
| 207 | + |
| 208 | + test('client options have priority over env variables', async () => { |
| 209 | + process.env.https_proxy = 'https://env-example.com:8080'; |
| 210 | + const transport = createTransport({ |
| 211 | + dsn, |
| 212 | + httpsProxy: 'https://example.com:8080', |
| 213 | + }); |
| 214 | + const client = (transport.client as unknown) as { proxy: Record<string, string | number>; secureProxy: boolean }; |
| 215 | + expect(client).toBeInstanceOf(HttpsProxyAgent); |
| 216 | + expect(client.secureProxy).toEqual(true); |
| 217 | + expect(client.proxy).toEqual(expect.objectContaining({ protocol: 'https:', port: 8080, host: 'example.com' })); |
| 218 | + delete process.env.https_proxy; |
| 219 | + }); |
| 220 | + |
| 221 | + test('no_proxy allows for skipping specific hosts', async () => { |
| 222 | + process.env.no_proxy = 'sentry.io'; |
| 223 | + const transport = createTransport({ |
| 224 | + dsn, |
| 225 | + httpsProxy: 'https://example.com:8080', |
| 226 | + }); |
| 227 | + expect(transport.client).toBeInstanceOf(https.Agent); |
| 228 | + }); |
| 229 | + |
| 230 | + test('no_proxy works with a port', async () => { |
| 231 | + process.env.https_proxy = 'https://example.com:8080'; |
| 232 | + process.env.no_proxy = 'sentry.io:8989'; |
| 233 | + const transport = createTransport({ |
| 234 | + dsn, |
| 235 | + }); |
| 236 | + expect(transport.client).toBeInstanceOf(https.Agent); |
| 237 | + delete process.env.https_proxy; |
| 238 | + }); |
| 239 | + |
| 240 | + test('no_proxy works with multiple comma-separated hosts', async () => { |
| 241 | + process.env.http_proxy = 'https://example.com:8080'; |
| 242 | + process.env.no_proxy = 'example.com,sentry.io,wat.com:1337'; |
| 243 | + const transport = createTransport({ |
| 244 | + dsn, |
| 245 | + }); |
| 246 | + expect(transport.client).toBeInstanceOf(https.Agent); |
| 247 | + delete process.env.https_proxy; |
| 248 | + }); |
| 249 | + |
| 250 | + test('can configure tls certificate through client option', async () => { |
| 251 | + mockReturnCode = 200; |
| 252 | + const transport = createTransport({ |
| 253 | + caCerts: './some/path.pem', |
| 254 | + dsn, |
| 255 | + }); |
| 256 | + await transport.sendEvent({ |
| 257 | + message: 'test', |
| 258 | + }); |
| 259 | + const requestOptions = (transport.module!.request as jest.Mock).mock.calls[0][0]; |
| 260 | + expect(requestOptions.ca).toEqual('mockedCert'); |
183 | 261 | });
|
184 |
| - const requestOptions = (transport.module!.request as jest.Mock).mock.calls[0][0]; |
185 |
| - assertBasicOptions(requestOptions); |
186 |
| - expect(requestOptions.ca).toEqual('mockedCert'); |
187 | 262 | });
|
188 | 263 | });
|
0 commit comments