-
Notifications
You must be signed in to change notification settings - Fork 407
Expand file tree
/
Copy pathdirect.spec.js
More file actions
120 lines (102 loc) · 3.26 KB
/
Copy pathdirect.spec.js
File metadata and controls
120 lines (102 loc) · 3.26 KB
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
'use strict'
const assert = require('node:assert/strict')
const { describe, it, beforeEach } = require('mocha')
const proxyquire = require('proxyquire').noPreserveCache()
const sinon = require('sinon')
describe('direct EVP route', () => {
let createDirectEVPRoute
let getProxyForUrl
let HttpsProxyAgent
let log
beforeEach(() => {
getProxyForUrl = sinon.stub().returns('')
HttpsProxyAgent = sinon.stub().callsFake(proxyUrl => ({ proxyUrl }))
log = { debug: sinon.spy() }
;({ createDirectEVPRoute } = proxyquire('../../src/evp_proxy/direct', {
'https-proxy-agent': { HttpsProxyAgent },
'proxy-from-env': { getProxyForUrl },
'../log': log,
}))
})
it('creates an authenticated route from API key and site', () => {
const route = createDirectEVPRoute({
DD_API_KEY: 'test-api-key',
site: 'datadoghq.com',
}, 'event-platform-intake')
assert.deepStrictEqual(route, {
url: new URL('https://event-platform-intake.datadoghq.com'),
basePath: '',
headers: {
'DD-API-KEY': 'test-api-key',
},
})
})
it('normalizes site casing', () => {
const route = createDirectEVPRoute({
DD_API_KEY: 'test-api-key',
site: 'DATADOGHQ.EU',
}, 'event-platform-intake')
assert.deepStrictEqual(route, {
url: new URL('https://event-platform-intake.datadoghq.eu'),
basePath: '',
headers: {
'DD-API-KEY': 'test-api-key',
},
})
})
it('uses the standard HTTPS proxy for direct intake', () => {
const proxyUrl = 'http://proxy:8202'
getProxyForUrl.returns(proxyUrl)
const route = createDirectEVPRoute({
DD_API_KEY: 'test-api-key',
site: 'datadoghq.com',
}, 'event-platform-intake')
assert.deepStrictEqual(route.agent, { proxyUrl })
sinon.assert.calledOnceWithExactly(
getProxyForUrl,
'https://event-platform-intake.datadoghq.com/'
)
sinon.assert.calledOnceWithExactly(HttpsProxyAgent, proxyUrl)
})
it('does not create a route without an API key', () => {
assert.strictEqual(createDirectEVPRoute({
site: 'datadoghq.com',
}, 'event-platform-intake'), undefined)
})
it('does not create a route without a site', () => {
assert.strictEqual(createDirectEVPRoute({
DD_API_KEY: 'test-api-key',
}, 'event-platform-intake'), undefined)
})
it('does not create a route for an invalid site', () => {
assert.strictEqual(createDirectEVPRoute({
DD_API_KEY: 'test-api-key',
site: 'not a host',
}, 'event-platform-intake'), undefined)
sinon.assert.calledOnceWithExactly(
log.debug,
'Unable to configure direct EVP intake: %s',
sinon.match.string
)
})
for (const site of [
'datadoghq.com@evil.example',
'datadoghq.com:password@evil.example',
'datadoghq.com:443',
'datadoghq.com/path',
'datadoghq.com?query',
'datadoghq.com#fragment',
]) {
it(`does not create a route for a site with URL components: ${site}`, () => {
assert.strictEqual(createDirectEVPRoute({
DD_API_KEY: 'test-api-key',
site,
}, 'event-platform-intake'), undefined)
sinon.assert.calledOnceWithExactly(
log.debug,
'Unable to configure direct EVP intake: %s',
sinon.match.string
)
})
}
})