/
http.test.ts
178 lines (144 loc) · 6.13 KB
/
http.test.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
import * as sentryCore from '@sentry/core';
import { Hub } from '@sentry/hub';
import * as hubModule from '@sentry/hub';
import { addExtensionMethods, Span, TRACEPARENT_REGEXP, Transaction } from '@sentry/tracing';
import { parseSemver } from '@sentry/utils';
import * as http from 'http';
import * as https from 'https';
import * as nock from 'nock';
import { Breadcrumb } from '../../src';
import { NodeClient } from '../../src/client';
import { Http as HttpIntegration } from '../../src/integrations/http';
const NODE_VERSION = parseSemver(process.versions.node);
describe('tracing', () => {
function createTransactionOnScope() {
const hub = new Hub(
new NodeClient({
dsn: 'https://dogsarebadatkeepingsecrets@squirrelchasers.ingest.sentry.io/12312012',
tracesSampleRate: 1.0,
integrations: [new HttpIntegration({ tracing: true })],
}),
);
addExtensionMethods();
// we need to mock both of these because the tracing handler relies on `@sentry/core` while the sampler relies on
// `@sentry/hub`, and mocking breaks the link between the two
jest.spyOn(sentryCore, 'getCurrentHub').mockReturnValue(hub);
jest.spyOn(hubModule, 'getCurrentHub').mockReturnValue(hub);
const transaction = hub.startTransaction({ name: 'dogpark' });
hub.getScope()?.setSpan(transaction);
return transaction;
}
it("creates a span for each outgoing non-sentry request when there's a transaction on the scope", () => {
nock('http://dogs.are.great')
.get('/')
.reply(200);
const transaction = createTransactionOnScope();
const spans = (transaction as Span).spanRecorder?.spans as Span[];
http.get('http://dogs.are.great/');
// TODO: For some reason in node 6 two request spans are appearing. Once we stop testing against it, this can go
// back to being `toEqual()`.
expect(spans.length).toBeGreaterThanOrEqual(2);
// our span is at index 1 because the transaction itself is at index 0
expect(spans[1].description).toEqual('GET http://dogs.are.great/');
expect(spans[1].op).toEqual('request');
});
it("doesn't create a span for outgoing sentry requests", () => {
nock('http://squirrelchasers.ingest.sentry.io')
.get('/api/12312012/store/')
.reply(200);
const transaction = createTransactionOnScope();
const spans = (transaction as Span).spanRecorder?.spans as Span[];
http.get('http://squirrelchasers.ingest.sentry.io/api/12312012/store/');
// only the transaction itself should be there
expect(spans.length).toEqual(1);
expect((spans[0] as Transaction).name).toEqual('dogpark');
});
it('attaches the sentry-trace header to outgoing non-sentry requests', async () => {
nock('http://dogs.are.great')
.get('/')
.reply(200);
createTransactionOnScope();
const request = http.get('http://dogs.are.great/');
const sentryTraceHeader = request.getHeader('sentry-trace') as string;
expect(sentryTraceHeader).toBeDefined();
expect(TRACEPARENT_REGEXP.test(sentryTraceHeader)).toBe(true);
});
it("doesn't attach the sentry-trace header to outgoing sentry requests", () => {
nock('http://squirrelchasers.ingest.sentry.io')
.get('/api/12312012/store/')
.reply(200);
createTransactionOnScope();
const request = http.get('http://squirrelchasers.ingest.sentry.io/api/12312012/store/');
const sentryTraceHeader = request.getHeader('sentry-trace');
expect(sentryTraceHeader).not.toBeDefined();
});
});
describe('default protocols', () => {
function captureBreadcrumb(key: string): Promise<Breadcrumb> {
const hub = new Hub();
jest.spyOn(sentryCore, 'getCurrentHub').mockReturnValue(hub);
let resolve: (value: Breadcrumb | PromiseLike<Breadcrumb>) => void;
const p = new Promise<Breadcrumb>(r => {
resolve = r;
});
hub.bindClient(
new NodeClient({
dsn: 'https://dogsarebadatkeepingsecrets@squirrelchasers.ingest.sentry.io/12312012',
integrations: [new HttpIntegration({ breadcrumbs: true })],
beforeBreadcrumb: (b: Breadcrumb) => {
if ((b.data?.url as string).includes(key)) {
resolve(b);
}
return b;
},
}),
);
return p;
}
it('http module', async () => {
const key = 'catrunners';
const p = captureBreadcrumb(key);
nock(`http://${key}.ingest.sentry.io`)
.get('/api/123122332/store/')
.reply(200);
http.get({
host: `${key}.ingest.sentry.io`,
path: '/api/123122332/store/',
});
const b = await p;
expect(b.data?.url).toEqual(expect.stringContaining('http://'));
});
it('https module', async () => {
const key = 'catcatchers';
const p = captureBreadcrumb(key);
let nockProtocol = 'https:';
// NOTE: Prior to Node 9, `https` used internals of `http` module, so
// the integration doesn't patch the `https` module. However this then
// causes issues with nock, because nock will patch the `https` module
// regardless (if it asked to mock a https:// url), preventing the
// request from reaching the integrations patch of the `http` module.
// The result is test failures in Node v8 and lower.
//
// We can work around this by telling giving nock a http:// url, so it
// only patches the `http` module, then Nodes usage of the `http` module
// in the `https` module results in both nock's and the integrations
// patch being called. All this relies on nock not properly checking
// the agent passed to `http.get` / `http.request`, thus resulting in it
// intercepting a https:// request with http:// mock. It's a safe bet
// because the latest versions of nock no longer support Node v8 and lower,
// so won't bother dealing with this old Node edge case.
if (NODE_VERSION.major && NODE_VERSION.major < 9) {
nockProtocol = 'http:';
}
nock(`${nockProtocol}://${key}.ingest.sentry.io`)
.get('/api/123122332/store/')
.reply(200);
https.get({
host: `${key}.ingest.sentry.io`,
path: '/api/123122332/store/',
timeout: 300,
});
const b = await p;
expect(b.data?.url).toEqual(expect.stringContaining('https://'));
});
});