Skip to content

Commit d80f37f

Browse files
committed
test(opentelemetry-plugin-xml-http-request): added un/patching tests
Signed-off-by: Tina Gao <[email protected]>
1 parent 2486c3d commit d80f37f

File tree

1 file changed

+102
-5
lines changed
  • packages/opentelemetry-plugin-xml-http-request/test

1 file changed

+102
-5
lines changed

packages/opentelemetry-plugin-xml-http-request/test/xhr.test.ts

+102-5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import {
2121
X_B3_SAMPLED,
2222
X_B3_SPAN_ID,
2323
X_B3_TRACE_ID,
24+
isWrapped,
25+
NoopLogger,
2426
} from '@opentelemetry/core';
2527
import { ZoneContextManager } from '@opentelemetry/context-zone';
2628
import * as tracing from '@opentelemetry/tracing';
@@ -63,7 +65,6 @@ const getData = (url: string, callbackAfterSend: Function, async?: boolean) => {
6365
resolve();
6466
};
6567
req.send();
66-
6768
callbackAfterSend();
6869
});
6970
};
@@ -132,6 +133,7 @@ describe('xhr', () => {
132133
let spyEntries: any;
133134
const url = `${window.location.origin}/xml-http-request.js`;
134135
let fakeNow = 0;
136+
let xmlHttpRequestPlugin: XMLHttpRequestPlugin;
135137

136138
clearData = () => {
137139
requests = [];
@@ -163,13 +165,13 @@ describe('xhr', () => {
163165

164166
spyEntries = sandbox.stub(performance, 'getEntriesByType');
165167
spyEntries.withArgs('resource').returns(resources);
166-
168+
xmlHttpRequestPlugin = new XMLHttpRequestPlugin({
169+
propagateTraceHeaderCorsUrls: propagateTraceHeaderCorsUrls,
170+
});
167171
webTracerProviderWithZone = new WebTracerProvider({
168172
logLevel: LogLevel.ERROR,
169173
plugins: [
170-
new XMLHttpRequestPlugin({
171-
propagateTraceHeaderCorsUrls: propagateTraceHeaderCorsUrls,
172-
}),
174+
xmlHttpRequestPlugin,
173175
],
174176
});
175177
webTracerWithZone = webTracerProviderWithZone.getTracer('xhr-test');
@@ -211,6 +213,20 @@ describe('xhr', () => {
211213
clearData();
212214
});
213215

216+
it('should patch to wrap XML HTTP Requests when enabled', () => {
217+
let xhttp = new XMLHttpRequest();
218+
assert.ok(isWrapped(xhttp.send));
219+
xmlHttpRequestPlugin.enable(XMLHttpRequest.prototype, new api.NoopTracerProvider(), new NoopLogger());
220+
assert.ok(isWrapped(xhttp.send));
221+
});
222+
223+
it('should unpatch to unwrap XML HTTP Requests when disabled', () => {
224+
let xhttp = new XMLHttpRequest();
225+
assert.ok(isWrapped(xhttp.send));
226+
xmlHttpRequestPlugin.disable()
227+
assert.ok(!isWrapped(xhttp.send));
228+
});
229+
214230
it('should create a span with correct root span', () => {
215231
const span: tracing.ReadableSpan = exportSpy.args[0][0][0];
216232
assert.strictEqual(
@@ -346,6 +362,87 @@ describe('xhr', () => {
346362
);
347363

348364
assert.strictEqual(events.length, 12, 'number of events is wrong');
365+
366+
// it('should create a span for preflight request', () => {
367+
// const span: tracing.ReadableSpan = exportSpy.args[0][0][0];
368+
// const parentSpan: tracing.ReadableSpan = exportSpy.args[1][0][0];
369+
// assert.strictEqual(
370+
// span.parentSpanId,
371+
// parentSpan.spanContext.spanId,
372+
// 'parent span is not root span'
373+
// );
374+
// });
375+
376+
// it('preflight request span should have correct name', () => {
377+
// const span: tracing.ReadableSpan = exportSpy.args[0][0][0];
378+
// assert.strictEqual(
379+
// span.name,
380+
// 'CORS Preflight',
381+
// 'preflight request span has wrong name'
382+
// );
383+
// });
384+
385+
// it('preflight request span should have correct kind', () => {
386+
// const span: tracing.ReadableSpan = exportSpy.args[0][0][0];
387+
// assert.strictEqual(
388+
// span.kind,
389+
// api.SpanKind.INTERNAL,
390+
// 'span has wrong kind'
391+
// );
392+
// });
393+
394+
// it('preflight request span should have correct events', () => {
395+
// const span: tracing.ReadableSpan = exportSpy.args[0][0][0];
396+
// const events = span.events;
397+
// assert.strictEqual(events.length, 9, 'number of events is wrong');
398+
399+
// assert.strictEqual(
400+
// events[0].name,
401+
// PTN.FETCH_START,
402+
// `event ${PTN.FETCH_START} is not defined`
403+
// );
404+
// assert.strictEqual(
405+
// events[1].name,
406+
// PTN.DOMAIN_LOOKUP_START,
407+
// `event ${PTN.DOMAIN_LOOKUP_START} is not defined`
408+
// );
409+
// assert.strictEqual(
410+
// events[2].name,
411+
// PTN.DOMAIN_LOOKUP_END,
412+
// `event ${PTN.DOMAIN_LOOKUP_END} is not defined`
413+
// );
414+
// assert.strictEqual(
415+
// events[3].name,
416+
// PTN.CONNECT_START,
417+
// `event ${PTN.CONNECT_START} is not defined`
418+
// );
419+
// assert.strictEqual(
420+
// events[4].name,
421+
// PTN.SECURE_CONNECTION_START,
422+
// `event ${PTN.SECURE_CONNECTION_START} is not defined`
423+
// );
424+
// assert.strictEqual(
425+
// events[5].name,
426+
// PTN.CONNECT_END,
427+
// `event ${PTN.CONNECT_END} is not defined`
428+
// );
429+
// assert.strictEqual(
430+
// events[6].name,
431+
// PTN.REQUEST_START,
432+
// `event ${PTN.REQUEST_START} is not defined`
433+
// );
434+
// assert.strictEqual(
435+
// events[7].name,
436+
// PTN.RESPONSE_START,
437+
// `event ${PTN.RESPONSE_START} is not defined`
438+
// );
439+
// assert.strictEqual(
440+
// events[8].name,
441+
// PTN.RESPONSE_END,
442+
// `event ${PTN.RESPONSE_END} is not defined`
443+
// );
444+
// });
445+
349446
});
350447

351448
describe('AND origin match with window.location', () => {

0 commit comments

Comments
 (0)