Skip to content

Set request headers to ones provided in transportOpts in XHR transport #2363

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/apm/test/span.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,9 @@ describe('Span', () => {
spanOne.initFinishedSpans();
const childSpanOne = spanOne.child();
childSpanOne.finish();
hub.configureScope((scope) => { scope.setSpan(spanOne) })
hub.configureScope(scope => {
scope.setSpan(spanOne);
});

const spanTwo = new Span({ transaction: 'testTwo', sampled: false }, hub);
spanTwo.initFinishedSpans();
Expand Down
4 changes: 4 additions & 0 deletions packages/browser/src/transports/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ export class FetchTransport extends BaseTransport {
referrerPolicy: (supportsReferrerPolicy() ? 'origin' : '') as ReferrerPolicy,
};

if (this.options.headers !== undefined) {
defaultOptions.headers = this.options.headers;
}

return this._buffer.add(
new SyncPromise<Response>((resolve, reject) => {
global
Expand Down
5 changes: 5 additions & 0 deletions packages/browser/src/transports/xhr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ export class XHRTransport extends BaseTransport {
};

request.open('POST', this.url);
for (const header in this.options.headers) {
if (this.options.headers.hasOwnProperty(header)) {
request.setRequestHeader(header, this.options.headers[header]);
}
}
request.send(JSON.stringify(event));
}),
);
Expand Down
26 changes: 26 additions & 0 deletions packages/browser/test/unit/transports/fetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,5 +129,31 @@ describe('FetchTransport', () => {

dateStub.restore();
});

it('passes in headers', async () => {
transport = new Transports.FetchTransport({
dsn: testDsn,
headers: {
Authorization: 'Basic GVzdDp0ZXN0Cg==',
},
});
const response = { status: 200 };

fetch.returns(Promise.resolve(response));

const res = await transport.sendEvent(payload);

expect(res.status).equal(Status.Success);
expect(
fetch.calledWith(transportUrl, {
body: JSON.stringify(payload),
headers: {
Authorization: 'Basic GVzdDp0ZXN0Cg==',
},
method: 'POST',
referrerPolicy: 'origin',
}),
).equal(true);
});
});
});
18 changes: 18 additions & 0 deletions packages/browser/test/unit/transports/xhr.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,23 @@ describe('XHRTransport', () => {

dateStub.restore();
});

it('passes in headers', async () => {
transport = new Transports.XHRTransport({
dsn: testDsn,
headers: {
Authorization: 'Basic GVzdDp0ZXN0Cg==',
},
});

server.respondWith('POST', transportUrl, [200, {}, '']);
const res = await transport.sendEvent(payload);
const request = server.requests[0];

expect(res.status).equal(Status.Success);
const requestHeaders: { [key: string]: string } = request.requestHeaders as { [key: string]: string };
const authHeaderLabel: string = 'Authorization';
expect(requestHeaders[authHeaderLabel]).equal('Basic GVzdDp0ZXN0Cg==');
});
});
});
2 changes: 1 addition & 1 deletion packages/types/src/transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export interface TransportOptions {
/** Sentry DSN */
dsn: DsnLike;
/** Define custom headers */
headers?: object;
headers?: { [key: string]: string };
/** Set a HTTP proxy that should be used for outbound requests. */
httpProxy?: string;
/** Set a HTTPS proxy that should be used for outbound requests. */
Expand Down