Skip to content

Commit 2d628c3

Browse files
edjmaoSija
authored andcommitted
Set headers from options in XHR/fetch transport (getsentry#2363)
* Set request headers to ones provided in transportOpts in XHR transport * Rename variable in transport header object. Co-Authored-By: Sijawusz Pur Rahnama <sija@sija.pl> * Lint fixes * Add headers to fetch transport with unit tests. Co-authored-by: Sijawusz Pur Rahnama <sija@sija.pl>
1 parent 02b8ab6 commit 2d628c3

File tree

6 files changed

+57
-2
lines changed

6 files changed

+57
-2
lines changed

packages/apm/test/span.test.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,9 @@ describe('Span', () => {
198198
spanOne.initFinishedSpans();
199199
const childSpanOne = spanOne.child();
200200
childSpanOne.finish();
201-
hub.configureScope((scope) => { scope.setSpan(spanOne) })
201+
hub.configureScope(scope => {
202+
scope.setSpan(spanOne);
203+
});
202204

203205
const spanTwo = new Span({ transaction: 'testTwo', sampled: false }, hub);
204206
spanTwo.initFinishedSpans();

packages/browser/src/transports/fetch.ts

+4
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ export class FetchTransport extends BaseTransport {
3232
referrerPolicy: (supportsReferrerPolicy() ? 'origin' : '') as ReferrerPolicy,
3333
};
3434

35+
if (this.options.headers !== undefined) {
36+
defaultOptions.headers = this.options.headers;
37+
}
38+
3539
return this._buffer.add(
3640
new SyncPromise<Response>((resolve, reject) => {
3741
global

packages/browser/src/transports/xhr.ts

+5
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ export class XHRTransport extends BaseTransport {
4646
};
4747

4848
request.open('POST', this.url);
49+
for (const header in this.options.headers) {
50+
if (this.options.headers.hasOwnProperty(header)) {
51+
request.setRequestHeader(header, this.options.headers[header]);
52+
}
53+
}
4954
request.send(JSON.stringify(event));
5055
}),
5156
);

packages/browser/test/unit/transports/fetch.test.ts

+26
Original file line numberDiff line numberDiff line change
@@ -129,5 +129,31 @@ describe('FetchTransport', () => {
129129

130130
dateStub.restore();
131131
});
132+
133+
it('passes in headers', async () => {
134+
transport = new Transports.FetchTransport({
135+
dsn: testDsn,
136+
headers: {
137+
Authorization: 'Basic GVzdDp0ZXN0Cg==',
138+
},
139+
});
140+
const response = { status: 200 };
141+
142+
fetch.returns(Promise.resolve(response));
143+
144+
const res = await transport.sendEvent(payload);
145+
146+
expect(res.status).equal(Status.Success);
147+
expect(
148+
fetch.calledWith(transportUrl, {
149+
body: JSON.stringify(payload),
150+
headers: {
151+
Authorization: 'Basic GVzdDp0ZXN0Cg==',
152+
},
153+
method: 'POST',
154+
referrerPolicy: 'origin',
155+
}),
156+
).equal(true);
157+
});
132158
});
133159
});

packages/browser/test/unit/transports/xhr.test.ts

+18
Original file line numberDiff line numberDiff line change
@@ -102,5 +102,23 @@ describe('XHRTransport', () => {
102102

103103
dateStub.restore();
104104
});
105+
106+
it('passes in headers', async () => {
107+
transport = new Transports.XHRTransport({
108+
dsn: testDsn,
109+
headers: {
110+
Authorization: 'Basic GVzdDp0ZXN0Cg==',
111+
},
112+
});
113+
114+
server.respondWith('POST', transportUrl, [200, {}, '']);
115+
const res = await transport.sendEvent(payload);
116+
const request = server.requests[0];
117+
118+
expect(res.status).equal(Status.Success);
119+
const requestHeaders: { [key: string]: string } = request.requestHeaders as { [key: string]: string };
120+
const authHeaderLabel: string = 'Authorization';
121+
expect(requestHeaders[authHeaderLabel]).equal('Basic GVzdDp0ZXN0Cg==');
122+
});
105123
});
106124
});

packages/types/src/transport.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export interface TransportOptions {
2727
/** Sentry DSN */
2828
dsn: DsnLike;
2929
/** Define custom headers */
30-
headers?: object;
30+
headers?: { [key: string]: string };
3131
/** Set a HTTP proxy that should be used for outbound requests. */
3232
httpProxy?: string;
3333
/** Set a HTTPS proxy that should be used for outbound requests. */

0 commit comments

Comments
 (0)