Skip to content

Commit

Permalink
test(opentelemetry-plugin-xml-http-request): added test for XHR reuse
Browse files Browse the repository at this point in the history
  • Loading branch information
thgao committed Jun 18, 2020
1 parent 5d8c2c6 commit b9fc81a
Showing 1 changed file with 96 additions and 0 deletions.
96 changes: 96 additions & 0 deletions packages/opentelemetry-plugin-xml-http-request/test/xhr.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,102 @@ describe('xhr', () => {
);
});
});

describe('when reusing the same XML Http request', () => {
let reusableReq: XMLHttpRequest; //= new XMLHttpRequest();
const firstUrl = 'http://localhost:8090/get';
const secondUrl = 'http://localhost:8099/get';
const getDataReuseXHR = (
url: string,
callbackAfterSend: Function,
async?: boolean
) => {
// eslint-disable-next-line no-async-promise-executor
return new Promise(async (resolve, reject) => {
if (async === undefined) {
async = true;
}
reusableReq.open('GET', url, async);
reusableReq.onload = function () {
resolve();
};

reusableReq.onerror = function () {
resolve();
};

reusableReq.ontimeout = function () {
resolve();
};
reusableReq.send();
callbackAfterSend();
});
};

beforeEach(done => {
requests = [];
const resources: PerformanceResourceTiming[] = [];
resources.push(
createResource({
name: firstUrl,
}),
createResource({
name: secondUrl,
})
);
reusableReq = new XMLHttpRequest();
webTracerWithZone.withSpan(rootSpan, () => {
getDataReuseXHR(
firstUrl,
() => {
fakeNow = 100;
},
testAsync
).then(() => {
fakeNow = 0;
sandbox.clock.tick(1000);
});
});

webTracerWithZone.withSpan(rootSpan, () => {
getDataReuseXHR(
secondUrl,
() => {
fakeNow = 100;
},
testAsync
).then(() => {
fakeNow = 0;
sandbox.clock.tick(1000);
done();
});

assert.strictEqual(
requests.length,
1,
'first request not called'
);

requests[0].respond(
200,
{ 'Content-Type': 'application/json' },
'{"foo":"bar"}'
);
});
});

it('should clear previous span information', () => {
const span: tracing.ReadableSpan = exportSpy.args[2][0][0];
const attributes = span.attributes;
const keys = Object.keys(attributes);

assert.strictEqual(
attributes[keys[2]],
secondUrl,
`attribute ${AttributeNames.HTTP_URL} is wrong`
);
});
});
});

describe('when request is NOT successful', () => {
Expand Down

0 comments on commit b9fc81a

Please sign in to comment.